167 lines
6.0 KiB
TypeScript
167 lines
6.0 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/multi-work/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("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();
|
|
}
|
|
});
|