use assert_cmd::Command; use assert_fs::TempDir; use assert_fs::fixture::{PathChild, PathCreateDir}; use std::fs; use std::path::{Path, PathBuf}; const BIN_NAME: &str = env!("CARGO_BIN_EXE_worklane"); const UUID: &str = "475534a3-8a0d-45cb-9a97-39c251dd8e39"; fn setup_worklane(temp: &TempDir) -> PathBuf { let worklane = temp.child(".worklane"); worklane.create_dir_all().unwrap(); for dir in ["backlog", "committed", "reviewing", "done"] { worklane.child(dir).create_dir_all().unwrap(); } worklane.path().to_path_buf() } /// One criterion already ticked, one handwritten line inside the section, and a /// checkbox in the description that must not be counted as a criterion. fn create_ticket(worklane: &Path) { // Written without line continuations on purpose: a trailing `\` strips the next // line's leading whitespace, so the indented line would arrive unindented and the // test would prove nothing about preserving it. let lines = [ "# Check me", "", "## Description", "- [ ] A checkbox in the description, which is not a criterion.", "", "## Acceptance Criteria", "- [ ] alpha shared", "- [x] beta alone", " A handwritten line inside the section.", "- [ ] gamma shared", "", "## Implementation Plan", "1. Step one", "", "## Metadata", "", &format!("- **ID**: {}", UUID), "- **Status**: backlog", "", ]; fs::write( worklane.join("backlog").join(format!("{}_check-me.md", UUID)), lines.join("\n"), ) .unwrap(); } /// A second ticket whose criteria carry the word `all`, so the keyword's precedence /// can be pinned without renumbering the shared fixture. const ALL_UUID: &str = "6b1f9c22-0d4e-4a1b-9f37-2c85ab4d1e70"; fn create_all_in_text_ticket(worklane: &Path) { let lines = [ "# All in the text", "", "## Acceptance Criteria", "- [ ] all the boxes are listed", "- [ ] the footer is drawn", "", "## Metadata", "", &format!("- **ID**: {}", ALL_UUID), "- **Status**: backlog", "", ]; fs::write( worklane .join("backlog") .join(format!("{}_all-in-the-text.md", ALL_UUID)), lines.join("\n"), ) .unwrap(); } fn all_in_text_ticket(worklane: &Path) -> String { fs::read_to_string( worklane .join("backlog") .join(format!("{}_all-in-the-text.md", ALL_UUID)), ) .unwrap() } fn run(temp: &TempDir, args: &[&str]) -> std::process::Output { let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()).args(args); cmd.assert().get_output().clone() } fn ticket(worklane: &Path) -> String { fs::read_to_string(worklane.join("backlog").join(format!("{}_check-me.md", UUID))).unwrap() } /// The whole point: one line changes and every other keeps its state. #[test] fn check_ticks_one_criterion_by_index() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane); let out = run(&temp, &["check", UUID, "1"]); assert!(out.status.success(), "{:?}", String::from_utf8_lossy(&out.stderr)); let content = ticket(&worklane); assert!(content.contains("- [x] alpha shared\n"), "{content}"); assert!(content.contains("- [x] beta alone\n"), "beta lost its tick: {content}"); assert!(content.contains("- [ ] gamma shared\n"), "{content}"); } #[test] fn check_ticks_one_criterion_by_substring() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane); run(&temp, &["check", UUID, "GAMMA"]); assert!(ticket(&worklane).contains("- [x] gamma shared\n")); } #[test] fn uncheck_unticks_one_criterion() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane); run(&temp, &["uncheck", UUID, "2"]); let content = ticket(&worklane); assert!(content.contains("- [ ] beta alone\n"), "{content}"); assert!(content.contains("- [ ] alpha shared\n"), "{content}"); } /// The handwritten line sits between two criteria, so it is the thing most likely to /// be lost by a rewrite, and it must not take a number either. #[test] fn a_handwritten_line_in_the_section_survives_and_is_not_numbered() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane); // gamma is the third criterion even though it is the fourth line of the section run(&temp, &["check", UUID, "3"]); let content = ticket(&worklane); assert!(content.contains("- [x] gamma shared\n"), "{content}"); assert!( content.contains(" A handwritten line inside the section.\n"), "the handwritten line was lost or reindented: {content}" ); } /// A checkbox outside the section is not a criterion, so the numbering starts inside it. #[test] fn a_checkbox_in_the_description_is_not_a_criterion() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane); run(&temp, &["check", UUID, "1"]); let content = ticket(&worklane); assert!( content.contains("- [ ] A checkbox in the description, which is not a criterion.\n"), "the description checkbox was ticked: {content}" ); } #[test] fn checking_an_already_checked_criterion_changes_nothing() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane); let before = ticket(&worklane); let out = run(&temp, &["check", UUID, "2"]); assert!(out.status.success()); assert!( String::from_utf8_lossy(&out.stdout).contains("Already checked"), "{:?}", String::from_utf8_lossy(&out.stdout) ); assert_eq!(ticket(&worklane), before, "the file was rewritten anyway"); } /// The listed numbers have to be the real ones, or the advice to use the number points /// at the wrong criterion. alpha and gamma are 1 and 3, never 1 and 2. #[test] fn an_ambiguous_substring_lists_the_matches_with_their_real_numbers() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane); let before = ticket(&worklane); let out = run(&temp, &["check", UUID, "shared"]); let stderr = String::from_utf8_lossy(&out.stderr); assert_eq!(out.status.code(), Some(1)); assert!(stderr.contains("matches 2 criteria"), "{stderr:?}"); assert!(stderr.contains("1. [ ] alpha shared"), "{stderr:?}"); assert!(stderr.contains("3. [ ] gamma shared"), "{stderr:?}"); assert!(!stderr.contains("2. [ ] gamma"), "renumbered: {stderr:?}"); assert_eq!(ticket(&worklane), before); } #[test] fn a_substring_matching_nothing_is_an_error_listing_the_criteria() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane); let out = run(&temp, &["check", UUID, "nowhere"]); let stderr = String::from_utf8_lossy(&out.stderr); assert_eq!(out.status.code(), Some(1)); assert!(stderr.contains("No criterion"), "{stderr:?}"); assert!(stderr.contains("1. [ ] alpha shared"), "{stderr:?}"); } /// A number out of range is a bad index, not a substring that happened to miss. #[test] fn an_index_out_of_range_says_so() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane); let out = run(&temp, &["check", UUID, "9"]); let stderr = String::from_utf8_lossy(&out.stderr); assert_eq!(out.status.code(), Some(1)); assert!(stderr.contains("is not one of the 3 criteria"), "{stderr:?}"); } /// No terminal to prompt at, so the list comes back as an error instead. This is the /// path the opencode wrapper takes, and it must not block on stdin. #[test] fn omitting_the_criterion_without_a_terminal_returns_the_list_as_an_error() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane); let before = ticket(&worklane); let out = run(&temp, &["check", UUID]); let stderr = String::from_utf8_lossy(&out.stderr); assert_eq!(out.status.code(), Some(1)); assert!(stderr.contains("Which criterion"), "{stderr:?}"); assert!(stderr.contains("1. [ ] alpha shared"), "{stderr:?}"); assert!(stderr.contains("2. [x] beta alone"), "{stderr:?}"); assert!(stderr.contains("3. [ ] gamma shared"), "{stderr:?}"); assert_eq!(ticket(&worklane), before); } #[test] fn a_ticket_with_no_criteria_says_so() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); fs::write( worklane.join("backlog").join("aaaaaaaa-0000-4000-8000-000000000000_bare.md"), "# Bare\n\n## Acceptance Criteria\n_No acceptance criteria provided._\n\n## Metadata\n\n- **ID**: aaaaaaaa-0000-4000-8000-000000000000\n- **Status**: backlog\n", ) .unwrap(); let out = run(&temp, &["check", "aaaaaaaa", "1"]); assert_eq!(out.status.code(), Some(1)); assert!( String::from_utf8_lossy(&out.stderr).contains("no acceptance criteria"), "{:?}", String::from_utf8_lossy(&out.stderr) ); } /// show numbers the criteria, so the index check takes is visible where it is read. #[test] fn show_numbers_the_criteria() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane); let out = run(&temp, &["show", UUID]); let stdout = String::from_utf8_lossy(&out.stdout); assert!(stdout.contains("1. [ ] alpha shared"), "{stdout:?}"); assert!(stdout.contains("2. [x] beta alone"), "{stdout:?}"); assert!(stdout.contains("3. [ ] gamma shared"), "{stdout:?}"); // The handwritten line is shown but takes no number assert!(stdout.contains(" A handwritten line inside the section."), "{stdout:?}"); assert!(!stdout.contains("4. ["), "the handwritten line was numbered: {stdout:?}"); // And the description checkbox is left as it is, outside the numbering assert!(stdout.contains("- [ ] A checkbox in the description"), "{stdout:?}"); } #[test] fn check_leaves_nothing_behind() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane); run(&temp, &["check", UUID, "1"]); let strays: Vec = fs::read_dir(&worklane) .unwrap() .filter_map(|e| e.ok()) .map(|e| e.file_name().into_string().unwrap()) .filter(|name| { !["backlog", "committed", "reviewing", "done", "history"].contains(&name.as_str()) }) .collect(); assert_eq!(strays, Vec::::new()); } /// The common case the batch exists for: the work is done, every box is true. #[test] fn all_ticks_every_criterion() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane); let out = run(&temp, &["check", UUID, "all"]); assert!(out.status.success(), "{:?}", String::from_utf8_lossy(&out.stderr)); let content = ticket(&worklane); assert!(content.contains("- [x] alpha shared\n"), "{content}"); assert!(content.contains("- [x] beta alone\n"), "{content}"); assert!(content.contains("- [x] gamma shared\n"), "{content}"); // beta was already ticked, so it is reported unchanged rather than as a new mark let stdout = String::from_utf8_lossy(&out.stdout); assert!(stdout.contains("Checked 1: alpha shared"), "{stdout:?}"); assert!(stdout.contains("Already checked 2: beta alone"), "{stdout:?}"); assert!(stdout.contains("Checked 3: gamma shared"), "{stdout:?}"); // And nothing outside the section was touched assert!( content.contains("- [ ] A checkbox in the description, which is not a criterion.\n"), "the description checkbox was ticked: {content}" ); assert!( content.contains(" A handwritten line inside the section.\n"), "the handwritten line was lost: {content}" ); } /// An index and a substring in one call, each addressed by the existing rules. #[test] fn one_call_takes_several_mixed_terms() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane); let out = run(&temp, &["check", UUID, "3", "ALPHA"]); assert!(out.status.success(), "{:?}", String::from_utf8_lossy(&out.stderr)); let content = ticket(&worklane); assert!(content.contains("- [x] alpha shared\n"), "{content}"); assert!(content.contains("- [x] gamma shared\n"), "{content}"); // Reported in show's order whatever order the terms arrived in let stdout = String::from_utf8_lossy(&out.stdout); let alpha = stdout.find("Checked 1: alpha"); let gamma = stdout.find("Checked 3: gamma"); assert!( matches!((alpha, gamma), (Some(alpha), Some(gamma)) if alpha < gamma), "reported out of order, or one of them is missing: {stdout:?}" ); } /// The same criterion named twice, once by number and once by text, is one mark and /// one line of output. #[test] fn a_criterion_named_twice_is_reported_once() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane); let out = run(&temp, &["check", UUID, "1", "alpha", "all"]); assert!(out.status.success(), "{:?}", String::from_utf8_lossy(&out.stderr)); let stdout = String::from_utf8_lossy(&out.stdout); assert_eq!( stdout.matches("alpha shared").count(), 1, "alpha was reported more than once: {stdout:?}" ); assert_eq!( stdout.lines().count(), 3, "one line per criterion, not per term: {stdout:?}" ); } /// Nothing to change, so nothing is written, the way a single already-ticked /// criterion already behaves. #[test] fn a_batch_that_would_change_nothing_writes_nothing() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane); run(&temp, &["check", UUID, "all"]); let before = ticket(&worklane); let out = run(&temp, &["check", UUID, "all"]); assert!(out.status.success()); let stdout = String::from_utf8_lossy(&out.stdout); assert_eq!(stdout.matches("Already checked").count(), 3, "{stdout:?}"); assert_eq!(ticket(&worklane), before, "the file was rewritten anyway"); } /// One term that matches nothing refuses the whole call, so the ticket is never left /// half-marked. #[test] fn a_term_matching_nothing_leaves_the_batch_unwritten() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane); let before = ticket(&worklane); let out = run(&temp, &["check", UUID, "1", "nowhere", "3"]); let stderr = String::from_utf8_lossy(&out.stderr); assert_eq!(out.status.code(), Some(1)); assert!(stderr.contains("nowhere"), "the term was not named: {stderr:?}"); assert_eq!(ticket(&worklane), before, "alpha was ticked anyway"); } /// An ambiguous term in a batch is refused the same way a lone one is, and names /// itself rather than whichever term happened to come first. #[test] fn an_ambiguous_term_in_a_batch_names_itself_and_writes_nothing() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane); let before = ticket(&worklane); let out = run(&temp, &["check", UUID, "1", "shared"]); let stderr = String::from_utf8_lossy(&out.stderr); assert_eq!(out.status.code(), Some(1)); assert!(stderr.contains("'shared' matches 2 criteria"), "{stderr:?}"); assert!(stderr.contains("1. [ ] alpha shared"), "{stderr:?}"); assert!(stderr.contains("3. [ ] gamma shared"), "{stderr:?}"); assert_eq!(ticket(&worklane), before); } /// An index out of range in a batch is still a bad index, not a failed text search. #[test] fn an_index_out_of_range_in_a_batch_leaves_the_file_untouched() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane); let before = ticket(&worklane); let out = run(&temp, &["check", UUID, "1", "9"]); let stderr = String::from_utf8_lossy(&out.stderr); assert_eq!(out.status.code(), Some(1)); assert!(stderr.contains("is not one of the 3 criteria"), "{stderr:?}"); assert_eq!(ticket(&worklane), before); } /// The decision this pins: `all` is the keyword, checked before the substring the way /// the index is, so it ticks every criterion and not the one whose text says "all". #[test] fn all_is_the_keyword_and_not_a_substring_of_the_text() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_all_in_text_ticket(&worklane); run(&temp, &["check", ALL_UUID, "all"]); let content = all_in_text_ticket(&worklane); assert!(content.contains("- [x] all the boxes are listed\n"), "{content}"); assert!( content.contains("- [x] the footer is drawn\n"), "all matched its own text instead of every criterion: {content}" ); } /// And the criterion whose text carries the word is still reachable, by its number or /// by a longer substring. #[test] fn a_criterion_whose_text_contains_all_is_still_reachable() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_all_in_text_ticket(&worklane); run(&temp, &["check", ALL_UUID, "all the boxes"]); let content = all_in_text_ticket(&worklane); assert!(content.contains("- [x] all the boxes are listed\n"), "{content}"); assert!(content.contains("- [ ] the footer is drawn\n"), "{content}"); } #[test] fn uncheck_takes_all() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane); let out = run(&temp, &["uncheck", UUID, "all"]); assert!(out.status.success(), "{:?}", String::from_utf8_lossy(&out.stderr)); let content = ticket(&worklane); assert!(content.contains("- [ ] alpha shared\n"), "{content}"); assert!(content.contains("- [ ] beta alone\n"), "{content}"); assert!(content.contains("- [ ] gamma shared\n"), "{content}"); let stdout = String::from_utf8_lossy(&out.stdout); assert!(stdout.contains("Unchecked 2: beta alone"), "{stdout:?}"); assert!(stdout.contains("Already unchecked 1: alpha shared"), "{stdout:?}"); } #[test] fn uncheck_takes_several_mixed_terms() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane); run(&temp, &["check", UUID, "all"]); let out = run(&temp, &["uncheck", UUID, "2", "gamma"]); assert!(out.status.success(), "{:?}", String::from_utf8_lossy(&out.stderr)); let content = ticket(&worklane); assert!(content.contains("- [x] alpha shared\n"), "{content}"); assert!(content.contains("- [ ] beta alone\n"), "{content}"); assert!(content.contains("- [ ] gamma shared\n"), "{content}"); } #[test] fn a_failing_term_refuses_an_uncheck_batch_too() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane); let before = ticket(&worklane); let out = run(&temp, &["uncheck", UUID, "2", "nowhere"]); assert_eq!(out.status.code(), Some(1)); assert_eq!(ticket(&worklane), before, "beta was unticked anyway"); }