Files
worklane/tests/cmd_set.rs
T
Ansho ReiandClaude Opus 5 3810e442c6 (da03690) Add set and append for editing a ticket, and bump to 0.5.0
Nothing could change a ticket after it was created. mv wrote status, comment
appended a file, and that was the whole set of mutations, so correcting a typo
or adding a criterion meant hand editing the markdown.

Two commands rather than the one vague "edit" the ticket described. set replaces
a field, append adds to a section, so the verb carries the meaning instead of a
prefix on seven flag names, and append only offers the three sections that hold
multiple lines. Owner is absent from both, it belongs to the chown command filed
alongside this, and status stays mv's. One field, one writer.

Sections are a table, DESCRIPTION, ACCEPTANCE and PLAN, each carrying a heading,
a label, the placeholder add writes when empty, and a kind. Both commands walk
it, so a section added later is one entry rather than edits in two places.

Everything is targeted line surgery and never a regenerated file, which is what
keeps a ticked criterion ticked and a handwritten section intact when an
unrelated field changes. Appending to a numbered section continues from the
count already there rather than renumbering above it, so a handwritten line
survives exactly as written. Appending to an empty section replaces the
placeholder instead of following it.

An empty value is a paragraph break for a section and a usage error for a
scalar, since it looks like a way to clear a field and clearing is not supported
yet. No-op detection compares the whole file before and after each edit, so a
field set to the value it already holds is reported unchanged and nothing is
written.

Two things came out of building it. move_ticket_file's staged write is extracted
as write_then_replace, now the only way any ticket file is written. And the file
is located by uuid rather than by title: titles are not unique, add allows
duplicates on purpose, and the old title scan meant mv <uuid-of-second> moved
the first ticket sharing its title. Reproduced, fixed, and pinned by a test.

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

418 lines
13 KiB
Rust

use assert_cmd::Command;
use assert_fs::TempDir;
use assert_fs::fixture::{PathChild, PathCreateDir};
use std::fs;
use std::path::PathBuf;
const BIN_NAME: &str = env!("CARGO_BIN_EXE_worklane");
const UUID: &str = "475534a3-8a0d-45cb-9a97-39c251dd8e39";
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()
}
/// A fully populated ticket, with one criterion already ticked and a section the tool
/// knows nothing about, so every test can check both survive.
fn create_ticket(worklane: &PathBuf) -> String {
let filename = format!("{}_original-title.md", UUID);
fs::write(
worklane.join("backlog").join(&filename),
format!(
"# Original title\n\
\n\
## Description\n\
First paragraph.\n\
\n\
Second paragraph.\n\
\n\
## Acceptance Criteria\n\
- [x] Crit one\n\
- [ ] Crit two\n\
\n\
## Implementation Plan\n\
1. Step one\n\
2. Step two\n\
\n\
## Notes\n\
A handwritten note the tool knows nothing about.\n\
\n\
## Metadata\n\
\n\
- **ID**: {}\n\
- **Priority**: 3\n\
- **Tags**: alpha\n\
- **Due**: 2026-09-01\n\
- **Owner**: alice\n\
- **Created At**: 2026-08-21 02:10:17\n\
- **Status**: backlog\n",
UUID
),
)
.unwrap();
filename
}
fn run(temp: &TempDir, args: &[&str]) -> std::process::Output {
let mut cmd = Command::cargo_bin(BIN_NAME).unwrap();
cmd.current_dir(&temp.path()).args(args);
cmd.assert().get_output().clone()
}
fn ticket(worklane: &PathBuf) -> String {
let entry = fs::read_dir(worklane.join("backlog"))
.unwrap()
.filter_map(|e| e.ok())
.find(|e| e.file_name().to_string_lossy().ends_with(".md"))
.expect("no ticket file");
fs::read_to_string(entry.path()).unwrap()
}
fn stray_files(worklane: &PathBuf) -> Vec<String> {
fs::read_dir(worklane)
.unwrap()
.filter_map(|e| e.ok())
.map(|e| e.file_name().into_string().unwrap())
.filter(|name| {
!["backlog", "committed", "reviewing", "done", "comments"].contains(&name.as_str())
})
.collect()
}
#[test]
fn set_replaces_each_field_it_is_given() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
let out = run(
&temp,
&[
"set", UUID,
"--description", "Only paragraph.",
"--acceptance", "New crit",
"--plan", "New step",
"--tags", "beta,gamma",
"--due", "2026-12-31",
"--priority", "1",
],
);
assert!(out.status.success(), "{:?}", String::from_utf8_lossy(&out.stderr));
let content = ticket(&worklane);
assert!(content.contains("## Description\nOnly paragraph.\n"), "{content}");
assert!(content.contains("## Acceptance Criteria\n- [ ] New crit\n"), "{content}");
assert!(content.contains("## Implementation Plan\n1. New step\n"), "{content}");
assert!(content.contains("- **Tags**: beta,gamma"), "{content}");
assert!(content.contains("- **Due**: 2026-12-31"), "{content}");
assert!(content.contains("- **Priority**: 1"), "{content}");
// The fields it was not given are untouched
assert!(content.contains("- **Owner**: alice"), "{content}");
assert!(content.contains("- **Status**: backlog"), "{content}");
}
/// The criterion is ticked in the fixture, so an unrelated edit must leave it ticked.
#[test]
fn set_leaves_a_ticked_criterion_and_an_unknown_section_alone() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
run(&temp, &["set", UUID, "--due", "2026-12-31"]);
let content = ticket(&worklane);
assert!(content.contains("- [x] Crit one"), "the tick was lost: {content}");
assert!(
content.contains("## Notes\nA handwritten note the tool knows nothing about.\n"),
"the unknown section was lost: {content}"
);
}
#[test]
fn set_renames_the_file_for_a_new_title_and_keeps_the_uuid() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
run(&temp, &["set", UUID, "--title", "A brand new title"]);
let names: Vec<String> = fs::read_dir(worklane.join("backlog"))
.unwrap()
.filter_map(|e| e.ok())
.map(|e| e.file_name().into_string().unwrap())
.collect();
assert_eq!(names, vec![format!("{}_a-brand-new-title.md", UUID)]);
assert!(ticket(&worklane).starts_with("# A brand new title\n"));
// And the renamed ticket is still reachable, by the new title and by the uuid
for search in ["brand new", "475534a3"] {
let out = run(&temp, &["show", search]);
assert!(out.status.success(), "{search} did not resolve");
assert!(String::from_utf8_lossy(&out.stdout).contains("A brand new title"));
}
}
#[test]
fn set_reports_no_change_and_leaves_the_file_alone() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
let before = ticket(&worklane);
let out = run(&temp, &["set", UUID, "--due", "2026-09-01", "--priority", "3"]);
assert!(out.status.success());
assert!(
String::from_utf8_lossy(&out.stdout).contains("No changes"),
"{:?}",
String::from_utf8_lossy(&out.stdout)
);
assert_eq!(ticket(&worklane), before, "the file was rewritten anyway");
}
/// Only the field that actually differs is claimed, so the report cannot overstate.
#[test]
fn set_reports_only_the_fields_that_changed() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
let out = run(&temp, &["set", UUID, "--due", "2026-09-01", "--priority", "5"]);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(stdout.contains("priority"), "{stdout:?}");
assert!(!stdout.contains("due"), "{stdout:?}");
}
/// An empty scalar looks like a way to clear a field, and clearing is not supported.
#[test]
fn set_refuses_an_empty_scalar_as_a_usage_error() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
let before = ticket(&worklane);
for flag in ["--title", "--due", "--tags"] {
let out = run(&temp, &["set", UUID, flag, ""]);
assert_eq!(out.status.code(), Some(2), "{flag} was accepted");
assert!(
String::from_utf8_lossy(&out.stderr).contains("clearing a field is not supported"),
"{flag}: {:?}",
String::from_utf8_lossy(&out.stderr)
);
}
assert_eq!(ticket(&worklane), before);
}
/// An empty value is a paragraph break for a section, so an all-blank one is a
/// clearing attempt rather than a break.
#[test]
fn set_refuses_to_clear_a_section() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
let before = ticket(&worklane);
let out = run(&temp, &["set", UUID, "--description", ""]);
assert_eq!(out.status.code(), Some(1));
assert!(
String::from_utf8_lossy(&out.stderr).contains("clearing is not supported"),
"{:?}",
String::from_utf8_lossy(&out.stderr)
);
assert_eq!(ticket(&worklane), before);
}
/// A bare `set <ticket>` would otherwise be a silent no-op.
#[test]
fn set_requires_at_least_one_field() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
let out = run(&temp, &["set", UUID]);
assert_eq!(out.status.code(), Some(2));
}
/// Interior blanks are paragraph breaks, per 4c516e6, so they have to reach the file.
#[test]
fn set_keeps_a_paragraph_break_in_a_replaced_description() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
run(
&temp,
&["set", UUID, "-d", "One.", "-d", "", "-d", "Two."],
);
assert!(
ticket(&worklane).contains("## Description\nOne.\n\nTwo.\n\n## Acceptance"),
"{}",
ticket(&worklane)
);
}
#[test]
fn set_accepts_prose_that_begins_with_a_hyphen() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
let out = run(
&temp,
&["set", UUID, "-a", "--print should be supported", "-t", "-t is the title flag"],
);
assert!(out.status.success(), "{:?}", String::from_utf8_lossy(&out.stderr));
let content = ticket(&worklane);
assert!(content.contains("- [ ] --print should be supported"), "{content}");
assert!(content.starts_with("# -t is the title flag\n"), "{content}");
}
#[test]
fn set_leaves_nothing_behind() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
run(&temp, &["set", UUID, "--due", "2026-12-31"]);
assert_eq!(stray_files(&worklane), Vec::<String>::new());
}
#[test]
fn append_adds_a_paragraph_items_and_numbered_steps() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
let out = run(
&temp,
&[
"append", UUID,
"-d", "A third paragraph.",
"-a", "Crit three",
"-a", "Crit four",
"-p", "Step three",
],
);
assert!(out.status.success(), "{:?}", String::from_utf8_lossy(&out.stderr));
let content = ticket(&worklane);
assert!(
content.contains("Second paragraph.\n\nA third paragraph.\n"),
"no paragraph break: {content}"
);
assert!(
content.contains("- [x] Crit one\n- [ ] Crit two\n- [ ] Crit three\n- [ ] Crit four\n"),
"{content}"
);
// Numbered on from the last step, and the steps above are untouched
assert!(
content.contains("1. Step one\n2. Step two\n3. Step three\n"),
"{content}"
);
}
#[test]
fn append_replaces_an_empty_section_rather_than_following_its_placeholder() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let mut add = Command::cargo_bin(BIN_NAME).unwrap();
add.current_dir(&temp.path())
.args(["add", "--title", "Bare ticket"])
.assert()
.success();
let uuid = fs::read_dir(worklane.join("backlog"))
.unwrap()
.filter_map(|e| e.ok())
.map(|e| e.file_name().into_string().unwrap())
.next()
.map(|name| name.split('_').next().unwrap().to_string())
.unwrap();
run(
&temp,
&["append", &uuid, "-d", "A description arrives late.", "-a", "First crit", "-p", "First step"],
);
let content = ticket(&worklane);
assert!(!content.contains("_No description provided._"), "{content}");
assert!(!content.contains("_No acceptance criteria provided._"), "{content}");
assert!(!content.contains("_No implementation plan provided._"), "{content}");
assert!(content.contains("## Description\nA description arrives late.\n"), "{content}");
assert!(content.contains("## Implementation Plan\n1. First step\n"), "{content}");
}
#[test]
fn append_leaves_a_ticked_criterion_and_an_unknown_section_alone() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
run(&temp, &["append", UUID, "-a", "Crit three"]);
let content = ticket(&worklane);
assert!(content.contains("- [x] Crit one"), "{content}");
assert!(
content.contains("## Notes\nA handwritten note the tool knows nothing about.\n"),
"{content}"
);
}
#[test]
fn append_refuses_when_every_value_is_blank() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
let before = ticket(&worklane);
let out = run(&temp, &["append", UUID, "-a", " "]);
assert_eq!(out.status.code(), Some(1));
assert!(
String::from_utf8_lossy(&out.stderr).contains("Nothing to append"),
"{:?}",
String::from_utf8_lossy(&out.stderr)
);
assert_eq!(ticket(&worklane), before);
}
#[test]
fn append_requires_at_least_one_section() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
assert_eq!(run(&temp, &["append", UUID]).status.code(), Some(2));
}
/// Owner belongs to chown and status to mv, so neither command offers them.
#[test]
fn neither_command_can_write_owner_or_status() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
for args in [
vec!["set", UUID, "--owner", "bob"],
vec!["set", UUID, "--status", "committed"],
vec!["append", UUID, "--owner", "bob"],
] {
let out = run(&temp, &args);
assert_eq!(out.status.code(), Some(2), "{args:?} was accepted");
}
let content = ticket(&worklane);
assert!(content.contains("- **Owner**: alice"));
assert!(content.contains("- **Status**: backlog"));
}