Files
worklane/tests/cmd_mv.rs
T

1492 lines
49 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");
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()
}
/// Every metadata field a ticket has, positionally. The lint is right that the call
/// site reads as a wall of strings, and the fix is a struct with named fields, but
/// that is a change to thirteen call sites in this file alone and belongs in its own
/// commit rather than in a warning sweep.
#[allow(clippy::too_many_arguments)]
fn create_ticket(
worklane_dir: &Path,
status: &str,
title: &str,
description: &str,
id: &str,
owner: &str,
due: &str,
tags: &str,
) -> String {
let slug = title
.to_lowercase()
.chars()
.filter(|c| c.is_alphanumeric() || *c == ' ' || *c == '-' || *c == '_')
.map(|c| if c == ' ' { '-' } else { c })
.collect::<String>();
let filename = format!("{}_{}.md", id, slug);
let ticket_body = format!(
"# {}\n\
\n\
## Description\n\
{}\n\
\n\
## Acceptance Criteria\n\
- [ ] test\n\
\n\
## Implementation Plan\n\
- test\n\
\n\
## Metadata\n\
\n\
- **ID**: {}\n\
- **Tags**: {}\n\
- **Due**: {}\n\
- **Owner**: {}\n\
- **Status**: {}\n\
",
title, description, id, tags, due, owner, status
);
let path = worklane_dir.join(status).join(&filename);
fs::write(&path, ticket_body).unwrap();
filename
}
fn get_stdout(cmd: &mut Command) -> String {
let assert = cmd.assert();
String::from_utf8(assert.get_output().stdout.clone()).unwrap()
}
fn get_stderr(cmd: &mut Command) -> String {
let assert = cmd.assert();
String::from_utf8(assert.get_output().stderr.clone()).unwrap()
}
#[test]
fn mv_forwards_from_backlog_to_committed() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let id = "abc123def456789012345678901234567890";
let filename = create_ticket(
&worklane,
"backlog",
"Task One",
"Move this ticket.",
id,
"alice",
"2026-12-31",
"test",
);
let mut cmd = Command::cargo_bin(BIN_NAME).unwrap();
cmd.current_dir(temp.path())
.arg("mv")
.arg("abc123def4567")
.arg("--status")
.arg("committed");
let assert = cmd.assert();
let stdout = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
assert.success();
assert!(stdout.contains("Task One"));
assert!(stdout.contains("backlog"));
assert!(stdout.contains("committed"));
// Verify file moved
assert!(!worklane.join("backlog").join(&filename).exists());
assert!(worklane.join("committed").join(&filename).exists());
// Verify status updated
let content = fs::read_to_string(worklane.join("committed").join(&filename)).unwrap();
assert!(content.contains("- **Status**: committed"));
// No stray blank line inserted after the rewritten field
assert!(content.ends_with("- **Status**: committed\n"));
}
#[test]
fn mv_blocks_backward_move() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let id = "abc123def456789012345678901234567890";
create_ticket(
&worklane,
"committed",
"Committed Task",
"Should stay committed.",
id,
"bob",
"2026-11-15",
"test",
);
let mut cmd = Command::cargo_bin(BIN_NAME).unwrap();
cmd.current_dir(temp.path())
.arg("mv")
.arg("abc123def4567")
.arg("--status")
.arg("backlog")
.assert()
.failure();
let stderr = get_stderr(&mut cmd);
assert!(stderr.contains("Cannot move backward"));
}
#[test]
fn mv_allows_backward_move_with_force() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let id = "abc123def456789012345678901234567890";
let filename = create_ticket(
&worklane,
"committed",
"Backward Move",
"Force backward.",
id,
"charlie",
"2026-10-01",
"test",
);
let mut cmd = Command::cargo_bin(BIN_NAME).unwrap();
cmd.current_dir(temp.path())
.arg("mv")
.arg("abc123def4567")
.arg("--status")
.arg("backlog")
.arg("-f");
let assert = cmd.assert();
let stdout = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
assert.success();
assert!(stdout.contains("Backward Move"));
assert!(stdout.contains("backlog"));
assert!(worklane.join("backlog").join(&filename).exists());
}
#[test]
fn mv_blocks_nonsequential_forward_move() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let id = "abc123def456789012345678901234567890";
create_ticket(
&worklane,
"backlog",
"Jump Task",
"Should not skip.",
id,
"dave",
"2026-09-01",
"test",
);
let mut cmd = Command::cargo_bin(BIN_NAME).unwrap();
cmd.current_dir(temp.path())
.arg("mv")
.arg("abc123def4567")
.arg("--status")
.arg("done")
.assert()
.failure();
let stderr = get_stderr(&mut cmd);
assert!(stderr.contains("one stage forward"));
}
#[test]
fn mv_allows_skipping_stages_with_force() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let id = "abc123def456789012345678901234567890";
let filename = create_ticket(
&worklane,
"backlog",
"Skip Task",
"Force jump.",
id,
"eve",
"2026-08-01",
"test",
);
let mut cmd = Command::cargo_bin(BIN_NAME).unwrap();
cmd.current_dir(temp.path())
.arg("mv")
.arg("abc123def4567")
.arg("--status")
.arg("done")
.arg("-f")
.assert()
.success();
assert!(worklane.join("done").join(&filename).exists());
let content = fs::read_to_string(worklane.join("done").join(&filename)).unwrap();
assert!(content.contains("- **Status**: done"));
}
#[test]
fn mv_ignores_already_in_same_status() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let id = "abc123def456789012345678901234567890";
let filename = create_ticket(
&worklane,
"reviewing",
"Same Status",
"Already there.",
id,
"frank",
"2026-07-01",
"test",
);
let mut cmd = Command::cargo_bin(BIN_NAME).unwrap();
cmd.current_dir(temp.path())
.arg("mv")
.arg("abc123def4567")
.arg("--status")
.arg("reviewing")
.assert()
.success();
let stdout = get_stdout(&mut cmd);
assert!(stdout.contains("already in"));
assert!(worklane.join("reviewing").join(&filename).exists());
}
/// A status is a value enum, so an unknown one is clap's to refuse: exit 2, the four
/// named, and the ticket untouched. The same bargain `--due` and `--priority` take, and
/// the reason `cmd_mv` no longer checks.
#[test]
fn mv_rejects_invalid_status() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let id = "abc123def456789012345678901234567890";
let filename = create_ticket(
&worklane,
"backlog",
"Invalid Target",
"Bad status.",
id,
"grace",
"2026-06-01",
"test",
);
let mut cmd = Command::cargo_bin(BIN_NAME).unwrap();
cmd.current_dir(temp.path())
.arg("mv")
.arg("abc123def4567")
.arg("--status")
.arg("invalid")
.assert()
.code(2);
let stderr = get_stderr(&mut cmd);
assert!(stderr.contains("invalid value 'invalid'"), "{stderr}");
for status in ["backlog", "committed", "reviewing", "done"] {
assert!(stderr.contains(status), "{status} is not offered: {stderr}");
}
assert!(
worklane.join("backlog").join(&filename).exists(),
"the ticket moved anyway"
);
}
#[test]
fn mv_uses_ticket_id_for_resolution() {
let temp = TempDir::new().unwrap();
let wl = setup_worklane(&temp);
let id = "abc123def456789012345678901234567890";
let filename = create_ticket(
&wl,
"backlog",
"ID Resolution",
"Use ID to find.",
id,
"hank",
"2026-05-01",
"test",
);
let mut cmd = Command::cargo_bin(BIN_NAME).unwrap();
cmd.current_dir(temp.path())
.arg("mv")
.arg("abc123def4567")
.arg("--status")
.arg("committed");
let assert = cmd.assert();
let stdout = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
assert.success();
assert!(stdout.contains("ID Resolution"));
assert!(wl.join("committed").join(&filename).exists());
}
#[test]
fn mv_keeps_status_in_sync_across_consecutive_moves() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let id = "abc123def456789012345678901234567890";
let filename = create_ticket(
&worklane,
"backlog",
"Two Hop",
"Moved twice.",
id,
"ida",
"2026-04-01",
"test",
);
for target in ["committed", "reviewing"] {
Command::cargo_bin(BIN_NAME)
.unwrap()
.current_dir(temp.path())
.arg("mv")
.arg("abc123def4567")
.arg("--status")
.arg(target)
.assert()
.success();
}
assert!(worklane.join("reviewing").join(&filename).exists());
// The second move must update the field too, not silently skip it
let content = fs::read_to_string(worklane.join("reviewing").join(&filename)).unwrap();
assert!(content.contains("- **Status**: reviewing"));
assert!(!content.contains("-Status"));
assert!(!content.contains("committed"));
}
#[test]
fn mv_repairs_legacy_status_field() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = "abc123def456789012345678901234567890_legacy.md";
let path = worklane.join("committed").join(filename);
fs::write(
&path,
"# Legacy Ticket\n\
\n\
## Metadata\n\
\n\
- **ID**: abc123def456789012345678901234567890\n\
- **-Status**: committed\n",
)
.unwrap();
Command::cargo_bin(BIN_NAME)
.unwrap()
.current_dir(temp.path())
.arg("mv")
.arg("abc123def4567")
.arg("--status")
.arg("reviewing")
.assert()
.success();
let content = fs::read_to_string(worklane.join("reviewing").join(filename)).unwrap();
assert!(content.contains("- **Status**: reviewing"));
assert!(!content.contains("-Status"));
}
#[test]
fn mv_fails_when_status_field_is_missing() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = "abc123def456789012345678901234567890_no-status.md";
let path = worklane.join("backlog").join(filename);
fs::write(&path, "# No Status\n\n## Metadata\n\n- **Owner**: jack\n").unwrap();
let mut cmd = Command::cargo_bin(BIN_NAME).unwrap();
cmd.current_dir(temp.path())
.arg("mv")
.arg("abc123def4567")
.arg("--status")
.arg("committed")
.assert()
.failure();
let stderr = get_stderr(&mut cmd);
assert!(stderr.contains("No Status"));
assert!(stderr.contains("has no Status field"));
// The ticket must not be half-moved
assert!(worklane.join("backlog").join(filename).exists());
assert!(!worklane.join("committed").join(filename).exists());
}
use std::os::unix::fs::PermissionsExt;
fn chmod(path: &std::path::Path, mode: u32) {
let mut perms = fs::metadata(path).unwrap().permissions();
perms.set_mode(mode);
fs::set_permissions(path, perms).unwrap();
}
fn md_files(dir: &std::path::Path) -> Vec<String> {
match fs::read_dir(dir) {
Ok(entries) => entries
.filter_map(|e| e.ok())
.map(|e| e.file_name().into_string().unwrap())
.filter(|name| name.ends_with(".md"))
.collect(),
Err(_) => vec![],
}
}
/// Nothing is staged in a status directory, so nothing can be left behind that the
/// listing would read as a ticket.
fn stray_files(worklane: &std::path::Path) -> Vec<String> {
let mut stray: 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", "history"].contains(&name.as_str())
})
.collect();
for dir in ["backlog", "committed", "reviewing", "done"] {
stray.extend(
fs::read_dir(worklane.join(dir))
.into_iter()
.flatten()
.filter_map(|e| e.ok())
.map(|e| e.file_name().into_string().unwrap())
.filter(|name| !name.ends_with(".md")),
);
}
stray
}
/// The bug this fixes. The old order renamed first and wrote second, so a write
/// that failed left the ticket in its new directory still claiming the old status.
/// A read-only ticket file is the cheapest way to make the write fail.
#[test]
fn mv_never_leaves_stale_content_in_the_target_directory() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = create_ticket(
&worklane,
"backlog",
"Read Only",
"A description.",
"abc123def456789012345678901234567890",
"alice",
"2026-12-31",
"test",
);
let source = worklane.join("backlog").join(&filename);
chmod(&source, 0o444);
let mut cmd = Command::cargo_bin(BIN_NAME).unwrap();
cmd.current_dir(temp.path())
.args(["mv", "abc123def4567", "--status", "committed"])
.assert()
.success();
// Wherever it ended up, its Status must agree with the directory it is in
let moved = worklane.join("committed").join(&filename);
assert!(moved.exists(), "ticket did not move");
let content = fs::read_to_string(&moved).unwrap();
assert!(content.contains("- **Status**: committed"), "stale status: {content:?}");
assert!(!content.contains("- **Status**: backlog"), "{content:?}");
assert_eq!(md_files(&worklane.join("backlog")).len(), 0);
assert_eq!(stray_files(&worklane), Vec::<String>::new());
}
/// The other criterion: one directory or the other, never both. An unwritable
/// target directory fails the move at the last step, which is the step that would
/// have to be reordered to break this.
#[test]
fn mv_leaves_the_ticket_in_one_place_when_the_move_fails() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = create_ticket(
&worklane,
"backlog",
"Blocked Move",
"A description.",
"abc123def456789012345678901234567890",
"alice",
"2026-12-31",
"test",
);
let target = worklane.join("committed");
chmod(&target, 0o555);
let mut cmd = Command::cargo_bin(BIN_NAME).unwrap();
let assert = cmd
.current_dir(temp.path())
.args(["mv", "abc123def4567", "--status", "committed"])
.assert()
.failure();
let stderr = String::from_utf8(assert.get_output().stderr.clone()).unwrap();
// Named, not a bare io error
assert!(stderr.contains("Blocked Move"), "{stderr}");
assert_eq!(
md_files(&worklane.join("backlog")),
vec![filename.clone()],
"the ticket left its own directory"
);
assert_eq!(md_files(&target).len(), 0, "the ticket is in both places");
assert_eq!(stray_files(&worklane), Vec::<String>::new());
// The documented worst case: still in its old directory, carrying a Status one
// move ahead because the content was written before the move was attempted.
let left = fs::read_to_string(worklane.join("backlog").join(&filename)).unwrap();
assert!(left.contains("- **Status**: committed"), "{left:?}");
// And the state is recoverable, because the directory is what decides the
// current status, so a retry moves it rather than refusing.
chmod(&target, 0o755);
let mut retry = Command::cargo_bin(BIN_NAME).unwrap();
retry
.current_dir(temp.path())
.args(["mv", "abc123def4567", "--status", "committed"])
.assert()
.success();
assert_eq!(md_files(&target), vec![filename]);
assert_eq!(md_files(&worklane.join("backlog")).len(), 0);
}
#[test]
fn mv_leaves_nothing_behind_on_success() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(
&worklane,
"backlog",
"Clean Move",
"A description.",
"abc123def456789012345678901234567890",
"alice",
"2026-12-31",
"test",
);
let mut cmd = Command::cargo_bin(BIN_NAME).unwrap();
cmd.current_dir(temp.path())
.args(["mv", "abc123def4567", "--status", "committed"])
.assert()
.success();
assert_eq!(stray_files(&worklane), Vec::<String>::new());
}
/// Duplicate titles are allowed, so the file cannot be located by title. It used to be,
/// and `mv <uuid-of-second>` moved the first ticket that shared its title.
#[test]
fn mv_moves_the_resolved_ticket_when_two_share_a_title() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
for uuid in [
"11111111-aaaa-bbbb-cccc-dddddddddddd",
"22222222-aaaa-bbbb-cccc-dddddddddddd",
] {
fs::write(
worklane.join("backlog").join(format!("{}_foo.md", uuid)),
format!("# Foo\n\n## Metadata\n\n- **ID**: {}\n- **Status**: backlog\n", uuid),
)
.unwrap();
}
let mut cmd = Command::cargo_bin(BIN_NAME).unwrap();
cmd.current_dir(temp.path())
.args(["mv", "22222222", "--status", "committed"])
.assert()
.success();
assert_eq!(
md_files(&worklane.join("committed")),
vec!["22222222-aaaa-bbbb-cccc-dddddddddddd_foo.md".to_string()],
"the wrong ticket moved"
);
assert_eq!(
md_files(&worklane.join("backlog")),
vec!["11111111-aaaa-bbbb-cccc-dddddddddddd_foo.md".to_string()]
);
}
// --------------------------------------------------------------- blocked by --
/// A ticket waiting on another, and a second one for it to wait on. Returns the
/// filename of the gated ticket, whose relations the moves are then checked against.
fn create_gated_pair(worklane: &std::path::Path, temp: &TempDir, status: &str) -> String {
let filename = create_ticket(
worklane,
status,
"Gated Task",
"Waits on the blocker.",
"aaaa1111aaaa1111aaaa1111aaaa1111aaaa",
"alice",
"_Not set_",
"test",
);
create_ticket(
worklane,
"backlog",
"The Blocker",
"Nothing waits on this.",
"bbbb2222bbbb2222bbbb2222bbbb2222bbbb",
"bob",
"_Not set_",
"test",
);
// Through the command, so the fixture cannot store a relation in a shape the tool
// does not actually write.
Command::cargo_bin(BIN_NAME)
.unwrap()
.current_dir(temp.path())
.args(["relate", "aaaa1111", "--blocked-by", "bbbb2222"])
.assert()
.success();
filename
}
fn move_ticket(temp: &TempDir, ticket: &str, status: &str, force: bool) -> std::process::Output {
let mut cmd = Command::cargo_bin(BIN_NAME).unwrap();
cmd.current_dir(temp.path())
.args(["mv", ticket, "--status", status]);
if force {
cmd.arg("-f");
}
cmd.assert().get_output().clone()
}
/// Refused, and nothing written: not the rename, not the Status field, not a stray
/// temp file. The blocker is named with its status, since that is what has to change.
#[test]
fn mv_refuses_a_move_while_a_blocker_is_open() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = create_gated_pair(&worklane, &temp, "backlog");
let before = fs::read_to_string(worklane.join("backlog").join(&filename)).unwrap();
let out = move_ticket(&temp, "aaaa1111", "committed", false);
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(stderr.contains("blocked by"), "{stderr}");
assert!(stderr.contains("The Blocker"), "{stderr}");
assert!(stderr.contains("backlog"), "{stderr}");
assert!(stderr.contains("--force"), "{stderr}");
assert!(worklane.join("backlog").join(&filename).exists());
assert_eq!(md_files(&worklane.join("committed")).len(), 0);
assert_eq!(
fs::read_to_string(worklane.join("backlog").join(&filename)).unwrap(),
before,
"a refused move rewrote the ticket"
);
assert_eq!(stray_files(&worklane), Vec::<String>::new());
}
/// Every open blocker, not the first: clearing the one the message happened to name
/// would leave the ticket blocked and the reader surprised.
#[test]
fn mv_names_every_open_blocker() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_gated_pair(&worklane, &temp, "backlog");
create_ticket(
&worklane,
"reviewing",
"The Second Blocker",
"Also not done.",
"cccc3333cccc3333cccc3333cccc3333cccc",
"bob",
"_Not set_",
"test",
);
Command::cargo_bin(BIN_NAME)
.unwrap()
.current_dir(temp.path())
.args(["relate", "aaaa1111", "--blocked-by", "cccc3333"])
.assert()
.success();
let out = move_ticket(&temp, "aaaa1111", "committed", false);
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(stderr.contains("The Blocker"), "{stderr}");
assert!(stderr.contains("The Second Blocker"), "{stderr}");
}
/// `-f` is where the deliberate overrides live, beside the backward and skipping moves.
#[test]
fn mv_moves_a_blocked_ticket_with_force() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = create_gated_pair(&worklane, &temp, "backlog");
let out = move_ticket(&temp, "aaaa1111", "committed", true);
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
assert!(worklane.join("committed").join(&filename).exists());
}
#[test]
fn mv_allows_the_move_once_the_blocker_is_done() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = create_gated_pair(&worklane, &temp, "backlog");
for status in ["committed", "reviewing", "done"] {
assert!(
move_ticket(&temp, "bbbb2222", status, false).status.success(),
"the blocker itself could not be moved to {status}"
);
}
let out = move_ticket(&temp, "aaaa1111", "committed", false);
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
assert!(worklane.join("committed").join(&filename).exists());
}
/// Every move, not only the one into committed. An open blocker is a reason not to
/// finish as much as a reason not to start.
#[test]
fn mv_checks_the_blockers_on_every_move() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = create_gated_pair(&worklane, &temp, "reviewing");
let out = move_ticket(&temp, "aaaa1111", "done", false);
assert!(!out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
assert!(worklane.join("reviewing").join(&filename).exists());
}
/// A move to the status the ticket is already in is not a move, so it is reported the
/// way it always was rather than refused.
#[test]
fn mv_does_not_check_the_blockers_on_a_no_op() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_gated_pair(&worklane, &temp, "backlog");
let out = move_ticket(&temp, "aaaa1111", "backlog", false);
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
assert!(String::from_utf8_lossy(&out.stdout).contains("already in"));
}
/// A dangling target is a bookkeeping fault, not a dependency. Refusing there would
/// wedge the ticket with no way out but `--force`.
#[test]
fn mv_warns_and_allows_when_a_blocker_is_not_on_the_board() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = create_gated_pair(&worklane, &temp, "backlog");
fs::remove_file(
worklane
.join("backlog")
.join("bbbb2222bbbb2222bbbb2222bbbb2222bbbb_the-blocker.md"),
)
.unwrap();
let out = move_ticket(&temp, "aaaa1111", "committed", false);
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
assert!(
String::from_utf8_lossy(&out.stderr).contains("not on the board"),
"{}",
String::from_utf8_lossy(&out.stderr)
);
assert!(worklane.join("committed").join(&filename).exists());
}
/// Only `blocked by` gates a move. The other two kinds are reference data.
#[test]
fn mv_ignores_the_kinds_that_are_not_blockers() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = create_ticket(
&worklane,
"backlog",
"Related Task",
"Points at an open ticket.",
"aaaa1111aaaa1111aaaa1111aaaa1111aaaa",
"alice",
"_Not set_",
"test",
);
create_ticket(
&worklane,
"backlog",
"The Other",
"Still open.",
"bbbb2222bbbb2222bbbb2222bbbb2222bbbb",
"bob",
"_Not set_",
"test",
);
Command::cargo_bin(BIN_NAME)
.unwrap()
.current_dir(temp.path())
.args([
"relate",
"aaaa1111",
"--regression-of",
"bbbb2222",
"--relates-to",
"bbbb2222",
])
.assert()
.success();
let out = move_ticket(&temp, "aaaa1111", "committed", false);
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
assert!(worklane.join("committed").join(&filename).exists());
}
/// The value of a metadata field, or nothing when the ticket does not carry it.
fn field(content: &str, name: &str) -> Option<String> {
let prefix = format!("- **{}**: ", name);
content
.lines()
.find_map(|line| line.strip_prefix(prefix.as_str()))
.map(str::to_string)
}
/// `%Y-%m-%d %H:%M:%S`, checked by shape. A test cannot pin the clock, so what it can
/// assert is that the stamp carries the same format `Created At` does.
fn is_timestamp(value: &str) -> bool {
value.len() == 19
&& value.chars().enumerate().all(|(index, c)| match index {
4 | 7 => c == '-',
10 => c == ' ',
13 | 16 => c == ':',
_ => c.is_ascii_digit(),
})
}
fn stamped_ticket(worklane: &Path, status: &str) -> String {
create_ticket(
worklane,
status,
"Stamped Task",
"Carries the stamps.",
"cccc3333cccc3333cccc3333cccc3333cccc",
"alice",
"_Not set_",
"test",
)
}
fn ticket_at(worklane: &Path, status: &str, filename: &str) -> String {
fs::read_to_string(worklane.join(status).join(filename)).unwrap()
}
/// One field per status, written with the time of the move and added to the ones
/// already there: walking the flow leaves the whole journey on the ticket.
#[test]
fn mv_stamps_the_field_of_the_status_it_enters() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = stamped_ticket(&worklane, "backlog");
// The fixture predates Created At as well as the stamps, and the four reading as
// one block is the point, so it is seeded rather than asserted around.
let path = worklane.join("backlog").join(&filename);
let seeded = fs::read_to_string(&path)
.unwrap()
.replace("- **Status**:", "- **Created At**: 2026-08-01 09:00:00\n- **Status**:");
fs::write(&path, seeded).unwrap();
let mut carried: Vec<&str> = Vec::new();
for (status, stamp) in [
("committed", "Committed On"),
("reviewing", "Submitted On"),
("done", "Completed On"),
] {
let out = move_ticket(&temp, "cccc3333", status, false);
assert!(out.status.success(), "{:?}", out);
carried.push(stamp);
let content = ticket_at(&worklane, status, &filename);
for stamp in &carried {
let value = field(&content, stamp)
.unwrap_or_else(|| panic!("no {stamp} after moving to {status}: {content}"));
assert!(is_timestamp(&value), "{stamp} reads '{value}'");
}
}
// The four side by side and in flow order, which is what a reader asking when this
// was filed, picked up, handed over and finished gets one answer each from.
let content = ticket_at(&worklane, "done", &filename);
let block: Vec<&str> = content
.lines()
.filter_map(|line| line.strip_prefix("- **"))
.filter_map(|line| line.split("**:").next())
.skip_while(|name| *name != "Created At")
.collect();
assert_eq!(
block,
[
"Created At",
"Committed On",
"Submitted On",
"Completed On",
"Status"
],
"{content}"
);
}
/// Arrival clears what comes after it and nothing before, so a ticket sent back out of
/// done keeps the stages it did get through.
#[test]
fn mv_removes_the_stamps_of_the_statuses_after_the_one_it_enters() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = stamped_ticket(&worklane, "backlog");
for status in ["committed", "reviewing", "done"] {
move_ticket(&temp, "cccc3333", status, false);
}
move_ticket(&temp, "cccc3333", "reviewing", true);
let content = ticket_at(&worklane, "reviewing", &filename);
assert_eq!(field(&content, "Completed On"), None, "{content}");
assert!(field(&content, "Committed On").is_some(), "{content}");
assert!(field(&content, "Submitted On").is_some(), "{content}");
}
/// Backlog has no stamp of its own and every status follows it, so a ticket forced back
/// to it carries none at all rather than a Completed On for work that has been undone.
#[test]
fn mv_back_to_backlog_removes_all_three_stamps() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = stamped_ticket(&worklane, "backlog");
move_ticket(&temp, "cccc3333", "done", true);
let done = ticket_at(&worklane, "done", &filename);
assert!(field(&done, "Completed On").is_some(), "{done}");
move_ticket(&temp, "cccc3333", "backlog", true);
let content = ticket_at(&worklane, "backlog", &filename);
for stamp in ["Committed On", "Submitted On", "Completed On"] {
assert_eq!(field(&content, stamp), None, "{content}");
}
}
/// A skip stamps where it lands and leaves the stages it jumped unwritten: an absent
/// field says the ticket never got that far, and inventing one would say it did.
#[test]
fn mv_skipping_forward_stamps_only_the_target() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = stamped_ticket(&worklane, "backlog");
move_ticket(&temp, "cccc3333", "done", true);
let content = ticket_at(&worklane, "done", &filename);
assert!(field(&content, "Completed On").is_some(), "{content}");
assert_eq!(field(&content, "Committed On"), None, "{content}");
assert_eq!(field(&content, "Submitted On"), None, "{content}");
}
/// The three are `mv`'s alone. Nothing that edits a ticket by hand offers a way to
/// write one or take one away, so a stamp is always the record of a real move. Pinned
/// so the omission stays a decision rather than becoming an oversight somebody fills in.
#[test]
fn no_other_command_can_write_or_clear_a_stamp() {
let temp = TempDir::new().unwrap();
setup_worklane(&temp);
for command in ["add", "set", "append", "chown"] {
let mut cmd = Command::cargo_bin(BIN_NAME).unwrap();
cmd.current_dir(temp.path()).args([command, "--help"]);
let out = cmd.assert().get_output().clone();
let help = String::from_utf8_lossy(&out.stdout).to_lowercase();
for flag in ["committed-on", "submitted-on", "completed-on", "stamp"] {
assert!(!help.contains(flag), "{command} --help names {flag}: {help}");
}
}
}
/// Re-entry is the time of the move that brought the ticket back, not the first one.
#[test]
fn mv_restamps_a_status_the_ticket_is_re_entering() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = stamped_ticket(&worklane, "committed");
// Hand-written so the earlier value is old enough to tell from the move's own time,
// which two moves in the same second would not be.
let path = worklane.join("committed").join(&filename);
let seeded = fs::read_to_string(&path)
.unwrap()
.replace("- **Status**:", "- **Committed On**: 2020-01-01 00:00:00\n- **Status**:");
fs::write(&path, seeded).unwrap();
move_ticket(&temp, "cccc3333", "backlog", true);
move_ticket(&temp, "cccc3333", "committed", false);
let content = ticket_at(&worklane, "committed", &filename);
let value = field(&content, "Committed On").expect(&content);
assert!(is_timestamp(&value), "{value}");
assert_ne!(value, "2020-01-01 00:00:00");
assert_eq!(content.matches("**Committed On**:").count(), 1, "{content}");
}
/// The fixture predates the fields, as every ticket on a real board does, so the line
/// is inserted rather than rewritten. Against METADATA_ORDER, which keeps Status last.
#[test]
fn mv_inserts_the_stamp_before_the_status_field() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = stamped_ticket(&worklane, "backlog");
move_ticket(&temp, "cccc3333", "committed", false);
let content = ticket_at(&worklane, "committed", &filename);
let value = field(&content, "Committed On").expect(&content);
assert!(
content.contains(&format!(
"- **Owner**: alice\n- **Committed On**: {}\n- **Status**: committed\n",
value
)),
"{content}"
);
assert!(content.ends_with("- **Status**: committed\n"), "{content}");
}
/// Behind the refusals, all of them: a move that does not happen leaves the file exactly
/// as it was, stamp included.
#[test]
fn mv_writes_no_stamp_when_the_move_is_refused() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let backward = stamped_ticket(&worklane, "committed");
let before = ticket_at(&worklane, "committed", &backward);
let out = move_ticket(&temp, "cccc3333", "backlog", false);
assert!(!out.status.success());
assert_eq!(ticket_at(&worklane, "committed", &backward), before);
// The blocked ticket takes the other refusal, which sits after the no-op return.
let blocked = create_gated_pair(&worklane, &temp, "backlog");
let before = ticket_at(&worklane, "backlog", &blocked);
let out = move_ticket(&temp, "aaaa1111", "committed", false);
assert!(!out.status.success());
assert_eq!(ticket_at(&worklane, "backlog", &blocked), before);
}
/// A move to the status the ticket is already in returns before anything is written, so
/// it does not restamp either.
#[test]
fn mv_does_not_restamp_a_no_op_move() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = stamped_ticket(&worklane, "committed");
let before = ticket_at(&worklane, "committed", &filename);
let out = move_ticket(&temp, "cccc3333", "committed", false);
assert!(out.status.success());
assert_eq!(ticket_at(&worklane, "committed", &filename), before);
}
/// A ticket whose file sits in one status directory while its Status field says another
/// is a hand-moved file or a partial restore. Moving it to the status it already sits in
/// is the repair: the field is rewritten to agree with the directory and reported as a
/// correction rather than refused as a no-op.
#[test]
fn mv_repairs_a_lagging_status_field_instead_of_refusing() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = stamped_ticket(&worklane, "done");
let drifted = ticket_at(&worklane, "done", &filename)
.replace("- **Status**: done", "- **Status**: backlog");
fs::write(worklane.join("done").join(&filename), &drifted).unwrap();
let out = move_ticket(&temp, "cccc3333", "done", false);
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(out.status.success(), "{stderr}");
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(stdout.contains("Corrected Status field"), "{stdout}");
assert!(stdout.contains("backlog"), "{stdout}");
// Not a move: nothing was relocated and only the field changed, byte for byte.
assert!(worklane.join("done").join(&filename).exists());
assert_eq!(md_files(&worklane.join("backlog")), Vec::<String>::new());
assert_eq!(
ticket_at(&worklane, "done", &filename),
drifted.replace("- **Status**: backlog", "- **Status**: done")
);
}
/// The repair corrects bookkeeping rather than moving work, so it stamps nothing and
/// logs nothing: an entry shaped like a transition would record a journey the ticket
/// never took.
#[test]
fn a_repair_writes_no_stamp_and_no_history_entry() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = stamped_ticket(&worklane, "backlog");
// A real move first, so the ticket carries the stamp and the log entry that a
// repair has to leave alone.
let out = move_ticket(&temp, "cccc3333", "committed", false);
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
let history = worklane
.join("history")
.join("cccc3333cccc3333cccc3333cccc3333cccc");
assert_eq!(md_files(&history).len(), 1);
let content = ticket_at(&worklane, "committed", &filename);
assert!(content.contains("Committed On"), "{content}");
let drifted = content.replace("- **Status**: committed", "- **Status**: done");
fs::write(worklane.join("committed").join(&filename), &drifted).unwrap();
let out = move_ticket(&temp, "cccc3333", "committed", false);
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
let repaired = ticket_at(&worklane, "committed", &filename);
assert!(repaired.contains("Committed On"), "{repaired}");
assert!(!repaired.contains("Submitted On"), "{repaired}");
assert_eq!(md_files(&history).len(), 1, "the repair logged an entry");
}
/// The mirror drift, a field ahead of its directory from a crash mid-move, repairs the
/// same way when the caller keeps the ticket where it sits rather than moving it on to
/// where the field claims.
#[test]
fn mv_repairs_a_field_ahead_of_its_directory_too() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = stamped_ticket(&worklane, "backlog");
let drifted = ticket_at(&worklane, "backlog", &filename)
.replace("- **Status**: backlog", "- **Status**: done");
fs::write(worklane.join("backlog").join(&filename), &drifted).unwrap();
let out = move_ticket(&temp, "cccc3333", "backlog", false);
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(out.status.success(), "{stderr}");
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(stdout.contains("Corrected Status field"), "{stdout}");
assert!(worklane.join("backlog").join(&filename).exists());
assert!(
ticket_at(&worklane, "backlog", &filename).contains("- **Status**: backlog"),
"{}",
ticket_at(&worklane, "backlog", &filename)
);
}
// ---------------------------------------------------------------- discarded --
/// Discarding is not a stage of work, so it is reachable from wherever the ticket
/// stands and needs no `-f`: a backlog ticket that turns out to be a duplicate should
/// not have to be walked through committed and reviewing to be abandoned.
#[test]
fn mv_discards_from_every_status_without_force() {
for from in ["backlog", "committed", "reviewing", "done"] {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = stamped_ticket(&worklane, from);
let out = move_ticket(&temp, "cccc3333", "discarded", false);
assert!(
out.status.success(),
"discarding from {from}: {}",
String::from_utf8_lossy(&out.stderr)
);
assert!(
worklane.join("discarded").join(&filename).exists(),
"the ticket did not land in discarded from {from}"
);
assert_eq!(md_files(&worklane.join(from)).len(), 0, "left behind in {from}");
}
}
/// The fixture board has the four directories the tool shipped with. The first discard
/// on a board that predates the status creates the fifth rather than refusing.
#[test]
fn mv_creates_the_discarded_directory_on_a_board_that_predates_it() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = stamped_ticket(&worklane, "backlog");
assert!(
!worklane.join("discarded").exists(),
"the fixture already had the directory"
);
let out = move_ticket(&temp, "cccc3333", "discarded", false);
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
assert!(worklane.join("discarded").join(&filename).exists());
}
/// Leaving discarded is a backward move, which is the rule that already needs `-f`.
/// Un-discarding is a correction rather than a stage, so it is allowed under it.
#[test]
fn leaving_discarded_needs_force() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = stamped_ticket(&worklane, "backlog");
assert!(
move_ticket(&temp, "cccc3333", "discarded", false)
.status
.success()
);
let refused = move_ticket(&temp, "cccc3333", "backlog", false);
assert!(!refused.status.success());
assert!(
String::from_utf8_lossy(&refused.stderr).contains("--force"),
"{}",
String::from_utf8_lossy(&refused.stderr)
);
assert!(worklane.join("discarded").join(&filename).exists());
let forced = move_ticket(&temp, "cccc3333", "backlog", true);
assert!(forced.status.success(), "{}", String::from_utf8_lossy(&forced.stderr));
assert!(worklane.join("backlog").join(&filename).exists());
}
/// The guard looks the other way round from `check_blockers`: a ticket being abandoned
/// owes nothing to what it waits on, but the tickets waiting on *it* recorded a
/// dependency on work that is about to be declared never-happening.
#[test]
fn mv_refuses_to_discard_a_ticket_open_tickets_are_blocked_by() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_gated_pair(&worklane, &temp, "backlog");
let out = move_ticket(&temp, "bbbb2222", "discarded", false);
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(stderr.contains("Gated Task"), "the dependent is not named: {stderr}");
assert!(stderr.contains("--force"), "{stderr}");
assert!(
!worklane.join("discarded").exists(),
"a refused discard made the directory"
);
}
/// `-f` is the caller saying they looked at the dependents.
#[test]
fn mv_discards_a_blocked_from_ticket_with_force() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_gated_pair(&worklane, &temp, "backlog");
let out = move_ticket(&temp, "bbbb2222", "discarded", true);
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
}
/// A dependent that is itself finished or abandoned is nobody's plan, so it does not
/// hold up the discard.
#[test]
fn a_done_or_discarded_dependent_does_not_hold_up_a_discard() {
for status in ["done", "discarded"] {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_gated_pair(&worklane, &temp, "backlog");
assert!(
move_ticket(&temp, "aaaa1111", status, true).status.success(),
"the dependent could not be moved to {status}"
);
let out = move_ticket(&temp, "bbbb2222", "discarded", false);
assert!(
out.status.success(),
"a {status} dependent held up the discard: {}",
String::from_utf8_lossy(&out.stderr)
);
}
}
/// A discarded blocker satisfies the block the way a done one does: there is nothing
/// left to wait for. Whether the dependent still makes sense was asked when the blocker
/// was discarded, which is the refusal above.
#[test]
fn a_discarded_blocker_does_not_block() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = create_gated_pair(&worklane, &temp, "backlog");
assert!(
move_ticket(&temp, "bbbb2222", "discarded", true)
.status
.success()
);
let out = move_ticket(&temp, "aaaa1111", "committed", false);
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
assert!(worklane.join("committed").join(&filename).exists());
}
/// Entering discarded clears no stamp, which is what discarded being last in
/// `Status::ALL` buys: a ticket abandoned from reviewing keeps the journey it made, so
/// the log still says how far it got before it was dropped.
#[test]
fn discarding_keeps_the_stamps_the_ticket_already_had() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = stamped_ticket(&worklane, "backlog");
assert!(
move_ticket(&temp, "cccc3333", "committed", false)
.status
.success()
);
assert!(
move_ticket(&temp, "cccc3333", "reviewing", false)
.status
.success()
);
let before = ticket_at(&worklane, "reviewing", &filename);
let committed_on = field(&before, "Committed On").expect(&before);
let submitted_on = field(&before, "Submitted On").expect(&before);
assert!(
move_ticket(&temp, "cccc3333", "discarded", false)
.status
.success()
);
let after = ticket_at(&worklane, "discarded", &filename);
assert_eq!(
field(&after, "Committed On").as_deref(),
Some(committed_on.as_str()),
"{after}"
);
assert_eq!(
field(&after, "Submitted On").as_deref(),
Some(submitted_on.as_str()),
"{after}"
);
assert!(field(&after, "Discarded On").is_some(), "no discard stamp: {after}");
assert!(
field(&after, "Completed On").is_none(),
"a discard claimed the ticket was done"
);
}
/// The other end of the same rule: a forced move back out of discarded truncates the
/// journey at the target the way every backward move does.
#[test]
fn un_discarding_drops_the_discard_stamp() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
let filename = stamped_ticket(&worklane, "backlog");
assert!(
move_ticket(&temp, "cccc3333", "discarded", false)
.status
.success()
);
assert!(
move_ticket(&temp, "cccc3333", "committed", true)
.status
.success()
);
let content = ticket_at(&worklane, "committed", &filename);
assert!(field(&content, "Discarded On").is_none(), "{content}");
assert!(field(&content, "Committed On").is_some(), "{content}");
}
/// Prose takes a leading hyphen, so a reason that reads like a flag is stored as the
/// content it is rather than refused or swallowed. The cost of that bargain is that a
/// genuinely mistyped flag here lands in the log too.
#[test]
fn a_reason_takes_a_leading_hyphen_as_content() {
let temp = TempDir::new().unwrap();
let worklane = setup_worklane(&temp);
create_ticket(
&worklane,
"backlog",
"Hyphen Reason",
"Prose trap.",
"bbbb2222",
"alice",
"_Not set_",
"",
);
let mut cmd = Command::cargo_bin(BIN_NAME).unwrap();
cmd.current_dir(temp.path())
.args([
"mv",
"bbbb2222",
"--status",
"committed",
"--reason",
"-not-a-flag",
])
.assert()
.success();
let dir = worklane.join("history").join("bbbb2222");
let entry = fs::read_dir(&dir)
.unwrap()
.filter_map(|e| e.ok())
.map(|e| fs::read_to_string(e.path()).unwrap())
.collect::<Vec<_>>()
.join("\n");
assert!(entry.contains("**Reason**: -not-a-flag\n"), "{entry:?}");
}