#![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};
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)
}
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()
}
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(' '));
}
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"); space(&mut shown);
let rows = settle(&mut shown, 120, 20); 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"));
keys(&mut shown, "r");
let rows = settle(&mut shown, 120, 20);
assert!(footer_has(&rows, &range), "{}", rows.join("\n"));
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"); 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);
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 });
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() });
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); keys(&mut shown, "kk");
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::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"]);
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(¬es);
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);
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);
}
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"));
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")]);
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"));
}
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"); 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);
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"));
press(&mut shown, KeyCode::Enter);
assert!(shown.picker_mut().is_none(), "back on the range's review");
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"));
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");
}
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);
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"));
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");
keys(&mut shown, "p");
let rows = settle(&mut shown, 120, 30);
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() };
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);
keys(&mut shown, "p");
settle(&mut shown, 120, 30);
press(&mut shown, KeyCode::Esc);
assert!(shown.picker_mut().is_none(), "esc left at once");
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); 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() });
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")); 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 });
}