Files
ThothII/backend/test/installation-workspace-source.test.ts

35 lines
2.6 KiB
TypeScript

import { execFileSync } from "node:child_process";
import { createHash } from "node:crypto";
import { mkdtempSync, mkdirSync, writeFileSync, rmSync, readFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, expect, test } from "vitest";
import { WorkspaceRegistry } from "../src/workspaces/registry.js";
const roots: string[] = [];
afterEach(() => roots.splice(0).forEach(root => rmSync(root, { recursive: true, force: true })));
test("activates only the prepared source contents and never retargets an existing checkout", async () => {
const root = mkdtempSync(join(tmpdir(), "installation-source-")); roots.push(root);
const remote = join(root, "source"); mkdirSync(join(remote, "chinook"), { recursive: true });
const files: Record<string, string> = {
"thoth-workspaces.yaml": "schema_version: 1\nworkspaces: [{id: chinook, name: Chinook}]\n",
"chinook/workspace.yaml": "workspace:\n schema_version: 4\n id: chinook\n name: Chinook\n language: en\n",
};
for (const [path, value] of Object.entries(files)) writeFileSync(join(remote, path), value);
const git = (args: string[], cwd = remote) => execFileSync("git", args, { cwd, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }).trim();
git(["init", "-b", "main"]); git(["add", "."]);
git(["-c", "user.name=Test", "-c", "user.email=test@example.invalid", "commit", "-m", "fixture"]);
const revision = git(["rev-parse", "HEAD"]);
const config = { root: join(root, "registry"), remoteUrl: remote, branch: "main", installationId: "test", secretRoots: [] };
const registry = new WorkspaceRegistry(config);
const expected = { remote, branch: "main", revision, files: Object.fromEntries(Object.entries(files).map(([path, value]) => [path, createHash("sha256").update(value).digest("hex")])) };
await expect(registry.prepareInstallation({ ...expected, files: { ...expected.files, "chinook/workspace.yaml": "0".repeat(64) } })).rejects.toThrow();
await expect(registry.verifyStoredState()).rejects.toThrow();
await expect(registry.prepareInstallation(expected)).resolves.toMatchObject({ revision });
await expect(registry.prepareInstallation(expected)).resolves.toMatchObject({ revision });
const checkout = join(config.root, "repo");
git(["remote", "set-url", "origin", join(root, "another")], checkout);
await expect(registry.prepareInstallation(expected)).rejects.toThrow();
expect(git(["remote", "get-url", "origin"], checkout)).toBe(join(root, "another"));
expect(readFileSync(join(checkout, "chinook/workspace.yaml"), "utf8")).toBe(files["chinook/workspace.yaml"]);
});