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() } #[test] fn add_creates_ticket_in_backlog() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .arg("add") .args(["--title", "Test ticket"]) .assert() .success(); let files: Vec<_> = fs::read_dir(worklane.join("backlog")) .unwrap() .filter_map(|e| e.ok()) .map(|e| e.file_name().into_string().unwrap()) .collect(); assert_eq!(files.len(), 1); let filename = &files[0]; assert!(filename.ends_with("_test-ticket.md")); } #[test] fn add_formats_ticket_correctly() { let temp = TempDir::new().unwrap(); setup_worklane(&temp); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .args([ "add", "--title", "Fix login bug", "--description", "Users cannot log in", "--acceptance", "Login succeeds with valid credentials", "--acceptance", "Invalid credentials show error", "--plan", "Check password hash", "--tags", "bug,frontend", "--due", "2025-06-01", "--owner", "alice", ]) .assert() .success(); let files: Vec<_> = fs::read_dir(temp.child(".worklane").join("backlog")) .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("# Fix login bug")); assert!(content.contains("# Description")); assert!(content.contains("Users cannot log in")); assert!(content.contains("# Acceptance Criteria")); assert!(content.contains("- [ ] Login succeeds with valid credentials")); assert!(content.contains("- [ ] Invalid credentials show error")); assert!(content.contains("# Implementation Plan")); assert!(content.contains("Check password hash")); assert!(content.contains("# Metadata")); assert!(content.contains("**ID**")); assert!(content.contains("**Tags**")); assert!(content.contains("bug,frontend")); assert!(content.contains("**Due**")); assert!(content.contains("2025-06-01")); assert!(content.contains("**Owner**")); assert!(content.contains("alice")); assert!(content.contains("**Status**")); assert!(content.contains("backlog")); } #[test] fn add_joins_multiple_descriptions() { let temp = TempDir::new().unwrap(); setup_worklane(&temp); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .args([ "add", "--title", "Multi desc ticket", "--description", "Line one", "--description", "Line two", ]) .assert() .success(); let files: Vec<_> = fs::read_dir(temp.child(".worklane").join("backlog")) .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("Line one")); assert!(content.contains("Line two")); assert!(content.contains("\n")); } #[test] fn add_with_optional_fields_uses_defaults() { let temp = TempDir::new().unwrap(); setup_worklane(&temp); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .args(["add", "--title", "Minimal ticket"]) .assert() .success(); let files: Vec<_> = fs::read_dir(temp.child(".worklane").join("backlog")) .unwrap() .filter_map(|e| e.ok()) .collect(); let content = fs::read_to_string(files[0].path()).unwrap(); assert!(content.contains("# Minimal ticket")); assert!(content.contains("_No acceptance criteria provided._")); assert!(content.contains("_No implementation plan provided._")); assert!(content.contains("**Status**")); assert!(content.contains("backlog")); } #[test] fn add_allows_duplicate_titles() { let temp = TempDir::new().unwrap(); setup_worklane(&temp); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .args(["add", "--title", "Existing title"]) .assert() .success(); let mut cmd2 = Command::cargo_bin(BIN_NAME).unwrap(); cmd2.current_dir(temp.path()) .args(["add", "--title", "Existing title"]) .assert() .success(); let files: Vec<_> = fs::read_dir(temp.child(".worklane").join("backlog")) .unwrap() .filter_map(|e| e.ok()) .collect(); // Each add call creates a new file with a different UUID assert_eq!(files.len(), 2); } #[test] fn add_file_has_uuid_prefix() { let temp = TempDir::new().unwrap(); setup_worklane(&temp); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .args(["add", "--title", "UUID test"]) .assert() .success(); let files: Vec<_> = fs::read_dir(temp.child(".worklane").join("backlog")) .unwrap() .filter_map(|e| e.ok()) .collect(); assert_eq!(files.len(), 1); let filename = files[0].file_name().into_string().unwrap(); // UUID format: 8-4-4-4-12 hex chars (36 chars with hyphens) let parts: Vec<&str> = filename.split('_').collect(); assert_eq!(parts[0].len(), 36); assert_eq!(parts[0].chars().filter(|c| c.is_ascii_hexdigit()).count(), 32); } #[test] fn add_writes_the_priority_field() { let temp = TempDir::new().unwrap(); setup_worklane(&temp); Command::cargo_bin(BIN_NAME) .unwrap() .current_dir(temp.path()) .args(["add", "--title", "Urgent thing", "--priority", "1"]) .assert() .success(); let files: Vec<_> = fs::read_dir(temp.child(".worklane").join("backlog")) .unwrap() .filter_map(|e| e.ok()) .collect(); let content = fs::read_to_string(files[0].path()).unwrap(); assert!(content.contains("- **Priority**: 1"), "was {content:?}"); } #[test] fn add_leaves_priority_unset_when_omitted() { let temp = TempDir::new().unwrap(); setup_worklane(&temp); Command::cargo_bin(BIN_NAME) .unwrap() .current_dir(temp.path()) .args(["add", "--title", "Whenever"]) .assert() .success(); let files: Vec<_> = fs::read_dir(temp.child(".worklane").join("backlog")) .unwrap() .filter_map(|e| e.ok()) .collect(); let content = fs::read_to_string(files[0].path()).unwrap(); assert!(content.contains("- **Priority**: _Not set_"), "was {content:?}"); } /// Rejected at parse time, so no ticket is written at all. #[test] fn add_rejects_a_level_outside_the_range() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); // The `=` form, so a negative value reaches the validator instead of being // read as a flag. for bad in ["0", "6", "high", "-1"] { let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .args(["add", "--title", "Bad level", &format!("--priority={bad}")]) .assert() .failure(); let stderr = String::from_utf8(cmd.assert().get_output().stderr.clone()).unwrap(); assert!(stderr.contains("priority must be"), "for {bad}: {stderr}"); } let count = fs::read_dir(worklane.join("backlog")).unwrap().count(); assert_eq!(count, 0, "a ticket was written despite a bad level"); } /// Ticket text quotes flag names constantly, and the space form is what both a /// person and the opencode wrapper produce. Every one of these used to be refused /// with "unexpected argument found", writing no ticket at all. #[test] fn add_accepts_prose_values_that_begin_with_a_hyphen() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .args(["add", "--title", "-t is a flag name"]) .args(["--description", "-x does nothing"]) .args(["--acceptance", "--print should be supported"]) .args(["--plan", "-f skips the check"]) .assert() .success(); let content = only_ticket(&worklane); // Stored verbatim, hyphens and all assert!(content.contains("# -t is a flag name"), "was {content:?}"); assert!(content.contains("-x does nothing"), "was {content:?}"); assert!(content.contains("- [ ] --print should be supported"), "was {content:?}"); assert!(content.contains("1. -f skips the check"), "was {content:?}"); } /// The docs and the habit both use it, and it reaches the parser as one token, so /// it has to keep working alongside the space form. #[test] fn add_still_accepts_the_equals_form() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .args(["add", "--title=-x equals form"]) .args(["--acceptance=--print via equals"]) .assert() .success(); let content = only_ticket(&worklane); assert!(content.contains("# -x equals form"), "was {content:?}"); assert!(content.contains("- [ ] --print via equals"), "was {content:?}"); } /// Never silently dropped: a value that is nothing but hyphens is content. #[test] fn add_stores_a_title_that_is_only_hyphens() { for bare in ["-", "--", "---"] { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .args(["add", "--title", bare]) .assert() .success(); let content = only_ticket(&worklane); assert!(content.starts_with(&format!("# {}\n", bare)), "for {bare:?}: {content:?}"); } } /// The other half of the trade. These arguments were deliberately left alone, so a /// mistyped flag there is still a usage error rather than stored text. #[test] fn add_still_rejects_a_mistyped_flag_on_a_non_prose_argument() { for flag in ["--due", "--priority", "--tags", "--owner"] { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .args(["add", "--title", "Mistyped", flag, "--bogus"]) .assert() .failure(); let count = fs::read_dir(worklane.join("backlog")).unwrap().count(); assert_eq!(count, 0, "{flag} wrote a ticket anyway"); } } /// `add` has no positionals, so a bare `--` ends the options and whatever follows /// has nowhere to go. Recorded because clap's separator and a hyphen-leading value /// are easy to confuse. #[test] fn add_rejects_arguments_after_a_bare_separator() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .args(["add", "--title", "Sep", "--", "--title", "second"]) .assert() .failure(); let count = fs::read_dir(worklane.join("backlog")).unwrap().count(); assert_eq!(count, 0); } fn only_ticket(worklane: &Path) -> String { let files: Vec<_> = fs::read_dir(worklane.join("backlog")) .unwrap() .filter_map(|e| e.ok()) .collect(); assert_eq!(files.len(), 1, "expected exactly one ticket"); fs::read_to_string(files[0].path()).unwrap() } /// The contract the opencode wrapper relies on: values are joined with a single /// newline, so an empty value is how a paragraph break is spelled. The wrapper used /// to drop those blanks, collapsing every multi paragraph description into one. #[test] fn add_keeps_a_paragraph_break_in_the_description() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .args(["add", "--title", "Two Paras"]) .args(["--description", "First paragraph."]) .args(["--description", ""]) .args(["--description", "Second paragraph."]) .assert() .success(); let content = only_ticket(&worklane); assert!( content.contains("## Description\nFirst paragraph.\n\nSecond paragraph.\n"), "was {content:?}" ); // And it survives the round trip back out let mut show = Command::cargo_bin(BIN_NAME).unwrap(); show.current_dir(temp.path()).args(["show", "Two Paras"]); let stdout = String::from_utf8(show.assert().success().get_output().stdout.clone()).unwrap(); assert!(stdout.contains("First paragraph.\n\nSecond paragraph."), "was {stdout:?}"); } /// A break belongs between paragraphs, not against the section headings. #[test] fn add_discards_blank_descriptions_at_either_end() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .args(["add", "--title", "Padded"]) .args(["--description", ""]) .args(["--description", " "]) .args(["--description", "The only line."]) .args(["--description", ""]) .assert() .success(); let content = only_ticket(&worklane); assert!( content.contains("## Description\nThe only line.\n\n## Acceptance Criteria"), "was {content:?}" ); } /// Nothing but blanks is no description, not an empty section. #[test] fn add_treats_an_all_blank_description_as_none() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .args(["add", "--title", "Blank"]) .args(["--description", ""]) .args(["--description", " "]) .assert() .success(); let content = only_ticket(&worklane); assert!( content.contains("## Description\n_No description provided._"), "was {content:?}" ); } /// The help, the README and the skill all promise `YYYY-MM-DD`, and until this the /// value was written to the ticket whatever it said. #[test] fn add_stores_a_valid_due_date_unchanged() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .arg("add") .args(["--title", "Dated"]) .args(["--due", "2026-08-29"]) .assert() .success(); let content = read_only_ticket(&worklane.join("backlog")); assert!(content.contains("- **Due**: 2026-08-29"), "{content}"); } /// Every one of these is refused at parse time, so the exit code is 2 and nothing is /// written. A date that cannot happen is as wrong as a word: `2026-02-30` has the shape /// and is not a day. #[test] fn add_refuses_a_due_date_that_is_not_a_real_iso_date() { for value in [ "next friday", "29-08-2026", "2026-13-01", "2026-02-30", "2026/08/29", "", ] { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); let out = cmd .current_dir(temp.path()) .arg("add") .args(["--title", "Dated"]) .args(["--due", value]) .assert() .failure() .get_output() .clone(); assert_eq!(out.status.code(), Some(2), "{value:?} was not a usage error"); assert_eq!( fs::read_dir(worklane.join("backlog")).unwrap().count(), 0, "{value:?} wrote a ticket anyway" ); } } /// An unpadded date parses as a date and would be stored as typed, which sorts before /// `2026-10-01` and is not the format anything promises. #[test] fn add_refuses_an_unpadded_due_date_rather_than_padding_it() { let temp = TempDir::new().unwrap(); setup_worklane(&temp); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); let stderr = String::from_utf8_lossy( &cmd.current_dir(temp.path()) .arg("add") .args(["--title", "Dated"]) .args(["--due", "2026-8-9"]) .assert() .failure() .get_output() .stderr, ) .to_string(); assert!(stderr.contains("YYYY-MM-DD"), "the error never names the format: {stderr}"); } /// An empty value keeps the message about clearing, since that is what somebody typing /// it is reaching for. On `add` the answer is to omit the flag, but the message names /// `--clear` because the same parser guards `set --due`, where that is the answer. #[test] fn an_empty_due_date_points_at_clear() { let temp = TempDir::new().unwrap(); setup_worklane(&temp); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); let stderr = String::from_utf8_lossy( &cmd.current_dir(temp.path()) .arg("add") .args(["--title", "Dated"]) .args(["--due", ""]) .assert() .failure() .get_output() .stderr, ) .to_string(); assert!(stderr.contains("use --clear"), "{stderr}"); assert!(!stderr.contains("not supported"), "{stderr}"); } fn read_only_ticket(dir: &Path) -> String { let file = fs::read_dir(dir) .unwrap() .filter_map(|e| e.ok()) .find(|e| e.path().extension().is_some_and(|ext| ext == "md")) .expect("no ticket written"); fs::read_to_string(file.path()).unwrap() } fn chmod(path: &Path, mode: u32) { use std::os::unix::fs::PermissionsExt; let mut perms = fs::metadata(path).unwrap().permissions(); perms.set_mode(mode); fs::set_permissions(path, perms).unwrap(); } /// A duplicate title is not a failure, so the only thing left to refuse is the write /// itself, and it is named with the ticket and the path rather than raised as a bare io /// error. The branch this replaces could not fire at all: the filename carries a fresh /// uuid, and its message claimed the title was taken. /// /// The add stages through `write_then_replace` like every other write, so the temp /// lives under `.worklane/`, outside the directory the chmod keeps closed, and the /// rename is the step that fails here. Both halves have to be gone afterwards: no half /// ticket in backlog, and no `.wl-*.tmp` beside it, which is what `stray_files` checks /// for a move. #[test] fn add_names_the_ticket_and_the_path_when_the_write_fails() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let backlog = worklane.join("backlog"); chmod(&backlog, 0o555); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); let assert = cmd .current_dir(temp.path()) .args(["add", "--title", "Unwritable"]) .assert() .failure(); let stderr = String::from_utf8(assert.get_output().stderr.clone()).unwrap(); assert!(stderr.contains("Unwritable"), "{stderr}"); assert!(stderr.contains("unwritable.md"), "{stderr}"); assert!(!stderr.contains("already exists"), "{stderr}"); chmod(&backlog, 0o755); assert_eq!(fs::read_dir(&backlog).unwrap().count(), 0); let strays: Vec = fs::read_dir(&worklane) .unwrap() .filter_map(|e| e.ok()) .map(|e| e.file_name().into_string().unwrap()) .filter(|name| name.starts_with(".wl-")) .collect(); assert_eq!(strays, Vec::::new(), "the staging file was left behind"); } /// The board above the project is real and empty, and stays empty: a dangling /// `.worklane` link used to read as no board here, and the walk then wrote the /// ticket into whatever board sat above it. #[test] fn add_writes_nothing_when_the_board_link_does_not_resolve() { let temp = TempDir::new().unwrap(); let above = setup_worklane(&temp); let project = temp.child("project"); project.create_dir_all().unwrap(); let target = temp.path().join("boards/2fed877-the-board"); std::os::unix::fs::symlink(&target, project.path().join(".worklane")).unwrap(); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); let assert = cmd .current_dir(project.path()) .args(["add", "--title", "Should not land"]) .assert() .failure(); let stderr = String::from_utf8(assert.get_output().stderr.clone()).unwrap(); assert!(stderr.contains(&target.display().to_string()), "{stderr}"); assert!(stderr.contains("boards repo"), "{stderr}"); assert_eq!(fs::read_dir(above.join("backlog")).unwrap().count(), 0); }