Skip to content

Commit

Permalink
chore(launchpad): fixing merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
mazzi authored and jacderida committed Sep 4, 2024
1 parent 34f9c0b commit a91fde0
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 62 deletions.
117 changes: 97 additions & 20 deletions node-launchpad/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ impl App {

// Popups
let reset_nodes = ResetNodesPopup::default();
let change_drive = ChangeDrivePopup::new(storage_mountpoint.clone())?;
let change_connection_mode = ChangeConnectionModePopUp::new(connection_mode)?;
let port_range = PortRangePopUp::new(connection_mode, port_from, port_to);
let beta_programme = BetaProgramme::new(app_data.discord_username.clone());
let manage_nodes = ManageNodes::new(app_data.nodes_to_start, storage_mountpoint.clone())?;
let change_drive = ChangeDrivePopup::new(storage_mountpoint.clone())?;
let change_connection_mode = ChangeConnectionModePopUp::new(connection_mode)?;
Expand All @@ -126,7 +122,15 @@ impl App {

Ok(Self {
config,
app_data,
app_data: AppData {
discord_username: app_data.discord_username.clone(),
nodes_to_start: app_data.nodes_to_start,
storage_mountpoint: Some(storage_mountpoint),
storage_drive: Some(storage_drive),
connection_mode: Some(connection_mode),
port_from: Some(port_from),
port_to: Some(port_to),
},
tick_rate,
frame_rate,
components: vec![
Expand Down Expand Up @@ -322,14 +326,17 @@ mod tests {

let mountpoint = get_primary_mount_point();

// Create a valid configuration file
// Create a valid configuration file with all fields
let valid_config = format!(
r#"
{{
"discord_username": "happy_user",
"nodes_to_start": 5,
"storage_mountpoint": "{}",
"storage_drive": "C:"
"storage_drive": "C:",
"connection_mode": "Automatic",
"port_from": 12000,
"port_to": 13000
}}
"#,
mountpoint.display()
Expand All @@ -348,13 +355,17 @@ mod tests {

match app_result {
Ok(app) => {
// Check if the discord_username and nodes_to_start were correctly loaded
// Check if all fields were correctly loaded
assert_eq!(app.app_data.discord_username, "happy_user");
assert_eq!(app.app_data.nodes_to_start, 5);
// Check if the storage_mountpoint is set correctly
assert_eq!(app.app_data.storage_mountpoint, Some(mountpoint));
// Check if the storage_drive is set correctly
assert_eq!(app.app_data.storage_drive, Some("C:".to_string()));
assert_eq!(
app.app_data.connection_mode,
Some(ConnectionMode::Automatic)
);
assert_eq!(app.app_data.port_from, Some(12000));
assert_eq!(app.app_data.port_to, Some(13000));

write!(output, "App created successfully with valid configuration")?;
}
Expand Down Expand Up @@ -382,11 +393,14 @@ mod tests {
let temp_dir = tempdir()?;
let test_app_data_path = temp_dir.path().join("test_app_data.json");

// Create a custom configuration file with only the first two settings
// Create a custom configuration file with only some settings
let custom_config = r#"
{
"discord_username": "test_user",
"nodes_to_start": 3
"nodes_to_start": 3,
"connection_mode": "Custom Ports",
"port_from": 12000,
"port_to": 13000
}
"#;
std::fs::write(&test_app_data_path, custom_config)?;
Expand All @@ -402,13 +416,20 @@ mod tests {

match app_result {
Ok(app) => {
// Check if the discord_username and nodes_to_start were correctly loaded
// Check if the fields were correctly loaded
assert_eq!(app.app_data.discord_username, "test_user");
assert_eq!(app.app_data.nodes_to_start, 3);
// Check if the storage_mountpoint is None (not set)
assert_eq!(app.app_data.storage_mountpoint, None);
// Check if the storage_drive is None (not set)
assert_eq!(app.app_data.storage_drive, None);
// Check if the storage_mountpoint is Some (automatically set)
assert!(app.app_data.storage_mountpoint.is_some());
// Check if the storage_drive is Some (automatically set)
assert!(app.app_data.storage_drive.is_some());
// Check the new fields
assert_eq!(
app.app_data.connection_mode,
Some(ConnectionMode::CustomPorts)
);
assert_eq!(app.app_data.port_from, Some(12000));
assert_eq!(app.app_data.port_to, Some(13000));

write!(
output,
Expand Down Expand Up @@ -453,8 +474,14 @@ mod tests {
Ok(app) => {
assert_eq!(app.app_data.discord_username, "");
assert_eq!(app.app_data.nodes_to_start, 1);
assert_eq!(app.app_data.storage_mountpoint, None);
assert_eq!(app.app_data.storage_drive, None);
assert!(app.app_data.storage_mountpoint.is_some());
assert!(app.app_data.storage_drive.is_some());
assert_eq!(
app.app_data.connection_mode,
Some(ConnectionMode::Automatic)
);
assert_eq!(app.app_data.port_from, Some(PORT_MIN));
assert_eq!(app.app_data.port_to, Some(PORT_MAX));

write!(
output,
Expand Down Expand Up @@ -491,7 +518,10 @@ mod tests {
"discord_username": "test_user",
"nodes_to_start": 5,
"storage_mountpoint": "/non/existent/path",
"storage_drive": "Z:"
"storage_drive": "Z:",
"connection_mode": "Custom Ports",
"port_from": 12000,
"port_to": 13000
}
"#;
std::fs::write(&config_path, invalid_config)?;
Expand Down Expand Up @@ -521,4 +551,51 @@ mod tests {

Ok(())
}

#[tokio::test]
async fn test_app_default_connection_mode_and_ports() -> Result<()> {
// Create a temporary directory for our test
let temp_dir = tempdir()?;
let test_app_data_path = temp_dir.path().join("test_app_data.json");

// Create a custom configuration file without connection mode and ports
let custom_config = r#"
{
"discord_username": "test_user",
"nodes_to_start": 3
}
"#;
std::fs::write(&test_app_data_path, custom_config)?;

// Create default PeersArgs
let peers_args = PeersArgs::default();

// Create and run the App
let app_result = App::new(60.0, 60.0, peers_args, None, Some(test_app_data_path)).await;

match app_result {
Ok(app) => {
// Check if the discord_username and nodes_to_start were correctly loaded
assert_eq!(app.app_data.discord_username, "test_user");
assert_eq!(app.app_data.nodes_to_start, 3);

// Check if the connection_mode is set to the default (Automatic)
assert_eq!(
app.app_data.connection_mode,
Some(ConnectionMode::Automatic)
);

// Check if the port range is set to the default values
assert_eq!(app.app_data.port_from, Some(PORT_MIN));
assert_eq!(app.app_data.port_to, Some(PORT_MAX));

println!("App created successfully with default connection mode and ports");
}
Err(e) => {
panic!("App creation failed: {}", e);
}
}

Ok(())
}
}
61 changes: 19 additions & 42 deletions node-launchpad/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// permissions and limitations relating to use of the SAFE Network Software.

use crate::connection_mode::ConnectionMode;
use crate::system;
use crate::system::get_primary_mount_point;
use crate::{action::Action, mode::Scene};
use color_eyre::eyre::{eyre, Result};
Expand Down Expand Up @@ -141,20 +140,9 @@ impl AppData {
let data = std::fs::read_to_string(&config_path)
.map_err(|_| color_eyre::eyre::eyre!("Failed to read app data file"))?;

let mut app_data: AppData = serde_json::from_str(&data)
let app_data: AppData = serde_json::from_str(&data)
.map_err(|_| color_eyre::eyre::eyre!("Failed to parse app data"))?;

if app_data.storage_mountpoint.is_none() || app_data.storage_drive.is_none() {
// If the storage drive is not set, set it to the default mount point
let drive_info = system::get_default_mount_point()?;
app_data.storage_drive = Some(drive_info.0);
app_data.storage_mountpoint = Some(drive_info.1);
debug!("Setting storage drive to {:?}", app_data.storage_mountpoint);
}

if app_data.connection_mode.is_none() {
app_data.connection_mode = Some(ConnectionMode::default());
}
Ok(app_data)
}

Expand Down Expand Up @@ -724,6 +712,9 @@ mod tests {
assert_eq!(app_data.nodes_to_start, 1);
assert_eq!(app_data.storage_mountpoint, None);
assert_eq!(app_data.storage_drive, None);
assert_eq!(app_data.connection_mode, None);
assert_eq!(app_data.port_from, None);
assert_eq!(app_data.port_to, None);

Ok(())
}
Expand All @@ -748,6 +739,9 @@ mod tests {
assert_eq!(app_data.nodes_to_start, 3);
assert_eq!(app_data.storage_mountpoint, None);
assert_eq!(app_data.storage_drive, None);
assert_eq!(app_data.connection_mode, None);
assert_eq!(app_data.port_from, None);
assert_eq!(app_data.port_to, None);

Ok(())
}
Expand All @@ -773,35 +767,9 @@ mod tests {
assert_eq!(app_data.nodes_to_start, 3);
assert_eq!(app_data.storage_mountpoint, None);
assert_eq!(app_data.storage_drive, Some("C:".to_string()));

Ok(())
}

#[test]
fn test_app_data_complete_info() -> Result<()> {
let temp_dir = tempdir()?;
let complete_data_path = temp_dir.path().join("complete_app_data.json");

let complete_data = r#"
{
"discord_username": "complete_user",
"nodes_to_start": 5,
"storage_mountpoint": "/mnt/data",
"storage_drive": "D:"
}
"#;

std::fs::write(&complete_data_path, complete_data)?;

let app_data = AppData::load(Some(complete_data_path))?;

assert_eq!(app_data.discord_username, "complete_user");
assert_eq!(app_data.nodes_to_start, 5);
assert_eq!(
app_data.storage_mountpoint,
Some(PathBuf::from("/mnt/data"))
);
assert_eq!(app_data.storage_drive, Some("D:".to_string()));
assert_eq!(app_data.connection_mode, None);
assert_eq!(app_data.port_from, None);
assert_eq!(app_data.port_to, None);

Ok(())
}
Expand All @@ -817,6 +785,9 @@ mod tests {
app_data.nodes_to_start = 4;
app_data.storage_mountpoint = Some(PathBuf::from("/mnt/test"));
app_data.storage_drive = Some("E:".to_string());
app_data.connection_mode = Some(ConnectionMode::CustomPorts);
app_data.port_from = Some(12000);
app_data.port_to = Some(13000);

// Save to custom path
app_data.save(Some(test_path.clone()))?;
Expand All @@ -831,6 +802,12 @@ mod tests {
Some(PathBuf::from("/mnt/test"))
);
assert_eq!(loaded_data.storage_drive, Some("E:".to_string()));
assert_eq!(
loaded_data.connection_mode,
Some(ConnectionMode::CustomPorts)
);
assert_eq!(loaded_data.port_from, Some(12000));
assert_eq!(loaded_data.port_to, Some(13000));

Ok(())
}
Expand Down

0 comments on commit a91fde0

Please sign in to comment.