tix-tracker 0.11.0

A terminal issue tracker: markdown + YAML frontmatter issues with a kanban TUI and CLI
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
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
#[allow(clippy::wildcard_imports)]
use super::super::*;
#[allow(clippy::wildcard_imports)]
use super::helpers::*;
use std::fs;

#[test]
fn new_issue_form_without_a_cycle_joins_the_single_running_cycle() {
    let (_tmp, mut app) = make_app();
    // One cycle whose window certainly contains today; no configured default.
    app.store.config.cycles.insert(
        "October 2026".into(),
        crate::config::PlanningCycle {
            description: None,
            start_date: Some("2020-01-01".into()),
            due_date: Some("2999-12-31".into()),
        },
    );
    app.store.config.planning.default_cycle = None;

    let _ = app.handle_key(press(KeyCode::Char('n'))).unwrap();
    for c in "Automatic".chars() {
        let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
    }
    // Enter on the (non-empty) Title row submits; no cycle was chosen, so
    // the form takes the same `default_cycle_name` path the CLI core does.
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    assert!(matches!(app.mode, Mode::Board), "the form submitted");

    let issue = app
        .store
        .list()
        .unwrap()
        .into_iter()
        .find(|issue| issue.title == "Automatic")
        .expect("created issue");
    assert_eq!(
        issue.cycles,
        Some(vec!["October 2026".to_string()]),
        "the TUI form assigns the single running cycle"
    );
}

#[test]
fn n_opens_form() {
    let (_tmp, mut app) = make_app();
    let _ = app.handle_key(press(KeyCode::Char('n'))).unwrap();
    assert!(matches!(app.mode, Mode::Form));
    assert!(app.form.is_some());
}

#[test]
fn form_submit_creates_issue() {
    let (_tmp, mut app) = make_app();
    let _ = app.handle_key(press(KeyCode::Char('n'))).unwrap();
    if let Mode::Form = &app.mode {
        // Type title
        for c in "My Issue".chars() {
            let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
        }
        // Submit
        let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    }
    assert!(matches!(app.mode, Mode::Board));
    assert!(
        app.status_msg
            .as_deref()
            .is_some_and(|m| m.starts_with("Created ")),
        "got {:?}",
        app.status_msg
    );
}

#[test]
fn form_submit_selects_created_issue() {
    let (_tmp, mut app) = make_app();
    let _ = app.handle_key(press(KeyCode::Char('n'))).unwrap();
    if let Mode::Form = &app.mode {
        for c in "Selected Issue".chars() {
            let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
        }
        let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    }
    assert!(matches!(app.mode, Mode::Board));
    let card = app.board.get_selected_card().expect("selected card");
    assert_eq!(card.title, "Selected Issue");
    assert_eq!(app.board.active_col, 0);
}

#[test]
fn form_submit_selects_created_issue_in_assignee_segment() {
    let (tmp, mut app) = make_app();
    let dir = tmp.path().join("issues");
    write_issue(&dir, "2026-01-01-a.md", 1, "Old", "todo", None);
    let config = app.store.config.clone();
    app.board = BoardState::build(&app.store, config.default_config());

    let _ = app.handle_key(press(KeyCode::Char('n'))).unwrap();
    if let Mode::Form = &app.mode {
        for c in "New".chars() {
            let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
        }
    }
    app.form.as_mut().unwrap().assignee = "alice".to_string();
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();

    assert!(matches!(app.mode, Mode::Board));
    let card = app.board.get_selected_card().expect("selected card");
    assert_eq!(
        card.title, "New",
        "the just-created issue is selected, not the previously selected one"
    );
    assert!(card.filename.starts_with("alice/"), "got {}", card.filename);
}

#[test]
fn form_empty_title_rejected() {
    let (_tmp, mut app) = make_app();
    let _ = app.handle_key(press(KeyCode::Char('n'))).unwrap();
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    assert!(matches!(app.mode, Mode::Form));
    assert_eq!(app.status_msg.as_deref(), Some("Title is required"));
}

#[test]
fn form_esc_cancels() {
    let (_tmp, mut app) = make_app();
    let _ = app.handle_key(press(KeyCode::Char('n'))).unwrap();
    assert!(matches!(app.mode, Mode::Form));
    let _ = app.handle_key(press(KeyCode::Esc)).unwrap();
    assert!(matches!(app.mode, Mode::Board));
}

#[test]
fn form_arrows_navigate_fields() {
    let (_tmp, mut app) = make_app();
    let _ = app.handle_key(press(KeyCode::Char('n'))).unwrap();
    use crate::ui::form::FormFocus::*;
    assert_eq!(form_focus(&app), Title);
    let _ = app.handle_key(press(KeyCode::Down)).unwrap();
    assert_eq!(form_focus(&app), Status);
    let _ = app.handle_key(press(KeyCode::Up)).unwrap();
    assert_eq!(form_focus(&app), Title);
    for _ in 0..crate::ui::form::FormFocus::RING.len() {
        let _ = app.handle_key(press(KeyCode::Down)).unwrap();
    }
    assert_eq!(form_focus(&app), Title, "Down wraps forward");
    let _ = app.handle_key(press(KeyCode::Up)).unwrap();
    assert_eq!(
        form_focus(&app),
        *crate::ui::form::FormFocus::RING
            .last()
            .expect("RING is non-empty"),
        "Up wraps backward, from the last field in the ring"
    );
}

#[test]
fn form_j_and_k_type_their_letters() {
    let (_tmp, mut app) = make_app();
    let _ = app.handle_key(press(KeyCode::Char('n'))).unwrap();
    let _ = app.handle_key(press(KeyCode::Char('j'))).unwrap();
    let _ = app.handle_key(press(KeyCode::Char('k'))).unwrap();
    let form = app.form.as_ref().unwrap();
    assert_eq!(form.title, "jk", "j and k must type, not navigate");
    assert_eq!(form.focus, crate::ui::form::FormFocus::Title);
}

#[test]
fn form_invalid_date_order_is_rejected() {
    let (_tmp, mut app) = make_app();
    let _ = app.handle_key(press(KeyCode::Char('n'))).unwrap();
    for c in "Date check".chars() {
        let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
    }
    for _ in 0..9 {
        let _ = app.handle_key(press(KeyCode::Down)).unwrap();
    }
    let _ = app.handle_key(press(KeyCode::F(2))).unwrap();
    assert!(matches!(app.mode, Mode::DatePicker { .. }));
    let _ = app.handle_key(press(KeyCode::PageDown)).unwrap();
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    let _ = app.handle_key(press(KeyCode::Down)).unwrap();
    let _ = app.handle_key(press(KeyCode::F(2))).unwrap();
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    assert!(matches!(app.mode, Mode::Form));
    assert_eq!(
        app.status_msg.as_deref(),
        Some("Due date must be >= start date")
    );
}

#[test]
fn form_date_field_masked_typing_and_picker() {
    let (_tmp, mut app) = make_app();
    let _ = app.handle_key(press(KeyCode::Char('n'))).unwrap();
    for c in "Dated".chars() {
        let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
    }
    for _ in 0..9 {
        let _ = app.handle_key(press(KeyCode::Down)).unwrap();
    }
    assert!(app.form.as_ref().unwrap().is_date_focus());

    // Enter on an empty date field opens the picker.
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    assert!(matches!(app.mode, Mode::DatePicker { .. }));
    let _ = app.handle_key(press(KeyCode::Esc)).unwrap();
    assert!(matches!(app.mode, Mode::Form));

    // Typing digits edits the date in place with the mask.
    for c in "20260910".chars() {
        let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
    }
    assert_eq!(app.form.as_ref().unwrap().start_date, "2026-09-10");

    // Enter on a complete date submits the form.
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    assert!(matches!(app.mode, Mode::Board));
    let issues = app.store.list().unwrap();
    let issue = issues
        .iter()
        .find(|i| i.title == "Dated")
        .expect("the form created the issue");
    assert_eq!(issue.start_date.as_deref(), Some("2026-09-10"));
}

#[test]
fn form_date_picker_select_and_clear() {
    let (_tmp, mut app) = make_app();
    let _ = app.handle_key(press(KeyCode::Char('n'))).unwrap();
    for c in "Dated".chars() {
        let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
    }
    for _ in 0..9 {
        let _ = app.handle_key(press(KeyCode::Down)).unwrap();
    }

    let _ = app.handle_key(press(KeyCode::F(2))).unwrap();
    assert!(matches!(app.mode, Mode::DatePicker { .. }));
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    assert!(matches!(app.mode, Mode::Form));
    assert_eq!(app.form.as_ref().unwrap().start_date, today());

    let _ = app.handle_key(press(KeyCode::F(2))).unwrap();
    let _ = app.handle_key(press(KeyCode::Esc)).unwrap();
    assert_eq!(
        app.form.as_ref().unwrap().start_date,
        today(),
        "Esc leaves the date unchanged"
    );

    let _ = app.handle_key(press(KeyCode::F(2))).unwrap();
    let _ = app.handle_key(press(KeyCode::Char('c'))).unwrap();
    assert_eq!(
        app.form.as_ref().unwrap().start_date,
        "",
        "c clears the date field"
    );
}

#[test]
fn date_picker_day_click_changes_selected_date() {
    let (_tmp, mut app) = make_app();
    app.frame_size.set(Rect::new(0, 0, 80, 24));
    let _ = app.handle_key(press(KeyCode::Char('n'))).unwrap();
    for _ in 0..9 {
        let _ = app.handle_key(press(KeyCode::Down)).unwrap();
    }
    app.form.as_mut().unwrap().start_date = "2026-03-15".into();
    let _ = app.handle_key(press(KeyCode::F(2))).unwrap();
    assert!(matches!(app.mode, Mode::DatePicker { .. }));

    let (x, y) = drawn_date_picker_day(&app, "2026-03-16");
    let action = app.handle_mouse(down_left(x, y)).unwrap();
    assert_eq!(
        action,
        Action::DatePickerHit(DatePickerHit::Day("2026-03-16".into()))
    );
    if let Action::DatePickerHit(DatePickerHit::Day(date)) = action {
        app.date_picker.as_mut().unwrap().select_date_str(&date);
    }
    assert_eq!(app.date_picker.as_ref().unwrap().selected(), "2026-03-16");
}

#[test]
fn date_picker_ok_click_submits_selected_date() {
    let (_tmp, mut app) = make_app();
    app.frame_size.set(Rect::new(0, 0, 80, 24));
    let _ = app.handle_key(press(KeyCode::Char('n'))).unwrap();
    for _ in 0..9 {
        let _ = app.handle_key(press(KeyCode::Down)).unwrap();
    }
    app.form.as_mut().unwrap().start_date = "2026-03-15".into();
    let _ = app.handle_key(press(KeyCode::F(2))).unwrap();

    let (x, y) = drawn_date_picker_day(&app, "2026-03-16");
    let _ = app.handle_mouse(down_left(x, y)).unwrap();
    app.date_picker
        .as_mut()
        .unwrap()
        .select_date_str("2026-03-16");

    let picker = app.date_picker.as_ref().unwrap();
    let rect = picker.popup_rect(app.frame_size.get());
    let ok_x = rect.x + rect.width / 2;
    let ok_y = rect.y + rect.height.saturating_sub(1);
    let action = app.handle_mouse(down_left(ok_x, ok_y)).unwrap();
    assert_eq!(action, Action::DatePickerHit(DatePickerHit::Submit));

    let _ = app.handle_date_picker_key(press(KeyCode::Enter)).unwrap();
    assert!(matches!(app.mode, Mode::Form));
    assert_eq!(app.form.as_ref().unwrap().start_date, "2026-03-16");
}

#[test]
fn form_non_date_field_still_opens_list_picker() {
    let (_tmp, mut app) = make_app();
    let _ = app.handle_key(press(KeyCode::Char('n'))).unwrap();
    for _ in 0..6 {
        let _ = app.handle_key(press(KeyCode::Down)).unwrap();
    }
    let _ = app.handle_key(press(KeyCode::F(2))).unwrap();
    assert!(
        app.form_picker.is_some(),
        "cycles field opens the list picker"
    );
    assert!(!matches!(app.mode, Mode::DatePicker { .. }));
}

#[test]
fn form_picker_inline_create_cycle_persists_and_populates_field() {
    let (_tmp, mut app) = make_app();
    let _ = app.handle_key(press(KeyCode::Char('n'))).unwrap();
    for c in "Picker cycle".chars() {
        let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
    }
    for _ in 0..6 {
        let _ = app.handle_key(press(KeyCode::Down)).unwrap();
    }

    let _ = app.handle_key(press(KeyCode::F(2))).unwrap();
    assert!(app.form_picker.is_some(), "picker should open on cycles");
    let _ = app.handle_key(press(KeyCode::Char('+'))).unwrap();
    for c in "Cycle 42".chars() {
        let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
    }
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();

    let form = app.form.as_ref().unwrap();
    assert!(
        form.cycles.split(',').any(|c| c.trim() == "Cycle 42"),
        "created cycle should populate form field, got {:?}",
        form.cycles
    );
    assert!(
        app.store.config.cycles.contains_key("Cycle 42"),
        "new cycle should be persisted in config"
    );

    let _ = app.handle_key(press(KeyCode::F(2))).unwrap();
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    assert!(matches!(app.mode, Mode::Board));
    let issue = app.store.list().unwrap().into_iter().next().unwrap();
    assert!(
        issue
            .cycles
            .unwrap_or_default()
            .iter()
            .any(|c| c == "Cycle 42")
    );
}

#[test]
fn form_picker_selects_existing_label() {
    let (tmp, mut app) = make_app();
    let dir = tmp.path().join("issues");
    let seed = "---\nnumber: 1\ntitle: \"Seed\"\nstatus: \"todo\"\nlabels: [\"bug\"]\npriority: \"medium\"\ncreated: \"2026-01-01\"\nupdated: \"2026-01-01\"\n---\n\nBody text.";
    fs::write(dir.join("2026-01-01-seed.md"), seed).unwrap();
    app.refresh_corpus().unwrap();

    let _ = app.handle_key(press(KeyCode::Char('n'))).unwrap();
    for c in "Picker labels".chars() {
        let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
    }
    for _ in 0..3 {
        let _ = app.handle_key(press(KeyCode::Down)).unwrap();
    }
    let _ = app.handle_key(press(KeyCode::F(2))).unwrap();
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    let _ = app.handle_key(press(KeyCode::F(2))).unwrap();
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();

    assert!(matches!(app.mode, Mode::Board));
    let issues = app.store.list().unwrap();
    let created = issues
        .into_iter()
        .find(|i| i.title == "Picker labels")
        .expect("created issue");
    assert!(created.labels.iter().any(|l| l == "bug"));
}

/// `tix new` starts the session on the create form (#1398). This is the state
/// `app::run_new_issue` opens, and it must be the *same* form `n` produces — both
/// go through [`App::open_new_issue_form`], and this pins the outcome so a future
/// second implementation of "prefill the form" shows up as a difference.
#[test]
fn new_issue_form_is_the_form_the_board_key_opens() {
    let (_tmp, mut by_key) = make_app();
    by_key.mode = Mode::Board;
    let _ = by_key.handle_key(press(KeyCode::Char('n'))).unwrap();

    let (_tmp2, mut by_command) = make_app();
    by_command.open_new_issue_form();

    assert!(matches!(by_command.mode, Mode::Form));
    assert!(matches!(by_key.mode, Mode::Form));
    let key_form = by_key.form.as_ref().expect("n opens a form");
    let cmd_form = by_command.form.as_ref().expect("the command opens a form");
    assert_eq!(cmd_form.title, key_form.title);
    assert_eq!(cmd_form.status, key_form.status);
    assert_eq!(cmd_form.assignee, key_form.assignee);
    assert_eq!(cmd_form.focus, key_form.focus);
}

/// Every printable ASCII character can be typed into a free-text prompt.
///
/// All four of `Comment`, `Asset`, `Filter` and `MoveIntoDone` read arbitrary
/// text into a buffer — a comment body, a **filesystem path**, a filter string, a
/// commit message. `q` used to be bound to `cancel` in all four, copied in from
/// the list-navigation contexts. That is not a style wart: the binding resolves
/// to the action's canonical event (`esc`), so pressing `q` **left the mode**,
/// and every character typed after it was then applied as a command in the mode
/// underneath.
///
/// For `Asset` the input is a path, so this is reachable from ordinary use: attach
/// an asset from `~/Pictures/q-report.png` and the prompt closes on the `q`, and
/// whatever you type next is executed as detail-view commands. This was found by
/// the `asset_round_trip_via_detail` flake (#1305): that test types the source
/// path through `handle_key`, and the path came from a `tempfile` directory whose
/// six random characters are `[A-Za-z0-9]`, so about one run in forty hit a `q` —
/// and one failure reported `status = "Commented by …"` with no asset written.
///
/// The test is deliberately **deterministic**: it enumerates the printable ASCII
/// range rather than depending on a random name, so it can never miss the bug it
/// exists to prevent. `esc` is the cancel key, which is already this codebase's
/// convention for text entry (`FilterModal` and `ConfirmDelete` bind `esc` only).
#[test]
fn every_printable_character_is_typeable_in_a_free_text_prompt() {
    let (_tmp, app) = make_app();
    let free_text = [
        InputContext::Comment,
        InputContext::Asset,
        InputContext::Filter,
        InputContext::MoveIntoDone,
    ];

    // `q` and `Q` are the two that used to be stolen. Asserted by name as well
    // as by the sweep below, so a future reader does not have to diff the binding
    // table to see what this test is about.
    for context in free_text {
        for ch in ['q', 'Q'] {
            let event = KeyEvent::new(KeyCode::Char(ch), crossterm::event::KeyModifiers::NONE);
            assert_ne!(
                app.bindings.action(context, event),
                Some(crate::keybindings::TuiAction::Cancel),
                "{context:?} must be able to type `{ch}`: it is bound to cancel, so \
                     pressing it leaves the mode and the rest of the input is applied as \
                     commands underneath"
            );
        }
    }

    // The sweep: no printable ASCII character may resolve to a binding at all in a
    // free-text context. `Esc` and `Enter` are control keys rather than
    // characters, so they are out of scope here — `Enter` submits, which is right.
    for context in free_text {
        let mut stolen: Vec<char> = Vec::new();
        for code in 0x20u8..0x7f {
            let ch = code as char;
            let event = KeyEvent::new(KeyCode::Char(ch), crossterm::event::KeyModifiers::NONE);
            if app.bindings.action(context, event).is_some() {
                stolen.push(ch);
            }
        }
        assert!(
            stolen.is_empty(),
            "{context:?} steals these printable characters, so they cannot be \
                 typed: {stolen:?}"
        );
    }

    // `esc` still cancels — the fix moved `q` out, it did not remove the escape
    // hatch. Asserted on the real handler, not just the binding table.
    let (tmp, mut app) = make_app();
    let dir = tmp.path().join("issues");
    write_issue(&dir, "2026-01-01-a.md", 1, "A", "todo", None);
    app.board = BoardState::build(&app.store, app.store.config.default_config());
    open_detail(&mut app);
    let _ = app.handle_key(press(KeyCode::Char('c'))).unwrap();
    assert!(
        matches!(app.mode, Mode::Comment { .. }),
        "enter the comment prompt"
    );
    for c in "has a q in it".chars() {
        let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
    }
    if let Mode::Comment { ref buffer, .. } = app.mode {
        assert_eq!(
            buffer, "has a q in it",
            "every character reached the buffer"
        );
    } else {
        panic!("the prompt must still be open, got {:?}", app.mode);
    }
    let _ = app.handle_key(press(KeyCode::Esc)).unwrap();
    assert!(
        !matches!(app.mode, Mode::Comment { .. }),
        "esc must still cancel, got {:?}",
        app.mode
    );
}

#[test]
fn form_creation_without_a_cycle_selection_joins_the_default_cycle() {
    let (_tmp, mut app) = make_app();
    app.handle_key(press(KeyCode::Char('n'))).unwrap();
    assert!(matches!(app.mode, Mode::Form));
    app.form.as_mut().unwrap().title = "Fresh".into();
    app.handle_key(press(KeyCode::Enter)).unwrap();
    let issue = app
        .store
        .list()
        .unwrap()
        .into_iter()
        .find(|issue| issue.title == "Fresh")
        .expect("the form created the issue");
    assert_eq!(issue.cycles, Some(vec!["Default".to_string()]));
}

#[test]
fn f1_opens_the_new_issue_mode_as_content() {
    let (_tmp, mut app) = make_shell_app();
    app.handle_key(press(KeyCode::F(1))).unwrap();
    assert!(matches!(app.mode, Mode::Form));
    assert_eq!(app.top_mode, TopMode::NewIssue);

    // The form is the mode content beside the navigation, not a modal: the
    // block's `New Issue` title shares row 0 with the nav column.
    let cells = draw_board(&app, 80, 24);
    let row0 = flush_line(&cells, 80, 0);
    assert!(
        row0.starts_with("F1"),
        "navigation still occupies the left column: {row0:?}"
    );
    assert!(
        row0.contains("New Issue"),
        "the form block is the mode content: {row0:?}"
    );
}

#[test]
fn new_issue_defaults_to_todo_and_creates_there() {
    let (_tmp, mut app) = make_shell_app();
    app.handle_key(press(KeyCode::F(1))).unwrap();
    let form = app.form.as_ref().unwrap();
    assert_eq!(form.target, crate::ui::form::NewIssueTarget::Todo);
    assert_eq!(
        form.status, "todo",
        "the default target seeds the status row"
    );

    type_text(&mut app, "Todo bound");
    app.handle_key(press(KeyCode::Enter)).unwrap();
    assert!(matches!(app.mode, Mode::Board));
    let issue = app
        .store
        .list()
        .unwrap()
        .into_iter()
        .find(|i| i.title == "Todo bound")
        .expect("the mode created the issue");
    assert_eq!(issue.status.to_string(), "todo");
}

#[test]
fn new_issue_mode_rejects_an_empty_title() {
    let (_tmp, mut app) = make_shell_app();
    app.handle_key(press(KeyCode::F(1))).unwrap();
    app.handle_key(press(KeyCode::Enter)).unwrap();
    assert!(matches!(app.mode, Mode::Form), "the mode stays open");
    assert_eq!(app.status_msg.as_deref(), Some("Title is required"));
    assert!(app.store.list().unwrap().is_empty(), "nothing was written");
}

/// The Status dropdown offers exactly the board's columns, in board order
/// (tix #1508), plus the field's current value when the board no longer
/// configures it — a status the board dropped must stay selectable.
#[test]
fn status_picker_options_are_the_board_columns_plus_an_off_board_current_value() {
    let (_tmp, mut app) = make_app();
    let columns = app.active_board_config().columns.clone();
    assert_eq!(columns, vec!["todo", "in progress", "in review", "done"]);

    app.handle_key(press(KeyCode::F(1))).unwrap();
    let ids = |app: &App| -> Vec<String> {
        app.picker_options(FormPickerField::Status)
            .into_iter()
            .map(|item| item.id)
            .collect()
    };
    assert_eq!(
        ids(&app),
        columns,
        "the options are the board's columns, in board order"
    );

    // A status the board does not configure is appended, once.
    app.form.as_mut().unwrap().status = "legacy".to_string();
    let mut expected = columns.clone();
    expected.push("legacy".to_string());
    assert_eq!(ids(&app), expected);
    assert_eq!(
        ids(&app)
            .iter()
            .filter(|id| id.as_str() == "legacy")
            .count(),
        1,
        "the current value is appended once, not duplicated"
    );

    // A current value that *is* a column is not repeated.
    app.form.as_mut().unwrap().status = "done".to_string();
    assert_eq!(ids(&app), columns);
}

/// The Priority dropdown is the built-in `low`/`medium`/`high` (tix #1508),
/// then every priority the store already uses, then the current value —
/// de-duplicated, built-ins first.
#[test]
fn priority_picker_options_are_built_ins_then_priorities_in_use() {
    let (tmp, mut app) = make_app();
    let dir = tmp.path().join("issues");
    fs::write(
        dir.join("2026-01-01-urgent.md"),
        "---\nnumber: 1\ntitle: \"Urgent\"\nstatus: \"todo\"\nlabels: []\n\
             priority: \"urgent\"\ncreated: \"2026-01-01\"\nupdated: \"2026-01-01\"\n---\n\n\
             Body.",
    )
    .unwrap();
    app.refresh_corpus().unwrap();
    app.handle_key(press(KeyCode::F(1))).unwrap();

    let ids = |app: &App| -> Vec<String> {
        app.picker_options(FormPickerField::Priority)
            .into_iter()
            .map(|item| item.id)
            .collect()
    };
    assert_eq!(
        ids(&app),
        vec!["low", "medium", "high", "urgent"],
        "built-ins first, then a priority the store uses"
    );

    // The default `medium` is already a built-in, so it is not repeated.
    app.form.as_mut().unwrap().priority = "medium".to_string();
    assert_eq!(ids(&app), vec!["low", "medium", "high", "urgent"]);

    // A custom current value the store does not carry yet is appended.
    app.form.as_mut().unwrap().priority = "blocker".to_string();
    assert_eq!(
        ids(&app),
        vec!["low", "medium", "high", "urgent", "blocker"]
    );
}

/// The change is scoped to Status and Priority (tix #1508): the other picker
/// rows still accept typing, and Enter still submits from a text row.
#[test]
fn the_other_form_rows_keep_their_behaviour() {
    let (_tmp, mut app) = make_app();
    app.handle_key(press(KeyCode::F(1))).unwrap();
    type_text(&mut app, "Scoped");
    app.handle_key(press(KeyCode::Down)).unwrap(); // Status
    app.handle_key(press(KeyCode::Down)).unwrap(); // Priority
    app.handle_key(press(KeyCode::Down)).unwrap(); // Labels
    assert_eq!(
        app.form.as_ref().unwrap().focus,
        crate::ui::form::FormFocus::Labels
    );
    type_text(&mut app, "bug");
    assert_eq!(
        app.form.as_ref().unwrap().labels,
        "bug",
        "Labels still accepts typed text"
    );

    app.handle_key(press(KeyCode::Enter)).unwrap();
    assert!(
        matches!(app.mode, Mode::Board),
        "Enter still submits from a plain text row"
    );
    let issue = app
        .store
        .list()
        .unwrap()
        .into_iter()
        .find(|i| i.title == "Scoped")
        .expect("the mode created the issue");
    assert_eq!(issue.labels, vec!["bug".to_string()]);
}

#[test]
fn clone_opens_the_new_issue_mode_and_creates_a_distinct_issue() {
    let (tmp, mut app) = make_shell_app();
    let dir = tmp.path().join("issues");
    fs::write(
        dir.join("2026-01-01-source.md"),
        "---\nnumber: 7\ntitle: \"Source issue\"\nstatus: \"todo\"\nlabels: [\"bug\"]\n\
             priority: \"high\"\ncreated: \"2026-01-01\"\nupdated: \"2026-01-01\"\n---\n\n\
             Original body.",
    )
    .unwrap();
    app.refresh_corpus().unwrap();
    // The starter board's first column is `triage`; select the seeded card in
    // `todo` so `C` has a source.
    app.select_card_by_filename("2026-01-01-source.md");

    app.handle_key(press(KeyCode::Char('C'))).unwrap();
    assert!(
        matches!(app.mode, Mode::Form),
        "clone lands in the New Issue mode, got {:?}",
        app.mode
    );
    assert_eq!(app.top_mode, TopMode::NewIssue);
    let form = app.form.as_ref().unwrap();
    assert_eq!(form.title, "Source issue");
    assert_eq!(form.labels, "bug");
    assert_eq!(form.priority, "high");
    assert_eq!(form.body, "Original body.", "the body is copied too");

    // Submit unchanged: the copy is a new issue, not an edit of the source.
    app.handle_key(press(KeyCode::Enter)).unwrap();
    let issues = app.store.list().unwrap();
    assert_eq!(issues.len(), 2, "clone creates a second file");
    let created = issues
        .iter()
        .find(|i| i.title == "Source issue" && i.number != 7)
        .expect("a distinct issue was created");
    assert_eq!(created.labels, vec!["bug".to_string()]);
    assert_eq!(created.body, "Original body.");
    let source = issues.iter().find(|i| i.number == 7).unwrap();
    assert_eq!(
        source.body, "Original body.",
        "the source is untouched by the clone"
    );
}

#[test]
fn template_prefills_body_and_labels_before_submission() {
    let (_tmp, mut app) = make_templated_app();
    app.handle_key(press(KeyCode::F(1))).unwrap();
    type_text(&mut app, "Templated");

    for _ in 0..13 {
        app.handle_key(press(KeyCode::Down)).unwrap();
    }
    assert_eq!(
        app.form.as_ref().unwrap().focus,
        crate::ui::form::FormFocus::Template
    );
    app.handle_key(press(KeyCode::Enter)).unwrap();
    assert!(
        app.form_picker.is_some(),
        "Enter on Template opens the picker"
    );
    // Options are `(none)`, `bug`, `feature`: step onto `bug` and choose it.
    app.handle_key(press(KeyCode::Down)).unwrap();
    app.handle_key(press(KeyCode::Enter)).unwrap();

    let form = app.form.as_ref().unwrap();
    assert_eq!(form.template, "bug");
    assert_eq!(form.labels, "bug", "the template pre-fills labels");
    assert!(
        form.body.contains("Steps to reproduce"),
        "the template pre-fills the body, got {:?}",
        form.body
    );
    assert!(
        app.form_picker.is_none(),
        "choosing closes the template picker"
    );

    app.form.as_mut().unwrap().focus = crate::ui::form::FormFocus::Title;
    app.handle_key(press(KeyCode::Enter)).unwrap();
    let issue = app
        .store
        .list()
        .unwrap()
        .into_iter()
        .find(|i| i.title == "Templated")
        .expect("the mode created the issue");
    assert_eq!(issue.labels, vec!["bug".to_string()]);
    assert!(issue.body.contains("Steps to reproduce"));
}

#[test]
fn new_issue_submits_without_a_template() {
    let (_tmp, mut app) = make_shell_app();
    app.handle_key(press(KeyCode::F(1))).unwrap();
    assert!(app.store.config.templates.is_empty());
    assert_eq!(
        app.form.as_ref().unwrap().template,
        "",
        "no template is selected by default"
    );

    type_text(&mut app, "No template");
    app.handle_key(press(KeyCode::Enter)).unwrap();
    let issue = app
        .store
        .list()
        .unwrap()
        .into_iter()
        .find(|i| i.title == "No template")
        .expect("submitting without a template works");
    assert_eq!(issue.labels, [] as [std::string::String; 0]);
    assert_eq!(issue.body, "");
}

#[test]
fn template_selector_is_mouse_operable() {
    let (_tmp, mut app) = make_templated_app();
    app.handle_key(press(KeyCode::F(1))).unwrap();
    let _ = draw_board(&app, 80, 24);

    // Click the Template row to open the picker.
    let row =
        crate::ui::form::field_row_rect(app.frame_size.get(), crate::ui::form::FormFocus::Template)
            .expect("the Template row is visible");
    let action = app.handle_mouse(down_left(row.x + 3, row.y)).unwrap();
    assert_eq!(action, Action::TemplateOpen);
    app.apply_new_issue_action(action).unwrap();
    assert!(
        app.form_picker
            .as_ref()
            .is_some_and(|p| p.field == FormPickerField::Template),
        "the click opened the template picker"
    );

    // Draw again so the picker's option rows record their regions, then click
    // the `bug` option (position 1, after `(none)`).
    let _ = draw_board(&app, 80, 24);
    let option = {
        let regions = app.frame_hit_regions.borrow();
        regions
            .iter()
            .find(|r| r.action == Action::TemplatePickOption(1))
            .map(|r| r.rect)
            .expect("the bug option recorded a click region")
    };
    let action = app.handle_mouse(down_left(option.x + 1, option.y)).unwrap();
    assert_eq!(action, Action::TemplatePickOption(1));
    app.apply_new_issue_action(action).unwrap();

    let form = app.form.as_ref().unwrap();
    assert_eq!(form.template, "bug");
    assert_eq!(form.labels, "bug");
    assert!(form.body.contains("Steps to reproduce"));
    assert!(app.form_picker.is_none(), "the click closed the picker");
}

/// Enter on a trunk switches modes, and Enter on a template limb opens the
/// seeded New Issue form.
#[test]
fn rail_enter_switches_modes_and_opens_a_template() {
    let (_tmp, mut app) = make_branchy_app();
    let _ = app.handle_key(press(KeyCode::Char('h'))).unwrap();
    // Board trunk (index 4) up to the New Issue trunk (index 0): Triage
    // trunk, the `feature` limb, the `bug` limb, then the trunk.
    for _ in 0..4 {
        let _ = app.handle_key(press(KeyCode::Up)).unwrap();
    }
    assert_eq!(app.nav_focus_index(), Some(0));
    assert_eq!(
        app.nav_rows()[0],
        Action::SwitchMode(TopMode::NewIssue),
        "the New Issue trunk is the first rail row"
    );
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    assert!(
        matches!(app.mode, Mode::Form),
        "a trunk Enter switches modes"
    );
    assert_eq!(app.top_mode, TopMode::NewIssue);
    assert_eq!(
        app.nav_focus_index(),
        None,
        "Enter hands the keyboard to the opened form"
    );

    let (_tmp, mut app) = make_branchy_app();
    let _ = app.handle_key(press(KeyCode::Char('h'))).unwrap();
    for _ in 0..4 {
        let _ = app.handle_key(press(KeyCode::Up)).unwrap();
    }
    let _ = app.handle_key(press(KeyCode::Char('j'))).unwrap();
    assert_eq!(
        app.nav_rows()[app.nav_focus_index().unwrap()],
        Action::NewFromTemplate("bug".into())
    );
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    assert!(matches!(app.mode, Mode::Form));
    assert_eq!(app.top_mode, TopMode::NewIssue);
    assert_eq!(app.form.as_ref().map(|f| f.template.as_str()), Some("bug"));
    assert_eq!(app.nav_focus_index(), None);
}

/// The title field is not append-only: Left/Right, Ctrl-A/E, Ctrl-Left/Right
/// and the delete keys act on an insertion point (tix #1746).
#[test]
fn form_title_supports_cursor_and_word_editing() {
    let (_tmp, mut app) = make_app();
    let _ = app.handle_key(press(KeyCode::Char('n'))).unwrap();
    assert!(matches!(app.mode, Mode::Form));

    for c in "hello world".chars() {
        let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
    }
    assert_eq!(app.form.as_ref().unwrap().title, "hello world");

    // Ctrl-Left lands at the start of the last word.
    let _ = app
        .handle_key(press_with_modifiers(
            KeyCode::Left,
            crossterm::event::KeyModifiers::CONTROL,
        ))
        .unwrap();
    let _ = app.handle_key(press(KeyCode::Char('X'))).unwrap();
    assert_eq!(app.form.as_ref().unwrap().title, "hello Xworld");

    // Ctrl-A goes to the start, typing there prepends.
    let _ = app
        .handle_key(press_with_modifiers(
            KeyCode::Char('a'),
            crossterm::event::KeyModifiers::CONTROL,
        ))
        .unwrap();
    let _ = app.handle_key(press(KeyCode::Char('>'))).unwrap();
    assert_eq!(app.form.as_ref().unwrap().title, ">hello Xworld");

    // Ctrl-E returns to the end; Backspace then removes the last character.
    let _ = app
        .handle_key(press_with_modifiers(
            KeyCode::Char('e'),
            crossterm::event::KeyModifiers::CONTROL,
        ))
        .unwrap();
    let _ = app.handle_key(press(KeyCode::Backspace)).unwrap();
    assert_eq!(app.form.as_ref().unwrap().title, ">hello Xworl");

    // Plain Right/Left step one character; Delete removes at the caret.
    let _ = app.handle_key(press(KeyCode::Left)).unwrap();
    let _ = app.handle_key(press(KeyCode::Delete)).unwrap();
    assert_eq!(app.form.as_ref().unwrap().title, ">hello Xwor");
}