//! The history log: what the CLI records about a ticket besides what somebody said. //! //! One file rather than a few tests in each command's suite: the rules are about the log //! and not about `mv` or `chown`, and the interesting cases are the ones where two //! commands have to agree — every mechanical entry has the same shape, and every refusal //! writes nothing. use assert_cmd::Command; use assert_fs::TempDir; use assert_fs::fixture::{PathChild, PathCreateDir}; use std::fs; use std::path::{Path, PathBuf}; use std::process::Output; 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() } /// A ticket with every field an entry can record a change to. fn create_ticket(worklane: &Path, status: &str) -> String { let filename = format!("{}_a-ticket.md", UUID); fs::write( worklane.join(status).join(&filename), format!( "# A ticket\n\ \n\ ## Description\n\ Something to do.\n\ \n\ ## Acceptance Criteria\n\ - [ ] Crit one\n\ - [ ] Crit two\n\ \n\ ## Implementation Plan\n\ 1. Step one\n\ \n\ ## Metadata\n\ \n\ - **ID**: {}\n\ - **Priority**: 3\n\ - **Tags**: alpha\n\ - **Due**: 2026-09-01\n\ - **Owner**: alice\n\ - **Status**: {}\n", UUID, status ), ) .unwrap(); filename } /// Run with the actor named, so every author assertion is about the log and not about /// the git user of whoever runs the suite. fn run(temp: &TempDir, args: &[&str]) -> Output { Command::cargo_bin(BIN_NAME) .unwrap() .current_dir(temp.path()) .env("WORKLANE_ACTOR", "claude") .args(args) .assert() .get_output() .clone() } /// Every entry on the ticket, name and content, in the order the log sorts them. fn entries(worklane: &Path) -> Vec<(String, String)> { let dir = worklane.join("history").join(UUID); if !dir.exists() { return vec![]; } let mut found: Vec<(String, String)> = fs::read_dir(&dir) .unwrap() .filter_map(|entry| entry.ok()) .map(|entry| { ( entry.file_name().into_string().unwrap(), fs::read_to_string(entry.path()).unwrap(), ) }) .collect(); found.sort_by(|a, b| a.0.cmp(&b.0)); found } /// The one entry a call wrote, with its kind checked off the filename. fn only_entry(worklane: &Path, kind: &str) -> String { let found = entries(worklane); assert_eq!(found.len(), 1, "expected one entry, got {found:?}"); assert!(found[0].0.ends_with(&format!("_{kind}.md")), "was {}", found[0].0); found[0].1.clone() } #[test] fn mv_writes_a_status_entry() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane, "backlog"); run(&temp, &["mv", UUID, "--status", "committed"]); let entry = only_entry(&worklane, "status"); assert!(entry.starts_with("**Status**\nbacklog → committed\n"), "{entry:?}"); assert!(entry.contains("**Author**: claude"), "{entry:?}"); } #[test] fn chown_writes_an_owner_entry() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane, "backlog"); run(&temp, &["chown", "bob", UUID]); let entry = only_entry(&worklane, "owner"); assert!(entry.starts_with("**Owner**\nalice → bob\n"), "{entry:?}"); } /// Clearing is the same field going somewhere, so it is logged, and the value it goes to /// is the sentinel the ticket now holds rather than a word this log invented. #[test] fn clearing_the_owner_writes_an_owner_entry() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane, "backlog"); run(&temp, &["chown", "--clear", UUID]); let entry = only_entry(&worklane, "owner"); assert!(entry.starts_with("**Owner**\nalice → _Not set_\n"), "{entry:?}"); } #[test] fn set_tags_writes_a_tags_entry() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane, "backlog"); run(&temp, &["set", UUID, "--tags", "beta", "--tags", "gamma"]); let entry = only_entry(&worklane, "tags"); assert!(entry.starts_with("**Tags**\nalpha → beta, gamma\n"), "{entry:?}"); } #[test] fn clearing_the_tags_writes_a_tags_entry() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane, "backlog"); run(&temp, &["set", UUID, "--clear", "tags"]); let entry = only_entry(&worklane, "tags"); assert!(entry.starts_with("**Tags**\nalpha → _None_\n"), "{entry:?}"); } /// The entry names the campaign the way a reader knows it, the codename, not the uuid /// the field actually holds. A ticket never assigned starts from the sentinel an unset /// field reads as everywhere else. #[test] fn assign_writes_a_campaign_entry_naming_the_codename() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane, "backlog"); run( &temp, &[ "campaign", "add", "--title", "First public release", "--codename", "v1", ], ); run(&temp, &["assign", "v1", UUID]); let entry = only_entry(&worklane, "campaign"); assert!(entry.starts_with("**Campaign**\n_Not set_ → v1\n"), "{entry:?}"); } /// A campaign filed without a codename is recorded by its title, the name `show` and /// `campaign ls` fall back to. #[test] fn a_campaign_without_a_codename_is_recorded_by_its_title() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane, "backlog"); run(&temp, &["campaign", "add", "--title", "First public release"]); run(&temp, &["assign", "First public", UUID]); let entry = only_entry(&worklane, "campaign"); assert!( entry.starts_with("**Campaign**\n_Not set_ → First public release\n"), "{entry:?}" ); } /// A ticket leaving one campaign for another records both ends, which is the move the /// log exists to answer for: the field is rewritten, so nothing else remembers where the /// ticket was. #[test] fn moving_between_campaigns_records_both_names() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane, "backlog"); run( &temp, &[ "campaign", "add", "--title", "First release", "--codename", "v1", ], ); run( &temp, &[ "campaign", "add", "--title", "Second release", "--codename", "beaver", ], ); run(&temp, &["assign", "v1", UUID]); run(&temp, &["assign", "beaver", UUID]); let found = entries(&worklane); assert_eq!(found.len(), 2, "{found:?}"); assert!(found[1].1.starts_with("**Campaign**\nv1 → beaver\n"), "{:?}", found[1].1); } /// Assigning the campaign a ticket already names is reported as no change, and a log of /// no change would be a log of nothing happening. #[test] fn a_no_op_assign_writes_nothing() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane, "backlog"); run( &temp, &[ "campaign", "add", "--title", "First release", "--codename", "v1", ], ); run(&temp, &["assign", "v1", UUID]); let out = run(&temp, &["assign", "v1", UUID]); assert!(out.status.success(), "{:?}", String::from_utf8_lossy(&out.stderr)); assert_eq!( entries(&worklane).len(), 1, "a no-op assign logged a change it did not make" ); } /// The fields that change often enough that a log of them is noise. `set` writing one of /// them is not an entry, and neither is a criterion being ticked. #[test] fn the_edits_that_are_not_logged_write_nothing() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane, "backlog"); for args in [ vec!["set", UUID, "--title", "A better title"], vec!["set", UUID, "--due", "2026-10-01"], vec!["set", UUID, "--priority", "1"], vec!["set", UUID, "--description", "Rewritten."], vec!["set", UUID, "--plan", "One step"], vec!["check", UUID, "1"], vec!["uncheck", UUID, "1"], vec!["append", UUID, "--description", "One more paragraph."], vec!["comment", UUID, "-", "--"], ] { let out = run(&temp, &args); assert!( out.status.success(), "{args:?}: {:?}", String::from_utf8_lossy(&out.stderr) ); } // The comment is the exception: it is an entry, and the only one this run wrote. let found = entries(&worklane); assert_eq!(found.len(), 1, "{found:?}"); assert!(found[0].0.ends_with("_comment.md"), "was {}", found[0].0); } /// An edit reported as no change writes no entry: the field is already what the call /// asked for, and a log of that would be a log of nothing happening. #[test] fn a_no_op_edit_writes_nothing() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane, "backlog"); for args in [ vec!["mv", UUID, "--status", "backlog"], vec!["chown", "alice", UUID], vec!["set", UUID, "--tags", "alpha"], ] { let out = run(&temp, &args); assert!( out.status.success(), "{args:?}: {:?}", String::from_utf8_lossy(&out.stderr) ); assert_eq!(entries(&worklane), vec![], "{args:?} logged a change it did not make"); } } /// A refused move is not a move. The entry is written after the edit for exactly this. #[test] fn a_refused_move_writes_nothing() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane, "reviewing"); let backward = run(&temp, &["mv", UUID, "--status", "backlog"]); assert_eq!(backward.status.code(), Some(1)); let skipped = run(&temp, &["mv", UUID, "--status", "backlog", "-f"]); assert!(skipped.status.success(), "{:?}", String::from_utf8_lossy(&skipped.stderr)); // The forced one moved, so it is logged; the refused one is not. let found = entries(&worklane); assert_eq!(found.len(), 1, "{found:?}"); assert!(found[0].1.contains("reviewing → backlog"), "{:?}", found[0].1); } /// A failed edit logs nothing, whatever the failure. This ticket has no Owner field, so /// `chown` refuses it after resolving it and before writing anything. #[test] fn a_failed_edit_writes_nothing() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); fs::write( worklane.join("backlog").join(format!("{}_bare.md", UUID)), format!("# Bare\n\n## Metadata\n\n- **ID**: {}\n- **Status**: backlog\n", UUID), ) .unwrap(); let out = run(&temp, &["chown", "bob", UUID]); assert_eq!(out.status.code(), Some(1)); assert_eq!(entries(&worklane), vec![], "a refused chown was logged"); } /// One sequence, oldest first, with a comment rendered as it always was and a field /// change as one line. #[test] fn show_prints_the_comments_and_the_changes_in_one_sequence() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane, "backlog"); run(&temp, &["comment", UUID, "Picked this up."]); run(&temp, &["mv", UUID, "--status", "committed"]); run(&temp, &["chown", "bob", UUID]); let out = run(&temp, &["show", UUID]); let stdout = String::from_utf8(out.stdout).unwrap(); assert!(stdout.contains("HISTORY"), "{stdout}"); let said = stdout.find("Picked this up.").expect("the comment is missing"); let moved = stdout.find("backlog → committed").expect("the move is missing"); let owned = stdout.find("alice → bob").expect("the chown is missing"); assert!(said < moved && moved < owned, "out of order:\n{stdout}"); for line in ["status backlog → committed", "owner alice → bob"] { assert!(stdout.contains(line), "{line:?} not in:\n{stdout}"); } assert!(stdout.contains("From claude ("), "{stdout}"); } /// A ticket nothing has happened to says so, where it used to say there were no comments. #[test] fn show_says_so_when_there_is_no_history() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane, "backlog"); let out = run(&temp, &["show", UUID]); assert!( String::from_utf8(out.stdout) .unwrap() .contains("No history yet.") ); } /// A board written by an older version: the directory is adopted on the first access, /// the entries in it are read as the comments they are, and nothing is rewritten. #[test] fn an_old_comments_directory_is_adopted() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane, "backlog"); let legacy = worklane.join("comments").join(UUID); fs::create_dir_all(&legacy).unwrap(); let name = "20260820_143012_123456.md"; let content = "**Comment**\nSaid long ago.\n\n**Author**: Ansho Rei\n**Time**: 2026-08-20 14:30:12\n"; fs::write(legacy.join(name), content).unwrap(); let out = run(&temp, &["show", UUID]); let stdout = String::from_utf8(out.stdout).unwrap(); assert!(stdout.contains("Said long ago."), "{stdout}"); assert!(stdout.contains("From Ansho Rei ("), "{stdout}"); assert!(!worklane.join("comments").exists(), "the old directory survived"); let moved = worklane.join("history").join(UUID).join(name); assert_eq!(fs::read_to_string(&moved).unwrap(), content, "the entry was rewritten"); // And a new entry lands beside it, in the order it was written. run(&temp, &["mv", UUID, "--status", "committed"]); let found = entries(&worklane); assert_eq!(found.len(), 2, "{found:?}"); assert_eq!(found[0].0, name); assert!(found[1].0.ends_with("_status.md"), "was {}", found[1].0); } /// The flag is spelled the same on every command that writes an entry, and it overrides /// the variable on each of them. #[test] fn the_actor_flag_names_the_writer_on_every_command_that_writes_one() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane, "backlog"); run( &temp, &[ "campaign", "add", "--title", "First release", "--codename", "v1", ], ); run(&temp, &["comment", UUID, "By hand.", "--actor", "opencode"]); run(&temp, &["mv", UUID, "--status", "committed", "--actor", "opencode"]); run(&temp, &["chown", "bob", UUID, "--actor", "opencode"]); run(&temp, &["set", UUID, "--tags", "beta", "--actor", "opencode"]); run(&temp, &["assign", "v1", UUID, "--actor", "opencode"]); let found = entries(&worklane); assert_eq!(found.len(), 5, "{found:?}"); for (name, content) in found { assert!(content.contains("**Author**: opencode"), "{name}: {content:?}"); } }