feat(FN-4500): complete Step 2 — harden zero-unique classifier

Fusion-Task-Id: FN-4500
Fusion-Task-Lineage: 81d33759-70f6-4958-abf8-3db20f918c01
This commit is contained in:
Fusion
2026-05-14 12:19:09 -07:00
committed by gsxdsm
parent b8e387a004
commit bcb0035d1a
2 changed files with 193 additions and 1 deletions

View File

@@ -0,0 +1,132 @@
import { afterEach, describe, expect, it } from "vitest";
import { mkdtemp, rm, writeFile, appendFile, mkdir } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { exec } from "node:child_process";
import { promisify } from "node:util";
import { inspectBranchConflict } from "../branch-conflicts.js";
const execAsync = promisify(exec);
async function run(command: string, cwd: string): Promise<string> {
const { stdout } = await execAsync(command, { cwd, encoding: "utf-8" });
return stdout.trim();
}
describe("inspectBranchConflict zero-unique behavior", () => {
const dirs: string[] = [];
afterEach(async () => {
await Promise.all(dirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true })));
});
async function setupRepo() {
const repoDir = await mkdtemp(path.join(tmpdir(), "fn-4500-branch-conflict-"));
dirs.push(repoDir);
await run("git init -b main", repoDir);
await run("git config user.email test@example.com", repoDir);
await run("git config user.name 'Test User'", repoDir);
await writeFile(path.join(repoDir, "note.txt"), "base\n", "utf-8");
await run("git add note.txt && git commit -m 'chore: base'", repoDir);
return repoDir;
}
it("returns fully-subsumed when branch tip is ancestor of main", async () => {
const repoDir = await setupRepo();
await run("git checkout -b fusion/fn-9001", repoDir);
await run("git checkout main", repoDir);
const livePath = path.join(repoDir, "..", "live-9001");
await run(`git worktree add ${JSON.stringify(livePath)} fusion/fn-9001`, repoDir);
const stalePath = path.join(repoDir, "..", "stale-9001");
await mkdir(stalePath, { recursive: true });
const result = await inspectBranchConflict({
repoDir,
branchName: "fusion/fn-9001",
conflictingWorktreePath: stalePath,
requestingTaskId: "FN-9001",
ownerTaskId: "FN-9001",
startPoint: "main",
});
expect(result.kind).toBe("fully-subsumed");
});
it("returns fully-subsumed when branch patch already exists upstream", async () => {
const repoDir = await setupRepo();
await run("git checkout -b fusion/fn-9001", repoDir);
await appendFile(path.join(repoDir, "note.txt"), "change\n", "utf-8");
await run("git add note.txt", repoDir);
await run("git commit -m 'feat(FN-9001): change' -m 'Fusion-Task-Id: FN-9001'", repoDir);
const branchCommit = await run("git rev-parse HEAD", repoDir);
await run("git checkout main", repoDir);
await run(`git cherry-pick ${branchCommit}`, repoDir);
const livePath = path.join(repoDir, "..", "live-9001-upstream");
await run(`git worktree add ${JSON.stringify(livePath)} fusion/fn-9001`, repoDir);
const stalePath = path.join(repoDir, "..", "stale-9001-upstream");
await mkdir(stalePath, { recursive: true });
const result = await inspectBranchConflict({
repoDir,
branchName: "fusion/fn-9001",
conflictingWorktreePath: stalePath,
requestingTaskId: "FN-9001",
ownerTaskId: "FN-9001",
startPoint: "main",
});
expect(result.kind).toBe("fully-subsumed");
});
it("returns reclaimable when branch still has unique commit", async () => {
const repoDir = await setupRepo();
await run("git checkout -b fusion/fn-9001", repoDir);
await appendFile(path.join(repoDir, "note.txt"), "unique\n", "utf-8");
await run("git add note.txt", repoDir);
await run("git commit -m 'feat(FN-9001): unique' -m 'Fusion-Task-Id: FN-9001'", repoDir);
await run("git checkout main", repoDir);
const livePath = path.join(repoDir, "..", "live-9001-unique");
await run(`git worktree add ${JSON.stringify(livePath)} fusion/fn-9001`, repoDir);
const stalePath = path.join(repoDir, "..", "stale-9001-unique");
await mkdir(stalePath, { recursive: true });
const result = await inspectBranchConflict({
repoDir,
branchName: "fusion/fn-9001",
conflictingWorktreePath: stalePath,
requestingTaskId: "FN-9001",
ownerTaskId: "FN-9001",
startPoint: "main",
});
expect(result.kind).toBe("reclaimable");
});
it("keeps zero-attributed foreign branch as live-foreign", async () => {
const repoDir = await setupRepo();
await run("git checkout -b topic/other", repoDir);
await appendFile(path.join(repoDir, "note.txt"), "other\n", "utf-8");
await run("git add note.txt", repoDir);
await run("git commit -m 'chore: other work'", repoDir);
await run("git checkout main", repoDir);
const livePath = path.join(repoDir, "..", "live-other");
await run(`git worktree add ${JSON.stringify(livePath)} topic/other`, repoDir);
const stalePath = path.join(repoDir, "..", "stale-other");
await mkdir(stalePath, { recursive: true });
const result = await inspectBranchConflict({
repoDir,
branchName: "topic/other",
conflictingWorktreePath: stalePath,
requestingTaskId: "FN-9001",
ownerTaskId: "FN-9001",
startPoint: "main",
});
expect(result.kind).toBe("live-foreign");
});
});