Files
stanp-opencode/tests/herdr.test.ts
StanPonomarev b90720d270
CI / verify (pull_request) Successful in 9s
fix tests
2026-09-18 10:15:50 +02:00

252 lines
9.4 KiB
TypeScript

import { afterEach, expect, test } from "bun:test";
import { mkdir, mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
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(join(tmpdir(), "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 = "";
let label: string | undefined;
let sourceWorkspaceIDOverride: string | undefined;
let sourcePathOverride: string | undefined;
let childWorkspaceIDOverride: string | undefined;
let childPathOverride: string | undefined;
let repoOverride: string | undefined;
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;
const sourceWorkspaceID = sourceWorkspaceIDOverride ?? (id === "w2" ? "w1" : id);
const sourcePath = sourcePathOverride ?? root;
return JSON.stringify({
result: {
source: {
repo_key: repoOverride ?? join(root, ".git"),
repo_root: root,
source_checkout_path: sourcePath,
source_workspace_id: sourceWorkspaceID,
},
worktrees: [
{
path: sourcePath,
is_linked_worktree: linked,
is_bare: false,
open_workspace_id: sourceWorkspaceID,
},
...(id === "w2"
? [
{
path: childPathOverride ?? path,
is_linked_worktree: true,
is_bare: false,
open_workspace_id: childWorkspaceIDOverride ?? 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 labelIndex = args.indexOf("--label");
label = labelIndex === -1 ? undefined : args[labelIndex + 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,
get label() {
return label;
},
herdr: new Herdr(run),
close() {
open = false;
},
link() {
linked = true;
},
duplicate() {
duplicate = true;
},
changeInventory(change: {
sourceWorkspaceID?: string | undefined;
sourcePath?: string | undefined;
childWorkspaceID?: string | undefined;
childPath?: string | undefined;
repo?: string | undefined;
}) {
sourceWorkspaceIDOverride = change.sourceWorkspaceID;
sourcePathOverride = change.sourcePath;
childWorkspaceIDOverride = change.childWorkspaceID;
childPathOverride = change.childPath;
repoOverride = change.repo;
},
};
}
test("create verifies a linked workspace whose inventory source remains the parent workspace", 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!,
destination.branch,
f.signal,
);
});
test("create passes an optional workspace label to Herdr", async () => {
const f = await fixture();
const root = (await f.herdr.root(f.root, f.signal))!;
const destination = await f.herdr.create(
root,
"main",
"fix/login-timeout",
f.signal,
"Fix login timeout",
);
expect(destination.branch).toBe("fix/login-timeout");
expect(f.label).toBe("Fix login timeout");
const creation = f.calls.find((args) => args[0] === "herdr" && args[2] === "create")!;
expect(creation.slice(creation.indexOf("--label"), creation.indexOf("--label") + 2)).toEqual([
"--label",
"Fix login timeout",
]);
});
test("create rejects an existing destination branch before invoking Herdr", async () => {
const f = await fixture();
const root = (await f.herdr.root(f.root, f.signal))!;
await expect(
f.herdr.create(root, "main", "invalid branch", f.signal, "Invalid"),
).rejects.toThrow();
await expect(f.herdr.create(root, "main", "other", f.signal, "Other")).rejects.toThrow(
"Destination branch already exists: other",
);
expect(f.calls.filter((args) => args[0] === "herdr" && args[2] === "create")).toHaveLength(0);
});
test("verify rejects changed parent, child and repository identities", async () => {
for (const change of [
{ sourceWorkspaceID: "w9" },
{ sourcePath: "child" },
{ childWorkspaceID: "w9" },
{ childPath: "root" },
{ repo: "child" },
]) {
const f = await fixture();
const root = (await f.herdr.root(f.root, f.signal))!;
const destination = await f.herdr.create(root, "main", "multi-work/fixture", f.signal);
f.changeInventory({
sourceWorkspaceID: change.sourceWorkspaceID,
sourcePath: change.sourcePath === "child" ? f.destination : undefined,
childWorkspaceID: change.childWorkspaceID,
childPath: change.childPath === "root" ? f.root : undefined,
repo: change.repo === "child" ? f.destination : undefined,
});
await expect(
f.herdr.verify(
destination.directory,
destination.workspaceID,
root,
destination.branch,
f.signal,
),
).rejects.toThrow("Bound Herdr checkout is no longer available");
}
});
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");
});