Files
worklane/tests/cmd_add.rs
T
Ansho ReiandClaude Opus 5 ddbd45e332 (dca0b61) Add priority as a ticket field
Priority lived in tags, as p1 to p3, which ls could neither sort nor
filter on, nothing validated, and a typo like P1 passed silently. ls
sorted by title only, so a backlog of any size gave no ordering signal.

Levels are 1 to 5 with 1 most urgent, matching what the tags meant.
Omitting the flag leaves the field _Not set_, like Due and Owner: there is
no implicit middle level, because a priority nobody chose is worse than
none. clap rejects anything outside the range before a ticket is written.

Priority parses to three states rather than two. A stored value outside
the range is Invalid, kept distinct from Unset, so a typo cannot read as
"no priority": it shows as ? and sorts just ahead of the unset tail.

ls sorts on priority and falls back to the old title sort, and --priority
filters, composing with the status filter in both the grouped and the
filtered listing. TicketItem becomes a struct, since a sixth tuple field
was past reading.

Migrated the 31 tickets already carrying p1 to p3 tags: the level moves to
the field and the tag is dropped.

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

265 lines
8.0 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");
}