import { execFile } from "node:child_process"; import { mkdtempSync, mkdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { promisify } from "node:util"; import { afterEach, expect, test } from "vitest"; import { GitWorkspaceRepository } from "../src/workspaces/git-repository.js"; import type { WorkspaceRegistryConfig } from "../src/workspaces/types.js"; const runFile = promisify(execFile); const temporaryRoots: string[] = []; afterEach(() => { temporaryRoots.splice(0).forEach((root) => rmSync(root, { recursive: true, force: true })); }); async function git(cwd: string, args: string[]): Promise { const { stdout } = await runFile("git", args, { cwd }); return stdout.trim(); } function config(root: string, remoteUrl: string): WorkspaceRegistryConfig { return { root, remoteUrl, branch: "main", gitAuthorName: "Evidence Test", gitAuthorEmail: "evidence@example.invalid", installationId: "test", secretRoots: [], maxImportBytes: 1024, maxImportEntries: 1, }; } type EvidenceLayout = "tree" | "empty" | "root-file" | "root-symlink" | "nested-symlink"; async function fixture(layout: EvidenceLayout): Promise<{ root: string; remote: string; commit: string }> { const root = mkdtempSync(join(tmpdir(), "thoth-evidence-git-")); temporaryRoots.push(root); const remote = join(root, "remote.git"); const source = join(root, "source"); await git(root, ["init", "--bare", "--initial-branch=main", remote]); mkdirSync(source); await git(source, ["init", "--initial-branch=main"]); await git(source, ["config", "user.name", "Evidence Test"]); await git(source, ["config", "user.email", "evidence@example.invalid"]); writeFileSync(join(source, "thoth-workspaces.yaml"), "schema_version: 1\nworkspaces: [{id: research, name: Research}]\n"); mkdirSync(join(source, "research"), { recursive: true }); writeFileSync(join(source, "research", "workspace.yaml"), "workspace:\n schema_version: 4\n id: research\n"); const evidence = join(source, "research", "evidence"); if (layout === "tree") { mkdirSync(join(evidence, "nested"), { recursive: true }); writeFileSync(join(evidence, "guide.md"), "# guide\n"); writeFileSync(join(evidence, "nested", "deep.md"), "# deep\n"); } else if (layout === "empty") { mkdirSync(evidence, { recursive: true }); } else if (layout === "root-file") { writeFileSync(evidence, "not a tree\n"); } else if (layout === "root-symlink") { writeFileSync(join(source, "research", "target.md"), "# target\n"); symlinkSync("target.md", evidence); } else if (layout === "nested-symlink") { mkdirSync(evidence, { recursive: true }); writeFileSync(join(source, "research", "outside.md"), "# outside\n"); symlinkSync("../outside.md", join(evidence, "link.md")); } await git(source, ["add", "-A"]); await git(source, ["commit", "-m", "initial"]); await git(source, ["remote", "add", "origin", remote]); await git(source, ["push", "origin", "main"]); const commit = await git(source, ["rev-parse", "HEAD"]); return { root, remote, commit }; } async function bootstrapped(fixture: { root: string; remote: string }): Promise { const repository = new GitWorkspaceRepository(config(join(fixture.root, "registry"), fixture.remote)); await repository.bootstrap(); return repository; } test("enumerates a regular Evidence tree with ordered relative paths", async () => { const fixtureValue = await fixture("tree"); const repository = await bootstrapped(fixtureValue); const objects = await repository.evidenceTreeObjects(fixtureValue.commit, "research"); expect(objects.map((entry) => entry.posixPath)).toEqual(["guide.md", "nested/deep.md"]); expect(objects.every((entry) => /^[0-9a-f]{40}$/.test(entry.oid))).toBe(true); expect(objects.every((entry) => entry.mode === "100644" || entry.mode === "100755")).toBe(true); }); test("accepts an empty Evidence tree", async () => { const fixtureValue = await fixture("empty"); const repository = await bootstrapped(fixtureValue); await expect(repository.evidenceTreeObjects(fixtureValue.commit, "research")).resolves.toEqual([]); }); test("refuses a non-tree Evidence root and symlinks at any depth", async () => { for (const layout of ["root-file", "root-symlink", "nested-symlink"] as const) { const fixtureValue = await fixture(layout); const repository = await bootstrapped(fixtureValue); await expect(repository.evidenceTreeObjects(fixtureValue.commit, "research")) .rejects.toMatchObject({ code: "workspace_invalid" }); } }); test("refuses malformed revisions and workspace ids", async () => { const fixtureValue = await fixture("tree"); const repository = await bootstrapped(fixtureValue); await expect(repository.evidenceTreeObjects("HEAD", "research")) .rejects.toMatchObject({ code: "workspace_invalid" }); await expect(repository.evidenceTreeObjects(fixtureValue.commit, "../research")) .rejects.toMatchObject({ code: "workspace_invalid" }); }); test("streams bounded Evidence blobs and refuses oversized objects", async () => { const fixtureValue = await fixture("tree"); const repository = await bootstrapped(fixtureValue); const [guide] = await repository.evidenceTreeObjects(fixtureValue.commit, "research"); const bytes = await repository.evidenceBlobBytes(guide.oid, 1024); expect(bytes.toString("utf8")).toBe("# guide\n"); await expect(repository.evidenceBlobBytes(guide.oid, 1)) .rejects.toMatchObject({ code: "workspace_invalid" }); });