scxtui 1.1.3

TUI for managing sched_ext schedulers via scx_loader
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
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
// SPDX-License-Identifier: GPL-2.0

//! Application state and event loop.

use std::collections::HashMap;
use std::io::ErrorKind;
use std::process::Command;
use std::time::{Duration, Instant};

use anyhow::Result;
use ratatui::DefaultTerminal;
use ratatui::crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use scx_loader::SchedMode;

use crate::args::{ArgsExpandError, expand_input};
use crate::backend::loader::LoaderBackend;
use crate::backend::service::ServiceBackend;
use crate::backend::{Capabilities, ModeArgs, SchedulerBackend, Status};
use crate::kernel::{self, KernelState};
use crate::logs::{self, LogLine};
use crate::ui;

/// All modes, in the cycling order used by the mode selector.
pub const MODES: [SchedMode; 5] = [
    SchedMode::Auto,
    SchedMode::Gaming,
    SchedMode::PowerSave,
    SchedMode::LowLatency,
    SchedMode::Server,
];

/// Lower-case mode name, matching the status panel and scxctl's CLI.
fn mode_label(mode: SchedMode) -> &'static str {
    <&str>::from(mode)
}

/// How often the input poll wakes up to redraw / refresh.
const TICK: Duration = Duration::from_millis(250);
/// How often the status is refreshed in the background. The scheduler can
/// change under us (scxctl, another scxtui, a desktop applet), so the view
/// must not assume it is the only writer. Kept moderate because with property
/// caching disabled every refresh is a real round-trip to the daemon. The
/// proper end state is the daemon emitting `PropertiesChanged` so clients
/// subscribe instead of polling; until that lands upstream this stays a poll.
const REFRESH_EVERY: Duration = Duration::from_secs(5);
/// Minimum spacing between two scheduler-affecting actions. Linux terminals
/// deliver key autorepeat as plain `Press` events (no kitty protocol), so
/// without this, holding `r` would fire one restart per repeat.
const ACTION_DEBOUNCE: Duration = Duration::from_millis(500);
/// How long informational messages stay in the message bar. Without a
/// TTL, a stale "refreshed" from minutes ago reads as fresh feedback.
/// Errors are exempt: they are actionable and stay until replaced.
const MESSAGE_TTL: Duration = Duration::from_secs(8);

/// Which scheduler-management backend drives the app.
#[derive(Clone, Copy, PartialEq)]
pub enum BackendKind {
    Loader,
    Service,
}

impl BackendKind {
    fn other(self) -> Self {
        match self {
            BackendKind::Loader => BackendKind::Service,
            BackendKind::Service => BackendKind::Loader,
        }
    }
}

/// Builds a backend of the given kind, verifying it is usable (connects to
/// D-Bus / finds the systemd unit).
pub fn make_backend(kind: BackendKind) -> Result<Box<dyn SchedulerBackend>> {
    Ok(match kind {
        BackendKind::Loader => Box::new(LoaderBackend::connect()?),
        BackendKind::Service => Box::new(ServiceBackend::connect()?),
    })
}

/// Deferred user actions: queued by key handlers, executed by the event
/// loop right after the frame announcing them has been drawn. Backend
/// calls block (a hung daemon holds the D-Bus timeout, ~25 s), so the UI
/// must show feedback *before* making them.
#[derive(Clone)]
enum PendingAction {
    StartOrSwitch,
    /// Expanded field content; see [`App::submit_args`].
    StartOrSwitchWithArgs(Vec<String>),
    Stop,
    Restart,
    RestoreDefault,
    ToggleBackend,
    Monitor,
}

/// State of the session-only custom-arguments field. The cursor is
/// counted in characters, not bytes, so editing multi-byte input stays
/// sound; the UI derives its cursor block from the same count.
#[derive(Default)]
pub struct ArgsInput {
    pub buffer: String,
    pub cursor: usize,
}

impl ArgsInput {
    /// Byte offset of the character cursor, for `String` edits.
    fn byte_cursor(&self) -> usize {
        self.buffer
            .char_indices()
            .nth(self.cursor)
            .map_or(self.buffer.len(), |(idx, _)| idx)
    }

    fn len_chars(&self) -> usize {
        self.buffer.chars().count()
    }

    fn insert(&mut self, c: char) {
        let at = self.byte_cursor();
        self.buffer.insert(at, c);
        self.cursor += 1;
    }

    fn backspace(&mut self) {
        if self.cursor > 0 {
            self.cursor -= 1;
            let at = self.byte_cursor();
            self.buffer.remove(at);
        }
    }

    fn delete(&mut self) {
        let at = self.byte_cursor();
        if at < self.buffer.len() {
            self.buffer.remove(at);
        }
    }
}

/// Which screen is currently shown.
#[derive(Clone, Copy, PartialEq)]
pub enum View {
    Schedulers,
    Logs,
}

pub struct Message {
    pub text: String,
    pub is_error: bool,
    /// When the message was posted; drives [`MESSAGE_TTL`] expiry.
    shown_at: Instant,
}

pub struct App {
    backend: Box<dyn SchedulerBackend>,
    backend_kind: BackendKind,
    pub schedulers: Vec<String>,
    pub selected: usize,
    pub mode_idx: usize,
    pub status: Option<Status>,
    /// The kernel's own view of `sched_ext`, refreshed alongside `status`.
    /// `None` = kernel without `sched_ext` support.
    pub kernel: Option<KernelState>,
    /// Lazily filled per-scheduler answers to the backend's mode-argument
    /// query. An absent key means "not fetched yet or the query failed" —
    /// never "nothing configured" — so consumers fail open on it. Valid
    /// for the lifetime of one daemon instance; see
    /// [`Self::check_daemon_instance`].
    mode_args: HashMap<String, ModeArgs>,
    /// Last observed backend instance token; a change invalidates
    /// `mode_args`.
    daemon_instance: Option<String>,
    /// Custom-arguments field; `Some` while the user is typing. While
    /// open it owns every key press.
    pub args_input: Option<ArgsInput>,
    pub message: Option<Message>,
    /// Timestamp of the last scheduler-affecting action, for debouncing.
    last_action: Option<Instant>,
    pub view: View,
    /// Index into [`logs::UNITS`].
    pub log_unit: usize,
    /// `false` = current boot, `true` = previous boot (`journalctl -b -1`).
    pub log_previous_boot: bool,
    /// Flattened journal lines, oldest first.
    pub log_lines: Vec<LogLine>,
    /// Scroll offset counted from the bottom; 0 sticks to the newest line.
    pub log_scroll: usize,
    /// Last known height of the log viewport, written back by the UI so
    /// `PgUp`/`PgDn` can page by exactly one screen.
    pub log_page: usize,
    /// Queued by key handlers, executed by the event loop after the next
    /// draw; see [`PendingAction`].
    pending_action: Option<PendingAction>,
    should_quit: bool,
}

impl App {
    pub fn new(kind: BackendKind, backend: Box<dyn SchedulerBackend>) -> Result<Self> {
        let schedulers = backend.supported_schedulers()?;
        let app = Self {
            backend,
            backend_kind: kind,
            schedulers,
            selected: 0,
            mode_idx: 0,
            status: None,
            kernel: None,
            mode_args: HashMap::new(),
            daemon_instance: None,
            args_input: None,
            message: None,
            last_action: None,
            view: View::Schedulers,
            log_unit: 0,
            log_previous_boot: false,
            log_lines: Vec::new(),
            log_scroll: 0,
            log_page: 20,
            pending_action: None,
            should_quit: false,
        };
        // No initial status/modes fetch here: `run` draws the first frame
        // with the scheduler list alone and fetches right after, so the UI
        // appears immediately instead of waiting on D-Bus round-trips.
        Ok(app)
    }

    pub fn backend_label(&self) -> &'static str {
        self.backend.label()
    }

    pub fn capabilities(&self) -> Capabilities {
        self.backend.capabilities()
    }

    pub fn selected_scheduler(&self) -> Option<&str> {
        self.schedulers.get(self.selected).map(String::as_str)
    }

    pub fn selected_mode(&self) -> SchedMode {
        MODES[self.mode_idx]
    }

    /// Resolved arguments for the selected scheduler and mode, when
    /// known. `None` covers "not fetched / query failed" as well as a mode
    /// the daemon did not report; the preview only renders a known,
    /// non-empty list, so both collapse into "nothing to show".
    pub fn selected_mode_args(&self) -> Option<&[String]> {
        let modes = self.mode_args.get(self.selected_scheduler()?)?;
        let mode = self.selected_mode();
        modes
            .iter()
            .find(|(m, _)| *m == mode)
            .map(|(_, args)| args.as_slice())
    }

    /// Whether the selected mode has configured arguments for the selected
    /// scheduler. Mirrors scxctl's client-side warning: `Auto` always
    /// counts, and an unknown answer (never fetched, or the query failed —
    /// either way no cache entry) fails open so we never scare the user
    /// over a transient D-Bus hiccup. A *successful* answer is exact: the
    /// daemon reports every mode, so a mode missing its arguments there
    /// genuinely has none configured.
    pub fn selected_mode_configured(&self) -> bool {
        let mode = self.selected_mode();
        if mode == SchedMode::Auto {
            return true;
        }
        let Some(modes) = self
            .selected_scheduler()
            .and_then(|sched| self.mode_args.get(sched))
        else {
            return true;
        };
        modes.iter().any(|(m, args)| *m == mode && !args.is_empty())
    }

    pub fn run(&mut self, mut terminal: DefaultTerminal) -> Result<()> {
        let mut last_refresh = Instant::now();
        let mut primed = false;
        while !self.should_quit {
            terminal.draw(|frame| ui::draw(frame, self))?;

            // First frame is a skeleton: scheduler list only, status panel
            // in its "unknown" placeholder. The initial fetch happens right
            // after it is on screen and the immediate redraw fills it in —
            // on a healthy system that is a single-digit-ms flicker, and on
            // a slow daemon the user at least sees a live UI instead of a
            // blank terminal.
            if !primed {
                primed = true;
                self.refresh_status();
                self.sync_selection_to_running();
                self.refresh_modes();
                last_refresh = Instant::now();
                continue;
            }

            // Informational messages expire so stale feedback never reads
            // as fresh; errors stay until replaced (see MESSAGE_TTL).
            if self
                .message
                .as_ref()
                .is_some_and(|m| !m.is_error && m.shown_at.elapsed() >= MESSAGE_TTL)
            {
                self.message = None;
            }

            // Execute a queued action only after the frame announcing it
            // ("working…") has been drawn, then redraw immediately so the
            // result replaces the notice.
            if let Some(action) = self.pending_action.take() {
                self.run_action(action, &mut terminal)?;
                last_refresh = Instant::now();
                continue;
            }

            if event::poll(TICK)?
                && let Event::Key(key) = event::read()?
                && key.kind == KeyEventKind::Press
            {
                self.on_key(key);
            }

            if last_refresh.elapsed() >= REFRESH_EVERY {
                self.refresh_status();
                last_refresh = Instant::now();
            }
        }
        Ok(())
    }

    /// Hands the terminal over to `scxtop` and takes it back afterwards —
    /// the lazygit-spawns-an-editor pattern. Restoring the terminal to
    /// cooked mode first lets scxtop own the alternate screen and raw mode
    /// itself; a fresh `ratatui::init()` afterwards re-enters ours, and the
    /// explicit clear forces a full repaint of a screen scxtop scribbled
    /// over. Keeping scxtop out-of-process also keeps its heavyweight BPF
    /// dependency chain (and its root/`CAP_BPF` requirement) out of this
    /// binary: scxtui itself stays an unprivileged D-Bus client.
    fn run_monitor(&mut self, terminal: &mut DefaultTerminal) -> Result<()> {
        ratatui::restore();
        let result = Command::new("scxtop").status();
        *terminal = ratatui::init();
        terminal.clear()?;

        match result {
            Ok(status) if status.success() => self.info("scxtop exited"),
            Ok(status) => self.error(&format!(
                "scxtop exited with {status} — it needs root/CAP_BPF; \
try running scxtui as root or granting scxtop capabilities"
            )),
            Err(err) if err.kind() == ErrorKind::NotFound => self.error(
                "scxtop not found in PATH — install it (cargo install scxtop, \
or your distro's scx tools package)",
            ),
            Err(err) => self.error(&format!("failed to launch scxtop: {err}")),
        }
        Ok(())
    }

    /// Queues a scheduler-affecting action behind a "working…" notice, so
    /// the notice renders before the blocking backend call starts.
    fn queue(&mut self, action: PendingAction) {
        self.info("working…");
        self.pending_action = Some(action);
    }

    fn run_action(&mut self, action: PendingAction, terminal: &mut DefaultTerminal) -> Result<()> {
        match action {
            PendingAction::StartOrSwitch => self.start_or_switch(),
            PendingAction::StartOrSwitchWithArgs(args) => self.start_or_switch_with_args(&args),
            PendingAction::Stop => self.act("stopped", |b| b.stop()),
            PendingAction::Restart => self.restart_scheduler(),
            PendingAction::RestoreDefault => {
                self.act("restored default scheduler", |b| b.restore_default());
            }
            PendingAction::ToggleBackend => self.toggle_backend(),
            PendingAction::Monitor => self.run_monitor(terminal)?,
        }
        Ok(())
    }

    fn on_key(&mut self, key: KeyEvent) {
        if self.view == View::Schedulers && self.args_input.is_some() {
            self.on_key_args(key);
            return;
        }
        match self.view {
            View::Schedulers => self.on_key_schedulers(key),
            View::Logs => self.on_key_logs(key),
        }
    }

    /// Key handling while the custom-arguments field is open. The field
    /// owns every key press: global shortcuts must not fire while the
    /// user is typing text that may well contain their letters — which
    /// also means `Esc` closes the field here instead of quitting.
    fn on_key_args(&mut self, key: KeyEvent) {
        match key.code {
            KeyCode::Esc => self.args_input = None,
            KeyCode::Enter => self.submit_args(),
            code => {
                let Some(input) = self.args_input.as_mut() else {
                    return;
                };
                match code {
                    KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
                        input.insert(c);
                    }
                    KeyCode::Backspace => input.backspace(),
                    KeyCode::Delete => input.delete(),
                    KeyCode::Left => input.cursor = input.cursor.saturating_sub(1),
                    KeyCode::Right => input.cursor = (input.cursor + 1).min(input.len_chars()),
                    KeyCode::Home => input.cursor = 0,
                    KeyCode::End => input.cursor = input.len_chars(),
                    _ => {}
                }
            }
        }
    }

    /// `Enter` in the field: expand exactly like scxctl's `--args`, then
    /// queue the start/switch. Expansion errors keep the field open with
    /// the message bar explaining what to fix; only a successfully queued
    /// action closes it.
    fn submit_args(&mut self) {
        let Some(buffer) = self.args_input.as_ref().map(|input| input.buffer.clone()) else {
            return;
        };
        match expand_input(&buffer) {
            Ok(args) => {
                if self.action_allowed() {
                    self.args_input = None;
                    self.queue(PendingAction::StartOrSwitchWithArgs(args));
                }
            }
            Err(ArgsExpandError::Parse(msg)) => {
                self.error(&format!(
                    "invalid arguments: {msg} — quotes must be balanced and cannot span a comma"
                ));
            }
            Err(ArgsExpandError::Empty) => {
                self.error(
                    "arguments expanded to nothing — to run with scheduler defaults, \
press Esc and use Enter instead",
                );
            }
        }
    }

    fn on_key_schedulers(&mut self, key: KeyEvent) {
        match key.code {
            KeyCode::Char('q') | KeyCode::Esc => self.should_quit = true,
            KeyCode::Char('l') => self.open_logs(),
            KeyCode::Char('t') => {
                if self.action_allowed() {
                    self.pending_action = Some(PendingAction::Monitor);
                }
            }
            KeyCode::Char('B') => {
                if self.action_allowed() {
                    self.queue(PendingAction::ToggleBackend);
                }
            }
            KeyCode::Down | KeyCode::Char('j') => self.select_next(),
            KeyCode::Up | KeyCode::Char('k') => self.select_prev(),
            KeyCode::Char('a') if self.backend.capabilities().custom_args => {
                if self.selected_scheduler().is_some() {
                    self.args_input = Some(ArgsInput::default());
                }
            }
            KeyCode::Tab | KeyCode::Char('m') if self.backend.capabilities().modes => {
                self.cycle_mode(true);
            }
            KeyCode::BackTab | KeyCode::Char('M') if self.backend.capabilities().modes => {
                self.cycle_mode(false);
            }
            KeyCode::Enter => {
                if self.action_allowed() {
                    self.queue(PendingAction::StartOrSwitch);
                }
            }
            KeyCode::Char('s') => {
                if self.action_allowed() {
                    self.queue(PendingAction::Stop);
                }
            }
            KeyCode::Char('r') => {
                if self.action_allowed() {
                    self.queue(PendingAction::Restart);
                }
            }
            KeyCode::Char('d') => {
                if self.backend.capabilities().restore_default && self.action_allowed() {
                    self.queue(PendingAction::RestoreDefault);
                }
            }
            KeyCode::Char('R') => {
                // The success notice must not clobber a list-refresh error,
                // same rule as refresh_status not touching the message bar.
                let list_ok = self.refresh_schedulers();
                self.refresh_status();
                // Manual refresh means "give me the current truth", so the
                // lazy cache must not satisfy it with old answers.
                self.mode_args.clear();
                self.refresh_modes();
                if list_ok {
                    self.info("refreshed");
                }
            }
            _ => {}
        }
    }

    fn on_key_logs(&mut self, key: KeyEvent) {
        match key.code {
            KeyCode::Char('q' | 'l') | KeyCode::Esc => {
                self.view = View::Schedulers;
            }
            KeyCode::Up | KeyCode::Char('k') => self.log_scroll_up(1),
            KeyCode::Down | KeyCode::Char('j') => self.log_scroll_down(1),
            KeyCode::PageUp => self.log_scroll_up(self.log_page),
            KeyCode::PageDown => self.log_scroll_down(self.log_page),
            KeyCode::Char('g') => self.log_scroll = usize::MAX, // clamped by the UI
            KeyCode::Char('G') => self.log_scroll = 0,
            KeyCode::Char('b') => {
                self.log_previous_boot = !self.log_previous_boot;
                self.reload_logs();
            }
            KeyCode::Char('u') => {
                self.log_unit = (self.log_unit + 1) % logs::UNITS.len();
                self.reload_logs();
            }
            KeyCode::Char('R') => self.reload_logs(),
            _ => {}
        }
    }

    /// Swaps the running backend for the other kind. The switch is
    /// transactional: the target backend must connect *and* enumerate its
    /// schedulers before any state is replaced, so a failure at either step
    /// leaves the current backend fully intact.
    fn toggle_backend(&mut self) {
        let target = self.backend_kind.other();
        let prepared = make_backend(target).and_then(|backend| {
            let schedulers = backend.supported_schedulers()?;
            Ok((backend, schedulers))
        });
        match prepared {
            Ok((backend, schedulers)) => {
                self.backend = backend;
                self.backend_kind = target;
                self.schedulers = schedulers;
                self.selected = 0;
                self.mode_idx = 0;
                // Instance tokens are per-backend; comparing across
                // backends would be meaningless, so both the cache and the
                // observed token start over.
                self.mode_args.clear();
                self.daemon_instance = None;
                self.refresh_status();
                self.sync_selection_to_running();
                self.refresh_modes();
                self.info(&format!("switched to {} backend", self.backend.label()));
            }
            Err(err) => {
                let label = self.backend.label();
                self.error(&format!(
                    "cannot switch backend: {err:#} (staying on {label})"
                ));
            }
        }
    }

    /// Public entry for one-off notices (e.g. the startup fallback note).
    pub fn notify(&mut self, text: &str) {
        self.info(text);
    }

    fn open_logs(&mut self) {
        self.view = View::Logs;
        self.reload_logs();
    }

    fn reload_logs(&mut self) {
        let unit = logs::UNITS[self.log_unit];
        match logs::fetch(unit, self.log_previous_boot) {
            Ok(fetch) => {
                let boot = if self.log_previous_boot { "-1" } else { "0" };
                let tail_note = if fetch.truncated {
                    " — older entries omitted, see journalctl for the full log"
                } else {
                    ""
                };
                self.info(&format!(
                    "loaded {} lines from {unit} (boot {boot}){tail_note}",
                    fetch.lines.len()
                ));
                self.log_lines = fetch.lines;
                self.log_scroll = 0;
            }
            Err(err) => {
                self.log_lines = Vec::new();
                self.error(&format!("{err:#}"));
            }
        }
    }

    /// The upper bound is clamped against the viewport in the UI, which
    /// knows the current height; only the zero bound is handled here.
    fn log_scroll_up(&mut self, lines: usize) {
        self.log_scroll = self.log_scroll.saturating_add(lines);
    }

    fn log_scroll_down(&mut self, lines: usize) {
        self.log_scroll = self.log_scroll.saturating_sub(lines);
    }

    fn select_next(&mut self) {
        if !self.schedulers.is_empty() {
            self.selected = (self.selected + 1) % self.schedulers.len();
            self.refresh_modes();
        }
    }

    fn select_prev(&mut self) {
        if !self.schedulers.is_empty() {
            self.selected = (self.selected + self.schedulers.len() - 1) % self.schedulers.len();
            self.refresh_modes();
        }
    }

    fn cycle_mode(&mut self, forward: bool) {
        let len = MODES.len();
        self.mode_idx = if forward {
            (self.mode_idx + 1) % len
        } else {
            (self.mode_idx + len - 1) % len
        };
    }

    /// Aligns the scheduler selection and the mode selector with what is
    /// actually running, so the first `Enter`/`r` after startup acts on
    /// what the user sees instead of on `schedulers[0]` + `Auto`. Only
    /// called on startup and after a backend switch — never on the
    /// periodic refresh, which must not fight the user's own selection.
    ///
    /// All-or-nothing: a running scheduler outside the advertised list
    /// (hand-launched next to the loader) leaves *both* selectors alone —
    /// syncing only the mode would pair the foreign scheduler's mode with
    /// an unrelated selection. The mode half is additionally skipped for
    /// custom args, since no selector position represents that state.
    fn sync_selection_to_running(&mut self) {
        let Some(status) = &self.status else {
            return;
        };
        let Some(current) = &status.current else {
            return;
        };
        let Some(idx) = self.schedulers.iter().position(|s| s == current) else {
            return;
        };
        self.selected = idx;
        if status.args.is_empty()
            && let Some(idx) = MODES.iter().position(|m| *m == status.mode)
        {
            self.mode_idx = idx;
        }
    }

    /// Debounce gate for scheduler-affecting actions (Enter/s/r/d/B).
    /// Returns `true` and arms the timer when enough time has passed since
    /// the last action; swallows the event otherwise. See
    /// [`ACTION_DEBOUNCE`].
    fn action_allowed(&mut self) -> bool {
        let now = Instant::now();
        if self
            .last_action
            .is_some_and(|last| now.duration_since(last) < ACTION_DEBOUNCE)
        {
            return false;
        }
        self.last_action = Some(now);
        true
    }

    /// `Enter`: start when nothing is running, switch otherwise — including
    /// "switching" the running scheduler to a different mode, which is how
    /// the loader models a mode change. The TUI can make that call itself
    /// instead of erroring like a CLI has to.
    fn start_or_switch(&mut self) {
        let Some(sched) = self.selected_scheduler().map(str::to_owned) else {
            return;
        };
        let mode = self.selected_mode();
        // Re-read the daemon state right before deciding start vs switch:
        // the cached status can be up to REFRESH_EVERY old, and another
        // client may have started a scheduler in that window.
        self.refresh_status();
        let running = self
            .status
            .as_ref()
            .and_then(|status| status.current.clone());

        let same_scheduler = running.as_deref() == Some(sched.as_str());
        let result = if running.is_some() {
            self.backend.switch(&sched, mode)
        } else {
            self.backend.start(&sched, mode)
        };

        match result {
            Ok(()) => {
                let mode_str = mode_label(mode);
                let base = if running.is_none() {
                    format!("started {sched} in {mode_str} mode")
                } else if same_scheduler {
                    format!("switched {sched} to {mode_str} mode")
                } else {
                    format!("switched to {sched} in {mode_str} mode")
                };
                let text = if self.selected_mode_configured() {
                    base
                } else {
                    format!("{base} — no arguments configured, scheduler defaults in use")
                };
                self.info(&text);
            }
            Err(err) => {
                let verb = if running.is_some() {
                    "switch to"
                } else {
                    "start"
                };
                self.error(&format!("{verb} {sched} failed: {err:#}"));
            }
        }
        self.refresh_status();
    }

    /// The with-args sibling of [`Self::start_or_switch`]: the same
    /// start-vs-switch decision against a re-read daemon state, but the
    /// scheduler receives the expanded field content instead of a mode.
    /// The arguments are session-only — the loader keeps them for this
    /// run and nothing is written to its config — and both the success
    /// notice and a failure render them in the same shell-words form the
    /// status panel and scxctl use.
    fn start_or_switch_with_args(&mut self, args: &[String]) {
        let Some(sched) = self.selected_scheduler().map(str::to_owned) else {
            return;
        };
        // Same staleness concern as in `start_or_switch`.
        self.refresh_status();
        let running = self
            .status
            .as_ref()
            .and_then(|status| status.current.clone());

        let result = if running.is_some() {
            self.backend.switch_with_args(&sched, args)
        } else {
            self.backend.start_with_args(&sched, args)
        };

        let rendered = shell_words::join(args);
        match result {
            Ok(()) => {
                let verb = if running.is_none() {
                    "started"
                } else if running.as_deref() == Some(sched.as_str()) {
                    "restarted"
                } else {
                    "switched to"
                };
                self.info(&format!(
                    "{verb} {sched} with args: {rendered} (session-only)"
                ));
            }
            Err(err) => {
                let verb = if running.is_some() {
                    "switch to"
                } else {
                    "start"
                };
                self.error(&format!(
                    "{verb} {sched} with args '{rendered}' failed: {err:#}"
                ));
            }
        }
        self.refresh_status();
    }

    /// `r`: restart. Plain `RestartScheduler` deliberately reuses the
    /// original configuration, so by itself it can never apply a mode
    /// change — and the very first piece of community feedback was someone
    /// restarting after flipping the mode selector and reasonably expecting
    /// the new mode to stick. When the selection points at the running
    /// scheduler, no custom arguments are in play and the selector differs
    /// from the active mode, `r` therefore means "restart into the selected
    /// mode" (a same-scheduler switch in loader terms). In every other case
    /// it stays a faithful restart of the original configuration.
    fn restart_scheduler(&mut self) {
        // Same staleness concern as in `start_or_switch`.
        self.refresh_status();
        let running = self
            .status
            .as_ref()
            .and_then(|status| status.current.clone());
        let custom_args = self
            .status
            .as_ref()
            .is_some_and(|status| !status.args.is_empty());
        let active_mode = self.status.as_ref().map(|status| status.mode);
        let selected = self.selected_scheduler().map(str::to_owned);
        let mode = self.selected_mode();

        let mode_change = self.backend.capabilities().modes
            && !custom_args
            && running.is_some()
            && running == selected
            && active_mode != Some(mode);

        if let (true, Some(sched)) = (mode_change, running) {
            match self.backend.switch(&sched, mode) {
                Ok(()) => {
                    self.info(&format!("restarted {sched} in {} mode", mode_label(mode)));
                }
                Err(err) => self.error(&format!("restart {sched} failed: {err:#}")),
            }
            self.refresh_status();
        } else {
            self.act("restarted", |b| b.restart());
        }
    }

    fn act(&mut self, ok_text: &str, op: impl FnOnce(&dyn SchedulerBackend) -> Result<()>) {
        match op(self.backend.as_ref()) {
            Ok(()) => self.info(ok_text),
            Err(err) => self.error(&format!("{err:#}")),
        }
        self.refresh_status();
    }

    /// Deliberately never touches the message bar: the status panel has
    /// its own channel for a failed query (`State: unknown` in red via
    /// `status = None`), and writing an error here would clobber a fresh
    /// action result — the action can succeed while the follow-up poll
    /// fails, and the user should still see the success.
    fn refresh_status(&mut self) {
        self.status = self.backend.status().ok();
        self.kernel = kernel::read();
        self.check_daemon_instance();
    }

    /// Re-enumerates the scheduler list. The list is otherwise fetched
    /// once at startup, so without this a daemon restarted with a changed
    /// configuration would keep serving a stale list until scxtui itself
    /// restarts. The selection survives by name where possible; on a
    /// failed query the old list stays — a stale list beats an empty one.
    fn refresh_schedulers(&mut self) -> bool {
        match self.backend.supported_schedulers() {
            Ok(schedulers) => {
                let previous = self.selected_scheduler().map(str::to_owned);
                self.selected = previous
                    .and_then(|name| schedulers.iter().position(|s| *s == name))
                    .unwrap_or(0);
                self.schedulers = schedulers;
                true
            }
            Err(err) => {
                self.error(&format!("scheduler list refresh failed: {err:#}"));
                false
            }
        }
    }

    /// Lazily fills the per-scheduler argument cache. The daemon reads its
    /// configuration once at startup, so a successful answer stays valid
    /// until the daemon itself is replaced — which `check_daemon_instance`
    /// watches for. A failed query is deliberately *not* cached: an absent
    /// entry reads as "unknown" (fail open in `selected_mode_configured`)
    /// and the next call here simply retries.
    fn refresh_modes(&mut self) {
        if !self.backend.capabilities().modes {
            return;
        }
        let Some(sched) = self.selected_scheduler() else {
            return;
        };
        if self.mode_args.contains_key(sched) {
            return;
        }
        let sched = sched.to_owned();
        if let Ok(modes) = self.backend.mode_args(&sched) {
            self.mode_args.insert(sched, modes);
        }
    }

    /// Detects a daemon restart via the backend's instance token and drops
    /// the mode-argument cache when one happened. A replaced daemon is the
    /// one event that can make cached configuration answers stale (the
    /// daemon reads its config once at startup) — watching the config
    /// file's mtime instead would be both weaker (an edit without a
    /// restart changes nothing) and blind to a restart with an unchanged
    /// file. Rides along the periodic status refresh, so an external
    /// `systemctl restart scx_loader` is picked up within one poll even
    /// though D-Bus activation hides it from the calls themselves.
    fn check_daemon_instance(&mut self) {
        let Some(token) = self.backend.instance_token() else {
            return;
        };
        if self
            .daemon_instance
            .as_deref()
            .is_some_and(|old| old != token)
        {
            self.mode_args.clear();
            self.refresh_modes();
        }
        self.daemon_instance = Some(token);
    }

    fn info(&mut self, text: &str) {
        self.message = Some(Message {
            text: text.to_owned(),
            is_error: false,
            shown_at: Instant::now(),
        });
    }

    fn error(&mut self, text: &str) {
        self.message = Some(Message {
            text: text.to_owned(),
            is_error: true,
            shown_at: Instant::now(),
        });
    }
}

#[cfg(test)]
mod tests {
    use std::cell::RefCell;
    use std::rc::Rc;

    use anyhow::anyhow;

    use super::*;

    /// Minimal in-memory backend: enough for exercising selection and
    /// caching logic without a D-Bus connection.
    struct StubBackend {
        schedulers: Vec<String>,
        /// Per-scheduler answer for `mode_args`; a missing key errors,
        /// modeling a failed D-Bus query.
        modes: HashMap<String, ModeArgs>,
        /// How many times `mode_args` reached the backend, shared with the
        /// test so caching is observable from the outside.
        mode_queries: Rc<RefCell<usize>>,
        /// Current instance token, shared so a test can "restart" the
        /// daemon underneath the app.
        token: Rc<RefCell<Option<String>>>,
    }

    impl StubBackend {
        fn new() -> Self {
            Self {
                schedulers: vec!["scx_bpfland".into(), "scx_lavd".into(), "scx_cake".into()],
                modes: HashMap::new(),
                mode_queries: Rc::new(RefCell::new(0)),
                token: Rc::new(RefCell::new(None)),
            }
        }
    }

    impl SchedulerBackend for StubBackend {
        fn label(&self) -> &'static str {
            "stub"
        }
        fn instance_token(&self) -> Option<String> {
            self.token.borrow().clone()
        }
        fn capabilities(&self) -> Capabilities {
            Capabilities {
                live_switch: true,
                modes: true,
                custom_args: true,
                restore_default: true,
            }
        }
        fn status(&self) -> Result<Status> {
            // Benign "nothing running" answer, so tests may drive paths
            // that refresh the status; tests interested in a particular
            // state still set `App::status` directly afterwards.
            Ok(Status {
                current: None,
                mode: SchedMode::Auto,
                args: Vec::new(),
                default_sched: None,
                default_mode: SchedMode::Auto,
            })
        }
        fn supported_schedulers(&self) -> Result<Vec<String>> {
            Ok(self.schedulers.clone())
        }
        fn mode_args(&self, sched: &str) -> Result<ModeArgs> {
            *self.mode_queries.borrow_mut() += 1;
            self.modes
                .get(sched)
                .cloned()
                .ok_or_else(|| anyhow!("no mode answer configured for {sched}"))
        }
        fn start(&self, _sched: &str, _mode: SchedMode) -> Result<()> {
            Ok(())
        }
        fn switch(&self, _sched: &str, _mode: SchedMode) -> Result<()> {
            Ok(())
        }
        fn start_with_args(&self, _sched: &str, _args: &[String]) -> Result<()> {
            Ok(())
        }
        fn switch_with_args(&self, _sched: &str, _args: &[String]) -> Result<()> {
            Ok(())
        }
        fn stop(&self) -> Result<()> {
            Ok(())
        }
        fn restart(&self) -> Result<()> {
            Ok(())
        }
        fn restore_default(&self) -> Result<()> {
            Ok(())
        }
    }

    /// Answer for the default selection (`scx_bpfland`): gaming has
    /// arguments, powersave is present but empty, the rest untouched.
    fn bpfland_modes() -> ModeArgs {
        vec![
            (SchedMode::Auto, Vec::new()),
            (
                SchedMode::Gaming,
                vec!["-k".into(), "-s".into(), "5000".into()],
            ),
            (SchedMode::PowerSave, Vec::new()),
        ]
    }

    fn app_with_backend(backend: StubBackend) -> App {
        App::new(BackendKind::Loader, Box::new(backend)).unwrap()
    }

    fn app_with_status(status: Status) -> App {
        let mut app = app_with_backend(StubBackend::new());
        app.status = Some(status);
        app
    }

    fn select_mode(app: &mut App, mode: SchedMode) {
        app.mode_idx = MODES.iter().position(|m| *m == mode).unwrap();
    }

    fn running(current: &str, mode: SchedMode, args: &[&str]) -> Status {
        Status {
            current: Some(current.to_owned()),
            mode,
            args: args.iter().map(|a| (*a).to_owned()).collect(),
            default_sched: None,
            default_mode: SchedMode::Auto,
        }
    }

    #[test]
    fn configured_follows_the_cached_argument_lists() {
        let mut backend = StubBackend::new();
        backend.modes.insert("scx_bpfland".into(), bpfland_modes());
        let mut app = app_with_backend(backend);
        app.refresh_modes();

        select_mode(&mut app, SchedMode::Auto);
        assert!(app.selected_mode_configured(), "Auto always counts");
        select_mode(&mut app, SchedMode::Gaming);
        assert!(app.selected_mode_configured(), "non-empty arguments");
        select_mode(&mut app, SchedMode::PowerSave);
        assert!(
            !app.selected_mode_configured(),
            "present in the answer with an empty argument list"
        );
        select_mode(&mut app, SchedMode::Server);
        assert!(
            !app.selected_mode_configured(),
            "absent from a successful answer means not configured"
        );
    }

    #[test]
    fn unknown_answer_fails_open() {
        // No cache entry — the query failed and nothing was stored. Every
        // mode must count as configured so a transient D-Bus hiccup never
        // produces a scary warning.
        let mut app = app_with_backend(StubBackend::new());
        select_mode(&mut app, SchedMode::Server);
        assert!(app.selected_mode_configured());
    }

    #[test]
    fn selected_mode_args_follows_the_selection() {
        let mut backend = StubBackend::new();
        backend.modes.insert("scx_bpfland".into(), bpfland_modes());
        let mut app = app_with_backend(backend);
        app.refresh_modes();

        select_mode(&mut app, SchedMode::Gaming);
        let expected = ["-k", "-s", "5000"].map(str::to_string);
        assert_eq!(app.selected_mode_args(), Some(&expected[..]));

        select_mode(&mut app, SchedMode::Server);
        assert_eq!(
            app.selected_mode_args(),
            None,
            "a mode absent from the answer has nothing to preview"
        );

        // scx_lavd has no cache entry at all: same outcome, for the
        // "unknown" reason.
        app.selected = 1;
        assert_eq!(app.selected_mode_args(), None);
    }

    #[test]
    fn cache_serves_repeat_queries() {
        let mut backend = StubBackend::new();
        backend.modes.insert("scx_bpfland".into(), bpfland_modes());
        let queries = Rc::clone(&backend.mode_queries);
        let mut app = app_with_backend(backend);

        app.refresh_modes();
        app.refresh_modes();
        assert_eq!(*queries.borrow(), 1, "second call must hit the cache");
    }

    #[test]
    fn failed_query_is_not_cached() {
        // The stub has no answer for any scheduler, so every query fails.
        let backend = StubBackend::new();
        let queries = Rc::clone(&backend.mode_queries);
        let mut app = app_with_backend(backend);

        app.refresh_modes();
        app.refresh_modes();
        assert_eq!(
            *queries.borrow(),
            2,
            "a failure must stay uncached so the next call retries"
        );
    }

    #[test]
    fn daemon_restart_clears_the_cache() {
        let mut backend = StubBackend::new();
        backend.modes.insert("scx_bpfland".into(), bpfland_modes());
        let queries = Rc::clone(&backend.mode_queries);
        let token = Rc::clone(&backend.token);
        let mut app = app_with_backend(backend);

        *token.borrow_mut() = Some(":1.7".into());
        app.check_daemon_instance();
        app.refresh_modes();
        assert_eq!(*queries.borrow(), 1);

        // Same daemon: the periodic check must not disturb the cache.
        app.check_daemon_instance();
        app.refresh_modes();
        assert_eq!(*queries.borrow(), 1);

        // "Restart" the daemon: new unique name, cache dropped and the
        // selected scheduler refetched immediately.
        *token.borrow_mut() = Some(":1.9".into());
        app.check_daemon_instance();
        assert_eq!(*queries.borrow(), 2);
    }

    #[test]
    fn manual_refresh_bypasses_the_cache() {
        let mut backend = StubBackend::new();
        backend.modes.insert("scx_bpfland".into(), bpfland_modes());
        let queries = Rc::clone(&backend.mode_queries);
        let mut app = app_with_backend(backend);

        app.refresh_modes();
        assert_eq!(*queries.borrow(), 1);
        app.on_key(KeyEvent::from(KeyCode::Char('R')));
        assert_eq!(
            *queries.borrow(),
            2,
            "R must refetch instead of serving the cache"
        );
    }

    fn key(code: KeyCode) -> KeyEvent {
        KeyEvent::from(code)
    }

    fn type_str(app: &mut App, text: &str) {
        for c in text.chars() {
            app.on_key(key(KeyCode::Char(c)));
        }
    }

    #[test]
    fn args_field_opens_on_a_and_owns_global_keys() {
        let mut app = app_with_backend(StubBackend::new());
        app.on_key(key(KeyCode::Char('a')));
        assert!(app.args_input.is_some());

        // 'q' quits globally, but inside the field it is just a letter.
        app.on_key(key(KeyCode::Char('q')));
        assert!(!app.should_quit);
        assert_eq!(app.args_input.as_ref().unwrap().buffer, "q");

        // Esc closes the field instead of quitting the app.
        app.on_key(key(KeyCode::Esc));
        assert!(app.args_input.is_none());
        assert!(!app.should_quit);
    }

    #[test]
    fn args_field_edits_at_the_character_cursor() {
        let mut app = app_with_backend(StubBackend::new());
        app.on_key(key(KeyCode::Char('a')));
        type_str(&mut app, "ad");
        app.on_key(key(KeyCode::Left));
        type_str(&mut app, "bc");
        app.on_key(key(KeyCode::End));
        app.on_key(key(KeyCode::Backspace));
        assert_eq!(app.args_input.as_ref().unwrap().buffer, "abc");

        // Multi-byte characters: the cursor counts characters, not bytes.
        app.on_key(key(KeyCode::Home));
        type_str(&mut app, "żó");
        app.on_key(key(KeyCode::Home));
        app.on_key(key(KeyCode::Delete));
        assert_eq!(app.args_input.as_ref().unwrap().buffer, "óabc");
    }

    #[test]
    fn args_parse_error_keeps_the_field_open() {
        let mut app = app_with_backend(StubBackend::new());
        app.on_key(key(KeyCode::Char('a')));
        type_str(&mut app, "--name \"foo");
        app.on_key(key(KeyCode::Enter));

        assert!(app.args_input.is_some(), "the user must be able to fix it");
        assert!(app.message.as_ref().unwrap().is_error);
        assert!(app.pending_action.is_none());
    }

    #[test]
    fn args_empty_input_is_rejected() {
        let mut app = app_with_backend(StubBackend::new());
        app.on_key(key(KeyCode::Char('a')));
        type_str(&mut app, "   ");
        app.on_key(key(KeyCode::Enter));

        assert!(app.args_input.is_some());
        assert!(app.message.as_ref().unwrap().is_error);
        assert!(app.pending_action.is_none());
    }

    #[test]
    fn args_valid_input_queues_the_expanded_action() {
        let mut app = app_with_backend(StubBackend::new());
        app.on_key(key(KeyCode::Char('a')));
        type_str(&mut app, "-s 20000,-m powersave");
        app.on_key(key(KeyCode::Enter));

        assert!(app.args_input.is_none(), "a queued action closes the field");
        let Some(PendingAction::StartOrSwitchWithArgs(args)) = &app.pending_action else {
            panic!("expected a queued with-args action");
        };
        assert_eq!(
            args,
            &["-s", "20000", "-m", "powersave"]
                .map(str::to_string)
                .to_vec()
        );
    }

    #[test]
    fn sync_aligns_selection_and_mode_with_running_scheduler() {
        let mut app = app_with_status(running("scx_lavd", SchedMode::Gaming, &[]));
        app.sync_selection_to_running();
        assert_eq!(app.selected_scheduler(), Some("scx_lavd"));
        assert_eq!(app.selected_mode(), SchedMode::Gaming);
    }

    #[test]
    fn sync_with_custom_args_keeps_mode_selector_untouched() {
        let mut app = app_with_status(running(
            "scx_cake",
            SchedMode::Gaming,
            &["--slice-us", "500"],
        ));
        app.sync_selection_to_running();
        // Selection follows the running scheduler, but no selector position
        // represents "custom args", so the mode stays where it was.
        assert_eq!(app.selected_scheduler(), Some("scx_cake"));
        assert_eq!(app.selected_mode(), SchedMode::Auto);
    }

    #[test]
    fn sync_with_unknown_scheduler_is_a_full_no_op() {
        // A hand-launched scheduler outside the advertised list must not
        // touch either selector: syncing only the mode would pair the
        // foreign scheduler's mode with an unrelated selection.
        let mut app = app_with_status(running("scx_homebrew", SchedMode::Server, &[]));
        app.selected = 1;
        app.mode_idx = 1;
        app.sync_selection_to_running();
        assert_eq!(app.selected_scheduler(), Some("scx_lavd"));
        assert_eq!(app.selected_mode(), SchedMode::Gaming);
    }

    #[test]
    fn sync_with_nothing_running_is_a_no_op() {
        let mut app = app_with_status(Status {
            current: None,
            mode: SchedMode::Auto,
            args: Vec::new(),
            default_sched: None,
            default_mode: SchedMode::Auto,
        });
        app.mode_idx = 2;
        app.sync_selection_to_running();
        assert_eq!(app.selected, 0);
        assert_eq!(app.mode_idx, 2);
    }
}