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
#[allow(clippy::wildcard_imports)]
use super::super::*;
#[allow(clippy::wildcard_imports)]
use super::helpers::*;
use std::fs;

#[test]
fn handle_mouse_in_mode_without_mouse_support_is_noop() {
    let (_tmp, mut app) = make_app();
    app.mode = Mode::Filter {
        buffer: "query".into(),
    };
    let before = app.mode.clone();
    let ev = MouseEvent {
        kind: crossterm::event::MouseEventKind::Down(crossterm::event::MouseButton::Left),
        column: 1,
        row: 1,
        modifiers: crossterm::event::KeyModifiers::NONE,
    };
    let action = app.handle_mouse(ev).unwrap();
    assert_eq!(action, Action::None);
    assert_eq!(app.mode, before);
}

/// Dragging a blocked card into `done` is a **move**, so it asks before it
/// writes — the same prompt the `>` binding raises.
///
/// WP04 covered the keyboard path only. A drag-drop is a second door into
/// `move_selected_to`, and the review flagged that no test drove it.
#[test]
fn a_drag_of_a_blocked_card_into_done_asks_before_it_writes() {
    let (_tmp, mut app) = make_blocked_mouse_app();
    let (sx, sy) = card_cell(&app, 0, 1); // #2 Beta, blocked by #1
    let (tx, ty) = column_cell(&app, done_col(&app));

    app.handle_mouse(down_left(sx, sy)).unwrap();
    app.handle_mouse(drag_event(tx, ty)).unwrap();
    app.handle_mouse(up_left(tx, ty)).unwrap();

    assert!(
        matches!(app.mode, Mode::ConfirmBlockedMove { .. }),
        "a drag into done must raise the acknowledgement prompt, got {:?}",
        app.mode
    );
    // Cancelling leaves the card where it was.
    app.handle_key(press(KeyCode::Esc)).unwrap();
    assert!(
        app.board.column_cards(0).iter().any(|c| c.number == 2),
        "a cancelled drag must not move the card"
    );
}

/// The same drag, confirmed: the prompt **defers** to the commit prompt
/// rather than applying with no commit, so a confirmed blocked move is
/// recorded exactly like a clear one.
#[test]
fn a_confirmed_blocked_drag_into_done_records_its_commit() {
    let (_tmp, mut app) = make_blocked_mouse_app();
    let dir = app.store.config.issues_dir.clone();
    let (sx, sy) = card_cell(&app, 0, 1);
    let (tx, ty) = column_cell(&app, done_col(&app));

    app.handle_mouse(down_left(sx, sy)).unwrap();
    app.handle_mouse(drag_event(tx, ty)).unwrap();
    app.handle_mouse(up_left(tx, ty)).unwrap();
    app.handle_key(press(KeyCode::Char('y'))).unwrap();

    assert!(
        matches!(app.mode, Mode::MoveIntoDone { .. }),
        "confirming must still ask for the commit hash, got {:?}",
        app.mode
    );
    for c in "deadbee".chars() {
        app.handle_key(press(KeyCode::Char(c))).unwrap();
    }
    app.handle_key(press(KeyCode::Enter)).unwrap();

    let file = fs::read_to_string(dir.join("b.md")).unwrap();
    assert!(file.contains("commit: deadbee"), "commit recorded:\n{file}");
    assert!(
        file.contains("blocked_move_acknowledged"),
        "and the acknowledgement is in the same record:\n{file}"
    );
}

#[test]
fn mouse_click_selects_card() {
    let (_tmp, mut app) = make_mouse_app();
    let (x, y) = card_cell(&app, 0, 1);
    let action = app.handle_mouse(down_left(x, y)).unwrap();
    assert_eq!(action, Action::None);
    assert_eq!((app.board.active_col, app.board.selected), (0, 1));
    assert_eq!(
        app.board.get_selected_card().map(|c| c.number),
        Some(2),
        "second todo card selected"
    );
}

#[test]
fn mouse_click_on_empty_column_space_selects_column() {
    let (_tmp, mut app) = make_mouse_app();
    let (x, y) = column_cell(&app, 1);
    let action = app.handle_mouse(down_left(x, y)).unwrap();
    assert_eq!(action, Action::None);
    assert_eq!(app.board.active_col, 1);
}

#[test]
fn mouse_double_click_opens_detail() {
    let (_tmp, mut app) = make_mouse_app();
    let (x, y) = card_cell(&app, 0, 0);
    app.handle_mouse(down_left(x, y)).unwrap();
    app.handle_mouse(up_left(x, y)).unwrap();
    assert!(
        matches!(app.mode, Mode::ActionMenu { .. }),
        "the first click opens the action menu, got {:?}",
        app.mode
    );
    app.handle_mouse(down_left(x, y)).unwrap();
    assert!(matches!(app.mode, Mode::Detail));
    assert_eq!(
        app.detail.as_ref().map(|d| d.issue.number),
        Some(1),
        "double-clicked card detail is open"
    );
}

#[test]
fn mouse_drag_moves_issue_to_another_real_column() {
    let (_tmp, mut app) = make_mouse_app();
    let (sx, sy) = card_cell(&app, 0, 1);
    let target = column_cell(&app, 1);
    app.handle_mouse(down_left(sx, sy)).unwrap();
    app.handle_mouse(drag_event(target.0, target.1)).unwrap();
    let action = app.handle_mouse(up_left(target.0, target.1)).unwrap();
    assert_eq!(action, Action::None);
    assert_eq!(app.board.active_col, 1);
    assert!(
        app.board.columns[1].cards.iter().any(|c| c.number == 2),
        "Beta landed in 'in progress'"
    );
    assert!(
        app.board.columns[0].cards.iter().all(|c| c.number != 2),
        "Beta left 'todo'"
    );
    assert_eq!(app.status_msg.as_deref(), Some("Moved #2 to in progress"));
}

#[test]
fn shift_chords_reorder_the_selected_card_like_the_drag() {
    // #1586: `Shift+K`/`Shift+Up` and `Shift+J`/`Shift+Down` are a second
    // door into the same reorder the mouse drag and `Ctrl+Up`/`Ctrl+Down`
    // already take. Each chord gets a fresh fixture, so the assertion is
    // about the chord and not about the order the previous one left behind.
    let titles = |app: &App| {
        app.board
            .column_cards(0)
            .iter()
            .map(|c| c.title.clone())
            .collect::<Vec<_>>()
    };
    for code in [KeyCode::Up, KeyCode::Char('K')] {
        let (_tmp, mut app) = make_mouse_app();
        app.board.active_col = 0;
        app.board.selected = 1;
        app.handle_key(press_with_modifiers(
            code,
            crossterm::event::KeyModifiers::SHIFT,
        ))
        .unwrap();
        assert_eq!(
            titles(&app),
            ["Beta", "Alpha"],
            "{code:?} with Shift moves the selected card up"
        );
    }
    for code in [KeyCode::Down, KeyCode::Char('J')] {
        let (_tmp, mut app) = make_mouse_app();
        app.board.active_col = 0;
        app.board.selected = 0;
        app.handle_key(press_with_modifiers(
            code,
            crossterm::event::KeyModifiers::SHIFT,
        ))
        .unwrap();
        assert_eq!(
            titles(&app),
            ["Beta", "Alpha"],
            "{code:?} with Shift moves the selected card down"
        );
    }
}

#[test]
fn mouse_drop_on_source_column_is_noop() {
    let (_tmp, mut app) = make_mouse_app();
    let (sx, sy) = card_cell(&app, 0, 1);
    let target = column_cell(&app, 0);
    app.handle_mouse(down_left(sx, sy)).unwrap();
    app.handle_mouse(drag_event(target.0, target.1)).unwrap();
    app.handle_mouse(up_left(target.0, target.1)).unwrap();
    assert!(
        app.board.columns[0].cards.iter().any(|c| c.number == 2),
        "Beta stayed in 'todo'"
    );
}

#[test]
fn mouse_drag_reorders_cards_within_same_column_and_persists_order() {
    let (_tmp, mut app) = make_mouse_app();
    let (sx, sy) = card_cell(&app, 0, 1);
    let (tx, ty) = card_cell(&app, 0, 0);
    app.handle_mouse(down_left(sx, sy)).unwrap();
    app.handle_mouse(drag_event(tx, ty)).unwrap();
    let action = app.handle_mouse(up_left(tx, ty)).unwrap();
    assert_eq!(action, Action::None);
    assert_eq!(
        app.board
            .column_cards(0)
            .iter()
            .map(|c| c.title.as_str())
            .collect::<Vec<_>>(),
        ["Beta", "Alpha"],
        "dropping on another card in the same column reorders it"
    );
    let board_name = app.active_board_name().to_string();
    let board_cfg = app
        .store
        .config
        .kanban
        .configurations
        .get(&board_name)
        .unwrap();
    assert_eq!(
        board_cfg.card_order,
        vec!["b.md".to_string(), "a.md".to_string()],
        "same-column drag persists the custom order in config"
    );
}

#[test]
fn mouse_drag_persists_only_cards_that_differ_from_default_order() {
    let (_tmp, mut app) = make_sparse_order_mouse_app();
    let (sx, sy) = card_cell(&app, 0, 3);
    let (tx, ty) = card_cell(&app, 0, 1);
    app.handle_mouse(down_left(sx, sy)).unwrap();
    app.handle_mouse(drag_event(tx, ty)).unwrap();
    app.handle_mouse(up_left(tx, ty)).unwrap();
    assert_eq!(
        app.board
            .column_cards(0)
            .iter()
            .map(|c| c.title.as_str())
            .collect::<Vec<_>>(),
        ["Alpha", "Delta", "Beta", "Gamma"],
        "dragging the last card into the middle updates the visible order"
    );
    let board_name = app.active_board_name().to_string();
    let board_cfg = app
        .store
        .config
        .kanban
        .configurations
        .get(&board_name)
        .unwrap();
    assert_eq!(
        board_cfg.card_order,
        vec!["d.md".to_string(), "b.md".to_string(), "c.md".to_string()],
        "only the cards whose positions changed are persisted"
    );
}

#[test]
fn mouse_drop_on_no_column_area_is_noop() {
    let (_tmp, mut app) = make_mouse_app();
    let (sx, sy) = card_cell(&app, 0, 0);
    app.handle_mouse(down_left(sx, sy)).unwrap();
    let outside = (sx, 0u16); // the hover line, above the board area
    app.handle_mouse(drag_event(outside.0, outside.1)).unwrap();
    app.handle_mouse(up_left(outside.0, outside.1)).unwrap();
    assert!(
        app.board.columns[0].cards.iter().any(|c| c.number == 1),
        "Alpha stayed in 'todo'"
    );
    assert_eq!(app.status_msg, None);
}

#[test]
fn mouse_drop_on_mismatch_column_is_noop() {
    let (_tmp, mut app) = make_app();
    app.mouse.double_click_window = Duration::from_secs(60);
    let dir = app.store.config.issues_dir.clone();
    write_issue(&dir, "x.md", 1, "Alpha", "todo", None);
    write_issue(&dir, "w.md", 9, "Oddball", "weird", None);
    app.refresh_corpus().unwrap();
    app.board.show_mismatch = true;
    app.frame_size.set(Rect::new(0, 0, 80, 40));

    let (sx, sy) = card_cell(&app, 0, 0);
    let mismatch_col = app.board.columns.len();
    let target = column_cell(&app, mismatch_col);
    app.handle_mouse(down_left(sx, sy)).unwrap();
    app.handle_mouse(drag_event(target.0, target.1)).unwrap();
    app.handle_mouse(up_left(target.0, target.1)).unwrap();
    assert!(
        app.board.columns[0].cards.iter().any(|c| c.number == 1),
        "Alpha stayed in 'todo' (mismatched column is not a move target)"
    );
}

#[test]
fn mouse_drag_into_done_without_commit_opens_prompt() {
    let (_tmp, mut app) = make_mouse_app();
    let (sx, sy) = card_cell(&app, 0, 0);
    let target = column_cell(&app, 3); // "done"
    app.handle_mouse(down_left(sx, sy)).unwrap();
    app.handle_mouse(drag_event(target.0, target.1)).unwrap();
    app.handle_mouse(up_left(target.0, target.1)).unwrap();
    assert!(
        matches!(&app.mode, Mode::MoveIntoDone { buffer } if buffer.is_empty()),
        "drag into done without a commit opens the MoveIntoDone prompt, got {:?}",
        app.mode
    );
}

#[test]
fn mouse_hover_updates_without_selection_change() {
    let (_tmp, mut app) = make_mouse_app();
    let (x, y) = card_cell(&app, 0, 1);
    let before = (app.board.active_col, app.board.selected);
    let action = app.handle_mouse(moved_to(x, y)).unwrap();
    assert_eq!(action, Action::None);
    assert_eq!(app.board.hover, Some((0, 1)));
    assert_eq!((app.board.active_col, app.board.selected), before);
}

#[test]
fn mouse_hover_marks_an_empty_column_without_a_card() {
    let (_tmp, mut app) = make_app();
    let dir = app.store.config.issues_dir.clone();
    // Only `todo` has a card, so `in progress` (column 1) is empty.
    write_issue(&dir, "a.md", 1, "Alpha", "todo", None);
    app.refresh_corpus().unwrap();
    app.frame_size.set(Rect::new(0, 0, 80, 40));

    let (x, y) = column_cell(&app, 1);
    app.handle_mouse(moved_to(x, y)).unwrap();
    assert_eq!(app.board.hover, None, "no card under the pointer");
    assert_eq!(
        app.board.hover_col,
        Some(1),
        "an empty column is still marked as hovered"
    );

    // Leaving the column clears both.
    let (ox, oy) = column_cell(&app, 0);
    app.handle_mouse(moved_to(ox, oy)).unwrap();
    assert_eq!(app.board.hover_col, Some(0));

    // Off the board entirely clears it.
    app.handle_mouse(moved_to(0, 0)).unwrap();
    assert_eq!(app.board.hover_col, None);
}

#[test]
fn mouse_hover_cleared_on_up_or_pointer_leaving_board() {
    let (_tmp, mut app) = make_mouse_app();
    let (x, y) = card_cell(&app, 0, 0);
    app.handle_mouse(moved_to(x, y)).unwrap();
    assert_eq!(app.board.hover, Some((0, 0)));

    // Leaving the board clears the hover.
    app.handle_mouse(moved_to(x, 0)).unwrap();
    assert_eq!(app.board.hover, None);

    // Hovering a card again then releasing the button also clears it.
    app.handle_mouse(moved_to(x, y)).unwrap();
    assert!(app.board.hover.is_some());
    app.handle_mouse(up_left(x, y)).unwrap();
    assert_eq!(app.board.hover, None);
}

#[test]
fn mouse_scroll_moves_board_selection_within_active_column() {
    let (_tmp, mut app) = make_mouse_app();
    let (x, y) = card_cell(&app, 0, 1);
    app.handle_mouse(down_left(x, y)).unwrap();
    assert_eq!(app.board.selected, 1);

    let action = app.handle_mouse(scroll_up(x, y)).unwrap();
    assert_eq!(action, Action::None);
    assert_eq!(app.board.selected, 0);

    let action = app.handle_mouse(scroll_down(x, y)).unwrap();
    assert_eq!(action, Action::None);
    assert_eq!(app.board.selected, 1);
}

#[test]
fn tab_click_switches_board_via_recorded_region() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    let (_tmp, mut app) = make_multi_board_app();
    assert!(app.tabs.needs_bar());
    assert_eq!(app.active_config, 0);

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

    // The tab bar is the top TAB_HEIGHT rows of the drawn content (the mode
    // navigation takes the left column, so this is not the full frame width).
    let content = app.frame_size.get();
    let tabs_area = Rect::new(content.x, content.y, content.width, 2);
    let second = app.tabs.tab_rect(1, tabs_area).unwrap();
    let action = app.handle_mouse(down_left(second.x + 1, 1)).unwrap();
    assert_eq!(
        action,
        Action::SwitchBoard(1),
        "tab click resolves the region"
    );

    // Replay the run loop's consumption of the action (the loop arm calls
    // the same switch_board, so the transition is shared).
    if let Action::SwitchBoard(index) = action {
        app.switch_board(index);
    }
    assert_eq!(app.active_config, 1);
    assert_eq!(app.tabs.selected, 1);
    assert_eq!(app.status_msg.as_deref(), Some("Switched board"));
}

#[test]
fn tab_hover_updates_hover_state_without_switching_board() {
    let (_tmp, mut app) = make_multi_board_app();
    let tabs_area = Rect::new(0, 0, 80, 2);
    let second = app.tabs.tab_rect(1, tabs_area).unwrap();

    let action = app.handle_mouse(moved_to(second.x + 1, 0)).unwrap();
    assert_eq!(action, Action::None);
    assert_eq!(app.active_config, 0);
    assert_eq!(app.tabs.selected, 0);
    assert_eq!(app.tabs.hovered, Some(1));
    assert_eq!(app.board.hover, None);
}

#[test]
fn confirm_delete_yes_click_deletes_issue() {
    let (_tmp, mut app) = make_app();
    let dir = app.store.config.issues_dir.clone();
    write_issue(&dir, "a.md", 1, "Alpha", "todo", None);
    app.refresh_corpus().unwrap();
    app.frame_size.set(Rect::new(0, 0, 80, 24));
    app.confirm_delete_target = Some("a.md".into());
    app.mode = Mode::ConfirmDelete;

    let ((yes_x, yes_y), _) = drawn_confirm_buttons(&app);
    let action = app.handle_mouse(down_left(yes_x, yes_y)).unwrap();
    assert_eq!(action, Action::ConfirmDeleteYes);

    let _ = app.confirm_delete_yes();
    assert_eq!(app.mode, Mode::Board);
    assert_eq!(app.confirm_delete_target, None);
    assert_eq!(
        app.status_msg.as_deref(),
        Some("Deleted a.md"),
        "Yes click deletes the issue (same branch as the y key)"
    );
    assert!(
        !dir.join("a.md").exists(),
        "the target file is actually removed"
    );
}

#[test]
fn confirm_delete_no_click_cancels() {
    let (_tmp, mut app) = make_app();
    let dir = app.store.config.issues_dir.clone();
    write_issue(&dir, "a.md", 1, "Alpha", "todo", None);
    app.refresh_corpus().unwrap();
    app.frame_size.set(Rect::new(0, 0, 80, 24));
    app.confirm_delete_target = Some("a.md".into());
    app.mode = Mode::ConfirmDelete;

    let (_, (no_x, no_y)) = drawn_confirm_buttons(&app);
    let action = app.handle_mouse(down_left(no_x, no_y)).unwrap();
    assert_eq!(action, Action::ConfirmDeleteNo);

    let _ = app.confirm_delete_no();
    assert_eq!(app.mode, Mode::Board);
    assert_eq!(app.confirm_delete_target, None);
    assert!(
        dir.join("a.md").exists(),
        "No click cancels: the issue is left in place (same branch as Esc)"
    );
}

#[test]
fn detail_row_click_selects_and_toggles_the_row() {
    let (_tmp, mut app) = make_app();
    app.frame_size.set(Rect::new(0, 0, 80, 24));
    let board_name = app.active_board_name().to_string();
    let col = "in progress".to_string();
    open_dashboard_detail(&mut app);

    // Row 1 is the "in progress" column row (columns in order, then the
    // DISPLAY_ROWS display rows). Clicking it must select it and run the
    // row's own activation (the detail's Enter, or `Space`): hide the column.
    let (x, y) = drawn_detail_row(&app, 1);
    let action = app.handle_mouse(down_left(x, y)).unwrap();
    assert_eq!(action, Action::ConfigDetailRow(1), "detail row resolves");

    app.apply_config_click(action);
    let _ = app.settings_toggle_selected();
    assert_eq!(app.settings.as_ref().unwrap().detail_selected, 1);
    assert!(
        app.store
            .config
            .kanban
            .configurations
            .get(&board_name)
            .unwrap()
            .hidden_columns
            .contains(&col),
        "clicking the column row runs the Enter toggle (hides the column)"
    );
}

#[test]
fn detail_display_row_click_mirrors_entering_browse() {
    let (_tmp, mut app) = make_app();
    app.frame_size.set(Rect::new(0, 0, 80, 24));
    let board_name = app.active_board_name().to_string();
    open_dashboard_detail(&mut app);

    // The first display row sits right after the board's columns (index 4).
    let display_row = app.active_board_config().columns.len();
    let (x, y) = drawn_detail_row(&app, display_row);
    let action = app.handle_mouse(down_left(x, y)).unwrap();
    assert_eq!(action, Action::ConfigDetailRow(display_row));

    app.apply_config_click(action);
    let _ = app.settings_toggle_selected();
    assert_eq!(app.settings.as_ref().unwrap().detail_selected, display_row);
    assert_eq!(
        app.status_msg.as_deref(),
        Some("Wrap titles: off"),
        "display row click runs the same edit as Enter on that row"
    );
    assert!(
        !app.store
            .config
            .kanban
            .configurations
            .get(&board_name)
            .unwrap()
            .wrap_titles
    );
}

#[test]
fn right_click_empty_column_space_is_a_noop() {
    let (_tmp, mut app) = make_mouse_app();
    let (x, y) = column_cell(&app, 1);
    app.handle_mouse(down_right(x, y)).unwrap();
    assert!(
        !matches!(app.mode, Mode::ActionMenu { .. }),
        "no menu on empty column space"
    );
    assert!(app.action_menu.is_none());
}

#[test]
fn hovering_a_navigation_entry_highlights_it_without_switching() {
    let (_tmp, mut app) = make_app();
    // Draw first: the entry rects are recorded by the renderer.
    let _ = draw_board(&app, 80, 24);
    assert!(!app.is_hovered(&Action::SwitchMode(TopMode::Cycle)));

    // Row 4 is `F4 Cycles`: row 3 is the single-board limb (`default`).
    let action = app.handle_mouse(moved_to(0, 4)).unwrap();
    assert_eq!(action, Action::None, "hover never acts");
    assert!(
        app.is_hovered(&Action::SwitchMode(TopMode::Cycle)),
        "the F4 entry must read as hovered"
    );
    assert_eq!(app.top_mode, TopMode::Board, "hover never switches mode");

    // And the hover is drawn: the rail row carries the hover background.
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    let backend = TestBackend::new(80, 24);
    let mut terminal = Terminal::new(backend).unwrap();
    terminal
        .draw(|f| crate::app::render(&app, f, f.area()))
        .unwrap();
    let buffer = terminal.backend().buffer();
    assert_eq!(
        buffer[(0, 4)].style().bg,
        Some(ratatui::style::Color::DarkGray),
        "the hovered Cycles entry is highlighted in the rail"
    );
    // The active mode's row (Board, row 2) keeps its own colour.
    assert_eq!(
        buffer[(0, 2)].style().bg,
        Some(ratatui::style::Color::Cyan),
        "the active entry is not overwritten by the hover"
    );

    // Leaving the rail clears it.
    app.handle_mouse(moved_to(60, 20)).unwrap();
    assert!(!app.is_hovered(&Action::SwitchMode(TopMode::Cycle)));
}

#[test]
fn clicking_a_navigation_entry_switches_mode() {
    let (_tmp, mut app) = make_app();
    app.nav_display = NavDisplay::Default;
    // Draw first: the entry rects are recorded by the renderer.
    let _ = draw_board(&app, 80, 24);
    // Row 4 is `F4 Cycles` (row 3 is the board limb).
    let action = app.handle_mouse(down_left(0, 4)).unwrap();
    assert_eq!(action, Action::SwitchMode(TopMode::Cycle));
    app.switch_top_mode(TopMode::Cycle).unwrap();
    assert!(matches!(app.mode, Mode::Cycle));
    assert_eq!(app.top_mode, TopMode::Cycle);
}

/// The keyboard-focused rail row is highlighted exactly like a hovered one.
#[test]
fn the_keyboard_focused_rail_row_is_highlighted_like_hover() {
    let (_tmp, mut app) = make_branchy_app();
    app.nav_display = NavDisplay::Default;
    let _ = app.handle_key(press(KeyCode::Char('h'))).unwrap();
    // Board trunk -> Triage trunk, which is not the active mode, so the
    // background is the focus highlight rather than the active-mode cyan.
    let _ = app.handle_key(press(KeyCode::Up)).unwrap();
    assert_eq!(
        app.nav_focus_index().map(|i| app.nav_rows()[i].clone()),
        Some(Action::SwitchMode(TopMode::Triage))
    );
    let buffer = draw_buffer(&app, 80, 24);
    assert_eq!(buffer[(0, 3)].style().bg, Some(Color::DarkGray));
}