node_launchpad/
app.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
// Copyright 2024 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use std::path::PathBuf;

use crate::{
    action::Action,
    components::{
        help::Help,
        options::Options,
        popup::{
            change_drive::ChangeDrivePopup, connection_mode::ChangeConnectionModePopUp,
            manage_nodes::ManageNodes, port_range::PortRangePopUp, reset_nodes::ResetNodesPopup,
            rewards_address::RewardsAddress, upgrade_nodes::UpgradeNodesPopUp,
        },
        status::{Status, StatusConfig},
        Component,
    },
    config::{get_launchpad_nodes_data_dir_path, AppData, Config},
    connection_mode::ConnectionMode,
    mode::{InputMode, Scene},
    node_mgmt::{PORT_MAX, PORT_MIN},
    style::SPACE_CADET,
    system::{get_default_mount_point, get_primary_mount_point, get_primary_mount_point_name},
    tui,
};
use ant_bootstrap::PeersArgs;
use color_eyre::eyre::Result;
use crossterm::event::KeyEvent;
use ratatui::{prelude::Rect, style::Style, widgets::Block};
use tokio::sync::mpsc;

pub struct App {
    pub config: Config,
    pub app_data: AppData,
    pub tick_rate: f64,
    pub frame_rate: f64,
    pub components: Vec<Box<dyn Component>>,
    pub should_quit: bool,
    pub should_suspend: bool,
    pub input_mode: InputMode,
    pub scene: Scene,
    pub last_tick_key_events: Vec<KeyEvent>,
}

impl App {
    pub async fn new(
        tick_rate: f64,
        frame_rate: f64,
        peers_args: PeersArgs,
        antnode_path: Option<PathBuf>,
        app_data_path: Option<PathBuf>,
        network_id: Option<u8>,
    ) -> Result<Self> {
        // Configurations
        let app_data = AppData::load(app_data_path)?;
        let config = Config::new()?;

        // Tries to set the data dir path based on the storage mountpoint set by the user,
        // if not set, it tries to get the default mount point (where the executable is) and
        // create the nodes data dir there.
        // If even that fails, it will create the nodes data dir in the primary mount point.
        let data_dir_path = match &app_data.storage_mountpoint {
            Some(path) => get_launchpad_nodes_data_dir_path(&PathBuf::from(path), true)?,
            None => match get_default_mount_point() {
                Ok((_, path)) => get_launchpad_nodes_data_dir_path(&path, true)?,
                Err(_) => get_launchpad_nodes_data_dir_path(&get_primary_mount_point(), true)?,
            },
        };
        debug!("Data dir path for nodes: {data_dir_path:?}");

        // App data default values
        let connection_mode = app_data
            .connection_mode
            .unwrap_or(ConnectionMode::Automatic);
        let port_from = app_data.port_from.unwrap_or(PORT_MIN);
        let port_to = app_data.port_to.unwrap_or(PORT_MAX);
        let storage_mountpoint = app_data
            .storage_mountpoint
            .clone()
            .unwrap_or(get_primary_mount_point());
        let storage_drive = app_data
            .storage_drive
            .clone()
            .unwrap_or(get_primary_mount_point_name()?);

        // Main Screens
        let status_config = StatusConfig {
            allocated_disk_space: app_data.nodes_to_start,
            rewards_address: app_data.discord_username.clone(),
            peers_args,
            network_id,
            antnode_path,
            data_dir_path,
            connection_mode,
            port_from: Some(port_from),
            port_to: Some(port_to),
        };

        let status = Status::new(status_config).await?;
        let options = Options::new(
            storage_mountpoint.clone(),
            storage_drive.clone(),
            app_data.discord_username.clone(),
            connection_mode,
            Some(port_from),
            Some(port_to),
        )
        .await?;
        let help = Help::new().await?;

        // Popups
        let reset_nodes = ResetNodesPopup::default();
        let manage_nodes = ManageNodes::new(app_data.nodes_to_start, storage_mountpoint.clone())?;
        let change_drive =
            ChangeDrivePopup::new(storage_mountpoint.clone(), app_data.nodes_to_start)?;
        let change_connection_mode = ChangeConnectionModePopUp::new(connection_mode)?;
        let port_range = PortRangePopUp::new(connection_mode, port_from, port_to);
        let rewards_address = RewardsAddress::new(app_data.discord_username.clone());
        let upgrade_nodes = UpgradeNodesPopUp::new(app_data.nodes_to_start);

        Ok(Self {
            config,
            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![
                // Sections
                Box::new(status),
                Box::new(options),
                Box::new(help),
                // Popups
                Box::new(change_drive),
                Box::new(change_connection_mode),
                Box::new(port_range),
                Box::new(rewards_address),
                Box::new(reset_nodes),
                Box::new(manage_nodes),
                Box::new(upgrade_nodes),
            ],
            should_quit: false,
            should_suspend: false,
            input_mode: InputMode::Navigation,
            scene: Scene::Status,
            last_tick_key_events: Vec::new(),
        })
    }

    pub async fn run(&mut self) -> Result<()> {
        let (action_tx, mut action_rx) = mpsc::unbounded_channel();

        let mut tui = tui::Tui::new()?
            .tick_rate(self.tick_rate)
            .frame_rate(self.frame_rate);
        // tui.mouse(true);
        tui.enter()?;

        for component in self.components.iter_mut() {
            component.register_action_handler(action_tx.clone())?;
            component.register_config_handler(self.config.clone())?;
            let size = tui.size()?;
            let rect = Rect::new(0, 0, size.width, size.height);
            component.init(rect)?;
        }

        loop {
            if let Some(e) = tui.next().await {
                match e {
                    tui::Event::Quit => action_tx.send(Action::Quit)?,
                    tui::Event::Tick => action_tx.send(Action::Tick)?,
                    tui::Event::Render => action_tx.send(Action::Render)?,
                    tui::Event::Resize(x, y) => action_tx.send(Action::Resize(x, y))?,
                    tui::Event::Key(key) => {
                        if self.input_mode == InputMode::Navigation {
                            if let Some(keymap) = self.config.keybindings.get(&self.scene) {
                                if let Some(action) = keymap.get(&vec![key]) {
                                    info!("Got action: {action:?}");
                                    action_tx.send(action.clone())?;
                                } else {
                                    // If the key was not handled as a single key action,
                                    // then consider it for multi-key combinations.
                                    self.last_tick_key_events.push(key);

                                    // Check for multi-key combinations
                                    if let Some(action) = keymap.get(&self.last_tick_key_events) {
                                        info!("Got action: {action:?}");
                                        action_tx.send(action.clone())?;
                                    }
                                }
                            };
                        } else if self.input_mode == InputMode::Entry {
                            for component in self.components.iter_mut() {
                                let send_back_actions = component.handle_events(Some(e.clone()))?;
                                for action in send_back_actions {
                                    action_tx.send(action)?;
                                }
                            }
                        }
                    }
                    _ => {}
                }
            }

            while let Ok(action) = action_rx.try_recv() {
                if action != Action::Tick && action != Action::Render {
                    debug!("{action:?}");
                }
                match action {
                    Action::Tick => {
                        self.last_tick_key_events.drain(..);
                    }
                    Action::Quit => self.should_quit = true,
                    Action::Suspend => self.should_suspend = true,
                    Action::Resume => self.should_suspend = false,
                    Action::Resize(w, h) => {
                        tui.resize(Rect::new(0, 0, w, h))?;
                        tui.draw(|f| {
                            for component in self.components.iter_mut() {
                                let r = component.draw(f, f.area());
                                if let Err(e) = r {
                                    action_tx
                                        .send(Action::Error(format!("Failed to draw: {:?}", e)))
                                        .unwrap();
                                }
                            }
                        })?;
                    }
                    Action::Render => {
                        tui.draw(|f| {
                            f.render_widget(
                                Block::new().style(Style::new().bg(SPACE_CADET)),
                                f.area(),
                            );
                            for component in self.components.iter_mut() {
                                let r = component.draw(f, f.area());
                                if let Err(e) = r {
                                    action_tx
                                        .send(Action::Error(format!("Failed to draw: {:?}", e)))
                                        .unwrap();
                                }
                            }
                        })?;
                    }
                    Action::SwitchScene(scene) => {
                        info!("Scene switched to: {scene:?}");
                        self.scene = scene;
                    }
                    Action::SwitchInputMode(mode) => {
                        info!("Input mode switched to: {mode:?}");
                        self.input_mode = mode;
                    }
                    // Storing Application Data
                    Action::StoreStorageDrive(ref drive_mountpoint, ref drive_name) => {
                        debug!("Storing storage drive: {drive_mountpoint:?}, {drive_name:?}");
                        self.app_data.storage_mountpoint = Some(drive_mountpoint.clone());
                        self.app_data.storage_drive = Some(drive_name.as_str().to_string());
                        self.app_data.save(None)?;
                    }
                    Action::StoreConnectionMode(ref mode) => {
                        debug!("Storing connection mode: {mode:?}");
                        self.app_data.connection_mode = Some(*mode);
                        self.app_data.save(None)?;
                    }
                    Action::StorePortRange(ref from, ref to) => {
                        debug!("Storing port range: {from:?}, {to:?}");
                        self.app_data.port_from = Some(*from);
                        self.app_data.port_to = Some(*to);
                        self.app_data.save(None)?;
                    }
                    Action::StoreRewardsAddress(ref rewards_address) => {
                        debug!("Storing rewards address: {rewards_address:?}");
                        self.app_data.discord_username.clone_from(rewards_address);
                        self.app_data.save(None)?;
                    }
                    Action::StoreNodesToStart(ref count) => {
                        debug!("Storing nodes to start: {count:?}");
                        self.app_data.nodes_to_start = *count;
                        self.app_data.save(None)?;
                    }
                    _ => {}
                }
                for component in self.components.iter_mut() {
                    if let Some(action) = component.update(action.clone())? {
                        action_tx.send(action)?
                    };
                }
            }
            if self.should_suspend {
                tui.suspend()?;
                action_tx.send(Action::Resume)?;
                tui = tui::Tui::new()?
                    .tick_rate(self.tick_rate)
                    .frame_rate(self.frame_rate);
                // tui.mouse(true);
                tui.enter()?;
            } else if self.should_quit {
                tui.stop()?;
                break;
            }
        }
        tui.exit()?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use ant_bootstrap::PeersArgs;
    use color_eyre::eyre::Result;
    use serde_json::json;
    use std::io::Cursor;
    use std::io::Write;
    use tempfile::tempdir;

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

        let mountpoint = get_primary_mount_point();

        let config = json!({
            "discord_username": "happy_user",
            "nodes_to_start": 5,
            "storage_mountpoint": mountpoint.display().to_string(),
            "storage_drive": "C:",
            "connection_mode": "Automatic",
            "port_from": 12000,
            "port_to": 13000
        });

        let valid_config = serde_json::to_string_pretty(&config)?;
        std::fs::write(&config_path, valid_config)?;

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

        // Create a buffer to capture output
        let mut output = Cursor::new(Vec::new());

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

        match app_result {
            Ok(app) => {
                // 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);
                assert_eq!(app.app_data.storage_mountpoint, Some(mountpoint));
                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")?;
            }
            Err(e) => {
                write!(output, "App creation failed: {}", e)?;
            }
        }

        // Convert captured output to string
        let output_str = String::from_utf8(output.into_inner())?;

        // Check if the success message is in the output
        assert!(
            output_str.contains("App created successfully with valid configuration"),
            "Unexpected output: {}",
            output_str
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_app_should_run_when_storage_mountpoint_not_set() -> 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 with only some settings
        let custom_config = r#"
        {
            "discord_username": "test_user",
            "nodes_to_start": 3,
            "connection_mode": "Custom Ports",
            "port_from": 12000,
            "port_to": 13000
        }
        "#;
        std::fs::write(&test_app_data_path, custom_config)?;

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

        // Create a buffer to capture output
        let mut output = Cursor::new(Vec::new());

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

        match app_result {
            Ok(app) => {
                // 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 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,
                    "App created successfully with partial configuration"
                )?;
            }
            Err(e) => {
                write!(output, "App creation failed: {}", e)?;
            }
        }

        // Convert captured output to string
        let output_str = String::from_utf8(output.into_inner())?;

        // Check if the success message is in the output
        assert!(
            output_str.contains("App created successfully with partial configuration"),
            "Unexpected output: {}",
            output_str
        );

        Ok(())
    }

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

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

        // Create a buffer to capture output
        let mut output = Cursor::new(Vec::new());

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

        match app_result {
            Ok(app) => {
                assert_eq!(app.app_data.discord_username, "");
                assert_eq!(app.app_data.nodes_to_start, 1);
                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,
                    "App created successfully with default configuration"
                )?;
            }
            Err(e) => {
                write!(output, "App creation failed: {}", e)?;
            }
        }

        // Convert captured output to string
        let output_str = String::from_utf8(output.into_inner())?;

        // Check if the success message is in the output
        assert!(
            output_str.contains("App created successfully with default configuration"),
            "Unexpected output: {}",
            output_str
        );

        Ok(())
    }

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

        // Create a configuration file with an invalid storage_mountpoint
        let invalid_config = r#"
        {
            "discord_username": "test_user",
            "nodes_to_start": 5,
            "storage_mountpoint": "/non/existent/path",
            "storage_drive": "Z:",
            "connection_mode": "Custom Ports",
            "port_from": 12000,
            "port_to": 13000
        }
        "#;
        std::fs::write(&config_path, invalid_config)?;

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

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

        // Could be that the mountpoint doesn't exists
        // or that the user doesn't have permissions to access it
        match app_result {
            Ok(_) => {
                panic!("App creation should have failed due to invalid storage_mountpoint");
            }
            Err(e) => {
                assert!(
                    e.to_string().contains(
                        "Cannot find the primary disk. Configuration file might be wrong."
                    ) || e.to_string().contains("Failed to create nodes data dir in"),
                    "Unexpected error message: {}",
                    e
                );
            }
        }

        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), None).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(())
    }
}