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"); 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 create_ticket(worklane_dir: &Path, status: &str, title: &str) -> String { let uuid = uuid::Uuid::new_v4().to_string(); let slug = title .to_lowercase() .chars() .filter(|c| c.is_alphanumeric() || *c == ' ' || *c == '-' || *c == '_') .map(|c| if c == ' ' { '-' } else { c }) .collect::(); let filename = format!("{}_{}.md", uuid, slug); let content = format!( "# {}\n\ \n\ ## Metadata\n\ \n\ - **ID**: {}\n\ - **Status**: {}\n\ ", title, uuid, status ); fs::create_dir_all(worklane_dir.join(status)).unwrap(); fs::write(worklane_dir.join(status).join(&filename), content).unwrap(); uuid } /// The one comment on a ticket, which every author test writes exactly one of. fn only_comment(worklane_dir: &Path, uuid: &str) -> String { let files: Vec<_> = fs::read_dir(worklane_dir.join("history").join(uuid)) .unwrap() .filter_map(|e| e.ok()) .collect(); assert_eq!(files.len(), 1, "expected one comment"); fs::read_to_string(files[0].path()).unwrap() } /// A git config file holding nothing but a `user.name`, for the tests that need the /// fallback to answer something known instead of whatever the machine is configured with. fn git_config_naming(temp: &TempDir, name: &str) -> PathBuf { let path = temp.path().join("gitconfig"); fs::write(&path, format!("[user]\n\tname = {}\n", name)).unwrap(); path } #[test] fn comment_creates_file_with_valid_ticket_uuid() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "backlog", "Test Ticket"); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .arg("comment") .args([&uuid, "This is a test comment"]) .assert() .success(); let history_dir = worklane.join("history").join(&uuid); assert!(history_dir.exists()); let files: Vec<_> = fs::read_dir(&history_dir) .unwrap() .filter_map(|e| e.ok()) .collect(); assert_eq!(files.len(), 1); let content = fs::read_to_string(files[0].path()).unwrap(); assert!(content.contains("This is a test comment")); } #[test] fn comment_creates_file_with_valid_ticket_title() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane, "committed", "My Ticket"); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .arg("comment") .args(["My Ticket", "Testing title lookup"]) .assert() .success(); } #[test] fn comment_error_when_ticket_not_found() { let _temp = TempDir::new().unwrap(); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.arg("comment") .arg("Nonexistent") .arg("Some comment") .assert() .code(1); } #[test] fn comment_includes_git_user_and_body() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "backlog", "Comment Test"); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .arg("comment") .args([&uuid, "My comment body"]) .assert() .success(); let history_dir = worklane.join("history").join(&uuid); let files: Vec<_> = fs::read_dir(&history_dir) .unwrap() .filter_map(|e| e.ok()) .collect(); let content = fs::read_to_string(files[0].path()).unwrap(); assert!(content.contains("My comment body")); assert!(content.contains("**Author**")); } #[test] fn comment_stored_in_timestamped_file() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "done", "Timestamp Test"); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .arg("comment") .args([&uuid, "Timestamp test"]) .assert() .success(); let history_dir = worklane.join("history").join(&uuid); let files: Vec<_> = fs::read_dir(&history_dir) .unwrap() .filter_map(|e| e.ok()) .collect(); assert_eq!(files.len(), 1); let filename = files[0].file_name().into_string().unwrap(); assert!(filename.ends_with(".md")); } #[test] fn comment_creates_the_history_directory() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "reviewing", "Dir Test"); let history_dir = worklane.join("history").join(&uuid); assert!(!history_dir.exists()); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .arg("comment") .args([&uuid, "Create dir test"]) .assert() .success(); assert!(history_dir.exists()); } #[test] fn comment_uses_actor_flag_when_provided() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "backlog", "Actor Test"); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .arg("comment") .args([&uuid, "Test comment with actor"]) .args(["--actor", "TestUser"]) .assert() .success(); assert!(only_comment(&worklane, &uuid).contains("**Author**: TestUser")); } /// The flag it was, which named the ticket's assignee and not the writer of the entry. /// Renamed without an alias, so the old spelling is a usage error rather than something /// that half works. #[test] fn the_owner_flag_is_gone() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "backlog", "Gone Test"); for flag in ["--owner", "-o"] { Command::cargo_bin(BIN_NAME) .unwrap() .current_dir(temp.path()) .arg("comment") .args([&uuid, "A body"]) .args([flag, "TestUser"]) .assert() .failure(); } assert!( !worklane.join("history").join(&uuid).exists(), "a comment was written despite the usage error" ); } /// The variable is the case an agent runs in: set once by its configuration and /// inherited by every call, where a flag can be forgotten on the fortieth one. #[test] fn comment_takes_the_author_from_the_actor_variable() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "backlog", "Variable Test"); Command::cargo_bin(BIN_NAME) .unwrap() .current_dir(temp.path()) .env("WORKLANE_ACTOR", "claude") .arg("comment") .args([&uuid, "Written by an agent"]) .assert() .success(); assert!(only_comment(&worklane, &uuid).contains("**Author**: claude")); } #[test] fn the_actor_flag_overrides_the_variable() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "backlog", "Override Test"); Command::cargo_bin(BIN_NAME) .unwrap() .current_dir(temp.path()) .env("WORKLANE_ACTOR", "claude") .arg("comment") .args([&uuid, "Written by the other one"]) .args(["--actor", "opencode"]) .assert() .success(); assert!(only_comment(&worklane, &uuid).contains("**Author**: opencode")); } /// An empty variable counts as unset, the convention `NO_COLOR` follows, so a wrapper /// script can clear an inherited one and get the git user back. #[test] fn an_empty_actor_variable_falls_through_to_the_git_user() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "backlog", "Empty Variable Test"); Command::cargo_bin(BIN_NAME) .unwrap() .current_dir(temp.path()) .env("WORKLANE_ACTOR", "") .env("GIT_CONFIG_GLOBAL", git_config_naming(&temp, "Configured User")) .env("GIT_CONFIG_NOSYSTEM", "1") .arg("comment") .args([&uuid, "Written by a person"]) .assert() .success(); assert!(only_comment(&worklane, &uuid).contains("**Author**: Configured User")); } /// The last resort. Reached with a git that has no `user.name`, which is why the config /// files are redirected: the machine running the suite has one. #[test] fn the_author_is_unknown_when_nothing_names_one() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "backlog", "Unknown Test"); Command::cargo_bin(BIN_NAME) .unwrap() .current_dir(temp.path()) .env_remove("WORKLANE_ACTOR") .env("GIT_CONFIG_GLOBAL", temp.path().join("no-such-gitconfig")) .env("GIT_CONFIG_NOSYSTEM", "1") .arg("comment") .args([&uuid, "Written by nobody in particular"]) .assert() .success(); assert!(only_comment(&worklane, &uuid).contains("**Author**: unknown")); } #[test] fn comment_falls_back_to_git_user_when_actor_omitted() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "done", "Fallback Test"); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .arg("comment") .args([&uuid, "Test comment fallback"]) .assert() .success(); let history_dir = worklane.join("history").join(&uuid); let files: Vec<_> = fs::read_dir(&history_dir) .unwrap() .filter_map(|e| e.ok()) .collect(); let content = fs::read_to_string(files[0].path()).unwrap(); assert!(content.contains("**Author**")); } /// Second-resolution filenames meant the second of two quick comments /// truncated the first. #[test] fn comments_added_back_to_back_are_both_kept() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "backlog", "Rapid Fire"); for body in ["first comment", "second comment", "third comment"] { Command::cargo_bin(BIN_NAME) .unwrap() .current_dir(temp.path()) .arg("comment") .args([&uuid, body]) .assert() .success(); } let history_dir = worklane.join("history").join(&uuid); let files: Vec<_> = fs::read_dir(&history_dir) .unwrap() .filter_map(|e| e.ok()) .collect(); assert_eq!(files.len(), 3, "a comment was overwritten"); // And show has to render them in the order they were added let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()).arg("show").arg(&uuid); let assert = cmd.assert().success(); let stdout = String::from_utf8(assert.get_output().stdout.clone()).unwrap(); let first = stdout.find("first comment").expect("first comment missing"); let second = stdout.find("second comment").expect("second comment missing"); let third = stdout.find("third comment").expect("third comment missing"); assert!(first < second && second < third, "comments out of order"); } /// The body is a positional, and the case that motivated this: a review comment /// quoting a flag name used to be refused outright. #[test] fn comment_accepts_a_body_that_begins_with_a_hyphen() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "backlog", "Hyphen body"); for body in ["--print is the flag I mean", "-f skips the check", "-"] { let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .args(["comment", &uuid, body]) .assert() .success(); } let bodies: String = fs::read_dir(worklane.join("history").join(&uuid)) .unwrap() .filter_map(|e| e.ok()) .map(|e| fs::read_to_string(e.path()).unwrap()) .collect(); for body in ["--print is the flag I mean", "-f skips the check"] { assert!(bodies.contains(body), "{body:?} missing from {bodies:?}"); } } /// The ticket positional was deliberately left alone, so a hyphen-leading search /// term is still a usage error. A bare separator is the way through, and it is the /// only way through for `comment` since the body follows in the same position. #[test] fn comment_needs_a_separator_for_a_hyphen_leading_ticket() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane, "backlog", "-leading"); let mut without = Command::cargo_bin(BIN_NAME).unwrap(); without .current_dir(temp.path()) .args(["comment", "-leading", "a body"]) .assert() .failure(); let mut with = Command::cargo_bin(BIN_NAME).unwrap(); with.current_dir(temp.path()) .args(["comment", "--", "-leading", "a body"]) .assert() .success(); } /// The one shape `allow_hyphen_values` does not reach: a lone `--` in a positional /// is clap's end-of-options marker, consumed before any value is assigned. So a /// comment body of exactly `--` needs its own separator in front of it. /// /// Not a silent drop either way, which is what matters: without the separator the /// body is reported missing and nothing is written. #[test] fn a_bare_double_hyphen_body_needs_a_separator() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "backlog", "Bare separator"); let mut without = Command::cargo_bin(BIN_NAME).unwrap(); let assert = without .current_dir(temp.path()) .args(["comment", &uuid, "--"]) .assert() .failure(); let stderr = String::from_utf8(assert.get_output().stderr.clone()).unwrap(); assert!(stderr.contains(""), "unclear error: {stderr}"); assert!( !worklane.join("history").join(&uuid).exists(), "a comment was written despite the error" ); let mut with = Command::cargo_bin(BIN_NAME).unwrap(); with.current_dir(temp.path()) .args(["comment", &uuid, "--", "--"]) .assert() .success(); let stored: String = fs::read_dir(worklane.join("history").join(&uuid)) .unwrap() .filter_map(|e| e.ok()) .map(|e| fs::read_to_string(e.path()).unwrap()) .collect(); assert!(stored.contains("**Comment**\n--\n"), "was {stored:?}"); }