tix-tracker 0.12.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
#[allow(clippy::wildcard_imports)]
use super::super::*;
#[allow(clippy::wildcard_imports)]
use super::helpers::*;

#[test]
fn filter_modal_arrows_navigate_rows() {
    let (_tmp, mut app) = make_app();
    let _ = app.handle_key(press(KeyCode::Char('f'))).unwrap();
    assert!(matches!(app.mode, Mode::FilterModal));
    use crate::search::FilterField::*;
    assert_eq!(filter_modal_focus(&app), Assignee);
    let _ = app.handle_key(press(KeyCode::Down)).unwrap();
    assert_eq!(filter_modal_focus(&app), Label);
    let _ = app.handle_key(press(KeyCode::Up)).unwrap();
    assert_eq!(filter_modal_focus(&app), Assignee);
    // `FilterField::ALL` has **thirteen** rows since `Epic` was appended (WP06
    // T028), so a full lap is thirteen presses. Hard-coded rather than derived
    // from `ALL.len()` on purpose: a test that reads the length it is testing
    // cannot notice the length changing, which is exactly what happened here —
    // the loop was `0..9` and the wrap assertion then failed.
    for _ in 0..13 {
        let _ = app.handle_key(press(KeyCode::Down)).unwrap();
    }
    assert_eq!(filter_modal_focus(&app), Assignee, "Down wraps forward");
    let _ = app.handle_key(press(KeyCode::Up)).unwrap();
    assert_eq!(
        filter_modal_focus(&app),
        crate::search::FilterField::Epic,
        "Up wraps backward, to the last row — which is now `Epic` (WP08 \
             appended `Category` after `Parent`, WP06 T026 appended `Project` after \
             `Category`, and WP06 T028 appended `Epic` after `Project`)"
    );
}

#[test]
fn filter_modal_arrow_press_keeps_criterion_value() {
    let (_tmp, mut app) = make_app();
    let _ = app.handle_key(press(KeyCode::Char('f'))).unwrap();
    // Set a criterion through the picker: Down to Status, `enter` to open, `enter`
    // to choose the highlighted option.
    let _ = app.handle_key(press(KeyCode::Down)).unwrap();
    let _ = app.handle_key(press(KeyCode::Down)).unwrap();
    assert_eq!(filter_modal_focus(&app), FilterField::Status);
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    assert!(app.filter_picker.is_some(), "enter opens the row's picker");
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    let before = app
        .filter_modal
        .as_ref()
        .unwrap()
        .active_filter()
        .status
        .clone();
    assert!(before.is_some(), "choosing an option sets the criterion");
    let _ = app.handle_key(press(KeyCode::Down)).unwrap();
    let after = app
        .filter_modal
        .as_ref()
        .unwrap()
        .active_filter()
        .status
        .clone();
    assert_eq!(after, before, "arrow press must not alter the value");
    use crate::search::FilterField::*;
    assert_eq!(filter_modal_focus(&app), Priority);
}

#[test]
fn filter_modal_typing_does_not_enter_a_criterion() {
    let (_tmp, mut app) = make_app();
    let _ = app.handle_key(press(KeyCode::Char('f'))).unwrap();
    // Unbound letters are a no-op: they must not land in the criterion, nor
    // open anything.
    for c in "box".chars() {
        let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
    }
    let modal = app.filter_modal.as_ref().unwrap();
    assert_eq!(
        modal.active_filter().assignee,
        None,
        "rows are picker-only: typed characters must not become a criterion value"
    );
    assert_eq!(modal.focus(), FilterField::Assignee);
    assert!(
        app.filter_picker.is_none(),
        "an unbound letter opens nothing"
    );

    // `l` is the open-picker binding, so it opens the list; characters typed
    // afterwards filter that list rather than ever becoming a criterion value.
    let _ = app.handle_key(press(KeyCode::Char('l'))).unwrap();
    assert!(
        app.filter_picker.is_some(),
        "`l` opens the focused row's picker"
    );
    for c in "bo".chars() {
        let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
    }
    assert_eq!(
        app.filter_picker.as_ref().unwrap().state.filter,
        "bo",
        "characters typed while the list is open filter it"
    );
    assert_eq!(
        app.filter_modal.as_ref().unwrap().active_filter().assignee,
        None,
        "filtering the list still must not set the criterion"
    );
}

#[test]
fn filter_modal_date_field_opens_the_calendar() {
    let (_tmp, mut app) = make_app();
    let _ = app.handle_key(press(KeyCode::Char('f'))).unwrap();
    use crate::search::FilterField::*;
    for _ in 0..7 {
        let _ = app.handle_key(press(KeyCode::Down)).unwrap();
    }
    assert_eq!(filter_modal_focus(&app), StartDate);

    // `enter` on a date row opens the calendar rather than a value list.
    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::FilterModal));
    assert_eq!(
        app.filter_modal
            .as_ref()
            .unwrap()
            .active_filter()
            .start_date,
        None,
        "cancelling the calendar leaves the criterion unset"
    );

    // Choosing a day writes the criterion and closes the calendar.
    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::FilterModal));
    let chosen = app
        .filter_modal
        .as_ref()
        .unwrap()
        .active_filter()
        .start_date
        .clone();
    assert!(chosen.is_some(), "a chosen day sets the criterion");

    // Clearing the calendar unsets the criterion again.
    let _ = app.handle_key(press(KeyCode::F(2))).unwrap();
    let _ = app.handle_key(press(KeyCode::Delete)).unwrap();
    assert_eq!(
        app.filter_modal
            .as_ref()
            .unwrap()
            .active_filter()
            .start_date,
        None,
        "clearing the calendar unsets the criterion"
    );

    // `enter` is the opener now, so committing takes the apply key `a`.
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    assert!(matches!(app.mode, Mode::FilterModal));
    let picked = app
        .filter_modal
        .as_ref()
        .unwrap()
        .active_filter()
        .start_date
        .clone();
    assert!(picked.is_some(), "picking a day sets the criterion");
    let _ = app.handle_key(press(KeyCode::Char('a'))).unwrap();
    assert!(matches!(app.mode, Mode::Board));
    assert_eq!(app.structured.start_date, picked);
}

#[test]
fn filter_narrows_board_live() {
    let (tmp, mut app) = make_app();
    let dir = tmp.path().join("issues");
    write_issue(&dir, "2026-01-01-a.md", 1, "Target", "todo", None);
    write_issue(&dir, "2026-01-01-b.md", 2, "Other", "todo", None);
    let config = app.store.config.clone();
    app.board = BoardState::build(&app.store, config.default_config());
    app.refresh_corpus().unwrap();
    assert_eq!(app.board.column_cards(0).len(), 2);

    let _ = app.handle_key(press(KeyCode::Char('/'))).unwrap();
    assert!(matches!(app.mode, Mode::Filter { .. }));
    for c in "arget".chars() {
        let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
    }
    assert_eq!(app.filter, Some("arget".into()));
    assert_eq!(app.board.column_cards(0).len(), 1);
    assert_eq!(app.board.column_cards(0)[0].title, "Target");
    assert!(matches!(app.mode, Mode::Filter { .. }));
}

#[test]
fn filter_esc_clears_and_returns_to_board() {
    let (tmp, mut app) = make_app();
    let dir = tmp.path().join("issues");
    write_issue(&dir, "2026-01-01-a.md", 1, "Target", "todo", None);
    let config = app.store.config.clone();
    app.board = BoardState::build(&app.store, config.default_config());
    app.refresh_corpus().unwrap();
    let _ = app.handle_key(press(KeyCode::Char('/'))).unwrap();
    for c in "zzw".chars() {
        let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
    }
    assert_eq!(app.board.column_cards(0).len(), 0);
    let _ = app.handle_key(press(KeyCode::Esc)).unwrap();
    assert!(matches!(app.mode, Mode::Board));
    assert_eq!(app.filter, None);
    assert_eq!(app.board.column_cards(0).len(), 1);
}

#[test]
fn filter_enter_commits_and_stays_filtered() {
    let (tmp, mut app) = make_app();
    let dir = tmp.path().join("issues");
    write_issue(&dir, "2026-01-01-a.md", 1, "Target", "todo", None);
    write_issue(&dir, "2026-01-01-b.md", 2, "Other", "todo", None);
    let config = app.store.config.clone();
    app.board = BoardState::build(&app.store, config.default_config());
    app.refresh_corpus().unwrap();
    let _ = app.handle_key(press(KeyCode::Char('/'))).unwrap();
    for c in "arget".chars() {
        let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
    }
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    // A sole result opens its action menu immediately (tix #1744); the filter
    // itself was committed and stays applied.
    assert!(
        matches!(app.mode, Mode::ActionMenu { .. }),
        "Enter on a sole result opens the action menu, got {:?}",
        app.mode
    );
    assert_eq!(app.filter, Some("arget".into()));
    assert_eq!(app.board.column_cards(0).len(), 1);
}

#[test]
fn filter_enter_opens_the_sole_result_in_another_column() {
    let (tmp, mut app) = make_app();
    let dir = tmp.path().join("issues");
    // The single match lives in `done`, not in the entry column the cursor
    // starts on. `enter` must open *that* card without a navigation step.
    write_issue(&dir, "2026-01-01-a.md", 7, "Target", "done", None);
    let config = app.store.config.clone();
    app.board = BoardState::build(&app.store, config.default_config());
    app.refresh_corpus().unwrap();
    let _ = app.handle_key(press(KeyCode::Char('/'))).unwrap();
    for c in "7".chars() {
        let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
    }
    assert!(app.board.sole_visible_card().is_some());
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    assert!(
        matches!(app.mode, Mode::ActionMenu { .. }),
        "Enter opens the sole result's menu, got {:?}",
        app.mode
    );
    assert_eq!(
        app.board.get_selected_card().map(|card| card.number),
        Some(7),
        "the menu targets the sole displayed card"
    );
}

#[test]
fn filter_enter_with_several_results_stays_on_the_board() {
    let (tmp, mut app) = make_app();
    let dir = tmp.path().join("issues");
    write_issue(&dir, "2026-01-01-a.md", 1, "Target", "todo", None);
    write_issue(&dir, "2026-01-01-b.md", 2, "Targa", "todo", None);
    let config = app.store.config.clone();
    app.board = BoardState::build(&app.store, config.default_config());
    app.refresh_corpus().unwrap();
    let _ = app.handle_key(press(KeyCode::Char('/'))).unwrap();
    for c in "Tar".chars() {
        let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
    }
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    assert!(
        matches!(app.mode, Mode::Board),
        "a multi-result search returns to the board, got {:?}",
        app.mode
    );
}

#[test]
fn filter_zero_match_reports_status() {
    let (tmp, mut app) = make_app();
    let dir = tmp.path().join("issues");
    write_issue(&dir, "2026-01-01-a.md", 1, "Target", "todo", None);
    let config = app.store.config.clone();
    app.board = BoardState::build(&app.store, config.default_config());
    app.refresh_corpus().unwrap();
    let _ = app.handle_key(press(KeyCode::Char('/'))).unwrap();
    for c in "zzw".chars() {
        let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
    }
    assert_eq!(app.board.column_cards(0).len(), 0);
    assert!(
        app.status_msg
            .as_deref()
            .is_some_and(|m| m.contains("No matches")),
        "got {:?}",
        app.status_msg
    );
}

#[test]
fn filter_zero_match_number_reports_status() {
    let (tmp, mut app) = make_app();
    let dir = tmp.path().join("issues");
    write_issue(&dir, "2026-01-01-a.md", 7, "Target", "todo", None);
    // Issue 7's title contains the digit sequence "999"? No: a miss still
    // must surface the no-matches status because the bare number gate
    // bypasses fuzzy text matching.
    let config = app.store.config.clone();
    app.board = BoardState::build(&app.store, config.default_config());
    app.refresh_corpus().unwrap();
    let _ = app.handle_key(press(KeyCode::Char('/'))).unwrap();
    for c in "999".chars() {
        let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
    }
    assert_eq!(app.filter, Some("999".into()));
    assert_eq!(app.board.column_cards(0).len(), 0);
    assert!(
        app.status_msg
            .as_deref()
            .is_some_and(|m| m.contains("No matches")),
        "got {:?}",
        app.status_msg
    );
}

#[test]
fn render_board_unfiltered_has_no_filter_row() {
    let (_tmp, app) = make_app();
    let cells = draw_board(&app, 80, 24);
    assert!(
        !cells.iter().any(|s| s.contains("Search:")),
        "no filter row expected"
    );
}

#[test]
fn render_board_filtered_shows_filter_row_beneath_tabs() {
    let (_tmp, mut app) = make_app();
    app.filter = Some("55".into());
    app.board = BoardState::build(&app.store, app.store.config.default_config());
    let cells = draw_board(&app, 80, 24);
    // Single board: no tab bar. Row 0 = hovered-issue title line; row 1 =
    // the filter indicator.
    assert!(
        flush_line(&cells, 80, 1).contains("Search: #55"),
        "row 1 should read the filter, got {}",
        flush_line(&cells, 80, 1)
    );
}

#[test]
fn render_board_structured_filter_shows_row() {
    let (_tmp, mut app) = make_app();
    app.filter = None;
    app.structured = search::StructuredFilter {
        status: Some("todo".into()),
        ..Default::default()
    };
    app.board = BoardState::build(&app.store, app.store.config.default_config());
    let cells = draw_board(&app, 80, 24);
    assert!(
        flush_line(&cells, 80, 1).contains("Search:"),
        "got {}",
        flush_line(&cells, 80, 1)
    );
}

#[test]
fn render_board_filter_input_draws_row_and_prompt() {
    let (_tmp, mut app) = make_app();
    app.mode = Mode::Filter {
        buffer: "55".to_string(),
    };
    app.filter = Some("55".into());
    app.board = BoardState::build(&app.store, app.store.config.default_config());
    let cells = draw_board(&app, 80, 24);
    assert!(
        flush_line(&cells, 80, 1).contains("Search: #55"),
        "row 1 should read the filter while typing"
    );
    assert!(
        flush_line(&cells, 80, 23).contains("Search: 55"),
        "bottom row should hold the input prompt"
    );
}

#[test]
fn render_board_tiny_terminal_filtered_does_not_panic() {
    let (_tmp, mut app) = make_app();
    app.filter = Some("55".into());
    app.board = BoardState::build(&app.store, app.store.config.default_config());
    app.mode = Mode::Filter {
        buffer: "55".to_string(),
    };
    for h in [2_u16, 3, 4, 5] {
        let _ = draw_board(&app, 80, h);
    }
}

#[test]
fn filter_esc_clears_row_and_resolves_to_board() {
    let (tmp, mut app) = make_app();
    let dir = tmp.path().join("issues");
    write_issue(&dir, "2026-01-01-a.md", 55, "Target", "todo", None);
    let config = app.store.config.clone();
    app.board = BoardState::build(&app.store, config.default_config());
    app.refresh_corpus().unwrap();
    let _ = app.handle_key(press(KeyCode::Char('/'))).unwrap();
    for c in "55".chars() {
        let _ = app.handle_key(press(KeyCode::Char(c))).unwrap();
    }
    let cells = draw_board(&app, 80, 24);
    assert!(
        cells.concat().contains("Search:"),
        "filtered board must show the row; filter={:?} cells_row1={}",
        app.filter,
        flush_line(&cells, 80, 1)
    );
    let _ = app.handle_key(press(KeyCode::Esc)).unwrap();
    assert!(matches!(app.mode, Mode::Board));
    assert_eq!(app.filter, None);
    let after = draw_board(&app, 80, 24).concat();
    assert!(
        !after.contains("Search:"),
        "esc cleared the filter, row must disappear, got {after:?}"
    );
}

#[test]
fn filter_modal_row_click_focuses_and_opens_the_picker() {
    let (_tmp, mut app) = make_app();
    app.frame_size.set(Rect::new(0, 0, 80, 24));
    app.filter_modal = Some(crate::ui::filter_modal::FilterModal::new());
    app.mode = Mode::FilterModal;

    let backend = ratatui::backend::TestBackend::new(80, 24);
    let mut terminal = ratatui::Terminal::new(backend).unwrap();
    terminal
        .draw(|f| crate::app::render(&app, f, f.area()))
        .unwrap();

    // Rows mirror FilterField::ALL in order; click the Status row (index 2).
    // The modal is centered in the drawn content, which excludes the mode
    // navigation column.
    let rect = crate::ui::modal::modal_rect(app.frame_size.get());
    let status_row = rect.y + 1 + 2;
    let status_field = FilterField::ALL[2];
    assert_eq!(status_field, FilterField::Status);

    assert_ne!(
        app.filter_modal.as_ref().unwrap().focus(),
        FilterField::Status
    );
    let action = app.handle_mouse(down_left(rect.x + 2, status_row)).unwrap();
    assert_eq!(
        action,
        Action::FilterModalFocus(FilterField::Status),
        "row click resolves the criterion region"
    );
    app.apply_filter_modal_action(action).unwrap();
    assert_eq!(
        app.filter_modal.as_ref().unwrap().focus(),
        FilterField::Status,
        "clicked row becomes the focused criterion"
    );
    assert!(
        app.filter_picker
            .as_ref()
            .is_some_and(|picker| picker.field == FilterField::Status),
        "a row click opens that row's picker, exactly as enter would"
    );
}

#[test]
fn filter_picker_offers_the_current_value() {
    let (_tmp, mut app) = make_app();
    // A value nothing else produces must still be selectable, or a filter set
    // before the value disappeared could never be changed.
    app.filter_modal = Some(crate::ui::filter_modal::FilterModal::with_filter(
        search::StructuredFilter {
            label: Some("ghost".into()),
            ..Default::default()
        },
    ));
    app.mode = Mode::FilterModal;
    focus_filter_field(&mut app, FilterField::Label);
    assert!(
        app.filter_picker_options(FilterField::Label)
            .iter()
            .any(|option| option.id == "ghost"),
        "the current value must always be offered"
    );
}

#[test]
fn filter_picker_seeds_on_the_current_value() {
    let (_tmp, mut app) = make_app();
    let target = app.active_board_config().columns[2].clone();
    app.structured.status = Some(target.clone());
    app.filter_modal = Some(crate::ui::filter_modal::FilterModal::with_filter(
        app.structured.clone(),
    ));
    app.mode = Mode::FilterModal;
    focus_filter_field(&mut app, FilterField::Status);
    app.open_filter_picker();
    let picker = app.filter_picker.as_ref().expect("picker open");
    let highlighted = picker.state.visible_indices()[picker.state.cursor];
    assert_eq!(
        picker.state.options[highlighted].id, target,
        "the list starts on the criterion's current value"
    );
}

#[test]
fn filter_picker_refuses_inline_create() {
    let (_tmp, mut app) = make_app();
    let _ = app.handle_key(press(KeyCode::Char('f'))).unwrap();
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    assert!(app.filter_picker.is_some(), "enter opens the picker");
    let _ = app.handle_key(press(KeyCode::Char('+'))).unwrap();
    assert_ne!(
        app.filter_picker.as_ref().unwrap().state.mode,
        PickerMode::InlineCreate,
        "a filter lists values that exist; it must not offer to invent one"
    );
}

#[test]
fn filter_picker_applies_ad_hoc_and_esc_keeps_it() {
    let (_tmp, mut app) = make_app();
    let columns = app.active_board_config().columns.clone();
    let _ = app.handle_key(press(KeyCode::Char('f'))).unwrap();
    let _ = app.handle_key(press(KeyCode::Down)).unwrap();
    let _ = app.handle_key(press(KeyCode::Down)).unwrap();
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    assert_eq!(
        app.filter_modal
            .as_ref()
            .unwrap()
            .active_filter()
            .status
            .as_deref(),
        Some(columns[0].as_str()),
        "the highlighted option becomes the criterion"
    );
    // `esc` applies the live criteria and closes — there is no discard path.
    let _ = app.handle_key(press(KeyCode::Esc)).unwrap();
    assert!(matches!(app.mode, Mode::Board));
    assert_eq!(
        app.structured.status.as_deref(),
        Some(columns[0].as_str()),
        "closing the modal keeps the choice"
    );
}

#[test]
fn filter_reset_then_esc_persists_the_clear() {
    let (_tmp, mut app) = make_app();
    let columns = app.active_board_config().columns.clone();
    // Start from a committed criterion, then `f` -> `r` -> `esc`.
    app.structured.status = Some(columns[0].clone());
    let _ = app.handle_key(press(KeyCode::Char('f'))).unwrap();
    let _ = app.handle_key(press(KeyCode::Char('r'))).unwrap();
    let _ = app.handle_key(press(KeyCode::Esc)).unwrap();
    assert!(matches!(app.mode, Mode::Board));
    assert_eq!(
        app.structured,
        search::StructuredFilter::default(),
        "reset followed by close must persist the cleared filter"
    );
}

#[test]
fn filter_picker_esc_closes_the_list_and_the_apply_key_commits() {
    let (_tmp, mut app) = make_app();
    let columns = app.active_board_config().columns.clone();
    let _ = app.handle_key(press(KeyCode::Char('f'))).unwrap();
    let _ = app.handle_key(press(KeyCode::Down)).unwrap();
    let _ = app.handle_key(press(KeyCode::Down)).unwrap();
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    // Plain `enter` opens the picker again rather than committing.
    let _ = app.handle_key(press(KeyCode::Enter)).unwrap();
    assert!(app.filter_picker.is_some());
    // `esc` closes only the list; the criterion keeps its value.
    let _ = app.handle_key(press(KeyCode::Esc)).unwrap();
    assert!(app.filter_picker.is_none());
    assert_eq!(
        app.filter_modal
            .as_ref()
            .unwrap()
            .active_filter()
            .status
            .as_deref(),
        Some(columns[0].as_str()),
        "closing the list keeps the criterion"
    );
    // The apply key commits and closes the modal.
    let _ = app.handle_key(press(KeyCode::Char('a'))).unwrap();
    assert!(matches!(app.mode, Mode::Board));
    assert_eq!(app.structured.status.as_deref(), Some(columns[0].as_str()));
}

#[test]
fn filter_picker_mouse_selects_and_outside_closes() {
    let (_tmp, mut app) = make_app();
    app.frame_size.set(Rect::new(0, 0, 80, 24));
    app.filter_modal = Some(crate::ui::filter_modal::FilterModal::new());
    app.mode = Mode::FilterModal;
    focus_filter_field(&mut app, FilterField::Status);
    app.open_filter_picker();

    let backend = ratatui::backend::TestBackend::new(80, 24);
    let mut terminal = ratatui::Terminal::new(backend).unwrap();
    terminal
        .draw(|f| crate::app::render(&app, f, f.area()))
        .unwrap();

    let columns = app.active_board_config().columns.clone();
    let modal = crate::ui::modal::modal_rect(app.frame_size.get());
    let (_, rows) =
        crate::ui::filter_modal::picker_layout(modal, &app.filter_picker.as_ref().unwrap().state)
            .expect("the picker lays out inside the modal");

    // Clicking the first option chooses it.
    let first = rows[0];
    let action = app.handle_mouse(down_left(first.x + 1, first.y)).unwrap();
    assert_eq!(action, Action::FilterPickerOption(0));
    app.apply_filter_modal_action(action).unwrap();
    assert!(app.filter_picker.is_none(), "choosing closes the list");
    assert_eq!(
        app.filter_modal
            .as_ref()
            .unwrap()
            .active_filter()
            .status
            .as_deref(),
        Some(columns[0].as_str()),
        "the clicked option becomes the criterion"
    );

    // A click outside the list closes it and changes nothing.
    app.open_filter_picker();
    let outside = down_left(modal.x + modal.width - 2, modal.y + 1);
    let action = app.handle_mouse(outside).unwrap();
    assert_eq!(action, Action::FilterPickerClose);
    app.apply_filter_modal_action(action).unwrap();
    assert!(
        app.filter_picker.is_none(),
        "an outside click closes the list"
    );
    assert_eq!(
        app.filter_modal
            .as_ref()
            .unwrap()
            .active_filter()
            .status
            .as_deref(),
        Some(columns[0].as_str()),
        "an outside click leaves the criterion alone"
    );
}

#[test]
fn filter_picker_renders_without_panicking_on_a_tiny_terminal() {
    let (_tmp, mut app) = make_app();
    app.filter_modal = Some(crate::ui::filter_modal::FilterModal::new());
    app.mode = Mode::FilterModal;
    app.open_filter_picker();
    for (w, h) in [(80, 24), (20, 5)] {
        app.frame_size.set(Rect::new(0, 0, w, h));
        let backend = ratatui::backend::TestBackend::new(w, h);
        let mut terminal = ratatui::Terminal::new(backend).unwrap();
        terminal
            .draw(|f| crate::app::render(&app, f, f.area()))
            .unwrap();
    }
}