croft-software 0.2.232

VSCode-style TUI written in Rust
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
//! Mouse and keyboard handling no single feature owns: clicks, drags, focus
//! and modifiers.

use super::*;

#[test]
fn shift_alt_drag_starts_a_column_selection() {
    use crossterm::event::{MouseButton, MouseEventKind};
    let tmp = tempfile::tempdir().unwrap();
    let f = tmp.path().join("t.rs");
    std::fs::write(&f, "aaaaaaaa\nbbbbbbbb\ncccccccc\n").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.editor.open_pinned(&f).unwrap();
    let backend = ratatui::backend::TestBackend::new(100, 30);
    let mut term = ratatui::Terminal::new(backend).unwrap();
    term.draw(|frame| app.render(frame)).unwrap();

    // Clear of the gutter: the sign margin / line-number columns are not
    // buffer positions, so begin_box_select would refuse there.
    let col = app.editor.last_inner.x + 8;
    let row = app.editor.last_inner.y;
    let mut down = mouse(MouseEventKind::Down(MouseButton::Left), col, row);
    down.modifiers = KeyModifiers::ALT | KeyModifiers::SHIFT;
    app.handle_mouse(down);
    assert!(
        app.editor.box_selecting(),
        "Shift+Alt press must anchor a column selection"
    );
    // Drag DOWN AND ACROSS. A drag that only moves vertically produces
    // zero-width selections on every row, which an implementation that ignored
    // the anchor column entirely would also satisfy.
    let mut drag = mouse(MouseEventKind::Drag(MouseButton::Left), col + 3, row + 2);
    drag.modifiers = KeyModifiers::ALT | KeyModifiers::SHIFT;
    app.handle_mouse(drag);
    assert!(
        app.editor.carets.len() >= 2,
        "Shift+Alt+drag must build a column of carets, got {}",
        app.editor.carets.len()
    );
    // The head row's span lives in `selection`; the rows above it are carets.
    // Every one must cover the SAME three columns — that is what makes it a
    // column selection rather than a stack of bare cursors.
    let head = app
        .editor
        .selection
        .expect("the head row must span columns");
    assert_eq!(
        head.head.1 - head.anchor.1,
        3,
        "head row must span 3 columns"
    );
    for c in &app.editor.carets {
        assert_eq!(
            (c.anchor.1, c.head.1),
            (head.anchor.1, head.head.1),
            "every row of a column selection covers the same columns"
        );
    }
}

#[test]
fn the_at_click_commands_all_refuse_a_non_text_view() {
    // All three At Click commands guard on `has_non_text_view`, because
    // `buffer_pos_at` maps cells from the TEXT layout and still returns a
    // position on a diff/sheet/hex/log view — so without the guard they would
    // act on coordinates belonging to a buffer the user is not looking at.
    //
    // Short-circuiting all three guards to false leaves the full suite green,
    // so the guards shipped unpinned. Each arm asserts its OWN status, since
    // a shared assertion would pass with two of the three guards deleted.
    let tmp = tempfile::tempdir().unwrap();
    let bin = tmp.path().join("a.bin");
    std::fs::write(&bin, [0u8, 1, 2, 3, 4, 5, 6, 7]).unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.editor.hex = Some(crate::hex::HexView::open(&bin).unwrap());

    // Positive control: without this the test passes vacuously if the fixture
    // ever stops producing a non-text view.
    assert!(
        app.editor.has_non_text_view(),
        "precondition: the fixture must actually open a non-text view, or \
         every assertion below is about the ordinary text path"
    );
    // And the commands must have a click to act on, or they take the
    // keyboard-invocation arm and refuse for an unrelated reason.
    app.last_mouse_pos = Some((10, 10));

    for (cmd, want) in [
        (
            crate::widgets::command_palette::Command::MouseAddCursorAtClick,
            "No cursors in this view",
        ),
        (
            crate::widgets::command_palette::Command::MouseGoToDefinitionAtClick,
            "No definitions in this view",
        ),
        (
            crate::widgets::command_palette::Command::MouseOpenLinkAtClick,
            "No links in this view",
        ),
    ] {
        app.status = String::from("untouched");
        app.run_command(cmd);
        assert_eq!(
            app.status, want,
            "{cmd:?} must refuse on a non-text view rather than acting on a \
             buffer position the user cannot see"
        );
    }
}

#[test]
fn a_modified_double_click_can_fire_at_all() {
    // The dispatch deliberately refuses to record a MODIFIED click into the
    // built-in trackers: they are shared with the editor's plain select-word,
    // so arming them with a ctrl+click would make the user's next ORDINARY
    // click select a word they never asked for. But `double_click` is only
    // classified by asking a tracker whether it just saw a click here — so
    // with nothing recording modified clicks, `ctrl+double_click` could never
    // become true and the binding was unreachable. A separate tracker keeps
    // both rules: the built-in one stays unarmed, the modified pair is seen.
    use crossterm::event::{MouseButton, MouseEventKind};
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    // Bound in `terminal`, not `editor`: with no file open the editor is never
    // laid out (`last_area` comes back height 0), so no click coordinate can
    // land in it and the binding's `when` could never match. The terminal is
    // always present, and this test is about whether a modified double-click
    // can fire AT ALL — the region it fires in is incidental.
    app.keymap = crate::keymap::Keymap::from_json(
        r#"[{"key": "ctrl+double_click", "command": "toggle_word_wrap", "when": "terminal"}]"#,
    );
    let backend = ratatui::backend::TestBackend::new(120, 40);
    let mut term = ratatui::Terminal::new(backend).unwrap();
    term.draw(|f| app.render(f)).unwrap();

    let before = app.editor.wrap_enabled();
    let area = app.terminals[0].last_area;
    assert!(
        area.width > 4 && area.height > 2,
        "the terminal must be laid out, or the click lands nowhere and this \
         test passes or fails for reasons unrelated to double-click tracking"
    );
    let (col, row) = (area.x + 2, area.y + 1);

    // First ctrl+click: arms the modified tracker, fires nothing (no `click`
    // row is bound), and must NOT arm the plain tracker.
    let mut first = mouse(MouseEventKind::Down(MouseButton::Left), col, row);
    first.modifiers = KeyModifiers::CONTROL;
    app.handle_mouse(first);
    assert_eq!(
        app.editor.wrap_enabled(),
        before,
        "the first click of the pair is not yet a double"
    );

    // Second ctrl+click at the same cell: now it is a double, so it fires.
    let mut second = mouse(MouseEventKind::Down(MouseButton::Left), col, row);
    second.modifiers = KeyModifiers::CONTROL;
    app.handle_mouse(second);
    assert_ne!(
        app.editor.wrap_enabled(),
        before,
        "ctrl+double_click must be reachable, not permanently dead"
    );
}

#[test]
fn the_at_click_commands_refuse_a_keyboard_invocation() {
    // Each reads the click through `last_mouse_pos`, which a palette or
    // chord invocation leaves None. They must say so rather than guess at
    // the caret and act somewhere the user never pointed.
    use crate::widgets::command_palette::Command;
    let (mut app, _tmp) = tree_app_with_keymap("[]");
    for (cmd, want) in [
        (
            Command::MouseAddCursorAtClick,
            "Mouse: Add Cursor at Click needs a mouse binding",
        ),
        (
            Command::MouseGoToDefinitionAtClick,
            "Mouse: Go to Definition at Click needs a mouse binding",
        ),
        (
            Command::MouseOpenLinkAtClick,
            "Mouse: Open Link at Click needs a mouse binding",
        ),
    ] {
        app.last_mouse_pos = None;
        app.status.clear();
        app.run_command(cmd);
        assert_eq!(app.status, want, "{cmd:?} must refuse a keyboard call");
    }
}

#[test]
fn an_unmatched_modified_click_does_not_arm_the_tracker() {
    // The shape that actually reaches the built-in `terminal_click.record`,
    // which has no modifier guard: a modified click that CLASSIFIES, matches
    // no binding, and is NOT the first half of a bound double-click. Binding
    // `alt+double_click` and clicking with CTRL gives exactly that — the
    // swallow branch declines (the bound double is ALT, the click is CTRL, so
    // `is_double_click_prefix` is false for this gesture), and the dispatch
    // falls through to the built-in for real.
    //
    // If `record` armed on it, the user's next ordinary click would select a
    // word they never gestured for.
    use crossterm::event::{MouseButton, MouseEventKind};
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.keymap = crate::keymap::Keymap::from_json(
        r#"[{"key": "alt+double_click", "command": "toggle_word_wrap", "when": "terminal"}]"#,
    );
    let backend = ratatui::backend::TestBackend::new(120, 40);
    let mut term = ratatui::Terminal::new(backend).unwrap();
    term.draw(|f| app.render(f)).unwrap();

    let area = app.terminals[0].last_area;
    assert!(
        area.width > 4 && area.height > 2,
        "terminal must be laid out"
    );
    // A line of output IN FRONT of the token, so the ordering that used to
    // break these tests is the one this test always runs (#397), and the cell
    // is SEARCHED rather than assumed.
    //
    // Assuming it made this test conditionally vacuous, which is worse than
    // plainly vacuous: measured both ways, with `ClickTracker::record`'s
    // modifier guard deliberately broken, it FAILED with the token on the
    // assumed row and PASSED with the token one row down. So it discriminated
    // whenever it was checked by hand and went blind precisely under load,
    // which is the only condition it exists to cover. The negative assertion
    // at the end is satisfied by a click that lands on blank and selects
    // nothing, whether or not the tracker armed.
    // One call rather than two, which is fewer moving parts and nothing more:
    // `cell_carrying` searches every cell of every row, so a prompt landing
    // between two feeds and prefixing the payload row is tolerated either way.
    // The sibling tests still feed their decoy and payload separately.
    //
    // The payload row OPENS with a shell-shaped prefix on purpose. It is what
    // gives the span predicate a red state: under a row-level `contains` the
    // search resolves the `b` of `bash-5.2$`, `select_word_at` takes the word
    // there instead of the token, and the positive control below fails by
    // name. Every other fixture in this file prints its needle at column 0,
    // where a row test and a span test pick the same cell and the refinement
    // cannot go red.
    app.terminals[0].feed_bytes_for_test(
        b"a prompt got here first\r\nbash-5.2$ hello_world_token some other text\r\n",
    );
    term.draw(|f| app.render(f)).unwrap();
    let (col, row) = cell_carrying(&app.terminals[0], "hello_world_token")
        .expect("the pane must show THIS test's token, or there is no word to mis-select");

    // Precondition: the CTRL click must match nothing, or it returns early
    // from the matched dispatch and never reaches the built-in.
    let ctrl_gesture = crate::keymap::Gesture {
        kind: crate::keymap::GestureKind::Click,
        mods: KeyModifiers::CONTROL,
    };
    assert!(
        app.keymap
            .command_for_mouse(ctrl_gesture, crate::keymap::MouseContext::Terminal)
            .is_none(),
        "ctrl+click must match nothing, or this never reaches the built-in"
    );
    assert!(
        !app.keymap
            .is_double_click_prefix(ctrl_gesture, crate::keymap::MouseContext::Terminal),
        "ctrl+click must NOT be a double-click prefix here, or the swallow \
         branch returns first and the built-in is never reached"
    );

    let mut ctrl = mouse(MouseEventKind::Down(MouseButton::Left), col, row);
    ctrl.modifiers = KeyModifiers::CONTROL;
    app.handle_mouse(ctrl);

    // A following PLAIN click must be a fresh single click, not the second
    // half of a double the modified click armed.
    app.handle_mouse(mouse(MouseEventKind::Down(MouseButton::Left), col + 1, row));
    let selected = app.terminal().selection_text();
    assert!(
        !selected.contains("hello_world_token"),
        "a plain click after an unmatched ctrl+click must not select a WORD: \
         the ctrl+click must not have armed the double-click tracker, but \
         selection was {selected:?}"
    );

    // The POSITIVE half, over the same cell. Without it, "the tracker stayed
    // unarmed" and "word selection could never have happened here" are the
    // same green: a click landing somewhere unselectable satisfies the
    // negative whatever the tracker did.
    //
    // ONE more click, not two. The plain click above DID arm the tracker -
    // that is the behaviour being contrasted with the ctrl+click, which must
    // not - so this one pairs with it (`is_double` allows a one-column drift)
    // and selects the word. A second click would then start a FRESH
    // single-cell selection and wipe it, which is what the first draft of this
    // control did and why it failed.
    app.handle_mouse(mouse(MouseEventKind::Down(MouseButton::Left), col, row));
    // The WORD, exactly. `select_word_at` brackets the alphanumeric run
    // around the pivot and `selection_text` extracts that span, so a correct
    // select yields the token and nothing else; `contains` would also accept
    // a selection wider than the word, which is the shape a scroll-clock
    // rebase can produce. This PR tightened two status assertions for the
    // same reason, so its own new control should not be looser.
    assert_eq!(
        app.terminal().selection_text(),
        "hello_world_token",
        "a PLAIN click pairing with the plain click above must select the \
         word: the tracker arms on plain clicks and refuses only modified \
         ones, and without this the negative above proves nothing"
    );
}

#[test]
fn a_modified_click_that_drags_away_does_not_pair_with_the_click_it_returns_to() {
    // The built-in trackers get `clear_if_moved` on every drag, so click,
    // drag away, click back is not a double. `ModifiedClickTracker` has no
    // such call anywhere, so the modified pair fires where the identical
    // unmodified sequence correctly does not.
    use crossterm::event::{MouseButton, MouseEventKind};
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.keymap = crate::keymap::Keymap::from_json(
        r#"[{"key": "ctrl+double_click", "command": "toggle_word_wrap", "when": "terminal"}]"#,
    );
    let backend = ratatui::backend::TestBackend::new(120, 40);
    let mut term = ratatui::Terminal::new(backend).unwrap();
    term.draw(|f| app.render(f)).unwrap();

    let area = app.terminals[0].last_area;
    assert!(
        area.width > 4 && area.height > 2,
        "terminal must be laid out"
    );
    let (col, row) = (area.x + 2, area.y + 1);

    let before = app.editor.wrap_enabled();
    let mut ctrl = mouse(MouseEventKind::Down(MouseButton::Left), col, row);
    ctrl.modifiers = KeyModifiers::CONTROL;
    app.handle_mouse(ctrl);

    // Drag well away, then release, then click the origin again.
    let mut drag = mouse(MouseEventKind::Drag(MouseButton::Left), col + 20, row + 3);
    drag.modifiers = KeyModifiers::CONTROL;
    app.handle_mouse(drag);
    app.handle_mouse(mouse(
        MouseEventKind::Up(MouseButton::Left),
        col + 20,
        row + 3,
    ));

    let mut back = mouse(MouseEventKind::Down(MouseButton::Left), col, row);
    back.modifiers = KeyModifiers::CONTROL;
    app.handle_mouse(back);

    assert_eq!(
        app.editor.wrap_enabled(),
        before,
        "a modified click that dragged away must not pair with the click it \
         returns to, but the double fired"
    );
}

/// An OSC 8 hyperlink's URI is invisible: the pane shows "CLICK-ME" while
/// the cell carries whatever the printing program chose — including a
/// file:// or custom-scheme URI that `open`/`xdg-open` would hand to an
/// arbitrary application. Only web links may open; anything else is
/// refused with the REAL destination surfaced in the status line.
#[test]
fn cmd_clicking_a_non_web_hyperlink_refuses_instead_of_opening() {
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    let backend = ratatui::backend::TestBackend::new(120, 40);
    let mut term = ratatui::Terminal::new(backend).unwrap();
    app.terminal_mut().focused = true;
    term.draw(|f| app.render(f)).unwrap();

    let script = "printf 'ref \\033]8;;file:///nonexistent-croft-probe\\033\\\\CLICK-ME\\033]8;;\\033\\\\\\n'";
    app.terminal_mut()
        .write_input(format!("{script}\r").as_bytes());
    // Locate the linked cell in the click's own coordinate space
    // (`hyperlink_at` viewport rows), not via `visible_text`, whose
    // logical-line joining shifts row indices past any soft wrap.
    let linked_cell = |app: &App| -> Option<(usize, usize)> {
        (0..40usize).find_map(|r| {
            (0..120usize)
                .find(|&c| {
                    app.terminal().hyperlink_at(r, c).as_deref()
                        == Some("file:///nonexistent-croft-probe")
                })
                .map(|c| (r, c))
        })
    };
    // Load-scaled rather than a fresh constant (#307): this budget was raised
    // 5s -> 8s once already, which is the shape of a number that cannot be
    // guessed from inside one test. The 1000ms literal that first replaced
    // it was under-calibrated against its own siblings and still lost on a
    // loaded box, so it now shares `SHELL_PAINT_BASE` with the other
    // wait-for-the-shell-to-paint test (#397).
    crate::test_budget::await_spawned(
        crate::test_budget::tests::SHELL_PAINT_BASE,
        "the shell to paint the linked cell",
        || linked_cell(&app).is_some(),
    );
    term.draw(|f| app.render(f)).unwrap();
    let (row_idx, col) = linked_cell(&app).expect("shell must print the linked text");

    let inner = app.terminal().last_inner;
    app.handle_mouse(crossterm::event::MouseEvent {
        kind: crossterm::event::MouseEventKind::Down(crossterm::event::MouseButton::Left),
        column: inner.x + col as u16,
        row: inner.y + row_idx as u16,
        modifiers: KeyModifiers::SUPER,
    });

    assert!(
        !app.status.starts_with("Opened"),
        "a file:// hyperlink must never reach the system opener (status: {})",
        app.status
    );
    assert!(
        app.status.contains("file:///nonexistent-croft-probe"),
        "the refusal must surface the real destination (status: {})",
        app.status
    );
}

#[test]
fn keyboard_enhancement_flags_request_disambiguate_escape_codes() {
    let f = keyboard_enhancement_flags();
    assert!(
        f.contains(crossterm::event::KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES),
        "Cmd / modifier delivery requires DISAMBIGUATE_ESCAPE_CODES"
    );
}

#[test]
fn keyboard_enhancement_flags_do_not_force_all_keys_as_escapes() {
    // REPORT_ALL_KEYS_AS_ESCAPE_CODES rewrites plain printable input.
    // We only need the disambiguation bit, not the all-keys bit.
    let f = keyboard_enhancement_flags();
    assert!(
        !f.contains(crossterm::event::KeyboardEnhancementFlags::REPORT_ALL_KEYS_AS_ESCAPE_CODES),
        "we should not opt into REPORT_ALL_KEYS_AS_ESCAPE_CODES"
    );
}

/// Screencast Mode: Shift+F8 shows each key pressed, typing as one run, and
/// the command the palette ran beside its Enter, above the Zen hint, until
/// the keys have lingered. Shift+F8 again stops it.
#[test]
fn screencast_mode_shows_the_keys_and_names_what_the_palette_ran() {
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.handle_key(key(KeyCode::F(8), KeyModifiers::SHIFT))
        .unwrap();
    assert!(app.screencast.is_on(), "{}", app.status);
    assert!(run_palette_row_titled(&mut app, "View: Toggle Zen Mode"));
    let screen = draw_screen(&mut app, 120, 30);
    assert!(
        screen.contains(" View: Toggle Zen Mode   Enter  View: Toggle Zen Mode"),
        "the query typed, then Enter and what it ran:\n{screen}"
    );
    assert!(
        screen.contains(zen_hint_text()),
        "the keys sit above the Zen hint, not over it:\n{screen}"
    );
    let now = std::time::Instant::now();
    assert!(!app.screencast.expire(now));
    assert!(app.screencast.expire(now + screencast::LINGER));
    assert!(!draw_screen(&mut app, 120, 30).contains("Enter  View"));
    app.handle_key(key(KeyCode::F(8), KeyModifiers::SHIFT))
        .unwrap();
    assert!(!app.screencast.is_on());
    assert_eq!(app.status, "Screencast Mode off");
    assert!(
        app.perf.status_span().is_none(),
        "Shift+F8 is not the F8 HUD"
    );
}

/// Screencast Mode never puts a secret on screen: what is typed into a
/// masked prompt, or into the shell (sudo reads a password with echo off),
/// is left out, while the keys around it show.
#[test]
fn screencast_mode_leaves_out_typing_into_a_password_field_or_the_shell() {
    use crate::widgets::input_prompt::{InputPrompt, InputPurpose};
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.toggle_screencast_mode();
    app.input_prompt = Some(InputPrompt::new(InputPurpose::NewWorktreeLane, "Token", "").masked());
    for c in "hunter2".chars() {
        app.handle_key(key(KeyCode::Char(c), KeyModifiers::NONE))
            .unwrap();
    }
    app.handle_key(key(KeyCode::Char('q'), KeyModifiers::ALT))
        .unwrap();
    assert_eq!(
        app.input_prompt.as_ref().unwrap().value,
        "hunter2q",
        "the field took the Alt-held character as part of the secret"
    );
    app.handle_key(key(KeyCode::Esc, KeyModifiers::NONE))
        .unwrap();
    assert!(app.input_prompt.is_none(), "Esc closed the prompt");
    assert_eq!(app.screencast.row_text(200), " Esc ");

    app.focus = Pane::Terminal;
    for c in "s3cret".chars() {
        app.handle_key(key(KeyCode::Char(c), KeyModifiers::NONE))
            .unwrap();
    }
    app.handle_key(key(KeyCode::Char('c'), KeyModifiers::CONTROL))
        .unwrap();
    assert_eq!(app.screencast.row_text(200), " Esc   Ctrl+C ");
    // `Cmd+K` leads in the shell too. A second key that completes no chord
    // only ends the wait, as in VS Code, and a character is still left out:
    // it may be the start of a secret typed after a stray Cmd+K. One that
    // completes a chord is shown.
    let cmd = if cfg!(target_os = "macos") {
        "Cmd"
    } else {
        "Super"
    };
    app.handle_key(key(KeyCode::Char('k'), KeyModifiers::SUPER))
        .unwrap();
    app.handle_key(key(KeyCode::Char('?'), KeyModifiers::NONE))
        .unwrap();
    assert_eq!(
        app.screencast.row_text(200),
        format!(" Esc   Ctrl+C   {cmd}+K "),
        "the ? is left out"
    );
    assert!(
        app.status.ends_with("is not a command."),
        "the ? only ended the wait"
    );
    app.handle_key(key(KeyCode::Char('k'), KeyModifiers::SUPER))
        .unwrap();
    app.handle_key(key(KeyCode::Char('k'), KeyModifiers::NONE))
        .unwrap();
    assert_eq!(
        app.screencast.row_text(200),
        format!(" Esc   Ctrl+C   {cmd}+K   {cmd}+K K "),
        "Cmd+K K cleared the terminal"
    );

    app.focus = Pane::Editor;
    for c in "ok".chars() {
        app.handle_key(key(KeyCode::Char(c), KeyModifiers::NONE))
            .unwrap();
    }
    assert!(
        app.screencast.row_text(200).ends_with(" ok "),
        "typing in the editor shows"
    );
}

/// Screencast Mode lights the cell a click lands on, for a recording that
/// has no mouse pointer in it, and lets it fade.
#[test]
fn screencast_mode_lights_a_clicked_cell_until_it_fades() {
    use crossterm::event::MouseButton;
    let (mut app, _tmp, col, row) = app_with_open_file_and_editor_cell();
    let lit = |app: &mut App| {
        let mut term = ratatui::Terminal::new(ratatui::backend::TestBackend::new(100, 30)).unwrap();
        term.draw(|f| app.render(f)).unwrap();
        term.backend().buffer()[(col, row)].bg == app.theme.accent()
    };
    press(&mut app, MouseButton::Left, (col, row));
    assert!(!lit(&mut app), "only while Screencast Mode is on");
    app.toggle_screencast_mode();
    press(&mut app, MouseButton::Left, (col, row));
    assert!(lit(&mut app));
    let now = std::time::Instant::now();
    assert!(app.screencast.expire(now + screencast::CLICK_LIT_FOR));
    assert!(!lit(&mut app));
}

/// A click on the cell being edited keeps the edit open: nothing commits.
#[test]
fn a_click_on_the_cell_being_edited_keeps_editing() {
    let (_tmp, _p, mut app) = prices_xlsx_app();
    app.handle_sheet_key(key(KeyCode::Right, KeyModifiers::NONE));
    type_into_sheet(&mut app, "5");
    let (x, y) = sheet_cell_on_screen(&app, 0, 1);
    left_click(&mut app, x, y);
    let view = app.editor.sheet.as_ref().unwrap();
    assert!(view.editing.is_some());
    assert!(view.cell_edits.is_empty() && !app.editor.dirty);
}

#[test]
fn parse_dropped_paths_accepts_a_single_existing_absolute_path() {
    let tmp = tempfile::tempdir().unwrap();
    let f = tmp.path().join("dropped.txt");
    std::fs::write(&f, "hi").unwrap();
    let parsed = parse_dropped_paths(&format!("{}\n", f.display()));
    assert_eq!(parsed, vec![f]);
}

#[test]
fn parse_dropped_paths_keeps_user_pngs_that_happen_to_share_a_temp_dir() {
    // The filter must key on the `iTerm2.<rand>.png` filename
    // pattern, not just "anything under /tmp". A real user-authored
    // PNG that happens to live under a temp directory must still be
    // accepted, otherwise the heuristic over-fires.
    let tmp = tempfile::tempdir().unwrap();
    let temp_dir = tmp.path().join("T");
    std::fs::create_dir_all(&temp_dir).unwrap();
    let user_png = temp_dir.join("my_screenshot.png");
    std::fs::write(&user_png, b"\x89PNG\r\n\x1a\n").unwrap();
    let parsed = parse_dropped_paths(&format!("{}\n", user_png.display()));
    assert_eq!(
        parsed,
        vec![user_png.clone()],
        "a user-named PNG under a temp dir must still be droppable — only the literal iTerm2.<rand>.png pattern is the artefact"
    );
}

#[test]
fn parse_dropped_paths_strips_file_url_prefix_and_decodes() {
    let tmp = tempfile::tempdir().unwrap();
    let f = tmp.path().join("with space.txt");
    std::fs::write(&f, "hi").unwrap();
    let url = format!("file://{}/with%20space.txt", tmp.path().to_string_lossy());
    let parsed = parse_dropped_paths(&url);
    assert_eq!(parsed, vec![f]);
}

#[test]
fn parse_dropped_paths_handles_backslash_escaped_spaces() {
    let tmp = tempfile::tempdir().unwrap();
    let f = tmp.path().join("with space.txt");
    std::fs::write(&f, "hi").unwrap();
    let escaped = format!("{}/with\\ space.txt", tmp.path().to_string_lossy());
    let parsed = parse_dropped_paths(&escaped);
    assert_eq!(parsed, vec![f]);
}

#[test]
fn parse_dropped_paths_supports_multi_file_drop_via_newlines() {
    let tmp = tempfile::tempdir().unwrap();
    let a = tmp.path().join("a.txt");
    let b = tmp.path().join("b.txt");
    std::fs::write(&a, "1").unwrap();
    std::fs::write(&b, "2").unwrap();
    let payload = format!("{}\n{}\n", a.display(), b.display());
    let parsed = parse_dropped_paths(&payload);
    assert_eq!(parsed.len(), 2);
    assert!(parsed.contains(&a));
    assert!(parsed.contains(&b));
}

#[test]
fn parse_dropped_paths_supports_multi_file_drop_via_unescaped_spaces() {
    // iTerm2's Finder-drop format for two files with spaces in their
    // names is `<path1> <path2>`, where spaces inside a path are
    // backslash-escaped and the separator between paths is a literal
    // space. Make sure both paths come through.
    let tmp = tempfile::tempdir().unwrap();
    let a = tmp.path().join("02 Аленький цветочек.mp3");
    let b = tmp.path().join("03 Буратино.mp3");
    std::fs::write(&a, "x").unwrap();
    std::fs::write(&b, "y").unwrap();
    let payload = format!(
        "{}/02\\ Аленький\\ цветочек.mp3 {}/03\\ Буратино.mp3",
        tmp.path().to_string_lossy(),
        tmp.path().to_string_lossy(),
    );
    let parsed = parse_dropped_paths(&payload);
    assert_eq!(parsed.len(), 2, "expected two paths, got {parsed:?}");
    assert!(parsed.contains(&a));
    assert!(parsed.contains(&b));
}

#[test]
fn parse_dropped_paths_keeps_quoted_path_with_space_intact() {
    let tmp = tempfile::tempdir().unwrap();
    let a = tmp.path().join("with space.txt");
    std::fs::write(&a, "x").unwrap();
    let payload = format!("\"{}\"", a.display());
    let parsed = parse_dropped_paths(&payload);
    assert_eq!(parsed, vec![a]);
}

#[test]
fn parse_dropped_paths_handles_mix_of_quoted_and_escaped_paths() {
    let tmp = tempfile::tempdir().unwrap();
    let a = tmp.path().join("plain.txt");
    let b = tmp.path().join("with space.txt");
    std::fs::write(&a, "x").unwrap();
    std::fs::write(&b, "y").unwrap();
    let payload = format!("{} \"{}\"", a.display(), b.display(),);
    let parsed = parse_dropped_paths(&payload);
    assert_eq!(parsed.len(), 2, "got {parsed:?}");
    assert!(parsed.contains(&a));
    assert!(parsed.contains(&b));
}

#[test]
fn parse_dropped_paths_returns_empty_for_plain_text() {
    // Plain typed text must not be hijacked as a drop import.
    let parsed = parse_dropped_paths("hello world this is some text");
    assert!(parsed.is_empty());
}

#[test]
fn parse_dropped_paths_skips_nonexistent_paths() {
    let parsed = parse_dropped_paths("/this/path/definitely/does/not/exist");
    assert!(parsed.is_empty());
}

#[test]
fn finder_drop_into_explorer_moves_file_into_workspace() {
    let workspace = tempfile::tempdir().unwrap();
    let outside = tempfile::tempdir().unwrap();
    let src = outside.path().join("dragged.txt");
    std::fs::write(&src, "hello").unwrap();
    let mut app = App::new(workspace.path().to_path_buf()).unwrap();
    app.focus_pane(Pane::Tree);
    app.set_sidebar_view(SidebarView::Explorer);
    app.handle_paste(&format!("{}\n", src.display()));
    let dest = workspace.path().join("dragged.txt");
    assert!(dest.exists(), "file should land in the workspace");
    assert!(!src.exists(), "Finder drop must move, not copy");
    assert_eq!(std::fs::read_to_string(&dest).unwrap(), "hello");
}

#[test]
fn finder_drop_into_terminal_pastes_path_instead_of_scp_in_remote_view() {
    let workspace = tempfile::tempdir().unwrap();
    let outside = tempfile::tempdir().unwrap();
    let src = outside.path().join("dragged.txt");
    std::fs::write(&src, "hello").unwrap();
    let mut app = App::new(workspace.path().to_path_buf()).unwrap();
    app.remote.targets = vec![crate::remote::RemoteTarget {
        alias: String::from("alpha"),
        host_name: Some(String::from("alpha.example.com")),
        user: Some(String::from("vitali")),
    }];
    app.remote.selected = 0;
    app.set_sidebar_view(SidebarView::Remote);
    // Focus sits in the embedded terminal (e.g. a Claude Code session)
    // while the Remote sidebar happens to be the active view. A Finder
    // drag-drop here must forward the path into the terminal so the
    // running program receives it, NOT hijack it into an scp upload to
    // the auto-selected host. Focus wins, mirroring the Explorer branch.
    app.show_terminal = true;
    app.focus_pane(Pane::Terminal);
    let editor_before = app.editor.lines.clone();
    app.handle_paste(&format!("{}\n", src.display()));
    assert!(
        app.take_pending_scp_uploads().is_empty(),
        "terminal-focused drop must not queue an scp upload",
    );
    assert_eq!(
        app.editor.lines, editor_before,
        "terminal-focused drop must not leak the path into the editor",
    );
    assert!(src.exists(), "the drop must not move or delete the source");
}

#[test]
fn finder_drop_into_editor_inserts_path_instead_of_scp_in_remote_view() {
    let workspace = tempfile::tempdir().unwrap();
    let outside = tempfile::tempdir().unwrap();
    let src = outside.path().join("e.txt");
    std::fs::write(&src, "x").unwrap();
    let mut app = App::new(workspace.path().to_path_buf()).unwrap();
    app.remote.targets = vec![crate::remote::RemoteTarget {
        alias: String::from("beta"),
        host_name: None,
        user: None,
    }];
    app.remote.selected = 0;
    app.set_sidebar_view(SidebarView::Remote);
    app.focus_pane(Pane::Editor);
    app.handle_paste(&format!("{}\n", src.display()));
    assert!(
        app.take_pending_scp_uploads().is_empty(),
        "editor-focused drop must not queue an scp upload",
    );
    assert!(
        app.editor.lines.iter().any(|l| l.contains("e.txt")),
        "editor-focused drop should insert the dropped path, was: {:?}",
        app.editor.lines,
    );
}

#[test]
fn finder_drop_onto_remote_tree_still_queues_scp() {
    let workspace = tempfile::tempdir().unwrap();
    let outside = tempfile::tempdir().unwrap();
    let src = outside.path().join("dragged.txt");
    std::fs::write(&src, "hello").unwrap();
    let mut app = App::new(workspace.path().to_path_buf()).unwrap();
    app.remote.targets = vec![crate::remote::RemoteTarget {
        alias: String::from("alpha"),
        host_name: Some(String::from("alpha.example.com")),
        user: Some(String::from("vitali")),
    }];
    app.remote.selected = 0;
    app.set_sidebar_view(SidebarView::Remote);
    // Dropping ONTO the Remote sidebar (tree focused) is the deliberate
    // upload gesture and must still queue an scp copy to the host.
    app.focus_pane(Pane::Tree);
    app.handle_paste(&format!("{}\n", src.display()));
    let queued = app.take_pending_scp_uploads();
    assert_eq!(queued.len(), 1, "one scp upload should be queued");
    assert_eq!(queued[0].alias, "alpha");
    assert_eq!(queued[0].src, src);
    assert!(src.exists(), "scp must copy, not move");
}

#[test]
fn finder_drop_on_remote_view_with_no_target_reports_clear_status() {
    let workspace = tempfile::tempdir().unwrap();
    let outside = tempfile::tempdir().unwrap();
    let src = outside.path().join("dragged.txt");
    std::fs::write(&src, "hi").unwrap();
    let mut app = App::new(workspace.path().to_path_buf()).unwrap();
    app.remote.targets.clear();
    app.remote.selected = 0;
    app.set_sidebar_view(SidebarView::Remote);
    app.focus_pane(Pane::Tree);
    app.handle_paste(&format!("{}\n", src.display()));
    assert!(app.take_pending_scp_uploads().is_empty());
    assert!(
        app.status.contains("no Remote Explorer host"),
        "status must explain the failure, was: {:?}",
        app.status,
    );
    assert!(
        src.exists(),
        "with no host the source file must be left intact",
    );
}

#[test]
fn pure_path_payload_distinguishes_finder_drag_from_text_paste() {
    // A genuine Finder drag carries only absolute path tokens.
    assert!(super::is_pure_path_payload("/Users/v/Documents/foo.txt"));
    assert!(super::is_pure_path_payload("/Users/v/a.txt /Users/v/b.txt"));
    assert!(super::is_pure_path_payload("'/Users/v/with space.txt'"));
    // Text pastes that merely contain a path always carry a non-path token, so
    // they must NOT be hijacked into a relay pull.
    assert!(!super::is_pure_path_payload("cat /etc/hosts"));
    assert!(!super::is_pure_path_payload("see /Users/v/foo.txt please"));
    assert!(!super::is_pure_path_payload("relative/path.txt"));
    assert!(!super::is_pure_path_payload(""));
}

/// #1585, negative: an Esc that clears a selection, a key between the two,
/// two Escapes too far apart, or Escapes typed into a terminal do not
/// leave Zen.
#[test]
fn an_esc_that_did_something_is_not_half_of_esc_esc() {
    let (_tmp, mut app) = zen_app();
    app.toggle_zen_mode();
    app.handle_key(key(KeyCode::Right, KeyModifiers::SHIFT))
        .unwrap();
    assert!(app.editor.selection.is_some(), "precondition: a selection");
    app.handle_key(key(KeyCode::Esc, KeyModifiers::NONE))
        .unwrap();
    assert!(app.editor.selection.is_none(), "the first Esc cleared it");
    app.handle_key(key(KeyCode::Esc, KeyModifiers::NONE))
        .unwrap();
    assert!(
        app.zen_mode(),
        "the Esc that cleared the selection did not count"
    );

    app.zen_esc_at = None;
    app.handle_key(key(KeyCode::Esc, KeyModifiers::NONE))
        .unwrap();
    app.handle_key(key(KeyCode::Right, KeyModifiers::NONE))
        .unwrap();
    app.handle_key(key(KeyCode::Esc, KeyModifiers::NONE))
        .unwrap();
    assert!(app.zen_mode(), "a key between the Escapes breaks the pair");

    app.zen_esc_at = None;
    let t = std::time::Instant::now();
    app.note_idle_zen_esc(t);
    app.note_idle_zen_esc(t + ZEN_ESC_WINDOW + std::time::Duration::from_millis(1));
    assert!(app.zen_mode(), "too far apart");

    app.zen_esc_at = None;
    app.focus = Pane::Terminal;
    app.handle_key(key(KeyCode::Esc, KeyModifiers::NONE))
        .unwrap();
    app.handle_key(key(KeyCode::Esc, KeyModifiers::NONE))
        .unwrap();
    assert!(
        app.zen_mode(),
        "Escapes for a terminal program stay its own"
    );
}

/// Clicking an annotated span pops its note, but that branch used to
/// return before the mouse-up cleanup: the zero-area selection the
/// mouse-down planted stayed painted as a one-cell highlight and the
/// pane-drag latch stayed armed.
#[test]
fn clicking_an_annotated_span_still_clears_the_click_selection() {
    use crossterm::event::{MouseButton, MouseEventKind};
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    // A SILENT pane (#477). `App::new` spawns the user's interactive shell,
    // whose prompt lands whenever the machine gets round to it; a prompt
    // arriving between the anchor below and the click scrolled the
    // annotation off the row the click was aimed at, and the popup
    // assertion read as "annotation hover broke" on a merely busy host.
    // A child that prints nothing cannot move the grid, so the only bytes
    // on it are the ones this test feeds.
    app.terminals[0] = crate::widgets::terminal::PtyTerminal::new_running(
        "/bin/sh",
        &[String::from("-c"), String::from("sleep 30")],
        tmp.path(),
    )
    .unwrap();
    app.focus_pane(Pane::Terminal);
    let backend = ratatui::backend::TestBackend::new(100, 30);
    let mut term = ratatui::Terminal::new(backend).unwrap();
    term.draw(|f| app.render(f)).unwrap();
    app.terminals[0].feed_bytes_for_test(b"annotated words here\r\n");
    // Anchor the note to the grid line the text actually landed on and aim
    // the click at that same line, rather than assuming both are row 0.
    let line = app.terminals[0]
        .find_captured_line("annotated words here", None)
        .expect("the fed text is on the grid");
    assert!(
        line >= 0,
        "with no scrollback the text sits on a viewport row, got line {line}"
    );
    let clock = app.terminals[0].scroll_clock();
    app.terminals[0].add_annotation(line, clock, 0, 15, String::from("the note"));
    let inner = app.terminals[0].last_inner;
    let (cx, cy) = (inner.x + 2, inner.y + line as u16);
    assert!(
        app.terminals[0]
            .line_text_at(cx, cy)
            .is_some_and(|(text, idx)| text.starts_with("annotated words here") && idx == 2),
        "the click must land ON the annotated text, or the popup assertion is vacuous"
    );
    app.handle_mouse(mouse(MouseEventKind::Down(MouseButton::Left), cx, cy));
    app.handle_mouse(mouse(MouseEventKind::Up(MouseButton::Left), cx, cy));
    assert!(app.hover.popup.is_some(), "the click must pop the note");
    assert!(
        app.terminals[0].selection().is_none(),
        "the zero-area click selection must not stay painted"
    );
}

/// #414: a dropped `file://` URL naming a non-ASCII path must decode to that
/// path, not to one char per byte.
///
/// The decoder pushed each `%xx` byte, and each literal byte, `as char`,
/// which maps a byte to the Latin-1 code point of the same value, so every
/// multi-byte UTF-8 sequence came out as mojibake and the drop opened a
/// path that does not exist. The same defect class as #396, in the drop
/// path.
#[test]
fn a_dropped_file_url_with_a_non_ascii_path_decodes_to_utf8() {
    use std::path::PathBuf;
    assert_eq!(
        super::normalise_dropped_token("file:///tmp/caf%C3%A9/%E4%B8%AD%E6%96%87.txt"),
        Some(PathBuf::from("/tmp/caf\u{e9}/\u{4e2d}\u{6587}.txt")),
        "percent-encoded UTF-8 reassembles into the characters it encodes"
    );
    assert_eq!(
        super::normalise_dropped_token("file:///tmp/caf\u{e9}"),
        Some(PathBuf::from("/tmp/caf\u{e9}")),
        "a literal non-ASCII character in the URL survives too"
    );
    // The ASCII cases the decoder always handled are unchanged.
    assert_eq!(
        super::normalise_dropped_token("file:///tmp/a%20b"),
        Some(PathBuf::from("/tmp/a b"))
    );
    assert_eq!(
        super::normalise_dropped_token("'/tmp/x y'"),
        Some(PathBuf::from("/tmp/x y"))
    );
    // `+` is not a space in a file URL (form encoding does not apply), and a
    // `%` that is not an escape passes through literally, wherever it sits.
    for (url, want) in [
        ("file:///tmp/a+b", "/tmp/a+b"),
        ("file:///tmp/a%zzb", "/tmp/a%zzb"),
        ("file:///tmp/a%2", "/tmp/a%2"),
        ("file:///tmp/a%", "/tmp/a%"),
    ] {
        assert_eq!(
            super::normalise_dropped_token(url),
            Some(PathBuf::from(want)),
            "{url}"
        );
    }
}

/// A `file://` URL whose escapes do not form valid UTF-8 names no path this
/// process can open. The decoder used to read each byte as Latin-1 and open
/// the wrong path; a lossy decode would instead open a path with U+FFFD in
/// it, which is just as wrong and passes every "is this a path" screen. The
/// function's contract is to drop what is not a plausible path, so it does.
#[test]
fn a_dropped_file_url_with_invalid_utf8_is_dropped() {
    assert_eq!(super::normalise_dropped_token("file:///tmp/%FF"), None);
    assert_eq!(
        super::normalise_dropped_token("file:///tmp/%C3"),
        None,
        "a truncated multi-byte lead is not a path either"
    );
}

/// The terminal only reports focus in and out once asked to (mode 1004),
/// so the takeover turns it on and every restore path turns it back off.
#[test]
fn focus_reports_are_turned_on_at_startup_and_off_on_exit() {
    let takeover = String::from_utf8(takeover_mode_seq()).unwrap();
    assert!(
        takeover.contains("\x1b[?1004h"),
        "takeover must enable focus reports: {takeover:?}"
    );
    let restore = String::from_utf8_lossy(TERMINAL_RESTORE_SEQ);
    assert!(
        restore.contains("\x1b[?1004l"),
        "the restore sequence must disable focus reports"
    );
    assert!(
        !restore.contains("\x1b[?1004h"),
        "restoring must never re-enable them"
    );
}

/// A voice session's transcript can sit in the queue for a frame: the speech
/// dialog has finished and sent it, but the frame loop has not drained it yet.
/// A mic tap in that frame is a cancel, and the cancel must win: the text is
/// dropped, not typed.
#[test]
fn a_transcript_queued_when_the_mic_is_tapped_to_cancel_is_not_typed() {
    let (mut app, _tmp, _f) = app_with_painted_file("x\n");
    app.focus = Pane::Editor;
    app.voice_handle = Some(crate::voice::VoiceHandle::finished(
        crate::voice::VoiceMsg::Transcript(String::from("hello")),
    ));

    app.toggle_voice_input();
    assert!(app.drain_voice(), "draining the result must redraw");

    assert_eq!(app.editor.lines[0], "x", "a canceled transcript was typed");
    assert_eq!(app.status, "Voice input canceled");
    assert!(app.voice_handle.is_none(), "the session must be over");
}

/// The frame loop's own order: it drains voice results before it reads the
/// input that queued up meanwhile. So a tap that reached croft before the
/// transcript did can be read after the drain that received it. That tap is
/// still a cancel: the drain holds the transcript until the next one, after
/// the queued input has been read, so the tap cancels it instead of starting
/// a new session over typed text.
#[test]
fn a_cancel_tap_read_after_the_drain_that_received_the_transcript_still_wins() {
    let (mut app, _tmp, _f) = app_with_painted_file("x\n");
    app.focus = Pane::Editor;
    app.voice_handle = Some(crate::voice::VoiceHandle::finished(
        crate::voice::VoiceMsg::Transcript(String::from("hello")),
    ));

    app.drain_voice();
    assert_eq!(
        app.editor.lines[0], "x",
        "typed before queued input was read"
    );
    assert!(
        app.voice_handle.is_some(),
        "the tap must still find the session"
    );

    app.toggle_voice_input();
    assert!(app.drain_voice(), "dropping the result must redraw");

    assert_eq!(app.editor.lines[0], "x", "a canceled transcript was typed");
    assert_eq!(app.status, "Voice input canceled");
    assert!(
        app.voice_handle.is_none(),
        "the tap must not start a new session"
    );
}

/// The control for the tests above: with no cancel tap, the transcript is
/// typed into the focused editor on the drain after the one that received it.
#[test]
fn a_transcript_with_no_cancel_tap_is_typed() {
    let (mut app, _tmp, _f) = app_with_painted_file("x\n");
    app.focus = Pane::Editor;
    app.voice_handle = Some(crate::voice::VoiceHandle::finished(
        crate::voice::VoiceMsg::Transcript(String::from("hello")),
    ));

    app.drain_voice();
    assert!(app.drain_voice(), "typing the transcript must redraw");

    assert_eq!(app.editor.lines[0], "hellox");
    assert!(app.voice_handle.is_none());
}

/// #2602: a session whose reader crashed before it had a result ends, the
/// status line says so, and the next tap starts a session instead of
/// cancelling the dead one again.
#[test]
fn a_voice_session_whose_reader_crashed_ends_and_the_next_tap_starts_one() {
    let (mut app, _tmp, _f) = app_with_painted_file("x\n");
    app.focus = Pane::Editor;
    app.voice_handle = Some(crate::voice::VoiceHandle::crashed());
    crate::test_budget::await_spawned(
        std::time::Duration::from_secs(5),
        "the crashed session to end",
        || {
            app.drain_voice();
            app.voice_handle.is_none()
        },
    );
    assert_eq!(app.status, VOICE_READER_CRASHED);
    assert!(!app.voice_canceling);
    app.toggle_voice_input();
    assert_ne!(
        app.status, "Voice input canceled",
        "the tap starts a session rather than cancel the dead one"
    );
    assert!(!app.voice_canceling);
}