use assert_cmd::Command; use assert_fs::TempDir; use assert_fs::fixture::{PathChild, PathCreateDir}; use std::fs; const BIN_NAME: &str = env!("CARGO_BIN_EXE_worklane"); fn setup_worklane(temp: &TempDir) -> std::path::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() } fn create_ticket( worklane_dir: &std::path::PathBuf, 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::(); 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 ); fs::write(worklane_dir.join(status).join(&filename), 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 show_displays_ticket_by_full_uuid() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let _filename = create_ticket( &worklane, "backlog", "Test Ticket", "This is a test ticket description.", "abc123def456789012345678901234567890", "alice", "2026-12-31", "test", ); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()) .arg("show") .arg("abc123def456789012345678901234567890") .assert() .success(); let stdout = get_stdout(&mut cmd); assert!(stdout.contains("Test Ticket")); assert!(stdout.contains("This is a test ticket description.")); assert!(stdout.contains("alice")); assert!(stdout.contains("2026-12-31")); assert!(stdout.contains("test")); } #[test] fn show_displays_ticket_by_partial_uuid() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = "abc123def456789012345678901234567890"; create_ticket( &worklane, "backlog", "Test Ticket", "This is a test ticket description.", uuid, "alice", "2026-12-31", "test", ); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()) .arg("show") .arg("abc123def4567890") .assert() .success(); let stdout = get_stdout(&mut cmd); assert!(stdout.contains("Test Ticket")); assert!(stdout.contains("This is a test ticket description.")); } #[test] fn show_displays_ticket_by_title() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = "abc123def456789012345678901234567890"; create_ticket( &worklane, "backlog", "Fix Login Bug", "Users cannot log in.", uuid, "bob", "2026-11-15", "bug,frontend", ); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()) .arg("show") .arg("login") .assert() .success(); let stdout = get_stdout(&mut cmd); assert!(stdout.contains("Fix Login Bug")); assert!(stdout.contains("Users cannot log in.")); assert!(stdout.contains("bob")); assert!(stdout.contains("2026-11-15")); } #[test] fn show_displays_ticket_by_partial_title() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = "abc123def456789012345678901234567890"; create_ticket( &worklane, "backlog", "Fix Login Bug", "Users cannot log in.", uuid, "bob", "2026-11-15", "bug,frontend", ); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()) .arg("show") .arg("fix") .assert() .success(); let stdout = get_stdout(&mut cmd); assert!(stdout.contains("Fix Login Bug")); } #[test] fn show_shows_both_uuid_and_title_in_output() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = "abc123def456789012345678901234567890"; create_ticket( &worklane, "backlog", "Test Ticket", "Description here.", uuid, "charlie", "2026-09-01", "test", ); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()) .arg("show") .arg("test") .assert() .success(); let stdout = get_stdout(&mut cmd); assert!(stdout.contains("abc123def456789012345678901234567890")); } #[test] fn show_fails_for_nonexistent_ticket() { let temp = TempDir::new().unwrap(); setup_worklane(&temp); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()) .arg("show") .arg("nonexistent") .assert() .failure(); } #[test] fn show_asks_for_disambiguation_when_multiple_matches() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid1 = "abc123def456789012345678901234567890"; create_ticket( &worklane, "backlog", "Test Ticket A", "First test ticket.", uuid1, "alice", "2026-12-31", "test", ); let uuid2 = "fedcba0987654321fedcba09876543210987654321"; create_ticket( &worklane, "backlog", "Test Ticket B", "Second test ticket.", uuid2, "bob", "2026-10-15", "test", ); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()).arg("show").arg("est"); let stderr = get_stderr(&mut cmd); assert!(stderr.contains("Multiple tickets found")); assert!(stderr.contains("Test Ticket A")); assert!(stderr.contains("Test Ticket B")); assert!(stderr.contains("abc123d")); assert!(stderr.contains("fedcba0")); assert!(stderr.contains("Use the displayed ID")); } /// The candidate list has to travel the same way every other error does, or the /// opencode wrapper throws on the exit code and discards it. #[test] fn show_reports_ambiguity_on_stderr_with_a_failing_exit() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket( &worklane, "backlog", "Ambiguous One", "First.", "abc123def456789012345678901234567890", "alice", "2026-12-31", "test", ); create_ticket( &worklane, "backlog", "Ambiguous Two", "Second.", "fedcba0987654321fedcba09876543210987654321", "bob", "2026-10-15", "test", ); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()) .arg("show") .arg("ambiguous") .assert() .failure(); let stderr = get_stderr(&mut cmd); assert!(stderr.contains("Ambiguous One")); assert!(stderr.contains("Ambiguous Two")); // Nothing on stdout: that is where it used to go, and where it was lost let stdout = get_stdout(&mut cmd); assert!(stdout.is_empty(), "stdout was {stdout:?}"); } /// Filenames shorter than the displayed id width used to panic while printing /// the disambiguation list. #[test] fn show_disambiguates_tickets_with_short_filenames() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); fs::write( worklane.join("backlog").join("abc.md"), "# Short One\n\n## Metadata\n\n- **ID**: 1234\n", ) .unwrap(); fs::write( worklane.join("backlog").join("xy.md"), "# Short Two\n\n## Metadata\n\n- **ID**: 5678\n", ) .unwrap(); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()).arg("show").arg("short"); let stderr = get_stderr(&mut cmd); assert!(stderr.contains("Multiple tickets found")); assert!(stderr.contains("Short One")); assert!(stderr.contains("Short Two")); } /// A multibyte character straddling the cut used to panic. #[test] fn show_disambiguates_tickets_with_non_ascii_filenames() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); for (filename, title) in [("émigré-un.md", "Accented One"), ("日本語-二.md", "Accented Two")] { fs::write( worklane.join("backlog").join(filename), format!("# {}\n\n## Metadata\n\n- **ID**: 1234\n", title), ) .unwrap(); } let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()).arg("show").arg("accented"); let stderr = get_stderr(&mut cmd); assert!(stderr.contains("Multiple tickets found")); assert!(stderr.contains("Accented One")); assert!(stderr.contains("Accented Two")); } #[test] fn show_searches_across_all_statuses() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); // Create ticket in committed status let uuid = "abc123def456789012345678901234567890"; create_ticket( &worklane, "committed", "Committed Task", "This is in committed.", uuid, "charlie", "2026-08-01", "task", ); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()) .arg("show") .arg("committed") .assert() .success(); let stdout = get_stdout(&mut cmd); assert!(stdout.contains("Committed Task")); assert!(stdout.contains("This is in committed.")); } #[test] fn show_prints_the_title_once() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket( &worklane, "backlog", "Once Only", "A description that does not name the title.", "abc123def456789012345678901234567890", "alice", "2026-12-31", "test", ); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()).arg("show").arg("Once Only"); let stdout = get_stdout(&mut cmd); assert_eq!( stdout.matches("Once Only").count(), 1, "title repeated in {stdout:?}" ); // The rule under it is still the width of the title assert!(stdout.contains("Once Only\n─────────\n"), "{stdout:?}"); } #[test] fn show_renders_the_priority_mark_beside_the_number() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let filename = "abc123def456789012345678901234567890_prio.md"; fs::write( worklane.join("backlog").join(filename), "# Prio Ticket\n\n## Metadata\n\n- **ID**: abc123def456789012345678901234567890\n\ - **Priority**: 2\n- **Status**: backlog\n", ) .unwrap(); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()).arg("show").arg("Prio"); let stdout = get_stdout(&mut cmd); // Mark for the eye, number still there for the file's sake assert!(stdout.contains("\u{25cf}\u{25d0}"), "no mark in {stdout:?}"); assert!(stdout.contains("Priority")); assert!(stdout.contains('2')); // And the file itself is untouched let content = fs::read_to_string(worklane.join("backlog").join(filename)).unwrap(); assert!(content.contains("- **Priority**: 2")); }