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
//! Marking commits in the change picker and reviewing them as one range, against real
//! repositories (the review tests' fixture), drawn on a `TestBackend`.
#![allow(clippy::expect_used, clippy::indexing_slicing, clippy::panic, reason = "tests assert by panicking")]

use plannotator_tui_review::Scope;
use ratatui::crossterm::event::KeyCode;

use super::shown::Shown;
use super::tests::{commit, draw, keys, picking, position, press, row_with, settle, until_open};
use crate::review::tests::{Fixture, at, type_and_save};

/// The fixture with three commits on top of `init`, newest first in the picker:
/// `change b` (rows[3]), `add b` (rows[4]), `app after` (rows[5]), then `init`. `b.txt` is
/// added in one and changed in a later one. Returns the three ids, oldest first.
fn three_commits(tag: &str) -> (Fixture, [String; 3]) {
    let fixture = Fixture::new(tag);
    commit(&fixture, "app after");
    fixture.write("b.txt", "first\n");
    commit(&fixture, "add b");
    fixture.write("b.txt", "first\nsecond\n");
    commit(&fixture, "change b");
    let shas = [rev(&fixture, "HEAD~2"), rev(&fixture, "HEAD~1"), rev(&fixture, "HEAD")];
    (fixture, shas)
}

/// The full id `rev` names.
fn rev(fixture: &Fixture, rev: &str) -> String {
    let out = std::process::Command::new("git")
        .arg("-C")
        .arg(fixture.repo())
        .args(["rev-parse", rev])
        .output()
        .expect("git");
    String::from_utf8_lossy(&out.stdout).trim().to_owned()
}

/// Open the range from `change b` back to `app after` from the picker on screen.
fn open_range(shown: &mut Shown, rows: &[String]) {
    go_to(shown, rows, "change b");
    space(shown);
    keys(shown, "jj");
    space(shown);
    press(shown, KeyCode::Enter);
    until_open(shown);
}

fn short(sha: &str) -> &str {
    sha.get(..7).expect("sha")
}

fn space(shown: &mut Shown) {
    press(shown, KeyCode::Char(' '));
}

/// The cursor on the row whose text holds `subject`, from the top.
fn go_to(shown: &mut Shown, rows: &[String], subject: &str) {
    let top = position(rows, "Uncommitted");
    keys(shown, "g");
    keys(shown, &"j".repeat(position(rows, subject) - top));
}

fn footer_has(rows: &[String], text: &str) -> bool {
    rows.iter().any(|r| r.contains(text))
}

#[test]
fn space_marks_and_unmarks_a_commit_and_the_rows_between_two_marks_show_as_the_range() {
    let (fixture, [oldest, _, newest]) = three_commits("pick-range-marks");
    let mut shown = picking(&fixture);
    let rows = settle(&mut shown, 120, 20);
    assert!(!rows.iter().any(|r| r.contains('●')), "nothing marked yet:\n{}", rows.join("\n"));

    go_to(&mut shown, &rows, "change b");
    space(&mut shown);
    let rows = draw(&mut shown, 120, 20);
    assert!(row_with(&rows, "change b").contains("● "), "{}", rows.join("\n"));
    let one = format!(
        "Commit {} marked · enter reviews it · space on another commit marks a range",
        short(&newest)
    );
    assert!(footer_has(&rows, &one), "{}", rows.join("\n"));

    keys(&mut shown, "jj"); // `app after`
    space(&mut shown);
    let rows = settle(&mut shown, 120, 20); // the count comes from the background
    assert!(row_with(&rows, "change b").contains("● "), "{}", rows.join("\n"));
    assert!(row_with(&rows, "add b").contains("│ "), "the row between is in the range:\n{}", rows.join("\n"));
    assert!(row_with(&rows, "app after").contains("● "), "{}", rows.join("\n"));
    assert!(!row_with(&rows, "init").contains('│'), "{}", rows.join("\n"));
    let range = format!(
        "3 commits {}..{} · enter reviews them as one · esc clears the marks",
        short(&oldest),
        short(&newest)
    );
    assert!(footer_has(&rows, &range), "{}", rows.join("\n"));

    // `r` keeps the marks.
    keys(&mut shown, "r");
    let rows = settle(&mut shown, 120, 20);
    assert!(footer_has(&rows, &range), "{}", rows.join("\n"));

    // Unmarking the oldest leaves the newest marked alone; unmarking it too, nothing.
    space(&mut shown);
    let rows = draw(&mut shown, 120, 20);
    assert!(footer_has(&rows, &one), "{}", rows.join("\n"));
    assert!(!row_with(&rows, "add b").contains('│'), "{}", rows.join("\n"));
    go_to(&mut shown, &rows, "change b");
    space(&mut shown);
    let rows = draw(&mut shown, 120, 20);
    assert!(!rows.iter().any(|r| r.contains("marked") || r.contains('●')), "{}", rows.join("\n"));
    assert!(row_with(&rows, "change b").starts_with("▸ "), "the mark column went with the marks");
}

#[test]
fn space_on_a_row_that_is_not_a_commit_says_why_and_esc_clears_the_marks_first() {
    let (fixture, _) = three_commits("pick-range-not-commit");
    let mut shown = picking(&fixture);
    let rows = settle(&mut shown, 120, 20);
    keys(&mut shown, "g"); // Uncommitted
    space(&mut shown);
    let rows_after = draw(&mut shown, 120, 20);
    assert!(
        footer_has(&rows_after, "only commits can be marked; Uncommitted is not one"),
        "{}",
        rows_after.join("\n")
    );
    assert!(shown.picker_mut().expect("picking").marks.is_empty());

    go_to(&mut shown, &rows, "add b");
    space(&mut shown);
    assert!(!shown.picker_mut().expect("picking").marks.is_empty());
    press(&mut shown, KeyCode::Esc);
    let rows = draw(&mut shown, 120, 20);
    assert!(shown.picker_mut().expect("still picking").marks.is_empty(), "esc cleared the marks");
    assert!(!rows.iter().any(|r| r.contains('●') || r.contains("q quits")), "{}", rows.join("\n"));
    press(&mut shown, KeyCode::Esc);
    let rows = draw(&mut shown, 120, 20);
    assert!(footer_has(&rows, "q quits"), "then esc does what it did: {}", rows.join("\n"));
}

#[test]
fn enter_opens_the_cursor_row_with_no_mark_the_marked_commit_with_one_and_the_range_with_two() {
    let (fixture, [oldest, middle, newest]) = three_commits("pick-range-enter");
    let mut shown = picking(&fixture);
    let rows = settle(&mut shown, 120, 20);

    // No mark: the row under the cursor, as before.
    go_to(&mut shown, &rows, "add b");
    press(&mut shown, KeyCode::Enter);
    until_open(&mut shown);
    assert_eq!(shown.screen().expect("opened").capture.scope, Scope::Commit { sha: middle });

    // One mark: that commit, wherever the cursor is.
    keys(&mut shown, "p");
    let rows = settle(&mut shown, 120, 30);
    go_to(&mut shown, &rows, "app after");
    space(&mut shown);
    keys(&mut shown, "g");
    press(&mut shown, KeyCode::Enter);
    until_open(&mut shown);
    assert_eq!(shown.screen().expect("opened").capture.scope, Scope::Commit { sha: oldest.clone() });

    // Two marks, newest first or oldest first: the same range.
    let range = Scope::Range { from: oldest.clone(), to: newest.clone() };
    keys(&mut shown, "p");
    let rows = settle(&mut shown, 120, 30);
    go_to(&mut shown, &rows, "change b");
    space(&mut shown);
    keys(&mut shown, "jj");
    space(&mut shown);
    let picker = shown.picker_mut().expect("picking");
    assert_eq!(picker.marks.range(&picker.rows).as_ref(), Some(&range));
    press(&mut shown, KeyCode::Esc);
    space(&mut shown); // `app after` first
    keys(&mut shown, "kk");
    space(&mut shown); // then `change b`
    let picker = shown.picker_mut().expect("picking");
    assert_eq!(picker.marks.range(&picker.rows).as_ref(), Some(&range));
    press(&mut shown, KeyCode::Enter);
    until_open(&mut shown);
    let screen = shown.screen_mut().expect("the range's review");
    assert_eq!(screen.capture.scope, range);
    assert_eq!(screen.capture.commits, Some(3));
    let frame = crate::review::tests::frame(screen, 120, 30);
    let header = format!("3 commits {}..{}", short(&oldest), short(&newest));
    assert!(frame.iter().any(|r| r.contains(&header)), "the header names it:\n{}", frame.join("\n"));
    let files: Vec<&str> = screen.capture.files.iter().map(|f| f.path.as_str()).collect();
    assert_eq!(files, ["app.py", "b.txt"]);
    // What the agent gets names the range too.
    crate::review::tests::settle(screen);
    at(screen, 1);
    crate::review::tests::keys(screen, "c");
    type_and_save(screen, "why sys?");
    let notes: Vec<_> = screen.store.annotations().iter().collect();
    let feedback = screen.feedback(&notes);
    let names = format!("Commits `{}` through `{}` (3 commits", short(&oldest), short(&newest));
    assert!(feedback.contains(&names), "{feedback}");
}

#[test]
fn notes_on_a_range_come_back_when_it_is_reopened_or_reloaded_and_stay_off_its_commits() {
    let (fixture, [oldest, middle, newest]) = three_commits("pick-range-notes");
    let mut shown = picking(&fixture);
    let rows = settle(&mut shown, 120, 30);
    open_range(&mut shown, &rows);
    {
        let screen = shown.screen_mut().expect("the range's review");
        crate::review::tests::settle(screen);
        at(screen, 1);
        crate::review::tests::keys(screen, "c");
        type_and_save(screen, "one note for the range");
        assert_eq!(screen.store.annotations().len(), 1);
        // `r` reads the range again and keeps the note.
        crate::review::tests::keys(screen, "r");
        assert!(screen.status.as_deref().is_some_and(|s| s.starts_with("reloaded")), "{:?}", screen.status);
        assert_eq!(screen.capture.scope, Scope::Range { from: oldest.clone(), to: newest.clone() });
        assert_eq!(screen.store.annotations().len(), 1);
    }

    // A commit of the range alone is a review of its own, without the range's note. The
    // picker over the range shows it marked.
    keys(&mut shown, "p");
    let rows = settle(&mut shown, 120, 30);
    assert_eq!(marked(&mut shown), [short(&newest).to_owned(), short(&oldest).to_owned()]);
    go_to(&mut shown, &rows, "add b");
    press(&mut shown, KeyCode::Enter);
    until_open(&mut shown);
    let screen = shown.screen_mut().expect("the commit's review");
    assert_eq!(screen.capture.scope, Scope::Commit { sha: middle.clone() });
    assert_eq!(screen.store.annotations().len(), 0, "the range's note is not on its commit");
    let frame = crate::review::tests::frame(screen, 120, 30);
    let kept = format!("1 unsent note kept on 3 commits {}..{}", short(&oldest), short(&newest));
    assert!(frame.iter().any(|r| r.contains(&kept)), "{}", frame.join("\n"));

    // The same range again: its note is back.
    keys(&mut shown, "p");
    let rows = settle(&mut shown, 120, 30);
    open_range(&mut shown, &rows);
    let screen = shown.screen().expect("the range again");
    assert_eq!(screen.capture.scope, Scope::Range { from: oldest.clone(), to: newest.clone() });
    let notes: Vec<Option<&str>> = screen.store.annotations().iter().map(|n| n.text.as_deref()).collect();
    assert_eq!(notes, [Some("one note for the range")]);

    // Quitting from another review names the range, and "go to it" opens the picker with its
    // ends marked.
    keys(&mut shown, "p");
    let rows = settle(&mut shown, 120, 30);
    go_to(&mut shown, &rows, "add b");
    press(&mut shown, KeyCode::Enter);
    until_open(&mut shown);
    keys(&mut shown, "q");
    let rows = draw(&mut shown, 160, 30);
    let prompt = rows.last().expect("footer");
    let named = format!("1 unsent note on 3 commits {}..{}", short(&oldest), short(&newest));
    assert!(prompt.contains(&named) && prompt.contains("esc go to it"), "{prompt}");
    press(&mut shown, KeyCode::Esc);
    let rows = settle(&mut shown, 120, 30);
    assert_eq!(
        marked(&mut shown),
        [short(&newest).to_owned(), short(&oldest).to_owned()],
        "{}",
        rows.join("\n")
    );
    assert!(row_with(&rows, "change b").contains("▸ ● "), "{}", rows.join("\n"));
}

/// `count` more commits on top of the fixture's, `change 0` the oldest.
fn many_commits(tag: &str, count: usize) -> Fixture {
    let fixture = Fixture::new(tag);
    for n in 0..count {
        fixture.write("notes.txt", &format!("{n}\n"));
        commit(&fixture, &format!("change {n}"));
    }
    fixture
}

fn marked(shown: &mut Shown) -> Vec<String> {
    let picker = shown.picker_mut().expect("picking");
    picker
        .rows
        .iter()
        .filter(|row| picker.marks.has(row))
        .map(|row| row.title().0.unwrap_or_default())
        .collect()
}

#[test]
fn enter_on_more_lists_older_commits_with_a_mark_and_a_range_reaches_past_the_first_page() {
    let fixture = many_commits("pick-range-more", 24);
    let mut shown = picking(&fixture);
    let rows = settle(&mut shown, 120, 40);
    assert!(rows.iter().any(|r| r.contains("more…")) && !rows.iter().any(|r| r.contains("change 0 ")));
    go_to(&mut shown, &rows, "change 23");
    space(&mut shown);
    keys(&mut shown, "G"); // more…
    press(&mut shown, KeyCode::Enter);
    let rows = settle(&mut shown, 120, 40);
    assert!(shown.screen().is_none(), "nothing opened");
    assert!(rows.iter().any(|r| r.contains("change 0 ")), "older commits listed:\n{}", rows.join("\n"));
    assert!(row_with(&rows, "change 23").contains("● "), "the mark stayed:\n{}", rows.join("\n"));

    go_to(&mut shown, &rows, "change 1 ");
    space(&mut shown);
    let rows = settle(&mut shown, 120, 40);
    assert!(rows.iter().any(|r| r.contains("23 commits")), "{}", rows.join("\n"));
    press(&mut shown, KeyCode::Enter);
    until_open(&mut shown);
    let screen = shown.screen().expect("the range's review");
    assert!(matches!(screen.capture.scope, Scope::Range { .. }), "{:?}", screen.capture.scope);
    assert_eq!(screen.capture.commits, Some(23));
}

#[test]
fn the_picker_over_a_range_starts_with_its_ends_marked_even_past_the_first_page() {
    let fixture = many_commits("pick-range-over", 24);
    let mut shown = picking(&fixture);
    let rows = settle(&mut shown, 120, 40);
    go_to(&mut shown, &rows, "change 23");
    space(&mut shown);
    keys(&mut shown, "G");
    press(&mut shown, KeyCode::Enter);
    let rows = settle(&mut shown, 120, 40);
    go_to(&mut shown, &rows, "change 1 ");
    space(&mut shown);
    press(&mut shown, KeyCode::Enter);
    until_open(&mut shown);

    // `p` over the range's review: only the first page is listed at first, and the older
    // ones follow until the oldest end is found.
    keys(&mut shown, "p");
    let rows = settle(&mut shown, 120, 40);
    let short_of = |subject: &str| {
        let row = row_with(&rows, subject);
        row.split_whitespace()
            .find(|w| w.len() == 7 && w.chars().all(|c| c.is_ascii_hexdigit()))
            .map(str::to_owned)
    };
    let ends = [short_of("change 23").expect("sha"), short_of("change 1 ").expect("sha")];
    assert_eq!(marked(&mut shown), ends, "{}", rows.join("\n"));
    assert!(row_with(&rows, "change 23").contains("▸ ● "), "the cursor on the newest:\n{}", rows.join("\n"));
    assert!(rows.iter().any(|r| r.contains("23 commits")), "{}", rows.join("\n"));
    // Enter there goes back to it, as the row of the open review does.
    press(&mut shown, KeyCode::Enter);
    assert!(shown.picker_mut().is_none(), "back on the range's review");

    // On a narrow box the keys and quit keep their place; `space mark` gives way first.
    let rows = settle(&mut shown, 72, 30);
    assert!(!rows.iter().any(|r| r.contains("pick what to review")));
    keys(&mut shown, "p");
    let rows = settle(&mut shown, 72, 30);
    assert!(rows.iter().any(|r| r.contains("? keys · q quit")), "{}", rows.join("\n"));
}

#[test]
fn r_finds_a_marked_commit_new_commits_pushed_down_and_says_when_one_left_the_branch() {
    let fixture = many_commits("pick-range-reload", 19);
    let mut shown = picking(&fixture);
    let rows = settle(&mut shown, 120, 40);
    go_to(&mut shown, &rows, "change 0");
    space(&mut shown);
    for n in 0..3 {
        fixture.write("notes.txt", &format!("new {n}\n"));
        commit(&fixture, &format!("new {n}"));
    }
    keys(&mut shown, "r");
    let rows = settle(&mut shown, 120, 40);
    assert!(row_with(&rows, "change 0").contains("● "), "still marked:\n{}", rows.join("\n"));
    assert!(!rows.iter().any(|r| r.contains("not in the list")), "{}", rows.join("\n"));

    // The newest commit, marked, then reset away: unmarked, and the footer says so.
    go_to(&mut shown, &rows, "new 2");
    space(&mut shown);
    let sha = shown.picker_mut().expect("picking").rows.iter().find_map(|row| match row.title() {
        (Some(sha), subject) if subject == "new 2" => Some(sha),
        _ => None,
    });
    fixture.git(&["reset", "-q", "--hard", "HEAD~1"]);
    keys(&mut shown, "r");
    let rows = settle(&mut shown, 120, 40);
    let said = format!("commit {} is not in the list, so it is not marked", sha.expect("sha"));
    assert!(rows.iter().any(|r| r.contains(&said)), "{}", rows.join("\n"));
    assert_eq!(marked(&mut shown).len(), 1, "change 0 is still marked");
}

/// Commit everything with a fixed date, so `git log` lists the commits in a known order.
fn commit_at(fixture: &Fixture, message: &str, day: u32) {
    fixture.git(&["add", "-A"]);
    let date = format!("2030-01-{day:02}T12:00:00Z");
    let status = std::process::Command::new("git")
        .arg("-C")
        .arg(fixture.repo())
        .args(["-c", "user.name=Ada", "-c", "user.email=a@example.com", "commit", "-qm", message])
        .env("GIT_AUTHOR_DATE", &date)
        .env("GIT_COMMITTER_DATE", &date)
        .status()
        .expect("git");
    assert!(status.success());
}

#[test]
fn across_a_merge_the_footer_counts_the_commits_the_review_will_not_the_rows_between() {
    let fixture = Fixture::new("pick-range-merge");
    commit_at(&fixture, "main one", 1);
    fixture.git(&["checkout", "-q", "-b", "side"]);
    fixture.write("side.txt", "side\n");
    commit_at(&fixture, "side one", 2);
    fixture.git(&["checkout", "-q", "main"]);
    fixture.write("notes.txt", "main two\n");
    commit_at(&fixture, "main two", 3);
    let date = "2030-01-04T12:00:00Z";
    let merged = std::process::Command::new("git")
        .arg("-C")
        .arg(fixture.repo())
        .args(["-c", "user.name=Ada", "-c", "user.email=a@example.com", "merge", "-q", "--no-edit", "side"])
        .env("GIT_AUTHOR_DATE", date)
        .env("GIT_COMMITTER_DATE", date)
        .status()
        .expect("git");
    assert!(merged.success());

    let mut shown = picking(&fixture);
    let rows = settle(&mut shown, 120, 20);
    // Newest first by date: the merge, main two, side one, main one.
    let order = ["main two", "side one", "main one"].map(|t| position(&rows, t));
    assert!(order.windows(2).all(|w| w[0] < w[1]), "{}", rows.join("\n"));

    // main one .. main two: three rows, but side one is not in it.
    go_to(&mut shown, &rows, "main two");
    space(&mut shown);
    go_to(&mut shown, &rows, "main one");
    space(&mut shown);
    let rows = settle(&mut shown, 120, 20);
    assert!(row_with(&rows, "side one").contains("│ "), "{}", rows.join("\n"));
    assert!(rows.iter().any(|r| r.contains("2 commits ")), "{}", rows.join("\n"));
    press(&mut shown, KeyCode::Enter);
    until_open(&mut shown);
    assert_eq!(shown.screen().expect("opened").capture.commits, Some(2), "the header counts the same");

    // side one .. main two are on different lines of history: the footer says so at once.
    keys(&mut shown, "p");
    let rows = settle(&mut shown, 120, 30);
    // The open range shows marked: space on `main one` makes the marks the person's and
    // unmarks it, leaving `main two`.
    go_to(&mut shown, &rows, "main one");
    space(&mut shown);
    go_to(&mut shown, &rows, "side one");
    space(&mut shown);
    let rows = settle(&mut shown, 120, 30);
    assert!(rows.iter().any(|r| r.contains("does not come before")), "{}", rows.join("\n"));
}

#[test]
fn over_a_range_enter_goes_back_from_its_newest_commit_opens_any_other_row_and_esc_leaves_at_once() {
    let (fixture, [oldest, middle, newest]) = three_commits("pick-range-shown");
    let mut shown = picking(&fixture);
    let rows = settle(&mut shown, 120, 30);
    open_range(&mut shown, &rows);
    let range = Scope::Range { from: oldest.clone(), to: newest.clone() };

    // `p` then Enter: back to the range, untouched.
    keys(&mut shown, "p");
    let rows = settle(&mut shown, 120, 30);
    let open = format!("open: 3 commits {}..{} · space changes the range", short(&oldest), short(&newest));
    assert!(footer_has(&rows, &open), "{}", rows.join("\n"));
    assert!(!footer_has(&rows, "reviews them as one"), "{}", rows.join("\n"));
    press(&mut shown, KeyCode::Enter);
    assert!(shown.picker_mut().is_none(), "back on the range");
    assert_eq!(shown.screen().expect("review").capture.scope, range);

    // `p` then Esc: one press leaves.
    keys(&mut shown, "p");
    settle(&mut shown, 120, 30);
    press(&mut shown, KeyCode::Esc);
    assert!(shown.picker_mut().is_none(), "esc left at once");

    // `p`, another row, Enter: that row.
    keys(&mut shown, "p");
    let rows = settle(&mut shown, 120, 30);
    go_to(&mut shown, &rows, "add b");
    press(&mut shown, KeyCode::Enter);
    until_open(&mut shown);
    assert_eq!(shown.screen().expect("review").capture.scope, Scope::Commit { sha: middle });
}

#[test]
fn over_a_range_space_makes_the_marks_your_own_and_enter_opens_the_range_you_changed() {
    let (fixture, [_, _, newest]) = three_commits("pick-range-widen");
    let mut shown = picking(&fixture);
    let rows = settle(&mut shown, 120, 30);
    open_range(&mut shown, &rows);
    keys(&mut shown, "p");
    let rows = settle(&mut shown, 120, 30);
    go_to(&mut shown, &rows, "init");
    space(&mut shown);
    let rows = settle(&mut shown, 120, 30);
    assert!(
        footer_has(&rows, "4 commits") && footer_has(&rows, "enter reviews them as one"),
        "{}",
        rows.join("\n")
    );
    press(&mut shown, KeyCode::Enter);
    until_open(&mut shown);
    let root = rev(&fixture, "HEAD~3");
    assert_eq!(shown.screen().expect("review").capture.scope, Scope::Range { from: root, to: newest });
}

#[test]
fn go_to_it_on_a_commit_from_a_range_review_opens_that_commit_not_the_range() {
    let (fixture, [_, _, newest]) = three_commits("pick-range-go-to-commit");
    let mut shown = picking(&fixture);
    let rows = settle(&mut shown, 120, 30);
    go_to(&mut shown, &rows, "change b");
    press(&mut shown, KeyCode::Enter);
    until_open(&mut shown);
    {
        let screen = shown.screen_mut().expect("the commit's review");
        crate::review::tests::settle(screen);
        crate::review::tests::keys(screen, "M");
        type_and_save(screen, "a note on the commit");
    }
    keys(&mut shown, "p");
    let rows = settle(&mut shown, 120, 30);
    open_range(&mut shown, &rows);
    keys(&mut shown, "q");
    let rows = draw(&mut shown, 160, 30);
    let prompt = rows.last().expect("footer");
    assert!(prompt.contains(&format!("1 unsent note on Commit {}", short(&newest))), "{prompt}");
    press(&mut shown, KeyCode::Esc); // go to it
    let rows = settle(&mut shown, 120, 30);
    assert!(marked(&mut shown).is_empty(), "the range is not shown:\n{}", rows.join("\n"));
    assert!(row_with(&rows, "change b").contains("▸ "), "{}", rows.join("\n"));
    press(&mut shown, KeyCode::Enter);
    until_open(&mut shown);
    assert_eq!(shown.screen().expect("review").capture.scope, Scope::Commit { sha: newest });
}

#[test]
fn a_range_whose_oldest_end_is_past_the_page_limit_shows_no_marks_and_says_why() {
    let fixture = Fixture::new("pick-range-deep");
    commit(&fixture, "first");
    let root = rev(&fixture, "HEAD~1");
    for n in 0..230 {
        fixture.git(&[
            "-c",
            "user.name=t",
            "-c",
            "user.email=t@example.com",
            "commit",
            "-q",
            "--allow-empty",
            "-m",
            &format!("empty {n}"),
        ]);
    }
    fixture.write("notes.txt", "last\n");
    commit(&fixture, "last");
    let head = rev(&fixture, "HEAD");
    let mut shown = picking(&fixture);
    settle(&mut shown, 120, 30);
    shown.picker_mut().expect("picking").keep_at(Scope::Range { from: root.clone(), to: head.clone() });
    // While the older pages are listed, the footer names the range, not the one end listed.
    let rows = draw(&mut shown, 120, 30);
    let listing = format!("Commits {}..{} · listing older commits…", short(&root), short(&head));
    assert!(footer_has(&rows, &listing), "{}", rows.join("\n"));
    assert!(!footer_has(&rows, "reviews it"), "{}", rows.join("\n"));
    let rows = settle(&mut shown, 120, 30);
    assert!(marked(&mut shown).is_empty(), "not one mark of a range:\n{}", rows.join("\n"));
    let said = format!("commit {} is not in the list, so it is not marked", short(&root));
    assert!(footer_has(&rows, &said), "{}", rows.join("\n")); // Enter opens the row under the cursor, the range's newest commit, not the range.
    assert!(row_with(&rows, "last").starts_with("▸ "), "{}", rows.join("\n"));
    press(&mut shown, KeyCode::Enter);
    until_open(&mut shown);
    assert_eq!(shown.screen().expect("review").capture.scope, Scope::Commit { sha: head });
}