Files
worklane/tests/cmd_add.rs
T
Ansho ReiandClaude Opus 5 f5204f6d8b (866a4eb) Accept prose values that begin with a hyphen, and bump to 0.3.2
clap read any value starting with a hyphen as a flag, so a criterion quoting
--print was refused with "unexpected argument found" and no ticket was written
at all. Ticket text quotes flag names constantly, and the space form is what
both a person and the opencode wrapper produce, so the failure was routine.

allow_hyphen_values on the five arguments that carry prose: add --title,
--description, --acceptance, --plan, and the comment body. The trade is that a
genuinely mistyped flag there now lands as content instead of raising a usage
error, which is the right way round for prose and the wrong way round for
anything else, so due, priority, tags, owner and status are left alone and still
fail with exit 2. Written down in AGENTS.md.

The bare double hyphen is the one shape this does not reach: as an option value
it is stored as text, but in a positional it is clap's end-of-options marker and
is consumed before assignment. A comment body of exactly -- therefore needs its
own separator. Without it the body is reported missing and nothing is written,
so it is a clear failure rather than a silent drop. Tested, along with the
ordering that lets mv reach a hyphen-leading title.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-20 18:00:52 +02:00

383 lines
12 KiB
Rust

use assert_cmd::Command;
use assert_fs::fixture::{PathChild, PathCreateDir};
use assert_fs::TempDir;
use std::fs;
use std::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: &PathBuf) -> 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()
}