use std::fs;
use crate::delivery::Repo;
use crate::delivery_repo::tests::{project, tip};
use crate::delivery_repo::Project;
#[test]
fn deliver_aborts_when_the_branch_carries_content_beyond_its_delivery() {
let (tmp, root, p) = project();
let wt = tmp.path().join("wt");
p.materialize(&wt, "work/bl-x").unwrap();
fs::write(wt.join("feature.txt"), "shipped\n").unwrap();
Project::run(&wt, &["add", "-A"]).unwrap();
Project::run(&wt, &["commit", "-qm", "wip"]).unwrap();
p.deliver(&wt, "work/bl-x", "main", "Add feature [bl-x]", "[bl-x]").unwrap();
assert_eq!(tip(&root), "Add feature [bl-x]"); fs::write(wt.join("more.txt"), "undelivered\n").unwrap();
Project::run(&wt, &["add", "-A"]).unwrap();
Project::run(&wt, &["commit", "-qm", "more work"]).unwrap();
let err = p.deliver(&wt, "work/bl-x", "main", "Add feature [bl-x]", "[bl-x]").unwrap_err();
assert!(err.to_string().contains("already delivered"), "{err}");
assert!(err.to_string().contains("undelivered changes"), "{err}");
assert_eq!(tip(&root), "Add feature [bl-x]");
assert!(Project::ok(&root, &["cat-file", "-e", "work/bl-x:more.txt"]).unwrap());
}
#[test]
fn deliver_skips_a_forge_squash_merge_whose_content_landed() {
let (tmp, root, p) = project();
let wt = tmp.path().join("wt");
p.materialize(&wt, "work/bl-x").unwrap();
fs::write(wt.join("feature.txt"), "shipped\n").unwrap();
Project::run(&wt, &["add", "-A"]).unwrap();
Project::run(&wt, &["commit", "-qm", "wip"]).unwrap();
fs::write(root.join("feature.txt"), "shipped\n").unwrap();
Project::run(&root, &["add", "-A"]).unwrap();
Project::run(&root, &["commit", "-qm", "Add feature (#7) [bl-x]"]).unwrap();
p.deliver(&wt, "work/bl-x", "main", "Add feature [bl-x]", "[bl-x]").unwrap();
assert_eq!(p.marked("main", "[bl-x]").unwrap().len(), 1); assert_eq!(tip(&root), "Add feature (#7) [bl-x]");
}
#[test]
fn prune_preserves_a_diverged_branch_carrying_work_beyond_its_delivery() {
let (tmp, root, p) = project();
let wt = tmp.path().join("wt");
p.materialize(&wt, "work/bl-x").unwrap();
fs::write(wt.join("feature.txt"), "shipped\n").unwrap();
Project::run(&wt, &["add", "-A"]).unwrap();
Project::run(&wt, &["commit", "-qm", "wip"]).unwrap();
p.deliver(&wt, "work/bl-x", "main", "Add feature [bl-x]", "[bl-x]").unwrap();
fs::write(wt.join("more.txt"), "undelivered\n").unwrap();
Project::run(&wt, &["add", "-A"]).unwrap();
Project::run(&wt, &["commit", "-qm", "more work"]).unwrap();
p.release(&wt).unwrap();
p.prune().unwrap();
assert!(Project::ok(&root, &["rev-parse", "--verify", "--quiet", "refs/heads/work/bl-x"]).unwrap());
}