zero-tui 0.1.2

Terminal UI widgets and app state for supervising ZERO.
Documentation
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
//! Input translation — turn crossterm key events into app state
//! mutations. Isolated from the event loop so unit tests can drive
//! keystrokes directly against an `AppState`.

use std::time::Instant;

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

use crate::app::mode::Mode;
use crate::app::state::{ActiveOverlay, AppState};

pub fn handle_key(state: &mut AppState, key: KeyEvent) {
    handle_key_inner(state, key);
    // Picker mirrors the prompt — rebuild after any input event so
    // the highlighted entry and list stay in sync with what the
    // operator is typing. Cheap: the catalog is 14 entries.
    state.refresh_picker();
}

fn handle_key_inner(state: &mut AppState, key: KeyEvent) {
    let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
    let shift = key.modifiers.contains(KeyModifiers::SHIFT);

    // Ctrl+C, Ctrl+D: exit. No confirmation — this is a terminal,
    // not a dialog box. Risk-reducing exit is always instant per
    // the risk asymmetry (ADR-014). Also exits the overlay path,
    // since a modal that can trap a quit would violate the
    // "risk-reducing actions are frictionless" rule.
    if ctrl && matches!(key.code, KeyCode::Char('c' | 'd')) {
        state.should_quit = true;
        return;
    }

    // Modal overlays get priority over the prompt. Each variant
    // has its own affordance:
    //
    // * `State` — information-only. Any key dismisses. The gate
    //   contract the user is used to.
    // * `FrictionPause` — gated. Esc cancels (drops the pending
    //   command). At L1 the pause alone is the gate, so any other
    //   key is ignored. At L2+ typed characters feed the confirm
    //   buffer once the mandatory pause has elapsed; Backspace
    //   edits; Enter is a no-op (completion is detected by the
    //   event loop via `AppState::take_confirmed_friction_command`).
    if state.overlay.is_some() {
        match state.overlay.as_mut() {
            Some(ActiveOverlay::State | ActiveOverlay::Verdict(_) | ActiveOverlay::Risk { .. }) => {
                // Data overlays — same dismissal contract as the
                // state overview: any key closes. The verdict
                // overlay is read-only too; the `Evaluation`
                // payload is ephemeral (not kept after dismiss),
                // matching how the state overlay re-reads the
                // mirror every render. The Risk overlay (M2 §4)
                // also dismisses on any key; `dismiss_overlay`
                // records the dismissal timestamp so the auto-
                // open hook can enforce the 60 s cooldown.
                state.dismiss_overlay();
                return;
            }
            Some(ActiveOverlay::FrictionPause(fp)) => {
                if matches!(key.code, KeyCode::Esc) {
                    state.dismiss_overlay();
                    return;
                }
                // Ctrl+anything else at the friction overlay is
                // swallowed — the overlay is modal and we do not
                // want stray mode switches / splits through a
                // pending gate. Ctrl+C already exited above.
                if ctrl {
                    return;
                }
                let now = Instant::now();
                match key.code {
                    KeyCode::Char(c) if !ctrl => fp.push_char(c, now),
                    KeyCode::Backspace => fp.pop_char(now),
                    _ => {}
                }
                return;
            }
            None => unreachable!("overlay.is_some() established above"),
        }
    }

    // Mode switchers: Ctrl+0..5.
    if ctrl
        && let KeyCode::Char(c) = key.code
        && let Some(d) = c.to_digit(10)
        && let Ok(d) = u8::try_from(d)
        && let Some(mode) = Mode::from_digit(d)
    {
        state.mode = mode;
        return;
    }

    // Ctrl+R toggles screen-reader mode. Log a single system row
    // so the operator has a visible confirmation; the row itself
    // renders through the new mode so it also serves as a smoke
    // test of the alternate path.
    if ctrl && matches!(key.code, KeyCode::Char('r')) {
        let on = state.toggle_screen_reader();
        state.push_system(if on {
            "[system] screen-reader mode on (Ctrl+R to toggle)"
        } else {
            "[system] screen-reader mode off (Ctrl+R to toggle)"
        });
        return;
    }

    // Alt+] toggles the live-stream pane. Using the Alt modifier
    // (rather than a bare `]`) avoids clashing with operators
    // typing `]` into the prompt — the trading log has enough
    // hazards without a keystroke ambiguity. A confirmation row
    // in the conversation log is important the first few times
    // so the operator knows the toggle fired even when the pane
    // had nothing to render.
    if key.modifiers.contains(KeyModifiers::ALT) && matches!(key.code, KeyCode::Char(']')) {
        let on = state.toggle_live_stream();
        state.push_system(if on {
            "[system] live-stream pane on (Alt+] to toggle)"
        } else {
            "[system] live-stream pane off (Alt+] to toggle)"
        });
        return;
    }

    // Scrollback: PageUp/PageDown walk one "page" (12 rows is
    // enough to be useful on a short terminal and not too much on
    // a tall one). Ctrl+PageUp / Ctrl+PageDown jump to top/bottom
    // — the bottom jump re-attaches to the live tail.
    if handle_scrollback(state, key.code, ctrl) {
        return;
    }

    // Default: prompt editing, with picker-aware Up/Down/Tab.
    handle_prompt_edit(state, key.code, ctrl, shift);
}

/// Prompt-editing branch of [`handle_key_inner`]. Picker-aware:
/// `Up/Down` move selection inside an active picker, `Tab`
/// completes the highlighted entry, `Enter` submits (and
/// `Shift+Enter` inserts a newline).
fn handle_prompt_edit(state: &mut AppState, code: KeyCode, ctrl: bool, shift: bool) {
    match code {
        KeyCode::Enter => {
            if shift {
                state.prompt.insert_newline();
            } else {
                state.submit_prompt();
            }
        }
        KeyCode::Tab => {
            if let Some(picker) = state.picker.as_ref()
                && let Some(text) = picker.completion_text()
            {
                state.prompt.replace_all(&text);
            }
        }
        KeyCode::Up => {
            // Routing: picker > multi-row nav > history recall.
            if let Some(picker) = state.picker.as_mut() {
                picker.select_prev();
            } else if state.prompt.cursor_on_first_row() {
                state.prompt.recall_prev();
            } else {
                state.prompt.move_up();
            }
        }
        KeyCode::Down => {
            if let Some(picker) = state.picker.as_mut() {
                picker.select_next();
            } else if state.prompt.cursor_on_last_row() {
                state.prompt.recall_next();
            } else {
                state.prompt.move_down();
            }
        }
        KeyCode::Backspace => state.prompt.backspace(),
        KeyCode::Delete => state.prompt.delete(),
        KeyCode::Left => state.prompt.move_left(),
        KeyCode::Right => state.prompt.move_right(),
        KeyCode::Home => state.prompt.move_home(),
        KeyCode::End => state.prompt.move_end(),
        KeyCode::Esc => state.prompt.clear(),
        KeyCode::Char(c) => {
            // Strip Ctrl+Char where we didn't handle it above —
            // we don't want spurious chars leaking into the
            // prompt.
            if !ctrl {
                state.prompt.insert(c);
            }
        }
        _ => {}
    }
}

/// Scrollback step size for PageUp/PageDown. A dozen rows is a
/// comfortable scan speed on a 24-row pane and barely shifts on a
/// 60-row one — in both cases the operator sees context, not a
/// full flip.
const SCROLL_PAGE_ROWS: u16 = 12;

/// Scrollback key handler split out of [`handle_key_inner`] to
/// keep the dispatcher under the clippy line budget. Returns
/// `true` when the key was handled here.
fn handle_scrollback(state: &mut AppState, code: KeyCode, ctrl: bool) -> bool {
    match code {
        KeyCode::PageUp => {
            if ctrl {
                // Jump toward oldest. `u16::MAX` is effectively
                // unbounded relative to a 2048-cap log.
                state.scroll_log_up(u16::MAX);
            } else {
                state.scroll_log_up(SCROLL_PAGE_ROWS);
            }
            true
        }
        KeyCode::PageDown => {
            if ctrl {
                state.scroll_log_to_bottom();
            } else {
                state.scroll_log_down(SCROLL_PAGE_ROWS);
            }
            true
        }
        _ => false,
    }
}

#[cfg(test)]
mod tests {
    use super::handle_key;
    use crate::app::mode::Mode;
    use crate::app::state::{ActiveOverlay, AppState};
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
    use zero_engine_client::EngineState;

    fn mk() -> AppState {
        AppState::new(EngineState::shared())
    }

    #[test]
    fn typing_appends_to_prompt() {
        let mut s = mk();
        for c in "hi".chars() {
            handle_key(&mut s, KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE));
        }
        assert_eq!(s.prompt.as_string(), "hi");
    }

    #[test]
    fn enter_submits_and_clears() {
        let mut s = mk();
        for c in "/help".chars() {
            handle_key(&mut s, KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE));
        }
        handle_key(&mut s, KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
        assert!(s.prompt.is_empty());
    }

    #[test]
    fn ctrl_c_quits() {
        let mut s = mk();
        handle_key(
            &mut s,
            KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL),
        );
        assert!(s.should_quit);
    }

    #[test]
    fn ctrl_digit_switches_mode() {
        let mut s = mk();
        handle_key(
            &mut s,
            KeyEvent::new(KeyCode::Char('2'), KeyModifiers::CONTROL),
        );
        assert_eq!(s.mode, Mode::Positions);
        handle_key(
            &mut s,
            KeyEvent::new(KeyCode::Char('4'), KeyModifiers::CONTROL),
        );
        assert_eq!(s.mode, Mode::Heat);
        handle_key(
            &mut s,
            KeyEvent::new(KeyCode::Char('0'), KeyModifiers::CONTROL),
        );
        assert_eq!(s.mode, Mode::Conversation);
    }

    #[test]
    fn overlay_dismisses_on_any_key() {
        use crate::app::state::ActiveOverlay;
        let mut s = mk();
        s.overlay = Some(ActiveOverlay::State);
        handle_key(
            &mut s,
            KeyEvent::new(KeyCode::Char('x'), KeyModifiers::NONE),
        );
        assert!(s.overlay.is_none());
        assert!(
            s.prompt.is_empty(),
            "key that closes the overlay must not leak into prompt"
        );
    }

    #[test]
    fn overlay_does_not_trap_ctrl_c() {
        use crate::app::state::ActiveOverlay;
        let mut s = mk();
        s.overlay = Some(ActiveOverlay::State);
        handle_key(
            &mut s,
            KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL),
        );
        assert!(s.should_quit, "Ctrl+C must exit even through an overlay");
    }

    #[test]
    fn verdict_overlay_dismisses_on_any_key() {
        use crate::app::state::ActiveOverlay;
        use zero_engine_client::Evaluation;
        let mut s = mk();
        s.overlay = Some(ActiveOverlay::Verdict(Box::new(Evaluation {
            coin: Some("BTC".into()),
            direction: Some("LONG".into()),
            ..Default::default()
        })));
        handle_key(
            &mut s,
            KeyEvent::new(KeyCode::Char('x'), KeyModifiers::NONE),
        );
        assert!(s.overlay.is_none(), "verdict overlay must dismiss");
        assert!(
            s.prompt.is_empty(),
            "dismissing keystroke must not leak into prompt"
        );
    }

    #[test]
    fn verdict_overlay_survives_ctrl_c_exit() {
        use crate::app::state::ActiveOverlay;
        use zero_engine_client::Evaluation;
        let mut s = mk();
        s.overlay = Some(ActiveOverlay::Verdict(Box::<Evaluation>::default()));
        handle_key(
            &mut s,
            KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL),
        );
        assert!(
            s.should_quit,
            "Ctrl+C must still exit through a verdict overlay"
        );
    }

    #[test]
    fn overlay_dismiss_swallows_ctrl_digit_mode_switch() {
        use crate::app::state::ActiveOverlay;
        let mut s = mk();
        s.mode = Mode::Conversation;
        s.overlay = Some(ActiveOverlay::State);
        handle_key(
            &mut s,
            KeyEvent::new(KeyCode::Char('2'), KeyModifiers::CONTROL),
        );
        assert!(s.overlay.is_none(), "overlay should be dismissed");
        assert_eq!(
            s.mode,
            Mode::Conversation,
            "the dismissing keystroke must not double-fire as a mode switch"
        );
    }

    #[test]
    fn friction_overlay_esc_cancels_and_drops_command() {
        use crate::app::state::{ActiveOverlay, FrictionPause};
        use std::time::{Duration, Instant};
        use zero_commands::Command;
        use zero_operator_state::friction::FrictionLevel;
        let mut s = mk();
        s.overlay = Some(ActiveOverlay::FrictionPause(FrictionPause {
            command: Command::Execute,
            level: FrictionLevel::L1,
            started_at: Instant::now(),
            pause: Duration::from_secs(3),
            confirm_word: None,
            confirm_input: String::new(),
        }));
        handle_key(&mut s, KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
        assert!(s.overlay.is_none(), "Esc at friction overlay cancels it");
    }

    #[test]
    fn friction_overlay_l1_ignores_typed_keys() {
        use crate::app::state::{ActiveOverlay, FrictionPause};
        use std::time::{Duration, Instant};
        use zero_commands::Command;
        use zero_operator_state::friction::FrictionLevel;
        let mut s = mk();
        s.overlay = Some(ActiveOverlay::FrictionPause(FrictionPause {
            command: Command::Execute,
            level: FrictionLevel::L1,
            started_at: Instant::now(),
            pause: Duration::from_secs(3),
            confirm_word: None,
            confirm_input: String::new(),
        }));
        handle_key(
            &mut s,
            KeyEvent::new(KeyCode::Char('x'), KeyModifiers::NONE),
        );
        assert!(
            s.overlay.is_some(),
            "typed chars at L1 must not dismiss the overlay"
        );
        assert!(
            s.prompt.is_empty(),
            "typed chars at L1 must not leak into prompt"
        );
    }

    #[test]
    fn friction_overlay_l2_does_not_accept_typing_during_pause() {
        use crate::app::state::{ActiveOverlay, FrictionPause};
        use std::time::{Duration, Instant};
        use zero_commands::Command;
        use zero_operator_state::friction::FrictionLevel;
        let mut s = mk();
        s.overlay = Some(ActiveOverlay::FrictionPause(FrictionPause {
            command: Command::Execute,
            level: FrictionLevel::L2,
            started_at: Instant::now(),
            pause: Duration::from_secs(10),
            confirm_word: Some("execute".into()),
            confirm_input: String::new(),
        }));
        for c in "execute".chars() {
            handle_key(&mut s, KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE));
        }
        if let Some(ActiveOverlay::FrictionPause(fp)) = &s.overlay {
            assert!(
                fp.confirm_input.is_empty(),
                "mandatory pause must reject typing; got {:?}",
                fp.confirm_input
            );
        } else {
            panic!("overlay was dismissed unexpectedly");
        }
    }

    #[test]
    fn friction_overlay_l2_accepts_typing_after_pause() {
        use crate::app::state::{ActiveOverlay, FrictionPause};
        use std::time::{Duration, Instant};
        use zero_commands::Command;
        use zero_operator_state::friction::FrictionLevel;
        let mut s = mk();
        s.overlay = Some(ActiveOverlay::FrictionPause(FrictionPause {
            command: Command::Execute,
            level: FrictionLevel::L2,
            // started_at in the past so the pause is already done
            // at the time the event loop fires the next key event.
            started_at: Instant::now()
                .checked_sub(Duration::from_secs(11))
                .expect("monotonic Instant supports 11s subtraction"),
            pause: Duration::from_secs(10),
            confirm_word: Some("execute".into()),
            confirm_input: String::new(),
        }));
        for c in "exec".chars() {
            handle_key(&mut s, KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE));
        }
        if let Some(ActiveOverlay::FrictionPause(fp)) = &s.overlay {
            assert_eq!(fp.confirm_input, "exec");
        } else {
            panic!("overlay dismissed unexpectedly");
        }
        // Backspace edits the confirm buffer, not the prompt.
        handle_key(
            &mut s,
            KeyEvent::new(KeyCode::Backspace, KeyModifiers::NONE),
        );
        if let Some(ActiveOverlay::FrictionPause(fp)) = &s.overlay {
            assert_eq!(fp.confirm_input, "exe");
        }
    }

    #[test]
    fn shift_enter_inserts_newline_instead_of_submitting() {
        let mut s = mk();
        for c in "abc".chars() {
            handle_key(&mut s, KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE));
        }
        handle_key(&mut s, KeyEvent::new(KeyCode::Enter, KeyModifiers::SHIFT));
        for c in "def".chars() {
            handle_key(&mut s, KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE));
        }
        assert_eq!(s.prompt.as_string(), "abc\ndef");
        assert!(s.pending_input.is_none(), "Shift+Enter must not submit");
        // Plain Enter now submits the joined buffer.
        handle_key(&mut s, KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
        assert_eq!(s.pending_input.as_deref(), Some("abc\ndef"));
    }

    #[test]
    fn up_recalls_previous_history_when_on_first_row() {
        let mut s = mk();
        for c in "/status".chars() {
            handle_key(&mut s, KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE));
        }
        handle_key(&mut s, KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
        // pending_input is drained by the event loop in prod; clear it
        // here to reset submission state.
        s.pending_input = None;
        for c in "/risk".chars() {
            handle_key(&mut s, KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE));
        }
        handle_key(&mut s, KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
        s.pending_input = None;
        // Buffer is empty; Up should recall the newest entry.
        handle_key(&mut s, KeyEvent::new(KeyCode::Up, KeyModifiers::NONE));
        // After recall, the buffer starts with `/` so the picker
        // is active — Up now navigates the picker, not history.
        assert_eq!(s.prompt.as_string(), "/risk");
    }

    #[test]
    fn up_navigates_picker_when_active() {
        let mut s = mk();
        for c in "/".chars() {
            handle_key(&mut s, KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE));
        }
        assert!(s.picker.is_some(), "typing / must open the picker");
        let first_selected = s.picker.as_ref().unwrap().selected_index();
        handle_key(&mut s, KeyEvent::new(KeyCode::Down, KeyModifiers::NONE));
        assert_ne!(
            s.picker.as_ref().unwrap().selected_index(),
            first_selected,
            "Down with active picker should move selection"
        );
    }

    #[test]
    fn tab_completes_selected_picker_entry() {
        let mut s = mk();
        for c in "/he".chars() {
            handle_key(&mut s, KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE));
        }
        handle_key(&mut s, KeyEvent::new(KeyCode::Tab, KeyModifiers::NONE));
        assert_eq!(s.prompt.as_string(), "/help ");
    }

    #[test]
    fn esc_clears_prompt_and_picker_together() {
        let mut s = mk();
        for c in "/h".chars() {
            handle_key(&mut s, KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE));
        }
        assert!(s.picker.is_some());
        handle_key(&mut s, KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
        assert_eq!(s.prompt.as_string(), "");
        assert!(
            s.picker.is_none(),
            "clearing the buffer must also dismiss the ambient picker"
        );
    }

    #[test]
    fn pageup_detaches_pagedown_reattaches_scrollback() {
        let mut s = mk();
        for i in 0..30 {
            s.push_system(format!("row {i}"));
        }
        assert_eq!(s.log_scroll, 0);
        handle_key(&mut s, KeyEvent::new(KeyCode::PageUp, KeyModifiers::NONE));
        assert!(s.log_scroll > 0, "PageUp must detach the viewport");
        handle_key(
            &mut s,
            KeyEvent::new(KeyCode::PageDown, KeyModifiers::CONTROL),
        );
        assert_eq!(s.log_scroll, 0, "Ctrl+PageDown re-attaches to bottom");
    }

    #[test]
    fn ctrl_r_toggles_screen_reader_mode() {
        let mut s = mk();
        assert!(!s.screen_reader);
        handle_key(
            &mut s,
            KeyEvent::new(KeyCode::Char('r'), KeyModifiers::CONTROL),
        );
        assert!(s.screen_reader);
        handle_key(
            &mut s,
            KeyEvent::new(KeyCode::Char('r'), KeyModifiers::CONTROL),
        );
        assert!(!s.screen_reader);
    }

    #[test]
    fn submit_detaches_scroll_if_scrolled_up() {
        let mut s = mk();
        for i in 0..30 {
            s.push_system(format!("row {i}"));
        }
        s.scroll_log_up(10);
        assert_eq!(s.log_scroll, 10);
        for c in "/status".chars() {
            handle_key(&mut s, KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE));
        }
        handle_key(&mut s, KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
        assert_eq!(
            s.log_scroll, 0,
            "submit should re-attach to bottom so command output is visible"
        );
    }

    #[test]
    fn alt_right_bracket_toggles_live_stream_pane() {
        let mut s = mk();
        assert!(!s.live_stream_visible);
        handle_key(&mut s, KeyEvent::new(KeyCode::Char(']'), KeyModifiers::ALT));
        assert!(
            s.live_stream_visible,
            "Alt+] should turn the pane on from the hidden default"
        );
        handle_key(&mut s, KeyEvent::new(KeyCode::Char(']'), KeyModifiers::ALT));
        assert!(
            !s.live_stream_visible,
            "second Alt+] should turn the pane off again"
        );
    }

    #[test]
    fn bare_right_bracket_is_typed_into_prompt_not_a_toggle() {
        // The toggle is deliberately bound to Alt+] — a bare `]`
        // in the prompt must flow through to the buffer. This is
        // the conflict we chose the modifier to avoid.
        let mut s = mk();
        handle_key(
            &mut s,
            KeyEvent::new(KeyCode::Char(']'), KeyModifiers::NONE),
        );
        assert!(!s.live_stream_visible, "bare `]` must not toggle the pane");
        assert_eq!(
            s.prompt.as_string(),
            "]",
            "bare `]` must land in the prompt buffer"
        );
    }

    #[test]
    fn alt_right_bracket_inside_overlay_is_swallowed() {
        // Any modal overlay takes priority: Alt+] must not sneak
        // through and toggle the pane while the operator is
        // reading a state/verdict/friction overlay.
        let mut s = mk();
        s.overlay = Some(ActiveOverlay::State);
        handle_key(&mut s, KeyEvent::new(KeyCode::Char(']'), KeyModifiers::ALT));
        assert!(
            !s.live_stream_visible,
            "overlays swallow keys — toggle must not fire"
        );
    }

    #[test]
    fn ctrl_digit_five_opens_cockpit_mode() {
        let mut s = mk();
        s.mode = Mode::Decisions;
        handle_key(
            &mut s,
            KeyEvent::new(KeyCode::Char('5'), KeyModifiers::CONTROL),
        );
        assert_eq!(s.mode, Mode::Cockpit, "Ctrl+5 must open cockpit mode");
    }

    #[test]
    fn ctrl_digit_six_is_unbound() {
        let mut s = mk();
        s.mode = Mode::Decisions;
        handle_key(
            &mut s,
            KeyEvent::new(KeyCode::Char('6'), KeyModifiers::CONTROL),
        );
        assert_eq!(s.mode, Mode::Decisions, "Ctrl+6 must not change mode");
    }
}