Files
worklane/tests/cmd_chown.rs
T
Ansho ReiandClaude Opus 5 b56b31d2d9 (f39b651) Add chown for the ticket owner, and bump to 0.10.0
The Owner field had no writer after `add`. It was folded into `set` in da03690
and comes out here as its own command, so no field is writable from two places,
the same reasoning that keeps status in `mv` alone.

`worklane chown alice 641e3d5`, user first, after Unix `chown`. That reverses
the ticket-first order `mv`, `comment` and `show` use, deliberately: the name
borrows the Unix association, so the argument order borrows it too.

The swap is left to resolution rather than checked for. Given `chown 641e3d5
alice` the second argument has to resolve as a ticket, and a bare username does
not, so the call fails on its own; a check on the first argument would only fire
where resolution already refuses and would stay silent on the one case it cannot
catch, a ticket whose title is the username. `cmd_chown` is therefore
`set_metadata_field` plus `save_ticket`, with the whole-file no-op comparison
`set` uses and the same `report()` line, so an owner set to the value it already
holds is reported unchanged and nothing is written.

Nothing new writes a file: the update goes through `write_then_replace`, scoped
to the lines under `## Metadata`, so a description quoting `**Owner**:` is left
as prose and a failure leaves the ticket whole. Tested for the change, the
no-op, all three resolution tiers, a swapped call, an empty user, a ticket with
no Owner field, and that the tick, the handwritten section and the filename all
survive.

The skill and the opencode tool carry it too, the tool with the argument order
in its description, since that is the one place a model can get it wrong
without failing.

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

283 lines
8.8 KiB
Rust

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");
const UUID: &str = "641e3d5c-1b0e-4d2f-9a7e-3c8b5f0a2d61";
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: &Path) {
fs::write(
worklane
.join("backlog")
.join(format!("{}_original-title.md", UUID)),
format!(
"# Original title\n\
\n\
## Description\n\
The **Owner**: line quoted here is prose, not metadata.\n\
\n\
## Acceptance Criteria\n\
- [x] Crit one\n\
- [ ] Crit two\n\
\n\
## Implementation Plan\n\
1. Step one\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();
}
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: &Path) -> 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: &Path) -> 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 chown_sets_the_owner() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
let out = run(&temp, &["chown", "bob", UUID]);
assert!(
out.status.success(),
"{:?}",
String::from_utf8_lossy(&out.stderr)
);
assert!(
String::from_utf8_lossy(&out.stdout).contains("owner"),
"{:?}",
String::from_utf8_lossy(&out.stdout)
);
let content = ticket(&worklane);
assert!(content.contains("- **Owner**: bob"), "{content}");
// Every other field is left as it was
assert!(content.contains("- **Priority**: 3"), "{content}");
assert!(content.contains("- **Tags**: alpha"), "{content}");
assert!(content.contains("- **Due**: 2026-09-01"), "{content}");
assert!(content.contains("- **Status**: backlog"), "{content}");
}
/// The value is free text, matching `add --owner`, and is checked against nothing:
/// not git, not a list of users.
#[test]
fn chown_accepts_any_user_without_validating_it() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
run(&temp, &["chown", "Nobody At All", UUID]);
assert!(
ticket(&worklane).contains("- **Owner**: Nobody At All"),
"{}",
ticket(&worklane)
);
}
/// The tick and the unknown section are what a regenerated file would lose, and the
/// quoted `**Owner**:` line in the description is what an unscoped rewrite would take
/// instead of the real field.
#[test]
fn chown_leaves_everything_it_was_not_asked_about_alone() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
run(&temp, &["chown", "bob", UUID]);
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}"
);
assert!(
content.contains("The **Owner**: line quoted here is prose, not metadata.\n"),
"the prose was rewritten as metadata: {content}"
);
}
#[test]
fn chown_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, &["chown", "alice", UUID]);
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");
}
/// Resolution is `resolve_ticket`, the same as `mv` and `comment`, so all three tiers
/// reach the ticket. The filename search reaches the middle of the uuid, which the
/// prefix tier cannot.
#[test]
fn chown_resolves_a_uuid_prefix_a_title_or_a_filename() {
for search in ["641e", "Original", "4d2f"] {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
let out = run(&temp, &["chown", "bob", search]);
assert!(
out.status.success(),
"{search} did not resolve: {:?}",
String::from_utf8_lossy(&out.stderr)
);
assert!(
ticket(&worklane).contains("- **Owner**: bob"),
"{search}: {}",
ticket(&worklane)
);
}
}
/// The swap is left to resolution rather than checked for: the username has to resolve
/// as a ticket, and a bare one does not.
#[test]
fn chown_refuses_a_swapped_call() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
let before = ticket(&worklane);
let out = run(&temp, &["chown", UUID, "bob"]);
assert_eq!(out.status.code(), Some(1));
assert!(
String::from_utf8_lossy(&out.stderr).contains("No ticket found matching 'bob'"),
"{:?}",
String::from_utf8_lossy(&out.stderr)
);
assert_eq!(ticket(&worklane), before);
}
/// An empty user looks like a way to clear the field, and clearing is not supported.
#[test]
fn chown_refuses_an_empty_user_as_a_usage_error() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
let before = ticket(&worklane);
let out = run(&temp, &["chown", "", UUID]);
assert_eq!(out.status.code(), Some(2));
assert!(
String::from_utf8_lossy(&out.stderr).contains("clearing a field is not supported"),
"{:?}",
String::from_utf8_lossy(&out.stderr)
);
assert_eq!(ticket(&worklane), before);
}
/// A hand-made ticket without the field is refused rather than guessed at, the same
/// as every other metadata field.
#[test]
fn chown_refuses_a_ticket_with_no_owner_field() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
fs::write(
worklane.join("backlog").join(format!("{}_bare.md", UUID)),
format!("# Bare\n\n## Metadata\n\n- **ID**: {}\n", UUID),
)
.unwrap();
let out = run(&temp, &["chown", "bob", UUID]);
assert_eq!(out.status.code(), Some(1));
assert!(
String::from_utf8_lossy(&out.stderr).contains("has no Owner field"),
"{:?}",
String::from_utf8_lossy(&out.stderr)
);
}
/// The staged temp file is renamed into place, so a completed write leaves nothing to
/// be read as a stray ticket.
#[test]
fn chown_leaves_nothing_behind() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
run(&temp, &["chown", "bob", UUID]);
assert_eq!(stray_files(&worklane), Vec::<String>::new());
}
/// The file keeps its name: the slug follows the title, and the owner is not in it.
#[test]
fn chown_does_not_rename_the_file() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(&worklane);
run(&temp, &["chown", "bob", UUID]);
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!("{}_original-title.md", UUID)]);
}