use assert_cmd::Command; use assert_fs::fixture::{PathChild, PathCreateDir}; use assert_fs::TempDir; 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:?}" ); }