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"); /// Two campaigns, so a filter has something to exclude, and three tickets: one in each /// campaign and one in neither. const CAMP_A: &str = "aa11bb22-33cc-4dd4-8ee5-ff6677889900"; const CAMP_B: &str = "bb22cc33-44dd-4ee5-8ff6-001122334455"; const MEMBER: &str = "641e3d5c-1b0e-4d2f-9a7e-3c8b5f0a2d61"; const OTHER: &str = "b1b2c3d4-5e6f-4a7b-8c9d-0e1f2a3b4c5d"; const LONER: &str = "f1f2f3f4-5a6b-4c7d-8e9f-0a1b2c3d4e5f"; /// A second done member, for the one test that needs two in one column to see an order. const LATE: &str = "a9a8a7a6-5b4c-4d3e-8f2a-1b0c9d8e7f60"; fn setup_worklane(temp: &TempDir) -> PathBuf { let worklane = temp.child(".worklane"); worklane.create_dir_all().unwrap(); for dir in ["backlog", "committed", "reviewing", "done", "campaigns"] { worklane.child(dir).create_dir_all().unwrap(); } worklane.path().to_path_buf() } fn slug(title: &str) -> String { title .to_lowercase() .chars() .filter(|c| c.is_alphanumeric() || *c == ' ' || *c == '-' || *c == '_') .map(|c| if c == ' ' { '-' } else { c }) .collect() } /// A ticket with a ticked criterion, a section the tool knows nothing about and a /// metadata field it has never heard of, so every test can check that an assignment /// leaves all three exactly as they were. /// /// Deliberately carries no Campaign field: no ticket written before campaigns existed /// has one, and `add` still does not write one, so the insertion path is what every /// assignment goes through. fn create_ticket(worklane: &Path, status: &str, title: &str, uuid: &str) -> PathBuf { let path = worklane .join(status) .join(format!("{}_{}.md", uuid, slug(title))); fs::write( &path, format!( "# {title}\n\ \n\ ## Description\n\ Prose that mentions **Campaign**: not a field, in the body.\n\ \n\ ## Acceptance Criteria\n\ - [x] Crit one\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**: {uuid}\n\ - **Priority**: 3\n\ - **Tags**: alpha\n\ - **Due**: _Not set_\n\ - **Handled By Team**: ops\n\ - **Owner**: alice\n\ - **Created At**: 2026-08-21 02:10:17\n\ - **Status**: {status}\n" ), ) .unwrap(); path } fn create_campaign(worklane: &Path, title: &str, uuid: &str) -> PathBuf { let path = worklane .join("campaigns") .join(format!("{}_{}.md", uuid, slug(title))); fs::write( &path, format!( "# {title}\n\ \n\ ## Description\n\ What has to ship together.\n\ \n\ ## Metadata\n\ \n\ - **ID**: {uuid}\n\ - **Owner**: alice\n\ - **Created At**: 2026-08-21 02:10:17\n" ), ) .unwrap(); path } fn board(temp: &TempDir) -> PathBuf { let worklane = setup_worklane(temp); create_campaign(&worklane, "v1 release", CAMP_A); create_campaign(&worklane, "v2 release", CAMP_B); create_ticket(&worklane, "backlog", "The member", MEMBER); create_ticket(&worklane, "done", "The other member", OTHER); create_ticket(&worklane, "backlog", "The loner", LONER); worklane } 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 stdout(out: &std::process::Output) -> String { String::from_utf8_lossy(&out.stdout).to_string() } fn stderr(out: &std::process::Output) -> String { String::from_utf8_lossy(&out.stderr).to_string() } fn find(worklane: &Path, uuid: &str) -> PathBuf { for status in ["backlog", "committed", "reviewing", "done"] { for entry in fs::read_dir(worklane.join(status)) .unwrap() .filter_map(|e| e.ok()) { if entry.file_name().to_string_lossy().starts_with(uuid) { return entry.path(); } } } panic!("no file for ticket {uuid}"); } /// A campaign's content by uuid, found rather than named, since `campaign set --title` /// renames the file and a test that named it would be asserting the old name. fn campaign_file(worklane: &Path, uuid: &str) -> String { for entry in fs::read_dir(worklane.join("campaigns")) .unwrap() .filter_map(|e| e.ok()) { if entry.file_name().to_string_lossy().starts_with(uuid) { return fs::read_to_string(entry.path()).unwrap(); } } panic!("no file for campaign {uuid}"); } fn ticket(worklane: &Path, uuid: &str) -> String { fs::read_to_string(find(worklane, uuid)).unwrap() } fn campaigns(worklane: &Path) -> Vec { let mut names: Vec = fs::read_dir(worklane.join("campaigns")) .unwrap() .filter_map(|e| e.ok()) .map(|e| e.file_name().to_string_lossy().to_string()) .collect(); names.sort(); names } // ------------------------------------------------------------------ filing one -- #[test] fn campaign_add_writes_a_uuid_title_description_and_metadata() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let out = run( &temp, &[ "campaign", "add", "--title", "v1 release", "--description", "Everything that ships together.", "--owner", "alice", ], ); assert!(out.status.success(), "{}", stderr(&out)); let names = campaigns(&worklane); assert_eq!(names.len(), 1, "{names:?}"); let name = &names[0]; assert!(name.ends_with("_v1-release.md"), "{name}"); let content = fs::read_to_string(worklane.join("campaigns").join(name)).unwrap(); assert!(content.starts_with("# v1 release\n"), "{content}"); assert!(content.contains("Everything that ships together."), "{content}"); assert!(content.contains("- **Owner**: alice"), "{content}"); assert!(content.contains("- **Created At**: 20"), "{content}"); // The uuid in the file is the one in the filename, since that is what `assign` // stores and what every lookup compares against. let uuid = name.split('_').next().unwrap(); assert!(content.contains(&format!("- **ID**: {uuid}")), "{content}"); } /// A campaign has no status, so its file carries none. A Status field would be a second /// place for a state that is derived, and the two would disagree the moment a member /// moved. #[test] fn a_campaign_carries_no_status_and_no_criteria() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); run(&temp, &["campaign", "add", "--title", "v1 release"]); let name = campaigns(&worklane).remove(0); let content = fs::read_to_string(worklane.join("campaigns").join(name)).unwrap(); assert!(!content.contains("**Status**"), "{content}"); assert!(!content.contains("Acceptance Criteria"), "{content}"); assert!(!content.contains("Implementation Plan"), "{content}"); } #[test] fn nothing_is_created_without_a_title() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let out = run(&temp, &["campaign", "add", "--description", "No title here."]); assert_eq!(out.status.code(), Some(2), "{}", stderr(&out)); assert!(campaigns(&worklane).is_empty(), "{:?}", campaigns(&worklane)); } #[test] fn init_creates_the_campaigns_directory() { let temp = TempDir::new().unwrap(); let out = run(&temp, &["init"]); assert!(out.status.success(), "{}", stderr(&out)); assert!(temp.child(".worklane").child("campaigns").path().is_dir()); } /// A board made before campaigns existed has no such directory, and a `worklane init` /// over a live board to get one would be a strange thing to have to do. #[test] fn the_campaigns_directory_is_created_for_a_board_that_predates_it() { let temp = TempDir::new().unwrap(); let worklane = temp.child(".worklane"); worklane.create_dir_all().unwrap(); for dir in ["backlog", "committed", "reviewing", "done"] { worklane.child(dir).create_dir_all().unwrap(); } assert!(!worklane.child("campaigns").path().exists()); let out = run(&temp, &["campaign", "add", "--title", "v1 release"]); assert!(out.status.success(), "{}", stderr(&out)); assert_eq!(campaigns(worklane.path()).len(), 1); } // -------------------------------------------------------------------- assigning -- #[test] fn assign_writes_the_campaign_field_in_the_metadata_section() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); let out = run(&temp, &["assign", "v1 release", MEMBER]); assert!(out.status.success(), "{}", stderr(&out)); let content = ticket(&worklane, MEMBER); assert!(content.contains(&format!("- **Campaign**: {CAMP_A}")), "{content}"); // Scoped to the section: the description quotes the marker, and the field has to // land under the heading rather than over the prose. let metadata = content.split("## Metadata").nth(1).unwrap(); assert!(metadata.contains(&format!("- **Campaign**: {CAMP_A}")), "{content}"); assert!( content.contains("Prose that mentions **Campaign**: not a field, in the body."), "the description was rewritten: {content}" ); } /// The field goes in its place among the others rather than at the end, so Status stays /// last, which is where every file already keeps it. #[test] fn the_field_is_inserted_before_status() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); run(&temp, &["assign", "v1 release", MEMBER]); let content = ticket(&worklane, MEMBER); let campaign_at = content.find("- **Campaign**:").unwrap(); let status_at = content.find("- **Status**:").unwrap(); assert!(campaign_at < status_at, "{content}"); } /// All three tiers, since a campaign is addressed the way a ticket is: a uuid prefix, a /// title substring, and a substring reaching the middle of the filename. #[test] fn a_campaign_resolves_by_uuid_prefix_title_or_filename() { for search in ["aa11", "v1 release", "4dd4"] { let temp = TempDir::new().unwrap(); let worklane = board(&temp); let out = run(&temp, &["assign", search, MEMBER]); assert!(out.status.success(), "{search}: {}", stderr(&out)); assert!( ticket(&worklane, MEMBER).contains(&format!("- **Campaign**: {CAMP_A}")), "{search}: {}", ticket(&worklane, MEMBER) ); } } #[test] fn a_ticket_names_at_most_one_campaign() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); run(&temp, &["assign", "v1 release", MEMBER]); let out = run(&temp, &["assign", "v2 release", MEMBER]); assert!(out.status.success(), "{}", stderr(&out)); let content = ticket(&worklane, MEMBER); assert_eq!( content.matches("- **Campaign**:").count(), 1, "reassigning added a second field: {content}" ); assert!(content.contains(&format!("- **Campaign**: {CAMP_B}")), "{content}"); assert!(!content.contains(CAMP_A), "{content}"); } #[test] fn assigning_the_campaign_already_named_writes_nothing() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); run(&temp, &["assign", "v1 release", MEMBER]); let before = ticket(&worklane, MEMBER); let out = run(&temp, &["assign", "v1 release", MEMBER]); assert!(out.status.success(), "{}", stderr(&out)); assert!(stdout(&out).contains("already in campaign"), "{}", stdout(&out)); assert_eq!(ticket(&worklane, MEMBER), before); } /// The campaign is resolved before the ticket is touched, so a call naming a good /// ticket and a campaign that does not exist writes nothing at all. #[test] fn an_unknown_campaign_leaves_the_ticket_alone() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); let before = ticket(&worklane, MEMBER); let out = run(&temp, &["assign", "v9 release", MEMBER]); assert!(!out.status.success()); assert!(stderr(&out).contains("No campaign found matching"), "{}", stderr(&out)); assert_eq!(ticket(&worklane, MEMBER), before); } #[test] fn content_the_tool_does_not_know_survives_an_assignment() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); run(&temp, &["assign", "v1 release", MEMBER]); let content = ticket(&worklane, MEMBER); assert!(content.contains("## Notes"), "{content}"); assert!( content.contains("A handwritten note the tool knows nothing about."), "{content}" ); assert!(content.contains("- **Handled By Team**: ops"), "{content}"); // And the ticked criterion is still ticked. assert!(content.contains("- [x] Crit one"), "{content}"); } /// One field, one writer. `assign` owns Campaign the way `chown` owns Owner. #[test] fn neither_add_nor_set_offers_a_campaign_flag() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); let added = run(&temp, &["add", "--title", "New one", "--campaign", "v1 release"]); assert_eq!(added.status.code(), Some(2), "{}", stderr(&added)); let set = run(&temp, &["set", MEMBER, "--campaign", "v1 release"]); assert_eq!(set.status.code(), Some(2), "{}", stderr(&set)); // The stored shape, not the bare marker: the fixture description quotes the marker // on purpose, so an unscoped match here would pass whatever the commands did. assert!(!ticket(&worklane, MEMBER).contains("- **Campaign**:")); } // --------------------------------------------------------------------- reading -- #[test] fn campaign_ls_lists_campaigns_with_their_member_counts() { let temp = TempDir::new().unwrap(); let _worklane = board(&temp); run(&temp, &["assign", "v1 release", MEMBER]); run(&temp, &["assign", "v1 release", OTHER]); let out = run(&temp, &["campaign", "ls"]); assert!(out.status.success(), "{}", stderr(&out)); let listing = stdout(&out); // One of the two members is in done, so the campaign is open and half finished. assert!(listing.contains("v1 release | 1/2 done [open]"), "{listing}"); // Nothing has been filed against the other one. assert!(listing.contains("v2 release | no members"), "{listing}"); } /// The machine-readable listing, the same two fields and the same shape as `ls --ids`. #[test] fn campaign_ls_ids_prints_the_id_and_the_title_tab_separated() { let temp = TempDir::new().unwrap(); let _worklane = board(&temp); run(&temp, &["assign", "v1 release", MEMBER]); let out = run(&temp, &["campaign", "ls", "--ids"]); assert!(out.status.success(), "{}", stderr(&out)); let listing = stdout(&out); assert_eq!( listing.lines().collect::>(), vec![ format!("{}\tv1 release", &CAMP_A[..7]), format!("{}\tv2 release", &CAMP_B[..7]), ] ); // Nothing decorated: no heading, no member count, no escape. assert!(!listing.contains("Campaigns:"), "{listing}"); assert!(!listing.contains("done"), "{listing}"); assert!(!listing.contains('\u{1b}'), "{listing}"); } /// A completion offers nothing rather than a sentence, so the empty case that prints /// prose for a reader has to stay out of the parsed path. #[test] fn campaign_ls_ids_says_nothing_on_a_board_with_no_campaigns() { let temp = TempDir::new().unwrap(); setup_worklane(&temp); let out = run(&temp, &["campaign", "ls", "--ids"]); assert!(out.status.success(), "{}", stderr(&out)); assert_eq!(stdout(&out), ""); // The decorated listing still explains itself. assert!(stdout(&run(&temp, &["campaign", "ls"])).contains("No campaigns yet")); } /// A campaign is done when it holds members and every one of them is done. An empty one /// is neither, which is why it reads `no members` rather than `0/0 done`. #[test] fn a_campaign_whose_every_member_is_done_reads_done() { let temp = TempDir::new().unwrap(); let _worklane = board(&temp); run(&temp, &["assign", "v1 release", OTHER]); let listing = stdout(&run(&temp, &["campaign", "ls"])); assert!(listing.contains("v1 release | 1/1 done [done]"), "{listing}"); } #[test] fn campaign_show_prints_the_body_and_the_members_grouped_by_status() { let temp = TempDir::new().unwrap(); let _worklane = board(&temp); run(&temp, &["assign", "v1 release", MEMBER]); run(&temp, &["assign", "v1 release", OTHER]); let out = run(&temp, &["campaign", "show", "aa11"]); assert!(out.status.success(), "{}", stderr(&out)); let shown = stdout(&out); assert!(shown.contains("v1 release"), "{shown}"); assert!(shown.contains("What has to ship together."), "{shown}"); assert!(shown.contains("1/2 done"), "{shown}"); assert!(shown.contains("[BACKLOG]"), "{shown}"); assert!(shown.contains("The member"), "{shown}"); // Done members are shown here, unlike in `ls`: what is finished against what is // left is the whole question a campaign asks. assert!(shown.contains("[DONE]"), "{shown}"); assert!(shown.contains("The other member"), "{shown}"); // And a ticket in no campaign is not a member of this one. assert!(!shown.contains("The loner"), "{shown}"); } /// The members of a status are listed in that status's own order, not in the board-wide /// one: a campaign's done list answers what just landed, for the same reason the board's /// column does. Every fixture is a p3, so the stamp is doing all of the work. #[test] fn campaign_show_lists_each_status_in_its_own_order() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); let stamp = |uuid: &str, day: &str| { let path = find(&worklane, uuid); let body = fs::read_to_string(&path).unwrap().replace( "- **Status**: done\n", &format!("- **Completed On**: 2026-08-0{day} 09:00:00\n- **Status**: done\n"), ); fs::write(&path, body).unwrap(); }; // A second done member, finished after the one the fixture writes. create_ticket(&worklane, "done", "The late member", LATE); stamp(OTHER, "1"); stamp(LATE, "3"); run(&temp, &["assign", "v1 release", OTHER]); run(&temp, &["assign", "v1 release", LATE]); let shown = stdout(&run(&temp, &["campaign", "show", "aa11"])); let at = |needle: &str| shown.find(needle).unwrap_or_else(|| panic!("{needle}: {shown}")); assert!(at("The late member") < at("The other member"), "{shown}"); } #[test] fn ls_campaign_lists_only_that_campaigns_members() { let temp = TempDir::new().unwrap(); let _worklane = board(&temp); run(&temp, &["assign", "v1 release", MEMBER]); run(&temp, &["assign", "v2 release", LONER]); let out = run(&temp, &["ls", "--campaign", "v1 release"]); assert!(out.status.success(), "{}", stderr(&out)); let listing = stdout(&out); assert!(listing.contains("The member"), "{listing}"); assert!(!listing.contains("The loner"), "{listing}"); assert!(listing.contains("[BACKLOG] 1 ticket"), "{listing}"); } #[test] fn ls_campaign_refuses_a_campaign_that_does_not_exist() { let temp = TempDir::new().unwrap(); let _worklane = board(&temp); let out = run(&temp, &["ls", "--campaign", "v9 release"]); assert!(!out.status.success()); assert!(stderr(&out).contains("No campaign found matching"), "{}", stderr(&out)); } /// The stored value is a full uuid, so a campaign that has been deleted has to be /// reported as gone rather than shown raw, the way a dangling relation target is. #[test] fn a_campaign_that_no_longer_exists_reads_as_missing() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); run(&temp, &["assign", "v1 release", MEMBER]); fs::remove_file( worklane .join("campaigns") .join(format!("{CAMP_A}_v1-release.md")), ) .unwrap(); let out = run(&temp, &["show", MEMBER]); assert!(out.status.success(), "{}", stderr(&out)); let shown = stdout(&out); assert!(shown.contains("_missing_"), "{shown}"); // The short id survives, since it is the only thing left to go looking for. assert!(shown.contains("(aa11bb2)"), "{shown}"); assert!(!shown.contains(CAMP_A), "the bare uuid was shown: {shown}"); } /// The title is shown rather than the uuid the file stores: thirty-six characters of /// hex in a metadata table say nothing about which campaign it is. #[test] fn a_ticket_shows_the_campaign_title_not_its_uuid() { let temp = TempDir::new().unwrap(); let _worklane = board(&temp); run(&temp, &["assign", "v1 release", MEMBER]); let shown = stdout(&run(&temp, &["show", MEMBER])); assert!(shown.contains("(aa11bb2) v1 release"), "{shown}"); assert!(!shown.contains(CAMP_A), "{shown}"); } // -------------------------------------------------------------- not a ticket -- /// A campaign is in no status directory, so `mv` has nothing to move. It says so rather /// than reporting a ticket it could not find, which would send the caller hunting for a /// typo in a name that is perfectly correct. #[test] fn mv_refuses_a_campaign() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); let out = run(&temp, &["mv", "v1 release", "--status", "committed"]); assert!(!out.status.success()); let message = stderr(&out); assert!(message.contains("is a campaign, not a ticket"), "{message}"); assert!(message.contains("no status directory"), "{message}"); // And it stayed where it was: nothing was moved into a status directory. assert!( worklane .join("campaigns") .join(format!("{CAMP_A}_v1-release.md")) .exists() ); for status in ["backlog", "committed", "reviewing", "done"] { let moved = fs::read_dir(worklane.join(status)) .unwrap() .filter_map(|e| e.ok()) .any(|e| e.file_name().to_string_lossy().starts_with(CAMP_A)); assert!(!moved, "a campaign reached the {status} directory"); } } /// Every command that takes a ticket gives the same explanation, because the refusal is /// in resolution rather than in `mv`. #[test] fn a_campaign_is_refused_wherever_a_ticket_is_expected() { for args in [ vec!["show", "v1 release"], vec!["comment", "v1 release", "A note."], vec!["chown", "bob", "v1 release"], ] { let temp = TempDir::new().unwrap(); let _worklane = board(&temp); let out = run(&temp, &args); assert!(!out.status.success(), "{args:?}"); assert!( stderr(&out).contains("is a campaign, not a ticket"), "{args:?}: {}", stderr(&out) ); } } /// A search that names neither still says nothing was found, rather than complaining /// about a directory the caller never mentioned. #[test] fn a_search_that_names_nothing_still_reports_no_ticket() { let temp = TempDir::new().unwrap(); let _worklane = board(&temp); let out = run(&temp, &["show", "nothing at all"]); assert!(!out.status.success()); assert!(stderr(&out).contains("No ticket found matching"), "{}", stderr(&out)); } /// A campaign resolves by the same tiers a ticket does, so it can be ambiguous the same /// way, and the listing has to name campaigns rather than tickets. #[test] fn an_ambiguous_campaign_lists_the_candidates() { let temp = TempDir::new().unwrap(); let _worklane = board(&temp); let out = run(&temp, &["campaign", "show", "release"]); assert!(!out.status.success()); let message = stderr(&out); assert!(message.contains("Multiple campaigns found matching"), "{message}"); assert!(message.contains("v1 release"), "{message}"); assert!(message.contains("v2 release"), "{message}"); } // ------------------------------------------------------------------ codenames -- /// The codename is what the board draws on every card in a campaign, since a uuid does /// not fit there and a title is a sentence. #[test] fn campaign_add_writes_the_codename_under_the_id() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let out = run( &temp, &[ "campaign", "add", "--title", "First public release", "--codename", "v1", ], ); assert!(out.status.success(), "{}", stderr(&out)); let name = campaigns(&worklane).remove(0); let content = fs::read_to_string(worklane.join("campaigns").join(name)).unwrap(); assert!(content.contains("- **Codename**: v1"), "{content}"); // Under the ID, because that is what it is: a second way to say which campaign this // is, in a word rather than in thirty-six characters. let id = content.find("- **ID**:").expect("no id"); let codename = content.find("- **Codename**:").expect("no codename"); let owner = content.find("- **Owner**:").expect("no owner"); assert!(id < codename && codename < owner, "{content}"); } /// A campaign filed without one carries the line unset rather than not at all, the way /// `add` writes an unset Due on every ticket: one shape of file is one thing for the /// parser to be right about, and the line is there to fill in by hand. #[test] fn a_campaign_filed_without_a_codename_carries_the_field_unset() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); run(&temp, &["campaign", "add", "--title", "No codename here"]); let name = campaigns(&worklane).remove(0); let content = fs::read_to_string(worklane.join("campaigns").join(name)).unwrap(); assert!(content.contains("- **Codename**: _Not set_"), "{content}"); } /// One word, checked at parse time like `--due` and `--priority`, because the board draws /// it as a label with a mark and no quoting: `~beaver release` on that row reads as two /// labels and the second one is not a campaign. #[test] fn a_codename_of_more_than_one_word_is_a_usage_error() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let out = run( &temp, &[ "campaign", "add", "--title", "v1 release", "--codename", "beaver release", ], ); assert_eq!(out.status.code(), Some(2), "{}", stderr(&out)); assert!(stderr(&out).contains("single word"), "{}", stderr(&out)); assert!(campaigns(&worklane).is_empty(), "a campaign was filed anyway"); } #[test] fn an_empty_codename_is_a_usage_error() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let out = run(&temp, &["campaign", "add", "--title", "v1 release", "--codename", ""]); assert_eq!(out.status.code(), Some(2), "{}", stderr(&out)); assert!(campaigns(&worklane).is_empty(), "a campaign was filed anyway"); } /// The listing is where the codename is read back, so it goes between the id and the /// title with the mark the board draws it with: one word for one campaign, spelled the /// same wherever it is shown. #[test] fn campaign_ls_names_the_codename_between_the_id_and_the_title() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_campaign(&worklane, "No codename here", CAMP_B); run( &temp, &[ "campaign", "add", "--title", "First public release", "-c", "v1", ], ); let out = run(&temp, &["campaign", "ls"]); let listing = stdout(&out); assert!(listing.contains("~v1 First public release"), "{listing}"); // And a campaign without one spends nothing on it, the way an untagged card spends no // row. assert!(listing.contains(") No codename here"), "{listing}"); assert!(!listing.contains("~No codename"), "{listing}"); let _ = worklane; } /// `campaign show` renders its metadata as a table, so the field is there to read without /// anything being written twice. #[test] fn campaign_show_renders_the_codename_in_its_metadata() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); run( &temp, &[ "campaign", "add", "--title", "First public release", "-c", "v1", ], ); let out = run(&temp, &["campaign", "show", "First public"]); assert!(stdout(&out).contains("Codename"), "{}", stdout(&out)); assert!(stdout(&out).contains("v1"), "{}", stdout(&out)); let _ = worklane; } /// A codename nobody can type is half a field, so it resolves a campaign the way its /// uuid and its title do. Exact, and ahead of all three: it is a match on a field written /// for the purpose rather than a substring that could be a coincidence. #[test] fn a_campaign_resolves_by_its_codename() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); // Same file, plus a codename, since `create_campaign` writes none. let path = worklane .join("campaigns") .join(format!("{}_{}.md", CAMP_A, slug("v1 release"))); let content = fs::read_to_string(&path).unwrap(); fs::write( &path, content.replace("- **Owner**:", "- **Codename**: beaver\n- **Owner**:"), ) .unwrap(); let out = run(&temp, &["assign", "beaver", MEMBER]); assert!(out.status.success(), "{}", stderr(&out)); assert!( ticket(&worklane, MEMBER).contains(&format!("- **Campaign**: {CAMP_A}")), "{}", ticket(&worklane, MEMBER) ); // And every other command that takes a campaign, since they share one resolver. let out = run(&temp, &["campaign", "show", "beaver"]); assert!(stdout(&out).contains("v1 release"), "{}", stdout(&out)); let out = run(&temp, &["ls", "--campaign", "beaver"]); assert!(stdout(&out).contains("The member"), "{}", stdout(&out)); } /// An exact codename beats a title that merely contains the search, which is the whole /// reason it is a tier of its own: `v2` is a substring of `v2 release` and the exact /// match has to win outright rather than be reported as ambiguous against it. #[test] fn an_exact_codename_beats_a_title_that_contains_the_search() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); let path = worklane .join("campaigns") .join(format!("{}_{}.md", CAMP_A, slug("v1 release"))); let content = fs::read_to_string(&path).unwrap(); fs::write(&path, content.replace("- **Owner**:", "- **Codename**: v2\n- **Owner**:")).unwrap(); let out = run(&temp, &["assign", "v2", MEMBER]); assert!(out.status.success(), "{}", stderr(&out)); assert!( ticket(&worklane, MEMBER).contains(&format!("- **Campaign**: {CAMP_A}")), "the title tier won: {}", ticket(&worklane, MEMBER) ); } /// A ticket carries no codename, so the tier is empty for one and ticket resolution is /// exactly what it was. Pinned because the tier lives in the shared resolver: this is the /// half of it that must not have changed. #[test] fn a_ticket_search_is_unaffected_by_the_codename_tier() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); // The uuid prefix still wins over a title that quotes it, which is the precedence the // three tiers were written for. let out = run(&temp, &["show", "641e3d5"]); assert!(stdout(&out).contains("The member"), "{}", stdout(&out)); let _ = worklane; } // ---------------------------------------------------------------- campaign set -- /// The point of the command: a campaign already on the board can be given a codename, /// which `campaign add --codename` cannot do because `add` files a new one. #[test] fn campaign_set_gives_a_filed_campaign_a_codename() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); let out = run(&temp, &["campaign", "set", "v1 release", "--codename", "beaver"]); assert!(out.status.success(), "{}", stderr(&out)); assert!(stdout(&out).contains("Updated codename"), "{}", stdout(&out)); let content = campaign_file(&worklane, CAMP_A); assert!(content.contains("- **Codename**: beaver"), "{content}"); } /// `create_campaign` writes no Codename line at all, which is every campaign filed before /// the field existed, so the insertion path is the one this command is actually for. /// It lands under the ID, where `campaign add` writes it, so a campaign given a codename /// later is the same file as one filed with it. #[test] fn the_codename_is_inserted_under_the_id_when_the_line_is_absent() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); assert!( !campaign_file(&worklane, CAMP_A).contains("Codename"), "the fixture changed" ); run(&temp, &["campaign", "set", "v1 release", "-c", "v1"]); let content = campaign_file(&worklane, CAMP_A); let id = content.find("- **ID**:").expect("no id"); let codename = content.find("- **Codename**:").expect("no codename"); let owner = content.find("- **Owner**:").expect("no owner"); assert!(id < codename && codename < owner, "{content}"); } /// One parser, so a codename is one thing however it was written. The board draws it as a /// label with a mark and no quoting, and that does not depend on which command wrote it. #[test] fn campaign_set_validates_the_codename_the_way_add_does() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); for value in ["two words", ""] { let out = run(&temp, &["campaign", "set", "v1 release", "--codename", value]); assert_eq!(out.status.code(), Some(2), "{value:?}: {}", stderr(&out)); assert!( !campaign_file(&worklane, CAMP_A).contains("Codename"), "{value:?} was written" ); } // And the message is the one `campaign add` gives, since the parser is the same. let out = run(&temp, &["campaign", "set", "v1 release", "--codename", "two words"]); assert!(stderr(&out).contains("single word"), "{}", stderr(&out)); } /// A bare `campaign set ` is a usage error rather than a silent no-op, exactly /// as it is for `set`. #[test] fn campaign_set_with_no_field_is_a_usage_error() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); let out = run(&temp, &["campaign", "set", "v1 release"]); assert_eq!(out.status.code(), Some(2), "{}", stderr(&out)); assert!( !campaign_file(&worklane, CAMP_A).contains("Codename"), "something was written" ); } /// Replacing the title renames the file, keeping the uuid, the way `set --title` does. That /// is the reason this is a `set` rather than a flag on something narrower. #[test] fn campaign_set_renames_the_file_with_the_title() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); // A member first, so the rename has something to break. run(&temp, &["assign", "v1 release", MEMBER]); let out = run( &temp, &[ "campaign", "set", "v1 release", "--title", "First public release", ], ); assert!(out.status.success(), "{}", stderr(&out)); let names = campaigns(&worklane); assert!( names.contains(&format!("{CAMP_A}_first-public-release.md")), "not renamed: {names:?}" ); assert!( !names.contains(&format!("{CAMP_A}_v1-release.md")), "the old file is still there: {names:?}" ); let content = campaign_file(&worklane, CAMP_A); assert!(content.starts_with("# First public release\n"), "{content}"); // The uuid is kept, so membership survives the rename: it is stored on the member as // the uuid, and nothing has to be rewritten when the campaign is renamed. assert!(content.contains(&format!("- **ID**: {CAMP_A}")), "{content}"); let out = run(&temp, &["ls", "--campaign", "First public release"]); assert!( stdout(&out).contains("The member"), "membership was lost: {}", stdout(&out) ); } /// Both fields in one call, since the group is `multiple`. #[test] fn campaign_set_takes_the_title_and_the_codename_together() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); let out = run( &temp, &[ "campaign", "set", "v1 release", "-t", "First public release", "-c", "v1", ], ); assert!(out.status.success(), "{}", stderr(&out)); assert!(stdout(&out).contains("title, codename"), "{}", stdout(&out)); let content = campaign_file(&worklane, CAMP_A); assert!(content.starts_with("# First public release\n"), "{content}"); assert!(content.contains("- **Codename**: v1"), "{content}"); } /// The whole-file comparison `set` and `chown` use, so writing what is already there /// writes nothing and says so. #[test] fn setting_the_codename_a_campaign_already_has_writes_nothing() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); run(&temp, &["campaign", "set", "v1 release", "-c", "v1"]); let before = campaign_file(&worklane, CAMP_A); let out = run(&temp, &["campaign", "set", "v1", "-c", "v1"]); assert!(out.status.success(), "{}", stderr(&out)); assert!(stdout(&out).contains("No changes"), "{}", stdout(&out)); assert_eq!(campaign_file(&worklane, CAMP_A), before, "the file was rewritten"); } /// A codename set here resolves the campaign afterwards, which is the whole point of the /// field being typable: the tier is in the shared resolver, not in `campaign add`. #[test] fn a_codename_set_afterwards_resolves_the_campaign() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); run(&temp, &["campaign", "set", "v1 release", "-c", "beaver"]); let out = run(&temp, &["assign", "beaver", MEMBER]); assert!(out.status.success(), "{}", stderr(&out)); assert!( ticket(&worklane, MEMBER).contains(&format!("- **Campaign**: {CAMP_A}")), "{}", ticket(&worklane, MEMBER) ); } /// Nothing else in the file moves. A campaign carries prose and an owner this command /// cannot reach, and a hand-written section has to survive an edit to a field. #[test] fn content_the_command_cannot_reach_survives_a_set() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); let path = worklane .join("campaigns") .join(format!("{}_{}.md", CAMP_A, slug("v1 release"))); let original = fs::read_to_string(&path).unwrap(); fs::write( &path, original.replace("## Metadata", "## Notes\nA handwritten note.\n\n## Metadata"), ) .unwrap(); run(&temp, &["campaign", "set", "v1 release", "-c", "v1"]); let content = campaign_file(&worklane, CAMP_A); assert!(content.contains("A handwritten note."), "{content}"); assert!(content.contains("What has to ship together."), "{content}"); assert!(content.contains("- **Owner**: alice"), "{content}"); assert!(!content.contains("**Status**"), "a status was invented: {content}"); } /// A campaign that does not resolve leaves everything alone, the answer `assign` already /// gives an unknown campaign. #[test] fn an_unknown_campaign_is_refused_and_nothing_is_written() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); let out = run(&temp, &["campaign", "set", "nothing-by-that-name", "-c", "v1"]); assert_eq!(out.status.code(), Some(1), "{}", stderr(&out)); assert!(stderr(&out).contains("No campaign found"), "{}", stderr(&out)); assert!( !campaign_file(&worklane, CAMP_A).contains("Codename"), "something was written" ); assert!( !campaign_file(&worklane, CAMP_B).contains("Codename"), "something was written" ); } /// No `--description`: `set` and `append` are a pair for a prose section, and a way to /// replace a description with no way to add a paragraph is half of it. Pinned so the /// omission stays a decision rather than becoming an oversight somebody fills in. #[test] fn campaign_set_offers_no_description_and_no_owner() { let temp = TempDir::new().unwrap(); setup_worklane(&temp); let out = run(&temp, &["campaign", "set", "--help"]); let help = stdout(&out); assert!(help.contains("--codename"), "{help}"); assert!(help.contains("--title"), "{help}"); assert!(!help.contains("--description"), "{help}"); assert!(!help.contains("--owner"), "{help}"); assert!(!help.contains("--clear"), "{help}"); } /// A codename is how a campaign is referred to once it has one: on every card in it and at /// the command line. Replacing that silently is the mistake `install-skill --force` exists /// to prevent, so it is refused and the refusal names the flag. #[test] fn an_existing_codename_is_not_replaced_without_force() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); run(&temp, &["campaign", "set", "v1 release", "-c", "v1"]); let before = campaign_file(&worklane, CAMP_A); let out = run(&temp, &["campaign", "set", "v1", "-c", "beaver"]); assert_eq!(out.status.code(), Some(1), "{}", stderr(&out)); let said = stderr(&out); // Both words and the flag, so the message says what is there, what was asked for and // how to get it. assert!(said.contains("already codenamed 'v1'"), "{said}"); assert!(said.contains("'beaver'"), "{said}"); assert!(said.contains("--force"), "{said}"); assert_eq!(campaign_file(&worklane, CAMP_A), before, "the file was written anyway"); } #[test] fn force_replaces_an_existing_codename() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); run(&temp, &["campaign", "set", "v1 release", "-c", "v1"]); let out = run(&temp, &["campaign", "set", "v1", "-c", "beaver", "--force"]); assert!(out.status.success(), "{}", stderr(&out)); let content = campaign_file(&worklane, CAMP_A); assert!(content.contains("- **Codename**: beaver"), "{content}"); // The line is replaced, not added beside the old one. Asserted on the field rather // than on the word, since the campaign's title is "v1 release" and holds it too. assert!(!content.contains("- **Codename**: v1"), "the old line survived: {content}"); assert_eq!(content.matches("**Codename**").count(), 1, "two codenames: {content}"); } /// The guard is about overwriting a *different* word. A campaign with no codename has /// nothing to overwrite, so `--force` is not the price of the first one. #[test] fn the_first_codename_needs_no_force() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); assert!( !campaign_file(&worklane, CAMP_A).contains("Codename"), "the fixture changed" ); let out = run(&temp, &["campaign", "set", "v1 release", "-c", "v1"]); assert!(out.status.success(), "{}", stderr(&out)); assert!(campaign_file(&worklane, CAMP_A).contains("- **Codename**: v1")); } /// And setting the one it already has stays a no-op rather than becoming a refusal: there /// is nothing to overwrite, which is the same answer `install-skill` gives an identical /// document. #[test] fn setting_the_same_codename_again_is_a_no_op_not_a_refusal() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); run(&temp, &["campaign", "set", "v1 release", "-c", "v1"]); let out = run(&temp, &["campaign", "set", "v1", "-c", "v1"]); assert!(out.status.success(), "{}", stderr(&out)); assert!(stdout(&out).contains("No changes"), "{}", stdout(&out)); assert!(campaign_file(&worklane, CAMP_A).contains("- **Codename**: v1")); } /// Refused before any edit is applied, like `SetArgs::contested`, so a call carrying both /// fields writes neither rather than half of what was asked for. #[test] fn a_refused_codename_takes_the_title_down_with_it() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); run(&temp, &["campaign", "set", "v1 release", "-c", "v1"]); let before = campaign_file(&worklane, CAMP_A); let out = run( &temp, &[ "campaign", "set", "v1", "-t", "Renamed anyway", "-c", "beaver", ], ); assert_eq!(out.status.code(), Some(1), "{}", stderr(&out)); assert_eq!(campaign_file(&worklane, CAMP_A), before, "the title went through"); let names = campaigns(&worklane); assert!( !names.iter().any(|name| name.contains("renamed-anyway")), "renamed: {names:?}" ); } /// The title needs no guard, since a campaign always has one and replacing it is what the /// flag is for. `--force` is the codename's alone. #[test] fn the_title_is_replaced_without_force() { let temp = TempDir::new().unwrap(); let worklane = board(&temp); run(&temp, &["campaign", "set", "v1 release", "-c", "v1"]); let out = run(&temp, &["campaign", "set", "v1", "--title", "First public release"]); assert!(out.status.success(), "{}", stderr(&out)); assert!(campaign_file(&worklane, CAMP_A).starts_with("# First public release\n")); }