use super::*;
use std::cell::RefCell;
use std::path::Path;
#[derive(Default)]
struct FakeRepo {
calls: RefCell<Vec<String>>,
fail_integration: bool,
}
impl FakeRepo {
fn log(&self, call: String) {
self.calls.borrow_mut().push(call);
}
fn calls(&self) -> Vec<String> {
self.calls.borrow().clone()
}
}
impl Repo for FakeRepo {
fn materialize(&self, path: &Path, branch: &str) -> io::Result<()> {
self.log(format!("materialize {} {branch}", path.display()));
Ok(())
}
fn release(&self, path: &Path) -> io::Result<()> {
self.log(format!("release {}", path.display()));
Ok(())
}
fn discard(&self, path: &Path, branch: &str) -> io::Result<()> {
self.log(format!("discard {} {branch}", path.display()));
Ok(())
}
fn integration(&self) -> io::Result<String> {
if self.fail_integration {
return Err(io::Error::other("no integration branch"));
}
self.log("integration".into());
Ok("main".into())
}
fn deliver(&self, path: &Path, branch: &str, integration: &str, subject: &str, marker: &str) -> io::Result<()> {
self.log(format!("deliver {} {branch} -> {integration} : {subject} : {marker}", path.display()));
Ok(())
}
fn work_messages(&self, _branch: &str, _integration: &str) -> io::Result<Vec<String>> {
Ok(Vec::new()) }
fn is_git_repo(&self) -> io::Result<bool> {
unreachable!("dispatch never gates on the precondition (see delivery_precondition)")
}
}
fn spec() -> Spec<'static> {
Spec {
worktree: Path::new("/wt"),
branch: "work/bl-f813",
subject: "Title [bl-f813]",
override_msg: None,
marker: "[bl-f813]",
}
}
fn drive(op: &str, phase: &str, rolling_back: bool) -> Vec<String> {
let repo = FakeRepo::default();
dispatch(op, phase, rolling_back, &repo, &spec()).unwrap();
repo.calls()
}
#[test]
fn claim_post_materializes() {
assert_eq!(drive("claim", "post", false), ["materialize /wt work/bl-f813"]);
}
#[test]
fn prime_post_does_not_materialize() {
assert_eq!(drive("prime", "post", false), Vec::<String>::new());
}
#[test]
fn unclaim_post_releases() {
assert_eq!(drive("unclaim", "post", false), ["release /wt"]);
}
#[test]
fn close_pre_resolves_integration_then_delivers() {
assert_eq!(
drive("close", "pre", false),
["integration", "deliver /wt work/bl-f813 -> main : Title [bl-f813] : [bl-f813]"]
);
}
#[test]
fn close_post_releases() {
assert_eq!(drive("close", "post", false), ["release /wt"]);
}
#[test]
fn claim_post_rollback_discards_worktree_and_branch() {
assert_eq!(drive("claim", "post", true), ["discard /wt work/bl-f813"]);
}
#[test]
fn declining_rollbacks_and_unwired_hooks_are_noops() {
assert!(drive("close", "pre", true).is_empty());
assert!(drive("close", "post", true).is_empty()); assert!(drive("unclaim", "post", true).is_empty()); assert!(drive("create", "post", false).is_empty()); assert!(drive("claim", "pre", false).is_empty()); }
#[test]
fn an_integration_failure_aborts_a_close() {
let repo = FakeRepo { fail_integration: true, ..FakeRepo::default() };
assert!(dispatch("close", "pre", false, &repo, &spec()).is_err());
}
#[test]
fn resolve_id_prefers_the_sealed_metadata_trailer() {
let mut md = Metadata::new();
md.insert("bl-id".into(), vec!["bl-abc1".into()]);
let id = resolve_id(Some(&md), || unreachable!("git is not consulted when metadata carries the id")).unwrap();
assert_eq!(id, "bl-abc1");
}
#[test]
fn resolve_id_reads_the_single_changed_task_file_on_a_pre_hook() {
let id = resolve_id(None, || Ok(vec!["tasks/bl-9f9f.md".into(), "README.md".into()])).unwrap();
assert_eq!(id, "bl-9f9f");
}
#[test]
fn resolve_id_rejects_zero_or_many_changed_task_files() {
let none = resolve_id(None, || Ok(vec!["README.md".into()])).unwrap_err();
assert!(none.to_string().contains("found 0"));
let many = resolve_id(None, || Ok(vec!["tasks/a.md".into(), "tasks/b.md".into()])).unwrap_err();
assert!(many.to_string().contains("found 2"));
}
#[test]
fn resolve_id_propagates_a_lister_error() {
let err = resolve_id(None, || Err(io::Error::other("git blew up"))).unwrap_err();
assert_eq!(err.to_string(), "git blew up");
}
#[test]
fn claim_post_surfaces_the_bare_path() {
let wt = Path::new("/wt/bl-x");
assert_eq!(surfaced("claim", "post", false, wt, true).as_deref(), Some("/wt/bl-x"));
}
#[test]
fn show_read_surfaces_a_field_line_only_when_the_worktree_exists() {
let wt = Path::new("/wt/bl-x");
assert_eq!(surfaced("show", "read", false, wt, true).as_deref(), Some(" worktree /wt/bl-x"));
assert_eq!(surfaced("show", "read", false, wt, false), None);
}
#[test]
fn no_other_hook_or_rollback_surfaces_anything() {
for (op, phase, rb, exists) in [
("claim", "post", true, true), ("claim", "pre", false, true),
("prime", "post", false, true), ("unclaim", "post", false, true),
("close", "pre", false, true),
("show", "read", true, true), ] {
assert_eq!(surfaced(op, phase, rb, Path::new("/wt"), exists), None, "{op}.{phase} rb={rb}");
}
}
#[test]
fn protocol_self_description_lists_every_hooked_op() {
let v: serde_json::Value = serde_json::from_str(PROTOCOL_JSON).unwrap();
assert_eq!(v["protocol"], serde_json::json!([1]));
assert_eq!(v["ops"], serde_json::json!(["claim", "unclaim", "close", "prime", "show"]));
}