aptmatic 0.1.1

A TUI for managing apt updates across debian / ubuntu hosts
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
use std::collections::VecDeque;
use std::io;
use std::time::Duration;

use anyhow::Result;
use crossterm::{
    event::{
        DisableMouseCapture, EnableMouseCapture, Event, EventStream, KeyCode, KeyEventKind,
        KeyModifiers, MouseButton, MouseEventKind,
    },
    execute,
    terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
};
use futures::StreamExt;
use ratatui::{Terminal, backend::CrosstermBackend};
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
use tokio::time::interval;

use crate::apt::HostInfo;
use crate::config::{Config, HostConfig, SidebarRow};

pub const TASK_OUTPUT_CAP: usize = 5_000;

// ── Messages flowing from background tasks to the app ────────────────────────

#[derive(Debug)]
pub enum AppMessage {
    GatherDone {
        host_idx: usize,
        result: Result<HostInfo, String>,
    },
    TaskLine {
        host_idx: usize,
        line: String,
    },
    TaskDone {
        host_idx: usize,
        exit_code: i32,
    },
    TaskFailed {
        host_idx: usize,
        error: String,
    },
}

// ── Per-host status ───────────────────────────────────────────────────────────

#[derive(Debug, Clone, PartialEq)]
pub enum HostStatus {
    Unknown,
    Connecting,
    Gathering,
    Ready,
    Error(String),
}

// ── Task kinds (operations triggered by the user) ─────────────────────────────

#[derive(Debug, Clone, PartialEq)]
pub enum TaskKind {
    Update,
    Upgrade,
    FullUpgrade,
    AutoRemove,
    PurgeRc,
    Reboot,
}

impl TaskKind {
    pub fn label(&self) -> &'static str {
        match self {
            TaskKind::Update => "apt-get update",
            TaskKind::Upgrade => "apt-get upgrade",
            TaskKind::FullUpgrade => "apt-get full-upgrade",
            TaskKind::AutoRemove => "apt-get autoremove --purge",
            TaskKind::PurgeRc => "purge RC packages",
            TaskKind::Reboot => "reboot",
        }
    }

    pub fn command(&self, use_sudo: bool) -> String {
        let sudo = if use_sudo { "sudo -n " } else { "" };
        match self {
            TaskKind::Update => {
                format!("DEBIAN_FRONTEND=noninteractive LC_ALL=C {sudo}apt-get update 2>&1")
            }
            TaskKind::Upgrade => {
                format!("DEBIAN_FRONTEND=noninteractive LC_ALL=C {sudo}apt-get -y upgrade 2>&1")
            }
            TaskKind::FullUpgrade => {
                format!(
                    "DEBIAN_FRONTEND=noninteractive LC_ALL=C {sudo}apt-get -y full-upgrade 2>&1"
                )
            }
            TaskKind::AutoRemove => {
                format!(
                    "DEBIAN_FRONTEND=noninteractive LC_ALL=C {sudo}apt-get -y autoremove --purge 2>&1"
                )
            }
            TaskKind::PurgeRc => format!(
                r#"pkgs=$(LC_ALL=C dpkg -l | awk '/^rc/{{print $2}}'); [ -n "$pkgs" ] && echo "$pkgs" | xargs {sudo}dpkg --purge 2>&1 || echo "No RC packages to purge""#
            ),
            TaskKind::Reboot => format!("{sudo}reboot 2>&1"),
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum TaskStatus {
    Running,
    Done(i32),
    Failed(String),
}

#[derive(Debug, Clone)]
pub struct TaskState {
    pub kind: TaskKind,
    pub status: TaskStatus,
    pub output: VecDeque<String>,
    pub scroll_offset: u16,
    pub auto_scroll: bool,
}

impl TaskState {
    pub fn new(kind: TaskKind) -> Self {
        Self {
            kind,
            status: TaskStatus::Running,
            output: VecDeque::new(),
            scroll_offset: 0,
            auto_scroll: true,
        }
    }

    pub fn push_line(&mut self, line: String) {
        if self.output.len() >= TASK_OUTPUT_CAP {
            self.output.pop_front();
        }
        self.output.push_back(line);
    }
}

// ── Per-host application state ────────────────────────────────────────────────

#[derive(Debug)]
pub struct HostState {
    pub cfg: HostConfig,
    pub status: HostStatus,
    pub info: Option<HostInfo>,
    pub task: Option<TaskState>,
}

impl HostState {
    pub fn new(cfg: HostConfig) -> Self {
        Self {
            cfg,
            status: HostStatus::Unknown,
            info: None,
            task: None,
        }
    }
}

// ── Reboot confirmation modal state ──────────────────────────────────────────

#[derive(Debug)]
pub struct RebootConfirmState {
    pub host_idx: usize,
    pub input: String,
    /// True when the user pressed Enter with an incorrect hostname.
    pub mismatch: bool,
}

// ── Application state ─────────────────────────────────────────────────────────

const SIDEBAR_MIN_WIDTH: u16 = 10;
const SIDEBAR_MAX_WIDTH: u16 = 100;

pub struct App {
    pub hosts: Vec<HostState>,
    pub sidebar_rows: Vec<SidebarRow>,
    pub selected_row: usize,
    pub tx: UnboundedSender<AppMessage>,
    pub tick: u64,
    /// If Some, the user is viewing the task output overlay for this host.
    pub viewing_task: Option<usize>,
    /// When true, the sidebar is hidden and the detail panel fills the screen.
    pub detail_zoom: bool,
    /// Width of the sidebar panel in columns.
    pub sidebar_width: u16,
    /// Whether the user is currently dragging the sidebar divider.
    pub dragging_sidebar: bool,
    /// When Some, the reboot confirmation modal is active.
    pub reboot_confirm: Option<RebootConfirmState>,
}

impl App {
    pub fn new(config: &Config, tx: UnboundedSender<AppMessage>) -> Self {
        let host_cfgs = config.resolved_hosts();
        let sidebar_rows = config.sidebar_rows(&host_cfgs);
        let hosts: Vec<HostState> = host_cfgs.into_iter().map(HostState::new).collect();
        Self {
            hosts,
            sidebar_rows,
            selected_row: 0,
            tx,
            tick: 0,
            viewing_task: None,
            detail_zoom: false,
            sidebar_width: {
                let max = crossterm::terminal::size()
                    .map(|(w, _)| w.saturating_sub(SIDEBAR_MIN_WIDTH))
                    .unwrap_or(SIDEBAR_MAX_WIDTH);
                crossterm::terminal::size()
                    .map(|(w, _)| w / 2)
                    .unwrap_or(40)
                    .clamp(SIDEBAR_MIN_WIDTH, max)
            },
            dragging_sidebar: false,
            reboot_confirm: None,
        }
    }

    /// Indices of all hosts under the currently selected sidebar row.
    pub fn selected_host_indices(&self) -> Vec<usize> {
        match self.sidebar_rows.get(self.selected_row) {
            Some(SidebarRow::Host { host_idx }) => vec![*host_idx],
            Some(SidebarRow::Group { .. }) => {
                // Collect Host rows immediately following this Group row, stopping
                // at the next Group row. Matching by position (not name) avoids
                // incorrectly merging duplicate-named groups.
                let mut idxs = Vec::new();
                for row in self.sidebar_rows.iter().skip(self.selected_row + 1) {
                    match row {
                        SidebarRow::Group { .. } => break,
                        SidebarRow::Host { host_idx } => idxs.push(*host_idx),
                    }
                }
                idxs
            }
            None => vec![],
        }
    }

    /// Trigger a gather refresh for one host.
    pub fn start_refresh(&self, host_idx: usize) {
        let cfg = self.hosts[host_idx].cfg.clone();
        let tx = self.tx.clone();
        tokio::task::spawn_blocking(move || {
            let result = crate::gather::gather(&cfg).map_err(|e| format!("{e:#}"));
            let _ = tx.send(AppMessage::GatherDone { host_idx, result });
        });
    }

    /// Trigger an apt task on one host. Does nothing if a task is already running.
    pub fn start_task(&mut self, host_idx: usize, kind: TaskKind) {
        if matches!(
            self.hosts[host_idx].task.as_ref().map(|t| &t.status),
            Some(TaskStatus::Running)
        ) {
            return;
        }
        let cfg = self.hosts[host_idx].cfg.clone();
        let tx = self.tx.clone();
        let cmd = kind.command(cfg.use_sudo);
        self.hosts[host_idx].task = Some(TaskState::new(kind));
        tokio::task::spawn_blocking(move || {
            let sess = match crate::ssh::SshSession::connect(&cfg) {
                Ok(s) => s,
                Err(e) => {
                    let _ = tx.send(AppMessage::TaskLine {
                        host_idx,
                        line: format!("Connection failed: {e:#}"),
                    });
                    let _ = tx.send(AppMessage::TaskFailed {
                        host_idx,
                        error: format!("{e:#}"),
                    });
                    return;
                }
            };
            let tx_cb = tx.clone();
            let exit = sess.exec_streaming(&cmd, move |line| {
                let _ = tx_cb.send(AppMessage::TaskLine { host_idx, line });
            });
            let exit_code = match exit {
                Ok(code) => code,
                Err(e) => {
                    let _ = tx.send(AppMessage::TaskLine {
                        host_idx,
                        line: format!("Error: {e:#}"),
                    });
                    -1
                }
            };
            let _ = tx.send(AppMessage::TaskDone {
                host_idx,
                exit_code,
            });
        });
    }

    pub fn handle_message(&mut self, msg: AppMessage) {
        match msg {
            AppMessage::GatherDone { host_idx, result } => {
                let h = &mut self.hosts[host_idx];
                match result {
                    Ok(info) => {
                        h.info = Some(info);
                        h.status = HostStatus::Ready;
                    }
                    Err(e) => {
                        h.status = HostStatus::Error(e);
                    }
                }
            }
            AppMessage::TaskLine { host_idx, line } => {
                if let Some(task) = self.hosts[host_idx].task.as_mut() {
                    task.push_line(line);
                }
            }
            AppMessage::TaskDone {
                host_idx,
                exit_code,
            } => {
                if let Some(task) = self.hosts[host_idx].task.as_mut() {
                    task.status = TaskStatus::Done(exit_code);
                    task.auto_scroll = true;
                }
                // Re-gather host info after task completes
                self.hosts[host_idx].status = HostStatus::Gathering;
                self.start_refresh(host_idx);
            }
            AppMessage::TaskFailed { host_idx, error } => {
                if let Some(task) = self.hosts[host_idx].task.as_mut() {
                    task.status = TaskStatus::Failed(error);
                    task.auto_scroll = true;
                }
            }
        }
    }

    pub fn handle_key(&mut self, code: KeyCode, modifiers: KeyModifiers) -> bool {
        // ── Reboot confirmation modal (highest priority) ──
        if self.reboot_confirm.is_some() {
            return self.handle_key_reboot_confirm(code, modifiers);
        }

        // ── Task output overlay ──
        if let Some(host_idx) = self.viewing_task {
            return self.handle_key_task_view(host_idx, code);
        }

        match (code, modifiers) {
            // Navigate sidebar
            (KeyCode::Up, _) | (KeyCode::Char('k'), KeyModifiers::NONE)
                if self.selected_row > 0 =>
            {
                self.selected_row -= 1;
            }
            (KeyCode::Down, _) | (KeyCode::Char('j'), KeyModifiers::NONE)
                if self.selected_row + 1 < self.sidebar_rows.len() =>
            {
                self.selected_row += 1;
            }
            // apt-get update + refresh (selected)
            (KeyCode::Char('r'), KeyModifiers::NONE) => {
                for idx in self.selected_host_indices() {
                    self.start_task(idx, TaskKind::Update);
                }
            }
            // apt-get update + refresh (all)
            (KeyCode::Char('R'), _) => {
                for idx in 0..self.hosts.len() {
                    self.start_task(idx, TaskKind::Update);
                }
            }
            // Reboot — opens confirmation modal for single-host selection
            (KeyCode::Char('b'), _) if self.selected_host_indices().len() == 1 => {
                let idx = self.selected_host_indices()[0];
                self.reboot_confirm = Some(RebootConfirmState {
                    host_idx: idx,
                    input: String::new(),
                    mismatch: false,
                });
            }
            // apt-get upgrade (selected)
            (KeyCode::Char('u'), KeyModifiers::NONE) => {
                for idx in self.selected_host_indices() {
                    self.start_task(idx, TaskKind::Upgrade);
                }
            }
            // apt-get upgrade (all)
            (KeyCode::Char('U'), _) => {
                for idx in 0..self.hosts.len() {
                    self.start_task(idx, TaskKind::Upgrade);
                }
            }
            // apt-get full-upgrade (selected)
            (KeyCode::Char('f'), KeyModifiers::NONE) => {
                for idx in self.selected_host_indices() {
                    self.start_task(idx, TaskKind::FullUpgrade);
                }
            }
            // apt-get full-upgrade (all)
            (KeyCode::Char('F'), _) => {
                for idx in 0..self.hosts.len() {
                    self.start_task(idx, TaskKind::FullUpgrade);
                }
            }
            // apt-get autoremove --purge (selected)
            (KeyCode::Char('a'), KeyModifiers::NONE) => {
                for idx in self.selected_host_indices() {
                    self.start_task(idx, TaskKind::AutoRemove);
                }
            }
            // apt-get autoremove --purge (all)
            (KeyCode::Char('A'), _) => {
                for idx in 0..self.hosts.len() {
                    self.start_task(idx, TaskKind::AutoRemove);
                }
            }
            // Purge RC packages
            (KeyCode::Char('p'), KeyModifiers::NONE) => {
                for idx in self.selected_host_indices() {
                    self.start_task(idx, TaskKind::PurgeRc);
                }
            }
            // View task output
            (KeyCode::Char('t'), _) | (KeyCode::Enter, _) => {
                // Find a host with an active or completed task in the selection
                for idx in self.selected_host_indices() {
                    if self.hosts[idx].task.is_some() {
                        self.viewing_task = Some(idx);
                        break;
                    }
                }
            }
            // Zoom detail panel (hide sidebar for clean text selection)
            (KeyCode::Char('z'), KeyModifiers::NONE) => {
                self.detail_zoom = !self.detail_zoom;
            }
            // Quit
            (KeyCode::Char('q'), _) | (KeyCode::Char('c'), KeyModifiers::CONTROL) => {
                return false;
            }
            (KeyCode::Esc, _) => return false,
            _ => {}
        }
        true
    }

    fn handle_key_reboot_confirm(&mut self, code: KeyCode, modifiers: KeyModifiers) -> bool {
        match (code, modifiers) {
            (KeyCode::Esc, _) | (KeyCode::Char('c'), KeyModifiers::CONTROL) => {
                self.reboot_confirm = None;
            }
            (KeyCode::Backspace, _) => {
                if let Some(state) = self.reboot_confirm.as_mut() {
                    state.input.pop();
                    state.mismatch = false;
                }
            }
            (KeyCode::Enter, _) => {
                // Extract what we need before mutating, to satisfy the borrow checker.
                let check = self.reboot_confirm.as_ref().map(|state| {
                    let expected = self.hosts[state.host_idx].cfg.hostname.clone();
                    (state.host_idx, state.input == expected)
                });
                if let Some((host_idx, matches)) = check {
                    if matches {
                        self.reboot_confirm = None;
                        self.start_task(host_idx, TaskKind::Reboot);
                    } else if let Some(state) = self.reboot_confirm.as_mut() {
                        state.mismatch = true;
                    }
                }
            }
            // Accept printable characters (but not control/alt combinations).
            (KeyCode::Char(c), m) if !m.intersects(KeyModifiers::CONTROL | KeyModifiers::ALT) => {
                if let Some(state) = self.reboot_confirm.as_mut() {
                    state.input.push(c);
                    state.mismatch = false;
                }
            }
            _ => {}
        }
        true
    }

    fn handle_key_task_view(&mut self, host_idx: usize, code: KeyCode) -> bool {
        match code {
            KeyCode::Esc | KeyCode::Char('q') => {
                self.viewing_task = None;
            }
            KeyCode::Up | KeyCode::Char('k') => {
                if let Some(task) = self.hosts[host_idx].task.as_mut() {
                    task.auto_scroll = false;
                    task.scroll_offset = task.scroll_offset.saturating_sub(1);
                }
            }
            KeyCode::Down | KeyCode::Char('j') => {
                if let Some(task) = self.hosts[host_idx].task.as_mut() {
                    task.auto_scroll = false;
                    task.scroll_offset = task.scroll_offset.saturating_add(1);
                }
            }
            KeyCode::PageUp => {
                if let Some(task) = self.hosts[host_idx].task.as_mut() {
                    task.auto_scroll = false;
                    task.scroll_offset = task.scroll_offset.saturating_sub(20);
                }
            }
            KeyCode::PageDown => {
                if let Some(task) = self.hosts[host_idx].task.as_mut() {
                    task.auto_scroll = false;
                    task.scroll_offset = task.scroll_offset.saturating_add(20);
                }
            }
            KeyCode::End | KeyCode::Char('G') => {
                if let Some(task) = self.hosts[host_idx].task.as_mut() {
                    task.auto_scroll = true;
                }
            }
            _ => {}
        }
        true
    }

    pub fn handle_mouse(&mut self, kind: MouseEventKind, col: u16) {
        // Block mouse interaction while the confirmation modal is visible.
        if self.reboot_confirm.is_some() {
            return;
        }
        match kind {
            MouseEventKind::Down(MouseButton::Left)
                if !self.detail_zoom && col == self.sidebar_width.saturating_sub(1) =>
            {
                self.dragging_sidebar = true;
            }
            MouseEventKind::Drag(MouseButton::Left) if self.dragging_sidebar => {
                let max = crossterm::terminal::size()
                    .map(|(w, _)| w.saturating_sub(SIDEBAR_MIN_WIDTH))
                    .unwrap_or(SIDEBAR_MAX_WIDTH);
                self.sidebar_width = (col + 1).clamp(SIDEBAR_MIN_WIDTH, max);
            }
            MouseEventKind::Up(MouseButton::Left) => {
                self.dragging_sidebar = false;
            }
            _ => {}
        }
    }
}

// ── Terminal guard ────────────────────────────────────────────────────────────

pub struct TerminalGuard;

impl Drop for TerminalGuard {
    fn drop(&mut self) {
        let _ = execute!(io::stdout(), DisableMouseCapture, LeaveAlternateScreen);
        let _ = disable_raw_mode();
    }
}

// ── Main event loop ───────────────────────────────────────────────────────────

pub async fn run(mut app: App, mut rx: UnboundedReceiver<AppMessage>) -> Result<()> {
    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;

    let _guard = TerminalGuard;

    let backend = CrosstermBackend::new(io::stdout());
    let mut terminal = Terminal::new(backend)?;

    // Kick off initial gather for all hosts
    for idx in 0..app.hosts.len() {
        app.hosts[idx].status = HostStatus::Connecting;
        app.start_refresh(idx);
    }

    let mut events = EventStream::new();
    let mut tick_interval = interval(Duration::from_millis(100));

    loop {
        terminal.draw(|f| crate::ui::render(f, &mut app))?;

        tokio::select! {
            _ = tick_interval.tick() => {
                app.tick = app.tick.wrapping_add(1);
            }
            Some(msg) = rx.recv() => {
                app.handle_message(msg);
            }
            Some(Ok(event)) = events.next() => {
                match event {
                    Event::Key(key)
                        if key.kind == KeyEventKind::Press
                            && !app.handle_key(key.code, key.modifiers) =>
                    {
                        break;
                    }
                    Event::Mouse(mouse) => {
                        app.handle_mouse(mouse.kind, mouse.column);
                    }
                    _ => {}
                }
            }
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{Defaults, RawConfig, RawGroup, RawHost};

    fn make_app(raw: RawConfig) -> App {
        let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
        let config = crate::config::Config { raw };
        App::new(&config, tx)
    }

    fn raw_host(hostname: &str) -> RawHost {
        RawHost {
            hostname: hostname.to_string(),
            user: Some("alice".to_string()),
            port: None,
            use_sudo: None,
            identity_file: None,
        }
    }

    // ── TaskKind::command ─────────────────────────────────────────────────────

    #[test]
    fn task_kind_command_update_with_sudo() {
        let cmd = TaskKind::Update.command(true);
        assert!(cmd.contains("sudo -n"));
        assert!(cmd.contains("apt-get update"));
    }

    #[test]
    fn task_kind_command_update_without_sudo() {
        let cmd = TaskKind::Update.command(false);
        assert!(!cmd.contains("sudo"));
        assert!(cmd.contains("apt-get update"));
    }

    #[test]
    fn task_kind_command_upgrade_with_sudo() {
        let cmd = TaskKind::Upgrade.command(true);
        assert!(cmd.contains("sudo -n"));
        assert!(cmd.contains("apt-get") && cmd.contains("upgrade"));
    }

    #[test]
    fn task_kind_command_upgrade_without_sudo() {
        let cmd = TaskKind::Upgrade.command(false);
        assert!(!cmd.contains("sudo"));
        assert!(cmd.contains("apt-get") && cmd.contains("upgrade"));
    }

    #[test]
    fn task_kind_full_upgrade_label() {
        assert_eq!(TaskKind::FullUpgrade.label(), "apt-get full-upgrade");
    }

    #[test]
    fn task_kind_command_full_upgrade_with_sudo() {
        let cmd = TaskKind::FullUpgrade.command(true);
        assert!(cmd.contains("sudo -n"));
        assert!(cmd.contains("apt-get") && cmd.contains("full-upgrade"));
    }

    #[test]
    fn task_kind_command_full_upgrade_without_sudo() {
        let cmd = TaskKind::FullUpgrade.command(false);
        assert!(!cmd.contains("sudo"));
        assert!(cmd.contains("apt-get") && cmd.contains("full-upgrade"));
    }

    #[test]
    fn task_kind_command_full_upgrade_is_noninteractive() {
        let cmd = TaskKind::FullUpgrade.command(false);
        assert!(cmd.contains("DEBIAN_FRONTEND=noninteractive"));
    }

    #[test]
    fn task_kind_command_full_upgrade_not_same_as_upgrade() {
        let upgrade = TaskKind::Upgrade.command(false);
        let full_upgrade = TaskKind::FullUpgrade.command(false);
        assert_ne!(upgrade, full_upgrade);
    }

    #[test]
    fn task_kind_autoremove_label() {
        assert_eq!(TaskKind::AutoRemove.label(), "apt-get autoremove --purge");
    }

    #[test]
    fn task_kind_command_autoremove_with_sudo() {
        let cmd = TaskKind::AutoRemove.command(true);
        assert!(cmd.contains("sudo -n"));
        assert!(cmd.contains("autoremove") && cmd.contains("--purge"));
    }

    #[test]
    fn task_kind_command_autoremove_without_sudo() {
        let cmd = TaskKind::AutoRemove.command(false);
        assert!(!cmd.contains("sudo"));
        assert!(cmd.contains("autoremove") && cmd.contains("--purge"));
    }

    #[test]
    fn task_kind_command_autoremove_is_noninteractive() {
        let cmd = TaskKind::AutoRemove.command(false);
        assert!(cmd.contains("DEBIAN_FRONTEND=noninteractive"));
    }

    #[test]
    fn task_kind_command_purge_rc_with_sudo() {
        let cmd = TaskKind::PurgeRc.command(true);
        assert!(cmd.contains("sudo -n"));
        assert!(cmd.contains("dpkg --purge"));
    }

    #[test]
    fn task_kind_command_purge_rc_without_sudo() {
        let cmd = TaskKind::PurgeRc.command(false);
        assert!(!cmd.contains("sudo"));
        assert!(cmd.contains("dpkg --purge"));
    }

    // ── handle_mouse ──────────────────────────────────────────────────────────

    fn bare_app() -> App {
        make_app(RawConfig {
            defaults: Defaults {
                user: Some("alice".to_string()),
                ..Default::default()
            },
            hosts: vec![raw_host("h1.example.com")],
            ..Default::default()
        })
    }

    #[test]
    fn handle_mouse_down_on_border_starts_drag() {
        let mut app = bare_app();
        app.sidebar_width = 28;
        app.handle_mouse(MouseEventKind::Down(MouseButton::Left), 27);
        assert!(app.dragging_sidebar);
    }

    #[test]
    fn handle_mouse_down_off_border_does_not_start_drag() {
        let mut app = bare_app();
        app.sidebar_width = 28;
        app.handle_mouse(MouseEventKind::Down(MouseButton::Left), 10);
        assert!(!app.dragging_sidebar);
    }

    #[test]
    fn handle_mouse_drag_updates_sidebar_width() {
        let mut app = bare_app();
        app.dragging_sidebar = true;
        app.handle_mouse(MouseEventKind::Drag(MouseButton::Left), 39);
        assert_eq!(app.sidebar_width, 40);
    }

    #[test]
    fn handle_mouse_drag_clamps_to_minimum() {
        let mut app = bare_app();
        app.dragging_sidebar = true;
        app.handle_mouse(MouseEventKind::Drag(MouseButton::Left), 0);
        assert_eq!(app.sidebar_width, SIDEBAR_MIN_WIDTH);
    }

    #[test]
    fn handle_mouse_drag_clamps_to_maximum() {
        let mut app = bare_app();
        app.dragging_sidebar = true;
        app.handle_mouse(MouseEventKind::Drag(MouseButton::Left), 200);
        let expected_max = crossterm::terminal::size()
            .map(|(w, _)| w.saturating_sub(SIDEBAR_MIN_WIDTH))
            .unwrap_or(SIDEBAR_MAX_WIDTH);
        assert_eq!(app.sidebar_width, expected_max);
    }

    #[test]
    fn handle_mouse_up_stops_drag() {
        let mut app = bare_app();
        app.dragging_sidebar = true;
        app.handle_mouse(MouseEventKind::Up(MouseButton::Left), 0);
        assert!(!app.dragging_sidebar);
    }

    #[test]
    fn handle_mouse_down_on_border_blocked_when_detail_zoom() {
        let mut app = bare_app();
        app.sidebar_width = 28;
        app.detail_zoom = true;
        app.handle_mouse(MouseEventKind::Down(MouseButton::Left), 27);
        assert!(!app.dragging_sidebar);
    }

    // ── selected_host_indices ─────────────────────────────────────────────────

    #[test]
    fn selected_host_indices_single_host() {
        let app = make_app(RawConfig {
            defaults: Defaults {
                user: Some("alice".to_string()),
                ..Default::default()
            },
            hosts: vec![raw_host("h1.example.com")],
            ..Default::default()
        });
        // sidebar_rows = [Host{0}]; selected_row defaults to 0
        assert_eq!(app.selected_host_indices(), vec![0]);
    }

    #[test]
    fn selected_host_indices_group_returns_all_children() {
        let app = make_app(RawConfig {
            defaults: Defaults {
                user: Some("alice".to_string()),
                ..Default::default()
            },
            groups: vec![RawGroup {
                name: "web".to_string(),
                user: None,
                port: None,
                use_sudo: None,
                identity_file: None,
                hosts: vec![raw_host("web1.example.com"), raw_host("web2.example.com")],
            }],
            ..Default::default()
        });
        // sidebar_rows = [Group, Host{0}, Host{1}]; selected_row=0 => Group
        assert_eq!(app.selected_host_indices(), vec![0, 1]);
    }

    #[test]
    fn selected_host_indices_duplicate_group_names_are_independent() {
        // Two groups with the same name — selecting the first should only return its hosts
        let app = make_app(RawConfig {
            defaults: Defaults {
                user: Some("alice".to_string()),
                ..Default::default()
            },
            groups: vec![
                RawGroup {
                    name: "web".to_string(),
                    user: None,
                    port: None,
                    use_sudo: None,
                    identity_file: None,
                    hosts: vec![raw_host("web1.example.com")],
                },
                RawGroup {
                    name: "web".to_string(),
                    user: None,
                    port: None,
                    use_sudo: None,
                    identity_file: None,
                    hosts: vec![raw_host("web2.example.com")],
                },
            ],
            ..Default::default()
        });
        // rows: [Group"web", Host{0}, Group"web", Host{1}]
        // selecting row 0 (first "web" group) should only return [0]
        assert_eq!(app.selected_host_indices(), vec![0]);
    }

    #[test]
    fn selected_host_indices_empty_group_returns_empty() {
        let mut app = make_app(RawConfig {
            groups: vec![RawGroup {
                name: "empty".to_string(),
                user: None,
                port: None,
                use_sudo: None,
                identity_file: None,
                hosts: vec![],
            }],
            ..Default::default()
        });
        app.selected_row = 0; // Group "empty"
        assert_eq!(app.selected_host_indices(), vec![]);
    }

    #[test]
    fn selected_host_indices_out_of_bounds_returns_empty() {
        let mut app = bare_app();
        app.selected_row = 999;
        assert_eq!(app.selected_host_indices(), vec![]);
    }

    // ── Reboot confirmation modal ─────────────────────────────────────────────

    fn one_host_app() -> App {
        make_app(RawConfig {
            hosts: vec![raw_host("myserver")],
            ..Default::default()
        })
    }

    #[test]
    fn b_key_opens_reboot_modal() {
        let mut app = one_host_app();
        app.selected_row = 0;
        app.handle_key(KeyCode::Char('b'), KeyModifiers::NONE);
        let state = app.reboot_confirm.as_ref().expect("modal should be open");
        assert_eq!(state.host_idx, 0);
        assert!(state.input.is_empty());
        assert!(!state.mismatch);
    }

    #[test]
    fn esc_closes_reboot_modal() {
        let mut app = one_host_app();
        app.selected_row = 0;
        app.handle_key(KeyCode::Char('b'), KeyModifiers::NONE);
        assert!(app.reboot_confirm.is_some());
        app.handle_key(KeyCode::Esc, KeyModifiers::NONE);
        assert!(app.reboot_confirm.is_none());
    }

    #[test]
    fn ctrl_c_closes_reboot_modal() {
        let mut app = one_host_app();
        app.selected_row = 0;
        app.handle_key(KeyCode::Char('b'), KeyModifiers::NONE);
        assert!(app.reboot_confirm.is_some());
        app.handle_key(KeyCode::Char('c'), KeyModifiers::CONTROL);
        assert!(app.reboot_confirm.is_none());
    }

    #[test]
    fn typing_in_modal_updates_input() {
        let mut app = one_host_app();
        app.selected_row = 0;
        app.handle_key(KeyCode::Char('b'), KeyModifiers::NONE);
        app.handle_key(KeyCode::Char('m'), KeyModifiers::NONE);
        app.handle_key(KeyCode::Char('y'), KeyModifiers::NONE);
        let state = app.reboot_confirm.as_ref().unwrap();
        assert_eq!(state.input, "my");
    }

    #[test]
    fn backspace_removes_char_and_clears_mismatch() {
        let mut app = one_host_app();
        app.selected_row = 0;
        app.handle_key(KeyCode::Char('b'), KeyModifiers::NONE);
        app.handle_key(KeyCode::Char('x'), KeyModifiers::NONE);
        // Force mismatch state
        app.reboot_confirm.as_mut().unwrap().mismatch = true;
        app.handle_key(KeyCode::Backspace, KeyModifiers::NONE);
        let state = app.reboot_confirm.as_ref().unwrap();
        assert!(state.input.is_empty());
        assert!(!state.mismatch);
    }

    #[test]
    fn enter_with_wrong_hostname_sets_mismatch_keeps_modal_open() {
        let mut app = one_host_app();
        app.selected_row = 0;
        app.handle_key(KeyCode::Char('b'), KeyModifiers::NONE);
        app.handle_key(KeyCode::Char('w'), KeyModifiers::NONE); // "w" != "myserver"
        app.handle_key(KeyCode::Enter, KeyModifiers::NONE);
        let state = app
            .reboot_confirm
            .as_ref()
            .expect("modal should remain open");
        assert!(state.mismatch);
    }

    #[tokio::test]
    async fn enter_with_correct_hostname_closes_modal() {
        let mut app = one_host_app();
        app.selected_row = 0;
        app.handle_key(KeyCode::Char('b'), KeyModifiers::NONE);
        for c in "myserver".chars() {
            app.handle_key(KeyCode::Char(c), KeyModifiers::NONE);
        }
        app.handle_key(KeyCode::Enter, KeyModifiers::NONE);
        assert!(app.reboot_confirm.is_none());
    }

    #[test]
    fn handle_mouse_blocked_when_modal_active() {
        let mut app = one_host_app();
        app.selected_row = 0;
        app.handle_key(KeyCode::Char('b'), KeyModifiers::NONE);
        let original_width = app.sidebar_width;
        // Simulate a drag that would normally change sidebar_width.
        app.dragging_sidebar = true;
        app.handle_mouse(
            crossterm::event::MouseEventKind::Drag(crossterm::event::MouseButton::Left),
            50,
        );
        assert_eq!(
            app.sidebar_width, original_width,
            "mouse drag should be blocked by modal"
        );
    }
}