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(); } 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", "comments"].contains(&name.as_str()) }) .collect(); assert_eq!(strays, Vec::::new()); }