import { afterEach, expect, test } from "bun:test"; import { mkdir, mkdtemp, rm } from "node:fs/promises"; import { join } from "node:path"; import { command, Herdr } from "../src/herdr.js"; const directories: string[] = []; afterEach(async () => { for (const directory of directories.splice(0)) await rm(directory, { recursive: true, force: true }); }); async function fixture() { const temporary = await mkdtemp("/tmp/opencode/multi-work-"); directories.push(temporary); const root = join(temporary, "root"); const destination = join(temporary, "returned checkout"); await mkdir(root); const signal = new AbortController().signal; await command(["git", "init", "-b", "main"], root, signal); await command( [ "git", "-c", "user.name=Fixture", "-c", "user.email=fixture@example.test", "commit", "--allow-empty", "-m", "initial", ], root, signal, ); await command(["git", "branch", "other"], root, signal); const calls: string[][] = []; let open = true; let linked = false; let duplicate = false; let branch = ""; const run = async (args: string[], cwd: string, signal: AbortSignal) => { calls.push(args); if (args[0] !== "herdr") return command(args, cwd, signal); if (args[1] === "workspace") return JSON.stringify({ result: { workspaces: open ? [{ workspace_id: "w1" }, ...(duplicate ? [{ workspace_id: "w3" }] : [])] : [], }, }); if (args[2] === "list") { const id = args[4]!; const path = id === "w2" ? destination : root; return JSON.stringify({ result: { source: { repo_key: join(root, ".git"), repo_root: root, source_checkout_path: path, source_workspace_id: id, }, worktrees: [ { path, is_linked_worktree: id === "w2" || linked, is_bare: false, open_workspace_id: id, }, ], }, }); } expect(args[2]).toBe("create"); expect(args.slice(3, 5)).toEqual(["--cwd", root]); expect(args.at(-1)).toBe("--no-focus"); branch = args[args.indexOf("--branch") + 1]!; const base = args[args.indexOf("--base") + 1]!; // Real Git worktree behavior, but only in an isolated temporary repository. await command(["git", "worktree", "add", "-b", branch, destination, base], root, signal); return JSON.stringify({ result: { workspace: { workspace_id: "w2" }, worktree: { path: destination, branch, is_linked_worktree: true }, }, }); }; return { root, destination, signal, calls, herdr: new Herdr(run), close() { open = false; }, link() { linked = true; }, duplicate() { duplicate = true; }, }; } test("live inventory recognizes an unannotated root and creates a unique branch from another local branch", async () => { const f = await fixture(); const root = await f.herdr.root(f.root, f.signal); expect(root?.workspaceID).toBe("w1"); const destination = await f.herdr.create(root!, "other", "multi-work/fixture", f.signal); expect(destination.directory).toBe(f.destination); expect(destination.branch).toStartWith("multi-work/"); expect(await command(["git", "branch", "--show-current"], f.root, f.signal)).toBe("main"); await f.herdr.verify( destination.directory, destination.workspaceID, root!.repo, destination.branch, f.signal, ); }); test("closed, linked and ambiguous workspaces fail root gating", async () => { for (const mode of ["close", "link", "duplicate"] as const) { const f = await fixture(); f[mode](); expect(await f.herdr.root(f.root, f.signal)).toBeUndefined(); } }); test("revalidates before mutation and rejects non-branch refs without creating a worktree", async () => { const f = await fixture(); const root = (await f.herdr.root(f.root, f.signal))!; const oid = await command(["git", "rev-parse", "HEAD"], f.root, f.signal); for (const base of [oid, "--help", "main; touch injected"]) { await expect(f.herdr.create(root, base, "multi-work/fixture", f.signal)).rejects.toThrow(); } f.close(); await expect(f.herdr.create(root, "main", "multi-work/fixture", f.signal)).rejects.toThrow( "no longer eligible", ); expect(f.calls.filter((args) => args[0] === "herdr" && args[2] === "create")).toHaveLength(0); }); test("a missing or malformed Herdr CLI never qualifies", async () => { const f = await fixture(); await expect(new Herdr(async () => "not json").root(f.root, f.signal)).rejects.toThrow(); await expect( new Herdr(async () => { throw new Error("ENOENT"); }).root(f.root, f.signal), ).rejects.toThrow("ENOENT"); });