plannotator-tui 0.15.0

Annotate Markdown in the terminal - standalone, and as a Herdr plugin.
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
//! The change picker against real repositories (the review tests' fixture), drawn on a
//! `TestBackend`, and against a stand-in source that is slow or fails on purpose.
#![allow(clippy::expect_used, clippy::indexing_slicing, clippy::panic, reason = "tests assert by panicking")]

use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Condvar, Mutex};
use std::time::{Duration, Instant};

use plannotator_tui_review::{Capture, Repo, Scope};
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use ratatui::crossterm::event::{
    Event, KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseEvent, MouseEventKind,
};

use super::rows::Sized;
use super::shown::{Opener, Shown, Start};
use super::source::{GitSource, Listing, PAGE, Size, Source};
use super::{Picker, Row};
use crate::review::tests::{Fixture, at, type_and_save};

fn opener(fixture: &Fixture) -> Opener {
    let agent = fixture.agent.clone();
    let (wake, _ready) = std::sync::mpsc::sync_channel(1);
    Opener {
        data_dir: fixture.data(),
        wake,
        review: crate::config::review::ReviewConfig::DEFAULT,
        remembered: None,
        notes_rail: crate::notes_rail::RailStart::default(),
        keys: crate::keys::Keymap::default(),
        shift_enter: false,
        copy_on_select: true,
        notice: None,
        delivery: Box::new(move || Box::new(agent.clone())),
        live: false,
    }
}

fn repo(fixture: &Fixture) -> Repo {
    Repo::discover(&fixture.repo()).expect("repo")
}

/// The picker on its own, as `review --pick` opens it.
pub(super) fn picking(fixture: &Fixture) -> Shown {
    Shown::start(Start::Pick(repo(fixture)), opener(fixture)).expect("picker")
}

/// The review of the uncommitted changes, as `review` opens it.
fn reviewing(fixture: &Fixture) -> Shown {
    let (repo, capture) =
        crate::review::capture(&fixture.repo(), &plannotator_tui_review::Request::Uncommitted { base: None })
            .expect("capture");
    let mut shown = Shown::start(Start::Review(repo, Box::new(capture)), opener(fixture)).expect("review");
    let screen = shown.screen_mut().expect("a review");
    crate::review::tests::settle(screen);
    shown
}

pub(super) fn draw(shown: &mut Shown, cols: u16, rows: u16) -> Vec<String> {
    let mut terminal = Terminal::new(TestBackend::new(cols, rows)).expect("terminal");
    let mut drawn = Ok(());
    terminal.draw(|f| drawn = shown.draw(f)).expect("draw");
    drawn.expect("draw");
    shown.after_draw();
    let buffer = terminal.backend().buffer();
    (0..rows)
        .map(|y| (0..cols).filter_map(|x| buffer.cell((x, y)).map(|c| c.symbol().to_owned())).collect())
        .collect()
}

pub(super) fn press(shown: &mut Shown, code: KeyCode) {
    shown.handle_event(Event::Key(KeyEvent::new(code, KeyModifiers::NONE))).expect("key");
}

pub(super) fn keys(shown: &mut Shown, text: &str) {
    for c in text.chars() {
        press(shown, KeyCode::Char(c));
    }
}

/// Tick and draw until every row's size is known and nothing is being opened.
pub(super) fn settle(shown: &mut Shown, cols: u16, rows: u16) -> Vec<String> {
    let started = Instant::now();
    loop {
        shown.tick().expect("tick");
        let drawn = draw(shown, cols, rows);
        let Some(picker) = shown.picker_mut() else { return drawn };
        let busy = !picker.listed
            || picker.opening.is_some()
            || picker.counting.is_some()
            || picker.more == super::More::Loading
            || picker
                .rows
                .iter()
                .any(|row| matches!(row.size, Sized::Waiting | Sized::Asking) && row.scope().is_some());
        if !busy {
            return drawn;
        }
        assert!(
            started.elapsed() < Duration::from_secs(20),
            "the picker never settled:\n{}",
            drawn.join("\n")
        );
        std::thread::sleep(Duration::from_millis(5));
    }
}

/// Tick until the picker has closed (a review opened) or `give_up` passes.
pub(super) fn until_open(shown: &mut Shown) {
    let started = Instant::now();
    while shown.picker_mut().is_some_and(|p| p.opening.is_some()) {
        shown.tick().expect("tick");
        assert!(started.elapsed() < Duration::from_secs(20), "never opened");
        std::thread::sleep(Duration::from_millis(5));
    }
    shown.tick().expect("tick");
}

pub(super) fn row_with<'a>(rows: &'a [String], needle: &str) -> &'a str {
    crate::review::tests::row_with(rows, needle)
}

pub(super) fn position(rows: &[String], needle: &str) -> usize {
    rows.iter()
        .position(|r| r.contains(needle))
        .unwrap_or_else(|| panic!("no {needle:?}:\n{}", rows.join("\n")))
}

pub(super) fn commit(fixture: &Fixture, message: &str) {
    fixture.git(&["add", "-A"]);
    fixture.git(&["-c", "user.name=Ada", "-c", "user.email=a@example.com", "commit", "-qm", message]);
}

#[test]
fn after_the_agent_commits_the_rows_keep_their_order_the_tree_is_dimmed_and_the_cursor_is_on_the_commit() {
    let fixture = Fixture::new("pick-clean");
    commit(&fixture, "feat: greet from argv");
    let mut shown = picking(&fixture);
    let rows = settle(&mut shown, 120, 20);
    let order =
        ["Uncommitted", "Unstaged", "Staged", "feat: greet from argv", "init"].map(|t| position(&rows, t));
    assert!(order.windows(2).all(|w| w[0] < w[1]), "{order:?}\n{}", rows.join("\n"));
    for empty in ["Uncommitted", "Unstaged", "Staged"] {
        assert!(row_with(&rows, empty).contains("(no changes)"), "{}", rows.join("\n"));
    }
    assert!(!rows.iter().any(|r| r.contains("Branch vs")), "no origin, no branch row");
    let latest = row_with(&rows, "feat: greet from argv");
    assert!(latest.starts_with("▸ "), "{}", rows.join("\n"));
    assert!(
        latest.contains("+7 −3 · 1 file") && latest.contains("now") && latest.contains("Ada"),
        "{latest}"
    );
    let footer = "No uncommitted changes · pick a commit below";
    assert!(rows.iter().any(|r| r.contains(footer)), "{}", rows.join("\n"));
    assert!(!rows.iter().any(|r| r.contains("Nothing to review")), "{}", rows.join("\n"));
}

#[test]
fn with_uncommitted_staged_and_unstaged_changes_the_cursor_starts_on_uncommitted() {
    let fixture = Fixture::new("pick-parts");
    fixture.git(&["add", "app.py"]);
    fixture.write("notes.txt", "changed, not staged\n");
    fixture.write("new.txt", "untracked\n");
    let mut shown = picking(&fixture);
    let rows = settle(&mut shown, 120, 20);
    assert!(row_with(&rows, "Uncommitted").starts_with("▸ "), "{}", rows.join("\n"));
    assert!(row_with(&rows, "Uncommitted").contains("3 files"), "{}", rows.join("\n"));
    assert!(row_with(&rows, "Unstaged").contains("2 files"), "{}", rows.join("\n"));
    assert!(row_with(&rows, "Staged").contains("+7 −3 · 1 file"), "{}", rows.join("\n"));
}

#[test]
fn enter_reviews_the_row_exactly_and_an_empty_row_only_says_so() {
    let fixture = Fixture::new("pick-enter");
    let mut shown = picking(&fixture);
    settle(&mut shown, 120, 20);
    keys(&mut shown, "jj"); // Staged: nothing staged
    press(&mut shown, KeyCode::Enter);
    let rows = draw(&mut shown, 120, 20);
    assert!(rows.iter().any(|r| r.contains("Staged has no changes")), "{}", rows.join("\n"));
    assert!(shown.screen().is_none(), "still picking");

    keys(&mut shown, "k"); // Unstaged
    press(&mut shown, KeyCode::Enter);
    until_open(&mut shown);
    let screen = shown.screen().expect("the review opened");
    assert_eq!(screen.capture.scope, Scope::Unstaged);
    assert_eq!(screen.delivery.describe(), "test agent");
}

#[test]
fn esc_on_its_own_says_q_quits_and_q_quits() {
    let fixture = Fixture::new("pick-esc");
    let mut shown = picking(&fixture);
    press(&mut shown, KeyCode::Esc);
    let rows = draw(&mut shown, 80, 12);
    assert!(rows.iter().any(|r| r.contains("q quits")), "{}", rows.join("\n"));
    assert!(!shown.wants_quit() && shown.picker_mut().is_some());
    keys(&mut shown, "q");
    assert!(shown.wants_quit());
}

#[test]
fn p_opens_the_picker_over_the_review_and_a_new_target_keeps_the_old_ones_unsent_notes() {
    let fixture = Fixture::new("pick-over");
    commit(&fixture, "second");
    fixture.write("app.py", crate::review::tests::AFTER.replace("import sys", "import os").as_str());
    let mut shown = reviewing(&fixture);
    {
        let screen = shown.screen_mut().expect("review");
        at(screen, 1);
        crate::review::tests::keys(screen, "c");
        type_and_save(screen, "why os?");
    }
    // `p` while the comment box is open is text.
    crate::review::tests::keys(shown.screen_mut().expect("review"), "c");
    keys(&mut shown, "p");
    assert!(shown.picker_mut().is_none(), "p went into the draft");
    press(&mut shown, KeyCode::Esc);
    keys(&mut shown, "y"); // discard the draft

    keys(&mut shown, "p");
    let rows = settle(&mut shown, 120, 30);
    let badge = |r: &&String| r.contains("Uncommitted") && r.contains("open · 1 unsent");
    assert!(rows.iter().any(|r| badge(&r)), "{}", rows.join("\n"));
    press(&mut shown, KeyCode::Esc);
    assert!(shown.picker_mut().is_none(), "Esc goes back to the review");
    assert_eq!(shown.screen().expect("review").capture.scope, Scope::Uncommitted);

    keys(&mut shown, "p");
    settle(&mut shown, 120, 30);
    keys(&mut shown, "jjj"); // the commit `second`
    press(&mut shown, KeyCode::Enter);
    until_open(&mut shown);
    let screen = shown.screen_mut().expect("the commit's review");
    assert!(matches!(screen.capture.scope, Scope::Commit { .. }), "{:?}", screen.capture.scope);
    assert_eq!(screen.delivery.describe(), "test agent", "the delivery came along");
    assert_eq!(screen.store.annotations().len(), 0, "the commit has notes of its own: none yet");
    let rows = crate::review::tests::frame(screen, 120, 30);
    assert!(rows.iter().any(|r| r.contains("1 unsent note kept on Uncommitted")), "{}", rows.join("\n"));

    keys(&mut shown, "p");
    settle(&mut shown, 120, 30);
    keys(&mut shown, "g");
    press(&mut shown, KeyCode::Enter);
    until_open(&mut shown);
    let screen = shown.screen().expect("back on uncommitted");
    assert_eq!(screen.capture.scope, Scope::Uncommitted);
    assert_eq!(screen.store.annotations().len(), 1, "the note waited on its own scope");
}

#[test]
fn r_lists_a_new_commit_and_a_click_opens_the_row_under_it() {
    let fixture = Fixture::new("pick-reload");
    let mut shown = picking(&fixture);
    settle(&mut shown, 120, 20);
    commit(&fixture, "landed while picking");
    keys(&mut shown, "r");
    let rows = settle(&mut shown, 120, 20);
    let y = position(&rows, "landed while picking");
    assert!(row_with(&rows, "landed while picking").starts_with("▸ "), "the tree is clean now");
    assert!(row_with(&rows, "Uncommitted").contains("(no changes)"));
    let click = MouseEvent {
        kind: MouseEventKind::Down(MouseButton::Left),
        column: 10,
        row: y as u16 + 1, // `init`, below it
        modifiers: KeyModifiers::NONE,
    };
    shown.handle_event(Event::Mouse(click)).expect("click");
    until_open(&mut shown);
    let screen = shown.screen().expect("opened");
    assert_eq!(screen.capture.commit_subject.as_deref(), Some("init"));
}

#[test]
fn more_lists_older_commits_the_wheel_scrolls_and_small_screens_cut_titles() {
    let fixture = Fixture::new("pick-more");
    commit(&fixture, "change 0");
    for n in 1..=24 {
        fixture.write("notes.txt", &format!("{n}\n"));
        commit(
            &fixture,
            &format!("change {n} with a subject long enough to need cutting on a narrow screen"),
        );
    }
    let mut shown = picking(&fixture);
    let rows = settle(&mut shown, 120, 40);
    assert!(rows.iter().any(|r| r.contains("more…")), "{}", rows.join("\n"));
    assert!(!rows.iter().any(|r| r.contains("change 0")), "21 or more commits: 20 shown");
    keys(&mut shown, "G");
    press(&mut shown, KeyCode::Enter);
    let rows = settle(&mut shown, 120, 40);
    assert!(rows.iter().any(|r| r.contains("change 0")), "{}", rows.join("\n"));
    assert!(row_with(&rows, "init").contains("+12 −0"), "{}", rows.join("\n"));
    assert!(!rows.iter().any(|r| r.contains("more…")), "nothing older");

    for (cols, lines) in [(40, 10), (60, 14), (200, 12)] {
        let rows = draw(&mut shown, cols, lines);
        let shown_rows: Vec<&String> = rows.iter().filter(|r| r.contains("change 2")).collect();
        assert!(!shown_rows.is_empty() || cols == 40, "{cols}:\n{}", rows.join("\n"));
        for row in &rows {
            assert!(unicode_width::UnicodeWidthStr::width(row.as_str()) <= usize::from(cols));
        }
        if cols < 120 {
            assert!(
                rows.iter().any(|r| r.contains("change") && r.contains('…')),
                "{cols}:\n{}",
                rows.join("\n")
            );
        }
        if cols == 60 {
            // The author gives way before the subject is cut to a stub.
            assert!(rows.iter().any(|r| r.contains("with a subje")), "{cols}:\n{}", rows.join("\n"));
        }
    }
    // The wheel scrolls the list, the cursor staying on screen.
    keys(&mut shown, "g");
    draw(&mut shown, 60, 14);
    let wheel = |kind| MouseEvent { kind, column: 5, row: 5, modifiers: KeyModifiers::NONE };
    shown.handle_event(Event::Mouse(wheel(MouseEventKind::ScrollDown))).expect("wheel");
    let rows = draw(&mut shown, 60, 14);
    assert!(!rows.iter().any(|r| r.contains("Uncommitted")), "scrolled past the top:\n{}", rows.join("\n"));
    assert!(rows.iter().any(|r| r.starts_with("▸ ")), "the cursor came along:\n{}", rows.join("\n"));
}

/// A source whose sizes wait until the test lets them go, and whose captures can fail.
struct Gated {
    inner: GitSource,
    open: Arc<(Mutex<bool>, Condvar)>,
    capture_error: Option<String>,
}

impl Source for Gated {
    fn name(&self) -> String {
        self.inner.name()
    }
    fn list(&self, skip: usize, count: usize) -> Result<Listing, String> {
        self.inner.list(skip, count)
    }
    fn size(&self, scope: &Scope) -> Result<Size, String> {
        let (lock, cvar) = &*self.open;
        let mut open = lock.lock().expect("lock");
        while !*open {
            open = cvar.wait(open).expect("wait");
        }
        drop(open);
        self.inner.size(scope)
    }
    fn capture(&self, scope: &Scope) -> Result<(Repo, Capture), String> {
        match &self.capture_error {
            Some(error) => Err(error.clone()),
            None => self.inner.capture(scope),
        }
    }
    fn count(&self, from: &str, to: &str) -> Result<usize, String> {
        self.inner.count(from, to)
    }
}

fn gated(fixture: &Fixture, capture_error: Option<&str>) -> (Shown, Arc<(Mutex<bool>, Condvar)>) {
    let open = Arc::new((Mutex::new(false), Condvar::new()));
    let source = Gated {
        inner: GitSource { repo: repo(fixture), data_dir: fixture.data() },
        open: Arc::clone(&open),
        capture_error: capture_error.map(str::to_owned),
    };
    let listing = source.list(0, PAGE).ok();
    let opener = opener(fixture);
    let picker = Picker::open(Arc::new(source), listing, None, opener.keys.clone(), opener.wake.clone());
    (Shown::picking(picker, opener), open)
}

fn release(open: &Arc<(Mutex<bool>, Condvar)>) {
    *open.0.lock().expect("lock") = true;
    open.1.notify_all();
}

#[test]
fn a_slow_source_never_holds_up_a_frame_or_a_key() {
    let fixture = Fixture::new("pick-slow");
    let (mut shown, open) = gated(&fixture, None);
    let started = Instant::now();
    let rows = draw(&mut shown, 100, 16);
    shown.tick().expect("tick");
    keys(&mut shown, "jj");
    let rows_after = draw(&mut shown, 100, 16);
    assert!(started.elapsed() < Duration::from_secs(1), "{:?}", started.elapsed());
    assert!(row_with(&rows, "Uncommitted").trim_end().ends_with('…'), "{}", rows.join("\n"));
    assert!(row_with(&rows, "init").trim_end().ends_with('t'), "the commits are there at once");
    assert!(row_with(&rows_after, "Staged").starts_with("▸ "), "{}", rows_after.join("\n"));
    release(&open);
    let rows = settle(&mut shown, 100, 16);
    assert!(row_with(&rows, "Uncommitted").contains("1 file"), "{}", rows.join("\n"));
    assert!(row_with(&rows, "Staged").starts_with("▸ "), "a moved cursor stays where it was put");
}

#[test]
fn a_capture_that_fails_says_why_and_the_picker_stays() {
    let fixture = Fixture::new("pick-fails");
    let (mut shown, open) = gated(&fixture, Some("the patch is larger than 8 MiB"));
    release(&open);
    settle(&mut shown, 100, 16);
    press(&mut shown, KeyCode::Enter);
    let rows = settle(&mut shown, 100, 16);
    assert!(shown.screen().is_none());
    assert!(
        rows.iter().any(|r| r.contains("Uncommitted: the patch is larger than 8 MiB")),
        "{}",
        rows.join("\n")
    );
}

#[cfg(unix)]
#[test]
fn thousands_of_untracked_files_do_not_delay_the_first_frame() {
    let fixture = Fixture::new("pick-many");
    let many = fixture.repo().join("generated");
    std::fs::create_dir_all(&many).expect("dir");
    for n in 0..4000 {
        std::fs::write(many.join(format!("f{n}.txt")), "x\n").expect("write");
    }
    let started = Instant::now();
    let mut shown = picking(&fixture);
    let rows = draw(&mut shown, 100, 16);
    let first_frame = started.elapsed();
    let sized = Instant::now();
    repo(&fixture).diff_stat(&Scope::Uncommitted).expect("size");
    let size_takes = sized.elapsed();
    assert!(rows.iter().any(|r| r.contains("init")), "{}", rows.join("\n"));
    assert!(first_frame < size_takes, "first frame {first_frame:?}, one size {size_takes:?}");
    let rows = settle(&mut shown, 100, 16);
    assert!(row_with(&rows, "Uncommitted").contains("4001 files"), "{}", rows.join("\n"));
}

#[test]
fn rows_name_their_scope_as_the_review_header_does() {
    let commit = plannotator_tui_review::Commit {
        sha: "1a2b3c4d5e6f".into(),
        subject: "fix: login".into(),
        author: "Ada".into(),
        time: 0,
    };
    let row = Row::new(super::rows::Kind::Commit(commit));
    assert_eq!(row.name(), "Commit 1a2b3c4");
    assert_eq!(row.title(), (Some("1a2b3c4".into()), "fix: login".into()));
    assert_eq!(row.scope(), Some(Scope::Commit { sha: "1a2b3c4d5e6f".into() }));
}

/// A source that counts how many of its calls run at once, each taking a while.
struct Counting {
    inner: GitSource,
    now: Arc<AtomicUsize>,
    most: Arc<AtomicUsize>,
}

impl Counting {
    fn call<T>(&self, work: impl FnOnce() -> T) -> T {
        let now = self.now.fetch_add(1, Ordering::SeqCst) + 1;
        self.most.fetch_max(now, Ordering::SeqCst);
        std::thread::sleep(Duration::from_millis(30));
        let out = work();
        self.now.fetch_sub(1, Ordering::SeqCst);
        out
    }
}

impl Source for Counting {
    fn name(&self) -> String {
        self.inner.name()
    }
    fn list(&self, skip: usize, count: usize) -> Result<Listing, String> {
        self.call(|| self.inner.list(skip, count))
    }
    fn size(&self, scope: &Scope) -> Result<Size, String> {
        self.call(|| self.inner.size(scope))
    }
    fn capture(&self, scope: &Scope) -> Result<(Repo, Capture), String> {
        self.call(|| self.inner.capture(scope))
    }
    fn count(&self, from: &str, to: &str) -> Result<usize, String> {
        self.call(|| self.inner.count(from, to))
    }
}

#[test]
fn pressing_r_again_and_again_never_runs_more_than_two_jobs_at_once() {
    let fixture = Fixture::new("pick-spam");
    let most = Arc::new(AtomicUsize::new(0));
    let source = Counting {
        inner: GitSource { repo: repo(&fixture), data_dir: fixture.data() },
        now: Arc::new(AtomicUsize::new(0)),
        most: Arc::clone(&most),
    };
    let opener = opener(&fixture);
    let picker = Picker::open(Arc::new(source), None, None, opener.keys.clone(), opener.wake.clone());
    let mut shown = Shown::picking(picker, opener);
    for _ in 0..25 {
        keys(&mut shown, "r");
        shown.tick().expect("tick");
        let running = shown.picker_mut().expect("picking").work.running();
        assert!(running <= super::source::AT_ONCE, "{running} jobs running");
    }
    let rows = settle(&mut shown, 100, 16);
    assert!(most.load(Ordering::SeqCst) <= super::source::AT_ONCE, "{} at once", most.load(Ordering::SeqCst));
    assert!(row_with(&rows, "Uncommitted").contains("1 file"), "{}", rows.join("\n"));
}

/// The pid and index copy of every run of a clean filter that takes 30 s, from its log.
#[cfg(unix)]
fn filter_runs(log: &std::path::Path) -> Vec<(i32, String)> {
    std::fs::read_to_string(log)
        .unwrap_or_default()
        .lines()
        .filter_map(|line| {
            let (pid, index) = line.split_once(' ')?;
            Some((pid.parse().ok()?, index.to_owned()))
        })
        .collect()
}

#[cfg(unix)]
#[test]
fn quitting_while_sizes_are_counted_leaves_no_git_running_and_no_index_copy() {
    let fixture = Fixture::new("pick-quit");
    let log = fixture.dir.join("filter.log");
    let filter = format!("sh -c 'echo $$ $GIT_INDEX_FILE >> {}; sleep 30; cat'", log.display());
    fixture.git(&["config", "filter.slow.clean", &filter]);
    fixture.write(".gitattributes", "*.py filter=slow\n");
    let mut shown = picking(&fixture);
    let started = Instant::now();
    while filter_runs(&log).is_empty() {
        shown.tick().expect("tick");
        assert!(started.elapsed() < Duration::from_secs(20), "the filter never ran");
        std::thread::sleep(Duration::from_millis(10));
    }
    let runs = filter_runs(&log);
    let quitting = Instant::now();
    keys(&mut shown, "q");
    assert!(shown.wants_quit());
    drop(shown);
    assert!(quitting.elapsed() < Duration::from_secs(3), "quitting waited {:?}", quitting.elapsed());
    for (pid, index) in runs {
        let pid = rustix::process::Pid::from_raw(pid).expect("a pid");
        let gone = (0..100).any(|_| {
            std::thread::sleep(Duration::from_millis(10));
            rustix::process::test_kill_process(pid) == Err(rustix::io::Errno::SRCH)
        });
        assert!(gone, "the filter git started, {pid:?}, still runs");
        let copy = std::path::Path::new(&index).parent().expect("the copy's folder");
        assert!(!index.is_empty() && !copy.exists(), "{} was left behind", copy.display());
    }
}

/// A count whose git waits on the terminal (here a clean filter that stops itself, as a read
/// from the terminal stops a background process) ends with the row saying so, and the other
/// rows are counted all the same.
#[cfg(unix)]
#[test]
fn a_count_that_waits_on_the_terminal_is_stopped_and_the_row_says_so() {
    let fixture = Fixture::new("pick-tty");
    let log = fixture.dir.join("filter.log");
    let filter = format!("sh -c 'echo $$ - >> {}; kill -STOP $$; cat'", log.display());
    fixture.git(&["config", "filter.tty.clean", &filter]);
    fixture.write(".gitattributes", "*.py filter=tty\n");
    let mut shown = picking(&fixture);
    let started = Instant::now();
    let rows = loop {
        let rows = settle(&mut shown, 120, 20);
        let counted = rows.iter().filter(|row| row.contains(" \u{b7} ")).count();
        if row_with(&rows, "Uncommitted").contains("(needs the terminal)") && counted > 0 {
            break rows;
        }
        assert!(started.elapsed() < Duration::from_secs(20), "{}", rows.join("\n"));
        std::thread::sleep(Duration::from_millis(20));
    };
    assert!(row_with(&rows, "init").contains("file"), "{}", rows.join("\n"));
    assert!(!filter_runs(&log).is_empty(), "the filter ran");
    for (pid, _) in filter_runs(&log) {
        let pid = rustix::process::Pid::from_raw(pid).expect("a pid");
        let gone = (0..100).any(|_| {
            std::thread::sleep(Duration::from_millis(10));
            rustix::process::test_kill_process(pid) == Err(rustix::io::Errno::SRCH)
        });
        assert!(gone, "the stopped filter {pid:?} was left");
    }
}

#[test]
fn a_click_while_the_keys_box_is_open_only_closes_it() {
    let fixture = Fixture::new("pick-help-click");
    let mut shown = picking(&fixture);
    let rows = settle(&mut shown, 100, 16);
    let y = position(&rows, "init");
    keys(&mut shown, "?");
    let rows = draw(&mut shown, 100, 16);
    assert!(rows.iter().any(|r| r.contains("picker keys")), "{}", rows.join("\n"));
    let click = MouseEvent {
        kind: MouseEventKind::Down(MouseButton::Left),
        column: 50,
        row: y as u16,
        modifiers: KeyModifiers::NONE,
    };
    shown.handle_event(Event::Mouse(click)).expect("click");
    let wheel = MouseEvent { kind: MouseEventKind::ScrollDown, ..click };
    keys(&mut shown, "?");
    shown.handle_event(Event::Mouse(wheel)).expect("wheel");
    let picker = shown.picker_mut().expect("still picking");
    assert!(picker.opening.is_none(), "nothing under the box was opened");
    assert_eq!(picker.scroll, 0, "nor scrolled");
    let rows = settle(&mut shown, 100, 16);
    assert!(rows.iter().any(|r| r.contains("picker keys")), "the wheel left the box open");
    shown.handle_event(Event::Mouse(click)).expect("click");
    let rows = draw(&mut shown, 100, 16);
    assert!(!rows.iter().any(|r| r.contains("picker keys")), "a click closed it");
    assert!(shown.screen().is_none());
}

#[test]
fn the_keys_box_lines_its_descriptions_up_after_the_widest_keys() {
    let fixture = Fixture::new("pick-help-cols");
    let mut shown = picking(&fixture);
    keys(&mut shown, "?");
    let rows = draw(&mut shown, 100, 20);
    let column = |what: &str| {
        let row = row_with(&rows, what);
        row.find(what).map(|byte| unicode_width::UnicodeWidthStr::width(&row[..byte]))
    };
    let first = column("next / previous row");
    assert!(first.is_some(), "{}", rows.join("\n"));
    for what in ["a page down / up", "first / last row", "quit"] {
        assert_eq!(column(what), first, "{what}:\n{}", rows.join("\n"));
    }
}

#[test]
fn over_a_review_the_cursor_starts_on_it_and_enter_there_goes_back_untouched() {
    let fixture = Fixture::new("pick-same");
    commit(&fixture, "second");
    fixture.write("app.py", crate::review::tests::AFTER.replace("import sys", "import os").as_str());
    let mut shown = reviewing(&fixture);
    crate::review::tests::keys(shown.screen_mut().expect("review"), "jjjj");
    let before = draw(&mut shown, 120, 30);
    keys(&mut shown, "p");
    let rows = settle(&mut shown, 120, 30);
    let open = rows.iter().find(|r| r.contains("open")).expect("the open row");
    assert!(open.contains("▸ Uncommitted"), "{}", rows.join("\n"));
    press(&mut shown, KeyCode::Enter);
    assert!(shown.picker_mut().is_none(), "the picker closed at once");
    // The diff as it was; the footer may say the colours are still being worked out.
    let after = draw(&mut shown, 120, 30);
    assert_eq!(after[..29], before[..29], "the review is as it was");

    // Over the commit's review, its row is the one the cursor starts on.
    keys(&mut shown, "p");
    settle(&mut shown, 120, 30);
    keys(&mut shown, "jjj");
    press(&mut shown, KeyCode::Enter);
    until_open(&mut shown);
    keys(&mut shown, "p");
    let rows = settle(&mut shown, 120, 30);
    assert!(row_with(&rows, "second").contains("▸ ") && row_with(&rows, "second").contains("open"));
}

#[test]
fn quitting_counts_the_notes_left_unsent_on_another_review_and_can_go_to_it() {
    let fixture = Fixture::new("pick-quit-elsewhere");
    commit(&fixture, "second");
    fixture.write("app.py", crate::review::tests::AFTER.replace("import sys", "import os").as_str());
    let mut shown = reviewing(&fixture);
    {
        let screen = shown.screen_mut().expect("review");
        at(screen, 1);
        crate::review::tests::keys(screen, "c");
        type_and_save(screen, "why os?");
    }
    keys(&mut shown, "p");
    settle(&mut shown, 120, 30);
    keys(&mut shown, "jjj"); // the commit `second`
    press(&mut shown, KeyCode::Enter);
    until_open(&mut shown);
    keys(&mut shown, "q");
    assert!(!shown.wants_quit(), "it asks first");
    let rows = draw(&mut shown, 140, 30);
    let prompt = rows.last().expect("footer");
    assert!(prompt.contains("1 unsent note on Uncommitted"), "{prompt}");
    assert!(
        prompt.contains("n quit") && prompt.contains("esc go to it") && !prompt.contains("y send"),
        "{prompt}"
    );
    keys(&mut shown, "y");
    assert!(!shown.wants_quit(), "nothing here to send");

    // Back: the picker, on the review with the note.
    press(&mut shown, KeyCode::Esc);
    let rows = settle(&mut shown, 120, 30);
    assert!(row_with(&rows, "▸ Uncommitted").contains("1 unsent"), "{}", rows.join("\n"));
    press(&mut shown, KeyCode::Esc);

    // A note here too: send is offered for it, and the other is still named.
    {
        let screen = shown.screen_mut().expect("the commit's review");
        crate::review::tests::keys(screen, "jjjc");
        type_and_save(screen, "and here");
    }
    keys(&mut shown, "q");
    let rows = draw(&mut shown, 160, 30);
    let prompt = rows.last().expect("footer");
    // `y` reads as sending this review's notes only; the others are named apart.
    assert!(
        prompt.contains("quit? y send these to test agent \u{b7} 1 more unsent on Uncommitted (esc go to it) \u{b7} n quit"),
        "{prompt}"
    );
    keys(&mut shown, "n");
    assert!(shown.wants_quit());
}

#[test]
fn a_review_gone_from_the_branch_is_named_once_when_quitting_and_not_again() {
    let fixture = Fixture::new("pick-quit-gone");
    commit(&fixture, "second");
    fixture.write("app.py", crate::review::tests::AFTER.replace("import sys", "import os").as_str());
    let mut shown = reviewing(&fixture);
    // A note on the commit `second`, then back to the uncommitted changes.
    keys(&mut shown, "p");
    settle(&mut shown, 120, 30);
    keys(&mut shown, "jjj");
    press(&mut shown, KeyCode::Enter);
    until_open(&mut shown);
    {
        let screen = shown.screen_mut().expect("the commit's review");
        at(screen, 1);
        crate::review::tests::keys(screen, "c");
        type_and_save(screen, "why?");
    }
    keys(&mut shown, "p");
    settle(&mut shown, 120, 30);
    keys(&mut shown, "kkk");
    press(&mut shown, KeyCode::Enter);
    until_open(&mut shown);
    // The commit is amended away.
    fixture.git(&[
        "-c",
        "user.name=T",
        "-c",
        "user.email=t@example.com",
        "commit",
        "--amend",
        "-qm",
        "second, again",
    ]);

    keys(&mut shown, "q");
    assert!(!shown.wants_quit(), "it says so first");
    let rows = draw(&mut shown, 160, 30);
    let prompt = rows.last().expect("footer");
    assert!(prompt.contains("is gone from this branch; its 1 note is kept but cannot be shown"), "{prompt}");
    assert!(
        prompt.contains("n quit") && !prompt.contains("go to it") && !prompt.contains("y send"),
        "{prompt}"
    );
    press(&mut shown, KeyCode::Esc);
    assert!(!shown.wants_quit(), "esc stays");
    keys(&mut shown, "q");
    assert!(shown.wants_quit(), "once told, quitting does not ask about it again");
}

#[test]
fn a_two_column_box_over_a_review_draws_without_panicking() {
    let fixture = Fixture::new("pick-tiny");
    let mut shown = reviewing(&fixture);
    keys(&mut shown, "p");
    for (cols, rows) in [(2, 3), (3, 2), (1, 1), (40, 10)] {
        draw(&mut shown, cols, rows);
    }
    keys(&mut shown, "?");
    draw(&mut shown, 2, 2);
}

#[test]
fn control_characters_in_a_subject_or_author_keep_the_columns_lined_up() {
    let fixture = Fixture::new("pick-control");
    fixture.git(&["add", "-A"]);
    fixture.git(&[
        "-c",
        "user.name=Ada\tL\u{1b}[31m",
        "-c",
        "user.email=a@example.com",
        "commit",
        "-qm",
        "fix:\ttabs\u{7} and \u{1b}[1mbold",
    ]);
    let mut shown = picking(&fixture);
    let rows = settle(&mut shown, 100, 16);
    let dot = |needle: &str| {
        let row = row_with(&rows, needle);
        row.find(" · ").map(|byte| unicode_width::UnicodeWidthStr::width(&row[..byte]))
    };
    assert!(dot("tabs").is_some(), "{}", rows.join("\n"));
    assert_eq!(dot("tabs"), dot("init"), "{}", rows.join("\n"));
    assert!(row_with(&rows, "tabs").contains("fix: tabs and [1mbold"), "{}", rows.join("\n"));
    assert!(!rows.iter().any(|r| r.contains('\u{1b}') || r.contains('\t')));
}

#[test]
fn a_commit_gone_since_the_list_was_read_says_to_reload() {
    let fixture = Fixture::new("pick-gone");
    commit(&fixture, "second");
    let (mut shown, open) = gated(&fixture, Some("git show failed: fatal: bad object 0123abc"));
    release(&open);
    settle(&mut shown, 100, 16);
    keys(&mut shown, "g");
    keys(&mut shown, "jjj"); // the commit `second`
    press(&mut shown, KeyCode::Enter);
    let rows = settle(&mut shown, 100, 16);
    assert!(rows.iter().any(|r| r.contains("bad object 0123abc · press r to reload")), "{}", rows.join("\n"));
}

#[test]
fn a_long_footer_message_ends_in_an_ellipsis() {
    let fixture = Fixture::new("pick-long");
    let (mut shown, open) = gated(&fixture, Some(&"x".repeat(300)));
    release(&open);
    settle(&mut shown, 60, 12);
    press(&mut shown, KeyCode::Enter);
    let rows = settle(&mut shown, 60, 12);
    let footer = row_with(&rows, "Uncommitted: x");
    assert!(footer.trim_end().ends_with('…'), "{}", rows.join("\n"));
}

#[test]
fn a_repository_with_no_commits_says_there_is_nothing() {
    let fixture = Fixture::new("pick-none");
    let fresh = fixture.dir.join("fresh");
    std::fs::create_dir_all(&fresh).expect("dir");
    let status =
        std::process::Command::new("git").arg("-C").arg(&fresh).args(["init", "-q"]).status().expect("git");
    assert!(status.success());
    let repo = Repo::discover(&fresh).expect("repo");
    let mut shown = Shown::start(Start::Pick(repo), opener(&fixture)).expect("picker");
    let rows = settle(&mut shown, 80, 12);
    assert!(rows.iter().any(|r| r.contains("Nothing to review in fresh")), "{}", rows.join("\n"));
    assert!(!rows.iter().any(|r| r.contains("pick a commit")), "{}", rows.join("\n"));
}

#[test]
fn u_still_takes_back_a_removal_after_another_target_and_back() {
    let fixture = Fixture::new("pick-undo");
    commit(&fixture, "second");
    fixture.write("app.py", crate::review::tests::AFTER.replace("import sys", "import os").as_str());
    let mut shown = reviewing(&fixture);
    {
        let screen = shown.screen_mut().expect("review");
        at(screen, 1);
        crate::review::tests::keys(screen, "c");
        type_and_save(screen, "why os?");
        let id = screen.store.annotations().first().map(|n| n.id.clone()).expect("the note");
        screen.remove_note(&id).expect("removed");
        assert_eq!(screen.status.as_deref(), Some("note removed \u{b7} U undo"));
    }
    keys(&mut shown, "p");
    settle(&mut shown, 120, 30);
    keys(&mut shown, "jjj"); // the commit `second`
    press(&mut shown, KeyCode::Enter);
    until_open(&mut shown);
    keys(&mut shown, "U");
    let screen = shown.screen().expect("the commit's review");
    assert_eq!(screen.status.as_deref(), Some("nothing to undo"), "its own history: none");

    keys(&mut shown, "p");
    settle(&mut shown, 120, 30);
    keys(&mut shown, "g");
    press(&mut shown, KeyCode::Enter);
    until_open(&mut shown);
    keys(&mut shown, "U");
    let screen = shown.screen().expect("back on uncommitted");
    assert_eq!(screen.capture.scope, Scope::Uncommitted);
    assert_eq!(screen.status.as_deref(), Some("note restored"));
    assert_eq!(screen.store.annotations().len(), 1, "the note is back");
}