use std::path::Path;
use std::process::Command;
fn git(dir: &Path, args: &[&str]) -> String {
let out = Command::new("git")
.args(["-c", "user.name=test", "-c", "user.email=test@test.com"])
.args(args)
.current_dir(dir)
.output()
.unwrap();
assert!(
out.status.success(),
"git {args:?} failed: {}",
String::from_utf8_lossy(&out.stderr)
);
String::from_utf8_lossy(&out.stdout).trim().to_string()
}
fn write_file(p: &Path, text: &str) {
std::fs::create_dir_all(p.parent().unwrap()).unwrap();
std::fs::write(p, text).unwrap();
}
fn init_repo() -> tempfile::TempDir {
let t = tempfile::tempdir().unwrap();
git(t.path(), &["init", "-q", "-b", "main"]);
write_file(&t.path().join("README.md"), "initial\n");
git(t.path(), &["add", "."]);
git(t.path(), &["commit", "-qm", "initial", "--no-gpg-sign"]);
t
}
fn blyte_output(cwd: &Path, args: &[&str]) -> std::process::Output {
let env_dir = tempfile::tempdir().unwrap();
Command::new(env!("CARGO_BIN_EXE_blyte"))
.args(args)
.current_dir(cwd)
.env("BLYTE_NO_UPDATE_CHECK", "1")
.env("BLYTE_ENV_FILE", "")
.env("BLYTE_STATE_DIR", env_dir.path().join("state"))
.env("BLYTE_GATE_SLOT_DIR", env_dir.path().join("slots"))
.env("HOME", env_dir.path().join("home"))
.env("XDG_CONFIG_HOME", env_dir.path().join("home/.config"))
.env_remove("GEMINI_API_KEY")
.env_remove("GOOGLE_API_KEY")
.env_remove("ANTHROPIC_API_KEY")
.env_remove("OPENAI_API_KEY")
.env_remove("OPENROUTER_API_KEY")
.env_remove("OLLAMA_API_KEY")
.env_remove("OLLAMA_HOST")
.env_remove("BLYTE_MODEL")
.output()
.unwrap()
}
#[test]
fn test_clean_verdict_accepted_unmerged_kept() {
let repo = init_repo();
let root = repo.path();
let wt_path = root.join(".wt/T1");
git(
root,
&["worktree", "add", "-b", "wip/T1", wt_path.to_str().unwrap()],
);
write_file(&wt_path.join("file.txt"), "some work\n");
git(&wt_path, &["add", "."]);
git(&wt_path, &["commit", "-qm", "feat: work", "--no-gpg-sign"]);
let wt_dir = root.join(".wt");
write_file(
&wt_dir.join("T1.start.json"),
&serde_json::json!({ "id": "T1", "pid": 999999, "started_at": 1000 }).to_string(),
);
write_file(
&wt_dir.join("T1.json"),
&serde_json::json!({ "outcome": "verified" }).to_string(),
);
write_file(
&wt_dir.join("T1.verdict.json"),
&serde_json::json!({ "verdict": "accepted" }).to_string(),
);
write_file(&wt_dir.join("T1.log"), "log content\n");
write_file(
&wt_dir.join("T1.report.json"),
&serde_json::json!({ "turns": 10 }).to_string(),
);
write_file(&wt_dir.join("other_dir/some_file"), "leave me\n");
let out = blyte_output(root, &["clean"]);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(out.status.success(), "clean failed: {stdout}");
assert!(
stdout.contains("kept .wt/T1 (not merged yet)"),
"stdout was: {stdout}"
);
assert!(stdout.contains("freed 0 B"), "stdout was: {stdout}");
assert!(
!stdout.contains("other_dir"),
"stdout should not mention other_dir: {stdout}"
);
assert!(wt_path.exists());
assert!(wt_dir.join("T1.start.json").is_file());
assert!(wt_dir.join("T1.json").is_file());
assert!(wt_dir.join("T1.verdict.json").is_file());
assert!(wt_dir.join("T1.log").is_file());
assert!(wt_dir.join("T1.report.json").is_file());
assert!(wt_dir.join("other_dir/some_file").is_file());
let branch_check = Command::new("git")
.args(["rev-parse", "--verify", "refs/heads/wip/T1"])
.current_dir(root)
.output()
.unwrap();
assert!(branch_check.status.success());
}
#[test]
fn test_clean_cherry_picked_onto_main_deletes_branch() {
let repo = init_repo();
let root = repo.path();
let wt_path = root.join(".wt/T2");
git(
root,
&["worktree", "add", "-b", "wip/T2", wt_path.to_str().unwrap()],
);
write_file(&wt_path.join("cherry.txt"), "cherry content\n");
git(&wt_path, &["add", "."]);
git(
&wt_path,
&["commit", "-qm", "feat: cherry", "--no-gpg-sign"],
);
let commit_sha = git(&wt_path, &["rev-parse", "HEAD"]);
git(root, &["cherry-pick", &commit_sha]);
let wt_dir = root.join(".wt");
write_file(
&wt_dir.join("T2.start.json"),
&serde_json::json!({ "id": "T2", "pid": 999999 }).to_string(),
);
write_file(
&wt_dir.join("T2.verdict.json"),
&serde_json::json!({ "verdict": "accepted" }).to_string(),
);
let out = blyte_output(root, &["clean"]);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(out.status.success(), "clean failed: {stdout}");
assert!(
stdout.contains("removed .wt/T2 (merged,"),
"stdout was: {stdout}"
);
assert!(
stdout.contains("deleted branch wip/T2 (merged)"),
"stdout was: {stdout}"
);
assert!(!wt_path.exists());
let branch_check = Command::new("git")
.args(["rev-parse", "--verify", "refs/heads/wip/T2"])
.current_dir(root)
.output()
.unwrap();
assert!(!branch_check.status.success(), "branch should be deleted");
}
#[test]
fn test_clean_squash_merged_runs_removed_by_merge_tree_check() {
let repo = init_repo();
let root = repo.path();
let s1_path = root.join(".wt/S1");
git(
root,
&["worktree", "add", "-b", "wip/S1", s1_path.to_str().unwrap()],
);
write_file(&s1_path.join("s1.txt"), "feature 1\n");
git(&s1_path, &["add", "."]);
git(&s1_path, &["commit", "-qm", "feat: s1", "--no-gpg-sign"]);
let s2_path = root.join(".wt/S2");
git(
root,
&["worktree", "add", "-b", "wip/S2", s2_path.to_str().unwrap()],
);
write_file(&s2_path.join("s2.txt"), "feature 2\n");
git(&s2_path, &["add", "."]);
git(&s2_path, &["commit", "-qm", "feat: s2", "--no-gpg-sign"]);
let wt_dir = root.join(".wt");
write_file(
&wt_dir.join("S1.start.json"),
&serde_json::json!({ "id": "S1", "pid": 999999 }).to_string(),
);
write_file(
&wt_dir.join("S1.verdict.json"),
&serde_json::json!({ "verdict": "accepted" }).to_string(),
);
write_file(
&wt_dir.join("S2.start.json"),
&serde_json::json!({ "id": "S2", "pid": 999999 }).to_string(),
);
write_file(
&wt_dir.join("S2.verdict.json"),
&serde_json::json!({ "verdict": "accepted" }).to_string(),
);
write_file(&root.join("s1.txt"), "feature 1\n");
write_file(&root.join("s2.txt"), "feature 2\n");
git(root, &["add", "."]);
git(
root,
&["commit", "-qm", "squash: s1 and s2", "--no-gpg-sign"],
);
let out = blyte_output(root, &["clean"]);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(out.status.success(), "clean failed: {stdout}");
assert!(
stdout.contains("removed .wt/S1 (merged,"),
"stdout was: {stdout}"
);
assert!(
stdout.contains("deleted branch wip/S1 (merged)"),
"stdout was: {stdout}"
);
assert!(
stdout.contains("removed .wt/S2 (merged,"),
"stdout was: {stdout}"
);
assert!(
stdout.contains("deleted branch wip/S2 (merged)"),
"stdout was: {stdout}"
);
assert!(!s1_path.exists());
assert!(!s2_path.exists());
let b1 = Command::new("git")
.args(["rev-parse", "--verify", "refs/heads/wip/S1"])
.current_dir(root)
.output()
.unwrap();
assert!(!b1.status.success());
let b2 = Command::new("git")
.args(["rev-parse", "--verify", "refs/heads/wip/S2"])
.current_dir(root)
.output()
.unwrap();
assert!(!b2.status.success());
}
#[test]
fn test_clean_local_non_wip_branch_not_merged_kept() {
let repo = init_repo();
let root = repo.path();
let f1_path = root.join(".wt/F1");
git(
root,
&["worktree", "add", "-b", "wip/F1", f1_path.to_str().unwrap()],
);
write_file(&f1_path.join("feat.txt"), "feature\n");
git(&f1_path, &["add", "."]);
git(&f1_path, &["commit", "-qm", "feat: f1", "--no-gpg-sign"]);
git(root, &["branch", "feature/my-feat", "wip/F1"]);
let wt_dir = root.join(".wt");
write_file(
&wt_dir.join("F1.start.json"),
&serde_json::json!({ "id": "F1", "pid": 999999 }).to_string(),
);
write_file(
&wt_dir.join("F1.verdict.json"),
&serde_json::json!({ "verdict": "accepted" }).to_string(),
);
let out = blyte_output(root, &["clean"]);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(out.status.success(), "clean failed: {stdout}");
assert!(
stdout.contains("kept .wt/F1 (not merged yet)"),
"stdout was: {stdout}"
);
assert!(f1_path.exists(), "worktree should be kept");
let b1 = Command::new("git")
.args(["rev-parse", "--verify", "refs/heads/wip/F1"])
.current_dir(root)
.output()
.unwrap();
assert!(b1.status.success(), "wip branch should be kept");
let b2 = Command::new("git")
.args(["rev-parse", "--verify", "refs/heads/feature/my-feat"])
.current_dir(root)
.output()
.unwrap();
assert!(b2.status.success(), "feature branch should be kept");
}
#[test]
fn test_clean_verdict_redone_removes_worktree_keeps_unmerged_branch() {
let repo = init_repo();
let root = repo.path();
let r1_path = root.join(".wt/R1");
git(
root,
&["worktree", "add", "-b", "wip/R1", r1_path.to_str().unwrap()],
);
write_file(&r1_path.join("redone.txt"), "redone\n");
git(&r1_path, &["add", "."]);
git(&r1_path, &["commit", "-qm", "feat: r1", "--no-gpg-sign"]);
let wt_dir = root.join(".wt");
write_file(
&wt_dir.join("R1.start.json"),
&serde_json::json!({ "id": "R1", "pid": 999999 }).to_string(),
);
write_file(
&wt_dir.join("R1.json"),
&serde_json::json!({ "outcome": "verified" }).to_string(),
);
write_file(
&wt_dir.join("R1.verdict.json"),
&serde_json::json!({ "verdict": "redone" }).to_string(),
);
write_file(&wt_dir.join("R1.log"), "log content\n");
write_file(
&wt_dir.join("R1.report.json"),
&serde_json::json!({ "turns": 5 }).to_string(),
);
let out = blyte_output(root, &["clean"]);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(out.status.success(), "clean failed: {stdout}");
assert!(
stdout.contains("removed .wt/R1 (verdict redone,"),
"stdout was: {stdout}"
);
assert!(!r1_path.exists(), "worktree should be removed");
assert!(wt_dir.join("R1.start.json").is_file());
assert!(wt_dir.join("R1.json").is_file());
assert!(wt_dir.join("R1.verdict.json").is_file());
assert!(wt_dir.join("R1.log").is_file());
assert!(wt_dir.join("R1.report.json").is_file());
let branch_check = Command::new("git")
.args(["rev-parse", "--verify", "refs/heads/wip/R1"])
.current_dir(root)
.output()
.unwrap();
assert!(
branch_check.status.success(),
"unmerged branch wip/R1 must be kept"
);
}
#[test]
fn test_clean_uncommitted_changes_kept() {
let repo = init_repo();
let root = repo.path();
let wt_path = root.join(".wt/T3");
git(
root,
&["worktree", "add", "-b", "wip/T3", wt_path.to_str().unwrap()],
);
write_file(&wt_path.join("uncommitted.txt"), "not committed\n");
let wt_dir = root.join(".wt");
write_file(
&wt_dir.join("T3.start.json"),
&serde_json::json!({ "id": "T3", "pid": 999999 }).to_string(),
);
write_file(
&wt_dir.join("T3.verdict.json"),
&serde_json::json!({ "verdict": "redone" }).to_string(),
);
let out = blyte_output(root, &["clean"]);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(out.status.success(), "clean failed: {stdout}");
assert!(
stdout.contains("kept .wt/T3 (uncommitted changes)"),
"stdout was: {stdout}"
);
assert!(wt_path.exists(), "worktree should be kept");
let branch_check = Command::new("git")
.args(["rev-parse", "--verify", "refs/heads/wip/T3"])
.current_dir(root)
.output()
.unwrap();
assert!(branch_check.status.success(), "branch should be kept");
}
#[test]
fn test_clean_no_verdict_recent_kept_older_than_zero_removed() {
let repo = init_repo();
let root = repo.path();
let wt_path = root.join(".wt/T4");
git(
root,
&["worktree", "add", "-b", "wip/T4", wt_path.to_str().unwrap()],
);
write_file(&wt_path.join("file.txt"), "work\n");
git(&wt_path, &["add", "."]);
git(&wt_path, &["commit", "-qm", "feat: work", "--no-gpg-sign"]);
let wt_dir = root.join(".wt");
write_file(
&wt_dir.join("T4.start.json"),
&serde_json::json!({ "id": "T4", "pid": 999999 }).to_string(),
);
write_file(
&wt_dir.join("T4.json"),
&serde_json::json!({ "outcome": "verified" }).to_string(),
);
let out = blyte_output(root, &["clean"]);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(out.status.success());
assert!(
stdout.contains("kept .wt/T4 (no verdict)"),
"stdout was: {stdout}"
);
assert!(wt_path.exists(), "worktree should still exist");
let out = blyte_output(root, &["clean", "--older-than", "0"]);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(out.status.success());
assert!(
stdout.contains("removed .wt/T4 (finished over 0 days ago,"),
"stdout was: {stdout}"
);
assert!(!wt_path.exists(), "worktree should now be removed");
let branch_check = Command::new("git")
.args(["rev-parse", "--verify", "refs/heads/wip/T4"])
.current_dir(root)
.output()
.unwrap();
assert!(
branch_check.status.success(),
"unmerged branch wip/T4 must be kept"
);
}
#[test]
fn test_clean_dry_run_changes_nothing() {
let repo = init_repo();
let root = repo.path();
let wt_path = root.join(".wt/T5");
git(
root,
&["worktree", "add", "-b", "wip/T5", wt_path.to_str().unwrap()],
);
write_file(&wt_path.join("file.txt"), "work\n");
git(&wt_path, &["add", "."]);
git(&wt_path, &["commit", "-qm", "feat: work", "--no-gpg-sign"]);
let wt_dir = root.join(".wt");
write_file(
&wt_dir.join("T5.start.json"),
&serde_json::json!({ "id": "T5", "pid": 999999 }).to_string(),
);
write_file(
&wt_dir.join("T5.verdict.json"),
&serde_json::json!({ "verdict": "redone" }).to_string(),
);
let out = blyte_output(root, &["clean", "--dry-run"]);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(out.status.success());
assert!(
stdout.contains("would remove .wt/T5 (verdict redone,"),
"stdout was: {stdout}"
);
assert!(stdout.contains("would free "), "stdout was: {stdout}");
assert!(wt_path.exists(), "worktree must not be removed in dry-run");
let branch_check = Command::new("git")
.args(["rev-parse", "--verify", "refs/heads/wip/T5"])
.current_dir(root)
.output()
.unwrap();
assert!(branch_check.status.success(), "branch must still exist");
}
#[test]
fn test_review_verdict_accepted_unmerged_worktree_kept() {
let repo = init_repo();
let root = repo.path();
let wt_path = root.join(".wt/T6");
git(
root,
&["worktree", "add", "-b", "wip/T6", wt_path.to_str().unwrap()],
);
write_file(&wt_path.join("file.txt"), "work\n");
git(&wt_path, &["add", "."]);
git(&wt_path, &["commit", "-qm", "feat: work", "--no-gpg-sign"]);
let wt_dir = root.join(".wt");
write_file(
&wt_dir.join("T6.start.json"),
&serde_json::json!({ "id": "T6", "pid": 999999 }).to_string(),
);
write_file(
&wt_dir.join("T6.json"),
&serde_json::json!({ "outcome": "verified" }).to_string(),
);
let out = blyte_output(root, &["review", "T6", "--verdict", "accepted"]);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(out.status.success(), "review failed: {stdout}");
assert!(
stdout.contains("recorded verdict accepted for T6"),
"stdout was: {stdout}"
);
assert!(
stdout.contains("worktree .wt/T6 kept until wip/T6 is merged"),
"stdout was: {stdout}"
);
assert!(!stdout.contains("removed"), "stdout was: {stdout}");
assert!(wt_path.exists(), "worktree should be kept");
assert!(
wt_dir.join("T6.verdict.json").is_file(),
"verdict file should exist"
);
let branch_check = Command::new("git")
.args(["rev-parse", "--verify", "refs/heads/wip/T6"])
.current_dir(root)
.output()
.unwrap();
assert!(branch_check.status.success(), "branch should be kept");
}
#[test]
fn test_review_verdict_redone_removes_worktree_keeps_branch() {
let repo = init_repo();
let root = repo.path();
let wt_path = root.join(".wt/T7");
git(
root,
&["worktree", "add", "-b", "wip/T7", wt_path.to_str().unwrap()],
);
write_file(&wt_path.join("file.txt"), "work\n");
git(&wt_path, &["add", "."]);
git(&wt_path, &["commit", "-qm", "feat: work", "--no-gpg-sign"]);
let wt_dir = root.join(".wt");
write_file(
&wt_dir.join("T7.start.json"),
&serde_json::json!({ "id": "T7", "pid": 999999 }).to_string(),
);
write_file(
&wt_dir.join("T7.json"),
&serde_json::json!({ "outcome": "verified" }).to_string(),
);
let out = blyte_output(root, &["review", "T7", "--verdict", "redone"]);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(out.status.success(), "review failed: {stdout}");
assert!(
stdout.contains("recorded verdict redone for T7"),
"stdout was: {stdout}"
);
assert!(
stdout.contains("removed .wt/T7; branch wip/T7 kept"),
"stdout was: {stdout}"
);
assert!(!wt_path.exists(), "worktree should be removed");
assert!(
wt_dir.join("T7.verdict.json").is_file(),
"verdict file should exist"
);
let branch_check = Command::new("git")
.args(["rev-parse", "--verify", "refs/heads/wip/T7"])
.current_dir(root)
.output()
.unwrap();
assert!(branch_check.status.success(), "branch should be kept");
}
#[test]
fn test_review_verdict_accepted_merged_removes_worktree_deletes_branch() {
let repo = init_repo();
let root = repo.path();
let wt_path = root.join(".wt/T8");
git(
root,
&["worktree", "add", "-b", "wip/T8", wt_path.to_str().unwrap()],
);
write_file(&wt_path.join("merged.txt"), "merged content\n");
git(&wt_path, &["add", "."]);
git(
&wt_path,
&["commit", "-qm", "feat: merged", "--no-gpg-sign"],
);
let commit_sha = git(&wt_path, &["rev-parse", "HEAD"]);
git(root, &["cherry-pick", &commit_sha]);
let wt_dir = root.join(".wt");
write_file(
&wt_dir.join("T8.start.json"),
&serde_json::json!({ "id": "T8", "pid": 999999 }).to_string(),
);
write_file(
&wt_dir.join("T8.json"),
&serde_json::json!({ "outcome": "verified" }).to_string(),
);
let out = blyte_output(root, &["review", "T8", "--verdict", "accepted"]);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(out.status.success(), "review failed: {stdout}");
assert!(
stdout.contains("recorded verdict accepted for T8"),
"stdout was: {stdout}"
);
assert!(
stdout.contains("removed .wt/T8; branch wip/T8 deleted"),
"stdout was: {stdout}"
);
assert!(!wt_path.exists(), "worktree should be removed");
let branch_check = Command::new("git")
.args(["rev-parse", "--verify", "refs/heads/wip/T8"])
.current_dir(root)
.output()
.unwrap();
assert!(!branch_check.status.success(), "branch should be deleted");
}
#[test]
fn test_clean_outcome_no_changes() {
let repo = init_repo();
let root = repo.path();
let wt_path = root.join(".wt/NC1");
git(
root,
&[
"worktree",
"add",
"-b",
"wip/NC1",
wt_path.to_str().unwrap(),
],
);
let wt_dir = root.join(".wt");
write_file(
&wt_dir.join("NC1.start.json"),
&serde_json::json!({ "id": "NC1", "pid": 999999 }).to_string(),
);
write_file(
&wt_dir.join("NC1.json"),
&serde_json::json!({ "outcome": "no_changes" }).to_string(),
);
let out = blyte_output(root, &["clean"]);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(out.status.success());
assert!(
stdout.contains("removed .wt/NC1 (no changes,"),
"stdout was: {stdout}"
);
assert!(!wt_path.exists(), "worktree should be removed");
}
#[test]
fn test_clean_research_run() {
let repo = init_repo();
let root = repo.path();
let wt_path = root.join(".wt/R1");
std::fs::create_dir_all(&wt_path).unwrap();
write_file(&wt_path.join("scratch.txt"), "some notes\n");
let wt_dir = root.join(".wt");
write_file(
&wt_dir.join("R1.start.json"),
&serde_json::json!({ "id": "R1", "pid": 999999, "research": true }).to_string(),
);
write_file(&wt_dir.join("R1.report.md"), "# Research Report\n");
let out = blyte_output(root, &["clean"]);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(out.status.success());
assert!(
stdout.contains("removed .wt/R1 (research report,"),
"stdout was: {stdout}"
);
assert!(!wt_path.exists(), "scratch directory should be removed");
}
#[test]
fn test_dispatch_cleans_quietly() {
let repo = init_repo();
let root = repo.path();
let wt_old = root.join(".wt/OLD");
git(
root,
&["worktree", "add", "-b", "wip/OLD", wt_old.to_str().unwrap()],
);
let wt_dir = root.join(".wt");
write_file(
&wt_dir.join("OLD.start.json"),
&serde_json::json!({ "id": "OLD", "pid": 999999 }).to_string(),
);
write_file(
&wt_dir.join("OLD.verdict.json"),
&serde_json::json!({ "verdict": "redone" }).to_string(),
);
write_file(&root.join("task.md"), "implement something\n");
let out = blyte_output(
root,
&[
"dispatch",
"D1",
"task.md",
"--gate",
"true",
"--sandbox",
"off",
"--no-base-check",
],
);
let _ = out;
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);
while wt_old.exists() && std::time::Instant::now() < deadline {
std::thread::sleep(std::time::Duration::from_millis(10));
}
assert!(!wt_old.exists(), "old worktree should have been removed");
}
#[test]
fn test_clean_merged_on_origin_main_only() {
let repo = init_repo();
let root = repo.path();
let wt_path = root.join(".wt/T1");
git(
root,
&["worktree", "add", "-b", "wip/T1", wt_path.to_str().unwrap()],
);
write_file(&wt_path.join("file.txt"), "some work\n");
git(&wt_path, &["add", "."]);
git(&wt_path, &["commit", "-qm", "feat: work", "--no-gpg-sign"]);
let tip = git(&wt_path, &["rev-parse", "HEAD"]);
git(root, &["update-ref", "refs/remotes/origin/main", &tip]);
let wt_dir = root.join(".wt");
write_file(
&wt_dir.join("T1.start.json"),
&serde_json::json!({ "id": "T1", "pid": 999999, "started_at": 1000 }).to_string(),
);
write_file(
&wt_dir.join("T1.json"),
&serde_json::json!({ "outcome": "verified" }).to_string(),
);
write_file(
&wt_dir.join("T1.verdict.json"),
&serde_json::json!({ "verdict": "accepted" }).to_string(),
);
let out = blyte_output(root, &["clean"]);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(out.status.success(), "clean failed: {stdout}");
assert!(
stdout.contains("removed .wt/T1 (merged"),
"stdout was: {stdout}"
);
assert!(!wt_path.exists());
}
#[test]
fn test_clean_squash_merged_run_lines_later_changed_on_main() {
let repo = init_repo();
let root = repo.path();
write_file(&root.join(".gitignore"), ".wt/\n");
git(root, &["add", ".gitignore"]);
git(root, &["commit", "-qm", "ignore .wt", "--no-gpg-sign"]);
let s1_path = root.join(".wt/S1");
git(
root,
&["worktree", "add", "-b", "wip/S1", s1_path.to_str().unwrap()],
);
write_file(
&s1_path.join("code.rs"),
"fn hello() {\n println!(\"hello from S1\");\n}\n",
);
git(&s1_path, &["add", "."]);
git(&s1_path, &["commit", "-qm", "feat: s1", "--no-gpg-sign"]);
let wt_dir = root.join(".wt");
write_file(
&wt_dir.join("S1.start.json"),
&serde_json::json!({ "id": "S1", "pid": 999999, "started_at": 1000 }).to_string(),
);
write_file(
&wt_dir.join("S1.verdict.json"),
&serde_json::json!({ "verdict": "accepted" }).to_string(),
);
git(root, &["checkout", "main"]);
git(root, &["merge", "--squash", "wip/S1"]);
git(root, &["commit", "-qm", "squash merge S1", "--no-gpg-sign"]);
write_file(
&root.join("code.rs"),
"fn hello() {\n println!(\"hello from main later\");\n}\n",
);
git(root, &["add", "."]);
git(
root,
&["commit", "-qm", "main: edit code again", "--no-gpg-sign"],
);
let out = blyte_output(root, &["clean"]);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(out.status.success(), "clean failed: {stdout}");
assert!(
stdout.contains("removed .wt/S1 (merged,"),
"stdout was: {stdout}"
);
assert!(
stdout.contains("deleted branch wip/S1 (merged)"),
"stdout was: {stdout}"
);
assert!(!s1_path.exists(), "worktree should be removed");
let b = Command::new("git")
.args(["rev-parse", "--verify", "refs/heads/wip/S1"])
.current_dir(root)
.output()
.unwrap();
assert!(!b.status.success(), "wip/S1 branch should be deleted");
}
#[test]
fn test_clean_unmerged_run_whose_files_main_later_changed_is_kept() {
let repo = init_repo();
let root = repo.path();
write_file(&root.join(".gitignore"), ".wt/\n");
git(root, &["add", ".gitignore"]);
git(root, &["commit", "-qm", "ignore .wt", "--no-gpg-sign"]);
write_file(
&root.join("code.rs"),
"fn hello() {\n println!(\"hello\");\n}\n",
);
git(root, &["add", "."]);
git(root, &["commit", "-qm", "init code", "--no-gpg-sign"]);
let u1_path = root.join(".wt/U1");
git(
root,
&["worktree", "add", "-b", "wip/U1", u1_path.to_str().unwrap()],
);
write_file(
&u1_path.join("code.rs"),
"fn hello() {\n println!(\"unmerged work\");\n}\n",
);
git(&u1_path, &["add", "."]);
git(&u1_path, &["commit", "-qm", "feat: u1", "--no-gpg-sign"]);
let wt_dir = root.join(".wt");
write_file(
&wt_dir.join("U1.start.json"),
&serde_json::json!({ "id": "U1", "pid": 999999, "started_at": 1000 }).to_string(),
);
write_file(
&wt_dir.join("U1.verdict.json"),
&serde_json::json!({ "verdict": "accepted" }).to_string(),
);
write_file(
&root.join("code.rs"),
"fn hello() {\n println!(\"completely different on main\");\n}\n",
);
git(root, &["add", "."]);
git(
root,
&["commit", "-qm", "main: change code", "--no-gpg-sign"],
);
let out = blyte_output(root, &["clean"]);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(out.status.success(), "clean failed: {stdout}");
assert!(
stdout.contains("kept .wt/U1 (not merged yet)"),
"stdout was: {stdout}"
);
assert!(u1_path.exists(), "worktree should be kept");
let b = Command::new("git")
.args(["rev-parse", "--verify", "refs/heads/wip/U1"])
.current_dir(root)
.output()
.unwrap();
assert!(b.status.success(), "wip/U1 branch should be kept");
}
#[test]
fn test_clean_cherry_pick_record_squash_commit_blyte_clean_and_quiet() {
let repo = init_repo();
let root = repo.path();
write_file(&root.join(".gitignore"), ".wt/\n");
git(root, &["add", ".gitignore"]);
git(root, &["commit", "-qm", "ignore .wt", "--no-gpg-sign"]);
let content = "line 1: initial\nline 2: initial\nline 3: initial\n";
write_file(&root.join("file.txt"), content);
git(root, &["add", "file.txt"]);
git(root, &["commit", "-qm", "initial commit", "--no-gpg-sign"]);
let wt_dir = root.join(".wt");
std::fs::create_dir_all(&wt_dir).unwrap();
let t1_path = wt_dir.join("T1");
git(
root,
&["worktree", "add", "-b", "wip/T1", t1_path.to_str().unwrap()],
);
let t1_content = "line 1: T1 changed\nline 2: initial\nline 3: initial\n";
write_file(&t1_path.join("file.txt"), t1_content);
git(&t1_path, &["commit", "-am", "T1 work", "--no-gpg-sign"]);
let t1_sha = git(&t1_path, &["rev-parse", "HEAD"]);
write_file(
&wt_dir.join("T1.start.json"),
&serde_json::json!({ "id": "T1", "pid": 999999, "started_at": 1000 }).to_string(),
);
write_file(
&wt_dir.join("T1.verdict.json"),
&serde_json::json!({ "verdict": "accepted" }).to_string(),
);
let t1b_path = wt_dir.join("T1b");
git(
root,
&[
"worktree",
"add",
"-b",
"wip/T1b",
t1b_path.to_str().unwrap(),
],
);
let t1b_content = "line 1: initial\nline 2: T1b changed\nline 3: initial\n";
write_file(&t1b_path.join("file.txt"), t1b_content);
git(&t1b_path, &["commit", "-am", "T1b work", "--no-gpg-sign"]);
let t1b_sha = git(&t1b_path, &["rev-parse", "HEAD"]);
write_file(
&wt_dir.join("T1b.start.json"),
&serde_json::json!({ "id": "T1b", "pid": 999999, "started_at": 1000 }).to_string(),
);
write_file(
&wt_dir.join("T1b.verdict.json"),
&serde_json::json!({ "verdict": "accepted" }).to_string(),
);
let main_content =
"line 1: main squash conflict\nline 2: main squash conflict\nline 3: initial\n";
write_file(&root.join("file.txt"), main_content);
let commit_msg = format!(
"Squash merge PR\n\n* feat: T1\n (cherry picked from commit {t1_sha})\n* feat: T1b\n (cherry picked from commit {t1b_sha})\n"
);
git(root, &["commit", "-am", &commit_msg, "--no-gpg-sign"]);
let mt1 = Command::new("git")
.args(["merge-tree", "--write-tree", "main", "wip/T1"])
.current_dir(root)
.output()
.unwrap();
let main_tree = git(root, &["rev-parse", "main^{tree}"]);
assert!(
!mt1.status.success()
|| String::from_utf8_lossy(&mt1.stdout)
.lines()
.next()
.unwrap_or("")
!= main_tree
);
let out = blyte_output(root, &["clean"]);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(out.status.success(), "clean failed: {stdout}");
assert!(
stdout.contains("removed .wt/T1 (merged,"),
"stdout was: {stdout}"
);
assert!(
stdout.contains("deleted branch wip/T1 (merged)"),
"stdout was: {stdout}"
);
assert!(
stdout.contains("removed .wt/T1b (merged,"),
"stdout was: {stdout}"
);
assert!(
stdout.contains("deleted branch wip/T1b (merged)"),
"stdout was: {stdout}"
);
assert!(!t1_path.exists());
assert!(!t1b_path.exists());
}
#[test]
fn test_clean_cherry_pick_record_squash_commit_quiet_clean() {
let repo = init_repo();
let root = repo.path();
write_file(&root.join(".gitignore"), ".wt/\n");
git(root, &["add", ".gitignore"]);
git(root, &["commit", "-qm", "ignore .wt", "--no-gpg-sign"]);
let content = "line 1: initial\nline 2: initial\nline 3: initial\n";
write_file(&root.join("file.txt"), content);
git(root, &["add", "file.txt"]);
git(root, &["commit", "-qm", "initial commit", "--no-gpg-sign"]);
let wt_dir = root.join(".wt");
std::fs::create_dir_all(&wt_dir).unwrap();
let t1_path = wt_dir.join("T1");
git(
root,
&["worktree", "add", "-b", "wip/T1", t1_path.to_str().unwrap()],
);
let t1_content = "line 1: T1 changed\nline 2: initial\nline 3: initial\n";
write_file(&t1_path.join("file.txt"), t1_content);
git(&t1_path, &["commit", "-am", "T1 work", "--no-gpg-sign"]);
let t1_sha = git(&t1_path, &["rev-parse", "HEAD"]);
write_file(
&wt_dir.join("T1.start.json"),
&serde_json::json!({ "id": "T1", "pid": 999999, "started_at": 1000 }).to_string(),
);
write_file(
&wt_dir.join("T1.verdict.json"),
&serde_json::json!({ "verdict": "accepted" }).to_string(),
);
let t1b_path = wt_dir.join("T1b");
git(
root,
&[
"worktree",
"add",
"-b",
"wip/T1b",
t1b_path.to_str().unwrap(),
],
);
let t1b_content = "line 1: initial\nline 2: T1b changed\nline 3: initial\n";
write_file(&t1b_path.join("file.txt"), t1b_content);
git(&t1b_path, &["commit", "-am", "T1b work", "--no-gpg-sign"]);
let t1b_sha = git(&t1b_path, &["rev-parse", "HEAD"]);
write_file(
&wt_dir.join("T1b.start.json"),
&serde_json::json!({ "id": "T1b", "pid": 999999, "started_at": 1000 }).to_string(),
);
write_file(
&wt_dir.join("T1b.verdict.json"),
&serde_json::json!({ "verdict": "accepted" }).to_string(),
);
let main_content =
"line 1: main squash conflict\nline 2: main squash conflict\nline 3: initial\n";
write_file(&root.join("file.txt"), main_content);
let commit_msg = format!(
"Squash merge PR\n\n* feat: T1\n (cherry picked from commit {t1_sha})\n* feat: T1b\n (cherry picked from commit {t1b_sha})\n"
);
git(root, &["commit", "-am", &commit_msg, "--no-gpg-sign"]);
blyte_cli::clean::clean_repo(root, 14, false, None, true);
assert!(!t1_path.exists());
assert!(!t1b_path.exists());
let b1 = Command::new("git")
.args(["rev-parse", "--verify", "refs/heads/wip/T1"])
.current_dir(root)
.output()
.unwrap();
assert!(!b1.status.success(), "wip/T1 branch should be deleted");
let b2 = Command::new("git")
.args(["rev-parse", "--verify", "refs/heads/wip/T1b"])
.current_dir(root)
.output()
.unwrap();
assert!(!b2.status.success(), "wip/T1b branch should be deleted");
}
#[test]
fn test_clean_cherry_pick_record_partial_commits_not_merged() {
let repo = init_repo();
let root = repo.path();
write_file(&root.join(".gitignore"), ".wt/\n");
git(root, &["add", ".gitignore"]);
git(root, &["commit", "-qm", "ignore .wt", "--no-gpg-sign"]);
let wt_dir = root.join(".wt");
std::fs::create_dir_all(&wt_dir).unwrap();
let t2_path = wt_dir.join("T2");
git(
root,
&["worktree", "add", "-b", "wip/T2", t2_path.to_str().unwrap()],
);
write_file(&t2_path.join("file1.txt"), "commit 1\n");
git(&t2_path, &["add", "file1.txt"]);
git(
&t2_path,
&["commit", "-qm", "feat: commit 1", "--no-gpg-sign"],
);
let c1_sha = git(&t2_path, &["rev-parse", "HEAD"]);
write_file(&t2_path.join("file2.txt"), "commit 2\n");
git(&t2_path, &["add", "file2.txt"]);
git(
&t2_path,
&["commit", "-qm", "feat: commit 2", "--no-gpg-sign"],
);
write_file(
&wt_dir.join("T2.start.json"),
&serde_json::json!({ "id": "T2", "pid": 999999, "started_at": 1000 }).to_string(),
);
write_file(
&wt_dir.join("T2.verdict.json"),
&serde_json::json!({ "verdict": "accepted" }).to_string(),
);
let commit_msg =
format!("Squash PR with only first commit\n\n(cherry picked from commit {c1_sha})\n");
write_file(&root.join("other.txt"), "other work\n");
git(root, &["add", "other.txt"]);
git(root, &["commit", "-qm", &commit_msg, "--no-gpg-sign"]);
let out = blyte_output(root, &["clean"]);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(out.status.success(), "clean failed: {stdout}");
assert!(
stdout.contains("kept .wt/T2 (not merged yet)"),
"stdout was: {stdout}"
);
assert!(t2_path.exists(), "worktree should be kept");
let b = Command::new("git")
.args(["rev-parse", "--verify", "refs/heads/wip/T2"])
.current_dir(root)
.output()
.unwrap();
assert!(b.status.success(), "wip/T2 branch should be kept");
}