gitkraft-tui 0.9.0

GitKraft — Git IDE terminal application (Ratatui TUI)
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
use crossterm::event::{KeyCode, KeyEvent};

use crate::app::App;

/// True when the commit-action popup is open.
pub fn popup_is_open(app: &App) -> bool {
    app.tab().pending_commit_action_oid.is_some()
}

/// Get the OID of the commit at `idx` from the active commit list (search results or all commits).
fn get_commit_oid_at(app: &App, idx: usize) -> Option<String> {
    let commits = if app.tab().search_active && !app.tab().search_results.is_empty() {
        &app.tab().search_results
    } else {
        &app.tab().commits
    };
    commits.get(idx).map(|c| c.oid.clone())
}

/// Trigger a background diff load for the commit at `idx`.
fn load_diff_at(app: &mut App, idx: usize) {
    let oid = get_commit_oid_at(app, idx);
    if let Some(oid) = oid {
        app.tab_mut().selected_commit_oid = Some(oid);
        app.load_commit_diff_by_oid();
    }
}

/// Handle keys when the CommitLog pane is active.
pub fn handle_key(app: &mut App, key: KeyEvent) {
    // If the action popup is open, route keys there instead
    if popup_is_open(app) {
        handle_popup_key(app, key);
        return;
    }
    match key.code {
        KeyCode::Char('j') => {
            navigate_down(app);
        }
        KeyCode::Char('k') => {
            navigate_up(app);
        }
        // Range selection — uppercase J/K work in all terminals since Shift+letter
        // produces uppercase regardless of the terminal's modifier-key support.
        KeyCode::Char('J') => {
            select_commit_down(app);
        }
        KeyCode::Char('K') => {
            select_commit_up(app);
        }
        KeyCode::Enter => {
            // Exit blame and load the diff for the selected commit.
            app.tab_mut().blame_path = None;
            app.tab_mut().blame_lines.clear();
            app.tab_mut().blame_scroll = 0;
            if let Some(idx) = app.tab().commit_list_state.selected() {
                let commits = if app.tab().search_active && !app.tab().search_results.is_empty() {
                    &app.tab().search_results
                } else {
                    &app.tab().commits
                };
                if idx < commits.len() {
                    let oid = commits[idx].oid.clone();
                    app.tab_mut().selected_commit_oid = Some(oid);
                    app.load_commit_diff_by_oid();
                }
            }
        }
        KeyCode::Char('g') => {
            let len = active_commits_len(app);
            if len > 0 {
                // Jump to first commit
                app.tab_mut().commit_list_state.select(Some(0));
            }
        }
        KeyCode::Char('G') => {
            let len = active_commits_len(app);
            if len > 0 {
                // Jump to last commit
                app.tab_mut().commit_list_state.select(Some(len - 1));
            }
        }
        // Toggle current commit in/out of multi-selection, then advance
        KeyCode::Char(' ') => {
            if let Some(idx) = app.tab().commit_list_state.selected() {
                let commits_len = active_commits_len(app);
                let tab = app.tab_mut();
                if let Some(pos) = tab.selected_commits.iter().position(|&i| i == idx) {
                    tab.selected_commits.remove(pos);
                } else {
                    tab.selected_commits.push(idx);
                }
                let count = tab.selected_commits.len();
                tab.status_message = if count > 0 {
                    Some(format!("{count} commit(s) selected"))
                } else {
                    None
                };
                // Auto-advance to next commit (like Space does in staging)
                if idx + 1 < commits_len {
                    tab.commit_list_state.select(Some(idx + 1));
                }
            }
        }

        // Cherry-pick this commit (or all selected commits if multi-selection is active).
        // C is the natural uppercase alias since Shift+c → C in every terminal.
        KeyCode::Char('C') => {
            app.cherry_pick_selected();
        }

        // Reset to this commit — mixed mode (keeps working-tree changes, unstages everything).
        KeyCode::Char('n') => {
            app.reset_to_selected_commit("mixed");
        }

        // Revert selected commit
        KeyCode::Char('e') => {
            app.revert_selected_commit();
        }

        // Reset soft to selected commit
        KeyCode::Char('x') => {
            app.reset_to_selected_commit("soft");
        }

        // Reset hard to selected commit
        KeyCode::Char('X') => {
            app.reset_to_selected_commit("hard");
        }

        // Force push current branch
        KeyCode::Char('F') => {
            app.force_push_branch();
        }

        KeyCode::Esc => {
            if app.tab().search_active {
                app.tab_mut().search_active = false;
                app.tab_mut().search_results.clear();
                app.tab_mut().search_query.clear();
                app.tab_mut().status_message = Some("Search cleared".into());
            } else {
                app.tab_mut().commit_list_state.select(None);
            }
        }
        _ => {}
    }
}

/// Handle keys when the commit-action popup is open.
pub fn handle_popup_key(app: &mut App, key: KeyEvent) {
    use crate::app::{InputMode, InputPurpose};

    match key.code {
        // Navigate down in the popup
        KeyCode::Char('j') | KeyCode::Down => {
            let len = app.tab().commit_action_items.len();
            if len > 0 {
                let cur = app.tab().commit_action_cursor;
                app.tab_mut().commit_action_cursor = (cur + 1).min(len - 1);
            }
        }
        // Navigate up in the popup
        KeyCode::Char('k') | KeyCode::Up => {
            let cur = app.tab().commit_action_cursor;
            app.tab_mut().commit_action_cursor = cur.saturating_sub(1);
        }
        // Confirm selection
        KeyCode::Enter | KeyCode::Char(' ') => {
            let cursor = app.tab().commit_action_cursor;
            let kind = match app.tab().commit_action_items.get(cursor).copied() {
                Some(k) => k,
                None => return,
            };
            if kind.needs_input() {
                // Park the kind and ask for the first input
                app.tab_mut().pending_action_kind = Some(kind);
                app.tab_mut().action_input1.clear();
                app.input_buffer.clear();
                app.input_mode = InputMode::Input;
                app.input_purpose = InputPurpose::CommitActionInput1;
                let prompt = kind.input_prompt().unwrap_or("Input:");
                app.tab_mut().status_message = Some(prompt.to_string());
                // Close the popup list (keep pending_commit_action_oid so
                // execute_commit_action can find the OID)
                app.tab_mut().commit_action_items.clear();
            } else {
                // No input needed — execute directly
                let action = kind.into_action(String::new(), String::new());
                app.execute_commit_action(action);
            }
        }
        // Cancel
        KeyCode::Esc | KeyCode::Char('q') => {
            let tab = app.tab_mut();
            tab.pending_commit_action_oid = None;
            tab.commit_action_items.clear();
            tab.commit_action_cursor = 0;
            tab.status_message = Some("Action cancelled".into());
        }
        _ => {}
    }
}

/// Return the length of the currently visible commit list (search results or all commits).
fn active_commits_len(app: &App) -> usize {
    if app.tab().search_active && !app.tab().search_results.is_empty() {
        app.tab().search_results.len()
    } else {
        app.tab().commits.len()
    }
}

/// Shared implementation for commit-cursor movement.
/// `next_index` is a closure that, given the current index and list length,
/// returns the new index to move to.
fn navigate_to(app: &mut App, next_index: impl Fn(usize, usize) -> usize) {
    let len = active_commits_len(app);
    if len == 0 {
        return;
    }
    let current = app.tab().commit_list_state.selected().unwrap_or(0);
    let i = next_index(current, len);
    app.tab_mut().commit_list_state.select(Some(i));
    app.tab_mut().anchor_commit_index = Some(i);
    app.tab_mut().selected_commits.clear();
    app.tab_mut().commit_range_diffs.clear();
    // Exit blame when navigating to a different commit.
    app.tab_mut().blame_path = None;
    app.tab_mut().blame_lines.clear();
    app.tab_mut().blame_scroll = 0;
    load_diff_at(app, i);
}

/// Move commit selection down by one and auto-load the diff for the new selection.
pub fn navigate_down(app: &mut App) {
    navigate_to(app, |i, len| if i >= len - 1 { i } else { i + 1 });
}

/// Move commit selection up by one and auto-load the diff for the new selection.
pub fn navigate_up(app: &mut App) {
    navigate_to(app, |i, _| if i == 0 { 0 } else { i - 1 });
}

/// Shared implementation for extending the commit range selection.
/// `next_idx_fn` computes the new cursor index given `(current, len)`.
/// Returns early if the cursor is already at the boundary.
fn extend_commit_selection(app: &mut App, next_idx_fn: impl Fn(usize, usize) -> Option<usize>) {
    let len = active_commits_len(app);
    if len == 0 {
        return;
    }
    let current = app.tab().commit_list_state.selected().unwrap_or(0);
    let new_idx = match next_idx_fn(current, len) {
        Some(i) => i,
        None => return,
    };
    let anchor = app
        .tab()
        .anchor_commit_index
        .or(app.tab().commit_list_state.selected())
        .unwrap_or(new_idx);

    let range = gitkraft_core::ascending_range(anchor, new_idx);
    app.tab_mut().commit_list_state.select(Some(new_idx));
    app.tab_mut().selected_commits = range;
    let count = app.tab().selected_commits.len();
    app.tab_mut().status_message = Some(format!("{count} commits selected"));
    app.load_commit_range_diff();
}

/// Extend the commit selection range downward (Shift+Down).
pub fn select_commit_down(app: &mut App) {
    extend_commit_selection(
        app,
        |cur, len| {
            if cur + 1 >= len {
                None
            } else {
                Some(cur + 1)
            }
        },
    );
}

/// Extend the commit selection range upward (Shift+Up).
pub fn select_commit_up(app: &mut App) {
    extend_commit_selection(app, |cur, _| if cur == 0 { None } else { Some(cur - 1) });
}

#[cfg(test)]
mod tests {
    use super::*;
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    fn key(code: KeyCode) -> KeyEvent {
        KeyEvent::new(code, KeyModifiers::NONE)
    }

    fn make_commits(count: usize) -> Vec<gitkraft_core::CommitInfo> {
        (0..count)
            .map(|i| gitkraft_core::CommitInfo {
                oid: format!("{i:040x}"),
                short_oid: format!("{i:07x}"),
                summary: format!("commit {i}"),
                message: format!("commit {i}"),
                author_name: "Test".into(),
                author_email: "test@test.com".into(),
                time: Default::default(),
                parent_ids: Vec::new(),
            })
            .collect()
    }

    #[test]
    fn space_toggles_commit_into_selection() {
        let mut app = App::new();
        app.tab_mut().commits = make_commits(5);
        app.tab_mut().commit_list_state.select(Some(2));

        handle_key(&mut app, key(KeyCode::Char(' ')));

        assert!(app.tab().selected_commits.contains(&2));
    }

    #[test]
    fn space_deselects_already_selected_commit() {
        let mut app = App::new();
        app.tab_mut().commits = make_commits(5);
        app.tab_mut().commit_list_state.select(Some(2));
        app.tab_mut().selected_commits = vec![2];

        handle_key(&mut app, key(KeyCode::Char(' ')));

        assert!(!app.tab().selected_commits.contains(&2));
    }

    #[test]
    fn space_advances_cursor() {
        let mut app = App::new();
        app.tab_mut().commits = make_commits(5);
        app.tab_mut().commit_list_state.select(Some(1));

        handle_key(&mut app, key(KeyCode::Char(' ')));

        assert_eq!(app.tab().commit_list_state.selected(), Some(2));
    }

    #[test]
    fn navigate_down_clears_blame_overlay() {
        let mut app = App::new();
        app.tab_mut().commits = make_commits(3);
        app.tab_mut().commit_list_state.select(Some(0));
        app.tab_mut().blame_path = Some("src/lib.rs".to_string());
        app.tab_mut().blame_lines = vec![]; // populated in real usage

        navigate_down(&mut app);

        assert!(
            app.tab().blame_path.is_none(),
            "navigate_down must clear the blame overlay"
        );
    }

    #[test]
    fn navigate_up_clears_blame_overlay() {
        let mut app = App::new();
        app.tab_mut().commits = make_commits(3);
        app.tab_mut().commit_list_state.select(Some(2));
        app.tab_mut().blame_path = Some("src/main.rs".to_string());

        navigate_up(&mut app);

        assert!(
            app.tab().blame_path.is_none(),
            "navigate_up must clear the blame overlay"
        );
    }

    #[test]
    fn enter_clears_blame_overlay() {
        let mut app = App::new();
        app.tab_mut().commits = make_commits(3);
        app.tab_mut().commit_list_state.select(Some(1));
        app.tab_mut().blame_path = Some("src/main.rs".to_string());

        handle_key(&mut app, key(KeyCode::Enter));

        assert!(
            app.tab().blame_path.is_none(),
            "Enter in commit log must clear the blame overlay"
        );
    }

    #[test]
    fn space_does_not_advance_past_last_commit() {
        let mut app = App::new();
        app.tab_mut().commits = make_commits(3);
        app.tab_mut().commit_list_state.select(Some(2));

        handle_key(&mut app, key(KeyCode::Char(' ')));

        assert_eq!(app.tab().commit_list_state.selected(), Some(2));
    }

    #[test]
    fn space_sets_status_message_with_count() {
        let mut app = App::new();
        app.tab_mut().commits = make_commits(5);
        app.tab_mut().commit_list_state.select(Some(0));

        handle_key(&mut app, key(KeyCode::Char(' ')));
        assert_eq!(
            app.tab().status_message.as_deref(),
            Some("1 commit(s) selected")
        );

        app.tab_mut().commit_list_state.select(Some(1));
        handle_key(&mut app, key(KeyCode::Char(' ')));
        assert_eq!(
            app.tab().status_message.as_deref(),
            Some("2 commit(s) selected")
        );
    }

    #[test]
    fn space_clears_status_when_all_deselected() {
        let mut app = App::new();
        app.tab_mut().commits = make_commits(5);
        app.tab_mut().commit_list_state.select(Some(0));
        app.tab_mut().selected_commits = vec![0];

        handle_key(&mut app, key(KeyCode::Char(' ')));

        assert!(app.tab().status_message.is_none());
    }

    #[test]
    fn space_allows_non_contiguous_selection() {
        let mut app = App::new();
        app.tab_mut().commits = make_commits(5);

        app.tab_mut().commit_list_state.select(Some(0));
        handle_key(&mut app, key(KeyCode::Char(' ')));
        app.tab_mut().commit_list_state.select(Some(2));
        handle_key(&mut app, key(KeyCode::Char(' ')));
        app.tab_mut().commit_list_state.select(Some(4));
        handle_key(&mut app, key(KeyCode::Char(' ')));

        assert_eq!(app.tab().selected_commits, vec![0, 2, 4]);
    }

    use crate::app::App;

    fn make_commits_simple(count: usize) -> Vec<gitkraft_core::CommitInfo> {
        (0..count)
            .map(|_| gitkraft_core::CommitInfo {
                oid: String::new(),
                short_oid: String::new(),
                summary: String::new(),
                message: String::new(),
                author_name: String::new(),
                author_email: String::new(),
                time: Default::default(),
                parent_ids: Vec::new(),
            })
            .collect()
    }

    #[test]
    fn select_commit_down_creates_range() {
        let mut app = App::new();
        app.tab_mut().commits = make_commits_simple(5);
        app.tab_mut().commit_list_state.select(Some(1));
        app.tab_mut().anchor_commit_index = Some(1);

        select_commit_down(&mut app);

        assert_eq!(app.tab().selected_commits, vec![1, 2]);
        assert_eq!(app.tab().commit_list_state.selected(), Some(2));
    }

    #[test]
    fn select_commit_up_creates_range() {
        let mut app = App::new();
        app.tab_mut().commits = make_commits_simple(5);
        app.tab_mut().commit_list_state.select(Some(3));
        app.tab_mut().anchor_commit_index = Some(3);

        select_commit_up(&mut app);

        assert_eq!(app.tab().selected_commits, vec![2, 3]);
        assert_eq!(app.tab().commit_list_state.selected(), Some(2));
    }

    #[test]
    fn select_commit_down_stops_at_last() {
        let mut app = App::new();
        app.tab_mut().commits = make_commits_simple(3);
        app.tab_mut().commit_list_state.select(Some(2));
        app.tab_mut().anchor_commit_index = Some(2);

        select_commit_down(&mut app); // already at last

        assert_eq!(app.tab().commit_list_state.selected(), Some(2));
        assert!(app.tab().selected_commits.is_empty());
    }

    #[test]
    fn navigate_down_clears_selected_commits() {
        let mut app = App::new();
        app.tab_mut().commits = make_commits(3);
        app.tab_mut().selected_commits = vec![0, 1];
        app.tab_mut().commit_list_state.select(Some(0));

        navigate_down(&mut app);

        assert!(app.tab().selected_commits.is_empty());
    }

    // ── Popup helpers ─────────────────────────────────────────────────────

    #[test]
    fn popup_is_open_false_when_no_oid() {
        let app = App::new();
        assert!(!popup_is_open(&app));
    }

    #[test]
    fn popup_is_open_true_when_oid_set() {
        let mut app = App::new();
        app.tab_mut().pending_commit_action_oid = Some("abc123".to_string());
        assert!(popup_is_open(&app));
    }

    // ── handle_popup_key ─────────────────────────────────────────────────

    fn app_with_popup() -> App {
        let mut app = App::new();
        // Give it a fake commit so open_commit_action_popup works
        app.tab_mut().commits = make_commits(3);
        app.tab_mut().commits[0].oid = "abc1234567".to_string();
        app.tab_mut().commit_list_state.select(Some(0));
        app.open_commit_action_popup();
        app
    }

    #[test]
    fn handle_popup_key_j_moves_cursor_down() {
        let mut app = app_with_popup();
        assert_eq!(app.tab().commit_action_cursor, 0);
        handle_popup_key(&mut app, key(KeyCode::Char('j')));
        assert_eq!(app.tab().commit_action_cursor, 1);
    }

    #[test]
    fn handle_popup_key_down_arrow_moves_cursor_down() {
        let mut app = app_with_popup();
        handle_popup_key(&mut app, key(KeyCode::Down));
        assert_eq!(app.tab().commit_action_cursor, 1);
    }

    #[test]
    fn handle_popup_key_k_moves_cursor_up() {
        let mut app = app_with_popup();
        app.tab_mut().commit_action_cursor = 3;
        handle_popup_key(&mut app, key(KeyCode::Char('k')));
        assert_eq!(app.tab().commit_action_cursor, 2);
    }

    #[test]
    fn handle_popup_key_cursor_clamps_at_bottom() {
        let mut app = app_with_popup();
        let last = app.tab().commit_action_items.len() - 1;
        app.tab_mut().commit_action_cursor = last;
        handle_popup_key(&mut app, key(KeyCode::Char('j')));
        assert_eq!(app.tab().commit_action_cursor, last);
    }

    #[test]
    fn handle_popup_key_cursor_clamps_at_top() {
        let mut app = app_with_popup();
        app.tab_mut().commit_action_cursor = 0;
        handle_popup_key(&mut app, key(KeyCode::Char('k')));
        assert_eq!(app.tab().commit_action_cursor, 0);
    }

    #[test]
    fn handle_popup_key_esc_closes_popup() {
        let mut app = app_with_popup();
        assert!(popup_is_open(&app));
        handle_popup_key(&mut app, key(KeyCode::Esc));
        assert!(!popup_is_open(&app));
        assert!(app.tab().commit_action_items.is_empty());
        assert_eq!(app.tab().commit_action_cursor, 0);
    }

    #[test]
    fn handle_popup_key_q_closes_popup() {
        let mut app = app_with_popup();
        handle_popup_key(&mut app, key(KeyCode::Char('q')));
        assert!(!popup_is_open(&app));
    }

    #[test]
    fn handle_popup_key_enter_simple_action_sets_loading() {
        let mut app = app_with_popup();
        app.tab_mut().repo_path = Some(std::path::PathBuf::from("/tmp/fake-repo"));
        // Cursor 0 = CheckoutDetached — no input needed
        app.tab_mut().commit_action_cursor = 0;
        assert_eq!(
            app.tab().commit_action_items[0],
            gitkraft_core::CommitActionKind::CheckoutDetached
        );
        handle_popup_key(&mut app, key(KeyCode::Enter));
        // Should have dispatched to execute_commit_action → is_loading = true
        assert!(app.tab().is_loading);
        // Popup should be closed
        assert!(!popup_is_open(&app));
    }

    #[test]
    fn handle_popup_key_enter_input_action_enters_input_mode() {
        let mut app = app_with_popup();
        // Cursor 1 = CreateBranchHere — needs input
        app.tab_mut().commit_action_cursor = 1;
        assert_eq!(
            app.tab().commit_action_items[1],
            gitkraft_core::CommitActionKind::CreateBranchHere
        );
        handle_popup_key(&mut app, key(KeyCode::Enter));
        // Should enter input mode, NOT execute
        assert!(!app.tab().is_loading);
        assert_eq!(app.input_mode, crate::app::InputMode::Input);
        assert_eq!(
            app.input_purpose,
            crate::app::InputPurpose::CommitActionInput1
        );
        // pending_action_kind should be set
        assert_eq!(
            app.tab().pending_action_kind,
            Some(gitkraft_core::CommitActionKind::CreateBranchHere)
        );
        // The items list is cleared but the OID is kept for execute_commit_action
        assert!(app.tab().commit_action_items.is_empty());
        assert!(app.tab().pending_commit_action_oid.is_some());
    }

    #[test]
    fn navigate_down_clears_commit_range_diffs() {
        let mut app = App::new();
        app.tab_mut().commits = make_commits(3);
        app.tab_mut().commit_range_diffs = vec![gitkraft_core::DiffInfo {
            old_file: String::new(),
            new_file: "a.rs".to_string(),
            status: gitkraft_core::FileStatus::Modified,
            hunks: vec![],
        }];
        app.tab_mut().commit_list_state.select(Some(0));
        navigate_down(&mut app);
        assert!(app.tab().commit_range_diffs.is_empty());
    }

    #[test]
    fn select_commit_down_triggers_range_diff_load() {
        let mut app = App::new();
        app.tab_mut().commits = make_commits(5);
        app.tab_mut().commit_list_state.select(Some(1));
        app.tab_mut().anchor_commit_index = Some(1);
        // No repo_path, so load will be a no-op — just verify selected_commits is set
        select_commit_down(&mut app);
        assert_eq!(app.tab().selected_commits, vec![1, 2]);
    }

    #[test]
    fn j_extends_commit_range_selection_downward() {
        let mut app = App::new();
        app.tab_mut().commits = make_commits(5);
        app.tab_mut().commit_list_state.select(Some(1));
        app.tab_mut().anchor_commit_index = Some(1);

        handle_key(&mut app, key(KeyCode::Char('J')));

        assert!(app.tab().selected_commits.contains(&1));
        assert!(app.tab().selected_commits.contains(&2));
        assert_eq!(app.tab().commit_list_state.selected(), Some(2));
    }

    #[test]
    fn k_extends_commit_range_selection_upward() {
        let mut app = App::new();
        app.tab_mut().commits = make_commits(5);
        app.tab_mut().commit_list_state.select(Some(3));
        app.tab_mut().anchor_commit_index = Some(3);

        handle_key(&mut app, key(KeyCode::Char('K')));

        assert!(app.tab().selected_commits.contains(&2));
        assert!(app.tab().selected_commits.contains(&3));
        assert_eq!(app.tab().commit_list_state.selected(), Some(2));
    }

    #[test]
    fn j_does_not_go_past_last_commit() {
        let mut app = App::new();
        app.tab_mut().commits = make_commits(3);
        app.tab_mut().commit_list_state.select(Some(2));
        app.tab_mut().anchor_commit_index = Some(2);

        handle_key(&mut app, key(KeyCode::Char('J')));

        assert_eq!(app.tab().commit_list_state.selected(), Some(2));
        assert!(app.tab().selected_commits.is_empty());
    }

    #[test]
    fn c_cherry_picks_current_commit() {
        let mut app = App::new();
        app.tab_mut().repo_path = Some(std::path::PathBuf::from("/tmp/fake-repo"));
        app.tab_mut().commits = make_commits(3);
        app.tab_mut().commit_list_state.select(Some(1));

        handle_key(&mut app, key(KeyCode::Char('C')));

        // With a repo_path set, cherry_pick_selected should set is_loading.
        assert!(app.tab().is_loading);
    }

    #[test]
    fn n_resets_to_mixed() {
        let mut app = App::new();
        app.tab_mut().repo_path = Some(std::path::PathBuf::from("/tmp/fake-repo"));
        app.tab_mut().commits = make_commits(3);
        app.tab_mut().commit_list_state.select(Some(0));

        handle_key(&mut app, key(KeyCode::Char('n')));

        // reset_to_selected_commit("mixed") should set is_loading.
        assert!(app.tab().is_loading);
    }

    #[test]
    fn n_sets_status_message_mentioning_mixed() {
        let mut app = App::new();
        app.tab_mut().repo_path = Some(std::path::PathBuf::from("/tmp/fake-repo"));
        app.tab_mut().commits = make_commits(3);
        app.tab_mut().commit_list_state.select(Some(0));

        handle_key(&mut app, key(KeyCode::Char('n')));

        let msg = app.tab().status_message.as_deref().unwrap_or("");
        assert!(!msg.is_empty(), "n must set a status message");
    }

    #[test]
    fn c_with_no_cursor_is_noop() {
        let mut app = App::new();
        app.tab_mut().repo_path = Some(std::path::PathBuf::from("/tmp/fake-repo"));
        app.tab_mut().commits = make_commits(3);
        // No commit selected (cursor is None)

        handle_key(&mut app, key(KeyCode::Char('C')));

        assert!(!app.tab().is_loading, "C with no cursor must be a noop");
    }

    #[test]
    fn c_with_multi_selection_sets_loading() {
        let mut app = App::new();
        app.tab_mut().repo_path = Some(std::path::PathBuf::from("/tmp/fake-repo"));
        app.tab_mut().commits = make_commits(4);
        app.tab_mut().commit_list_state.select(Some(0));
        // Simulate two commits selected via Space or J/K
        app.tab_mut().selected_commits = vec![0, 1, 2];

        handle_key(&mut app, key(KeyCode::Char('C')));

        assert!(
            app.tab().is_loading,
            "C with multi-selection must set is_loading"
        );
        let msg = app.tab().status_message.as_deref().unwrap_or("");
        assert!(
            msg.contains("3"),
            "status message must mention 3 commits; got: {msg}"
        );
    }

    #[test]
    fn c_without_multi_selection_uses_cursor_commit() {
        let mut app = App::new();
        app.tab_mut().repo_path = Some(std::path::PathBuf::from("/tmp/fake-repo"));
        app.tab_mut().commits = make_commits(4);
        app.tab_mut().commit_list_state.select(Some(2));
        // selected_commits empty → single-commit path
        app.tab_mut().selected_commits = vec![];

        handle_key(&mut app, key(KeyCode::Char('C')));

        assert!(
            app.tab().is_loading,
            "C on cursor commit (no multi-selection) must set is_loading"
        );
    }
}