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"); /// The ticket under edit, the one it points at, and one that is already done. const GATED: &str = "641e3d5c-1b0e-4d2f-9a7e-3c8b5f0a2d61"; const BLOCKER: &str = "b1b2c3d4-5e6f-4a7b-8c9d-0e1f2a3b4c5d"; const FIXED: &str = "f1f2f3f4-5a6b-4c7d-8e9f-0a1b2c3d4e5f"; 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() } fn slug(title: &str) -> String { title .to_lowercase() .chars() .filter(|c| c.is_alphanumeric() || *c == ' ' || *c == '-' || *c == '_') .map(|c| if c == ' ' { '-' } else { c }) .collect() } /// A ticket with a ticked criterion and a section the tool knows nothing about, so /// every test can check that recording a relation leaves both exactly as they were. fn create_ticket(worklane: &Path, status: &str, title: &str, uuid: &str) -> PathBuf { let path = worklane .join(status) .join(format!("{}_{}.md", uuid, slug(title))); fs::write( &path, format!( "# {}\n\ \n\ ## Description\n\ Prose.\n\ \n\ ## Acceptance Criteria\n\ - [x] Crit one\n\ \n\ ## Implementation Plan\n\ 1. Step one\n\ \n\ ## Notes\n\ A handwritten note the tool knows nothing about.\n\ \n\ ## Metadata\n\ \n\ - **ID**: {}\n\ - **Priority**: 3\n\ - **Tags**: alpha\n\ - **Due**: _Not set_\n\ - **Owner**: alice\n\ - **Created At**: 2026-08-21 02:10:17\n\ - **Status**: {}\n", title, uuid, status ), ) .unwrap(); path } /// The three tickets every test starts from: the one being edited, one it can wait on, /// and one in done. fn board(temp: &TempDir) -> PathBuf { let worklane = setup_worklane(temp); create_ticket(&worklane, "backlog", "The gated one", GATED); create_ticket(&worklane, "backlog", "The blocker", BLOCKER); create_ticket(&worklane, "done", "The old fix", FIXED); worklane } 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 stdout(out: &std::process::Output) -> String { String::from_utf8_lossy(&out.stdout).to_string() } fn find(worklane: &Path, uuid: &str) -> PathBuf { for status in ["backlog", "committed", "reviewing", "done"] { let dir = worklane.join(status); for entry in fs::read_dir(&dir).unwrap().filter_map(|e| e.ok()) { if entry.file_name().to_string_lossy().starts_with(uuid) { return entry.path(); } } } panic!("no file for ticket {uuid}"); } fn ticket(worklane: &Path, uuid: &str) -> String { fs::read_to_string(find(worklane, uuid)).unwrap() } /// All three tiers, since the target is a ticket argument like any other: a uuid /// prefix, a title substring, and a substring reaching the middle of the filename. #[test] fn a_target_resolves_by_uuid_prefix_title_or_filename() { for search in ["b1b2", "blocker", "4a7b"] { let temp = TempDir::new().unwrap(); let worklane = board(&temp); let out = run(&temp, &["relate", GATED, "--blocked-by", search]); assert!(out.status.success(), "{search}: {}", String::from_utf8_lossy(&out.stderr)); assert!( ticket(&worklane, GATED).contains(&format!("- blocked by {}", BLOCKER)), "{search}: {}", ticket(&worklane, GATED) ); } } #[test] fn every_kind_is_recorded_under_its_own_label() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); let out = run( &temp, &[ "relate", GATED, "--blocked-by", BLOCKER, "--regression-of", FIXED, "--relates-to", "old fix", ], ); assert!(out.status.success()); let content = ticket(&worklane, GATED); assert!(content.contains(&format!("- blocked by {}", BLOCKER)), "{content}"); assert!(content.contains(&format!("- regression of {}", FIXED)), "{content}"); assert!(content.contains(&format!("- relates to {}", FIXED)), "{content}"); // Each kind named once in the report, whatever the order of the flags. let reported = stdout(&out); for kind in ["blocked by", "regression of", "relates to"] { assert!(reported.contains(kind), "{reported}"); } } /// The kinds are flags, so anything else never reaches the file: clap refuses it with /// a usage error, exit code 2, and nothing is written. #[test] fn an_unknown_kind_is_a_usage_error() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); let before = ticket(&worklane, GATED); let out = run(&temp, &["relate", GATED, "--depends-on", BLOCKER]); assert_eq!(out.status.code(), Some(2), "{}", stdout(&out)); assert_eq!(ticket(&worklane, GATED), before); } /// A bare `relate ` is a usage error too, not a silent no-op, the same way a /// bare `set` and a bare `append` are. #[test] fn a_relate_with_no_kind_is_a_usage_error() { let temp = TempDir::new().unwrap(); board(&temp); assert_eq!(run(&temp, &["relate", GATED]).status.code(), Some(2)); } /// Between the plan and the metadata, created for a ticket written before the section /// existed, and behind a handwritten section rather than in front of it. #[test] fn the_section_is_created_in_its_place() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); run(&temp, &["relate", GATED, "--blocked-by", BLOCKER]); let content = ticket(&worklane, GATED); let at = |heading: &str| content.find(heading).unwrap_or_else(|| panic!("{heading}")); assert!(at("## Implementation Plan") < at("## Relations"), "{content}"); assert!(at("## Notes") < at("## Relations"), "{content}"); assert!(at("## Relations") < at("## Metadata"), "{content}"); } /// The full uuid, not the seven characters `ls` prints, which is what makes the link /// survive a rename of the target and stay unambiguous as the board grows. #[test] fn the_stored_target_is_the_full_uuid() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); run(&temp, &["relate", GATED, "--blocked-by", BLOCKER]); assert!( ticket(&worklane, GATED).contains(&format!("- blocked by {}\n", BLOCKER)), "{}", ticket(&worklane, GATED) ); run(&temp, &["set", BLOCKER, "--title", "Renamed entirely"]); let shown = stdout(&run(&temp, &["show", GATED])); assert!(shown.contains("Renamed entirely"), "{shown}"); } #[test] fn a_ticket_cannot_relate_to_itself() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); let before = ticket(&worklane, GATED); let out = run(&temp, &["relate", GATED, "--relates-to", "gated"]); assert!(!out.status.success()); assert!( String::from_utf8_lossy(&out.stderr).contains("cannot relate to itself"), "{}", String::from_utf8_lossy(&out.stderr) ); assert_eq!(ticket(&worklane, GATED), before); } #[test] fn a_relation_already_held_reports_no_change_and_writes_nothing() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); run(&temp, &["relate", GATED, "--blocked-by", BLOCKER]); let before = ticket(&worklane, GATED); let out = run(&temp, &["relate", GATED, "--blocked-by", BLOCKER]); assert!(out.status.success()); assert!(stdout(&out).contains("No changes"), "{}", stdout(&out)); assert_eq!(ticket(&worklane, GATED), before, "the relation was recorded twice"); } /// One direction is stored. The inverse is derived by a scan, so the ticket at the /// other end is not touched at all. #[test] fn only_the_ticket_named_is_written() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); let before = ticket(&worklane, BLOCKER); run(&temp, &["relate", GATED, "--blocked-by", BLOCKER]); assert_eq!(ticket(&worklane, BLOCKER), before); assert!(!ticket(&worklane, BLOCKER).contains("Relations")); } /// The tick, the handwritten section and the prose are what a regenerated file would /// lose, and the section is spliced in rather than rewritten around them. #[test] fn relate_leaves_everything_it_was_not_asked_about_alone() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); run(&temp, &["relate", GATED, "--blocked-by", BLOCKER]); let content = ticket(&worklane, GATED); assert!(content.contains("- [x] Crit one"), "the tick was lost: {content}"); assert!( content.contains("## Notes\nA handwritten note the tool knows nothing about.\n"), "the unknown section was lost: {content}" ); assert!(content.contains("1. Step one"), "{content}"); assert!(content.contains("- **Owner**: alice"), "{content}"); } /// The mark each kind prints before the id. Written out rather than imported: these /// tests drive the binary, so they see only its output. /// /// The cross is the one a held-up card already carries, since both say the same word. const BLOCKED_MARK: &str = "\u{d7}"; const RELATES_MARK: &str = "\u{2192}"; /// And the mark for the status of the ticket the row names, which follows the kind: a /// circle filling up as the target moves through the flow. const BACKLOG_MARK: &str = "\u{25cb}"; const DONE_MARK: &str = "\u{25cf}"; /// A target the board no longer holds has no status, and `?` holds the column so the id /// does not move. const NO_STATUS_MARK: &str = "?"; /// The kind, the short id and the title, so the section reads without a second lookup. #[test] fn show_renders_the_kind_the_short_id_and_the_title() { let temp = TempDir::new().unwrap(); board(&temp); run(&temp, &["relate", GATED, "--blocked-by", BLOCKER]); let shown = stdout(&run(&temp, &["show", GATED])); assert!(shown.contains("Relations"), "{shown}"); assert!( shown.contains(&format!("blocked by {BLOCKED_MARK} {BACKLOG_MARK} (b1b2c3d) The blocker")), "{shown}" ); // Never the stored line: a bare uuid says nothing about what it points at. assert!(!shown.contains(BLOCKER), "{shown}"); } /// Derived by scanning, since the file at this end holds nothing at all. #[test] fn show_lists_the_relation_the_other_ticket_holds() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); run(&temp, &["relate", GATED, "--blocked-by", BLOCKER]); run(&temp, &["relate", GATED, "--relates-to", FIXED]); let shown = stdout(&run(&temp, &["show", BLOCKER])); assert!( shown.contains(&format!("blocks {BLOCKED_MARK} {BACKLOG_MARK} (641e3d5) The gated one")), "{shown}" ); assert!(!ticket(&worklane, BLOCKER).contains("Relations")); // Two kinds on one ticket, each with its own mark, and the second mark is the state // of the ticket at the far end: the old fix is in done and the blocker is not. That // is the pair the block is for, which kind of edge and whether it is finished. let shown = stdout(&run(&temp, &["show", GATED])); assert!( shown.contains(&format!("blocked by {BLOCKED_MARK} {BACKLOG_MARK} (b1b2c3d) The blocker")), "{shown}" ); assert!( shown.contains(&format!("relates to {RELATES_MARK} {DONE_MARK} (f1f2f3f) The old fix")), "{shown}" ); // `relates to` is symmetric, so it reads the same way from either end. let shown = stdout(&run(&temp, &["show", FIXED])); assert!( shown.contains(&format!( "relates to {RELATES_MARK} {BACKLOG_MARK} (641e3d5) The gated one" )), "{shown}" ); } #[test] fn a_target_that_no_longer_exists_renders_as_missing() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); run(&temp, &["relate", GATED, "--blocked-by", BLOCKER]); fs::remove_file(find(&worklane, BLOCKER)).unwrap(); let shown = stdout(&run(&temp, &["show", GATED])); assert!( shown.contains(&format!("blocked by {BLOCKED_MARK} {NO_STATUS_MARK} (b1b2c3d) _missing_")), "{shown}" ); } /// The section is the one part of a ticket read back by machine, so a line nobody can /// parse is an error naming the ticket rather than a line quietly skipped. #[test] fn a_line_that_is_not_a_relation_is_an_error() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); let path = find(&worklane, GATED); let content = fs::read_to_string(&path).unwrap().replace( "## Metadata", &format!("## Relations\n- depends on {}\n\n## Metadata", BLOCKER), ); fs::write(&path, content).unwrap(); let out = run(&temp, &["show", GATED]); assert!(!out.status.success()); let stderr = String::from_utf8_lossy(&out.stderr); assert!(stderr.contains("is not a relation"), "{stderr}"); assert!(stderr.contains("The gated one"), "{stderr}"); }