124 lines
5.1 KiB
TypeScript
124 lines
5.1 KiB
TypeScript
import { execFile } from "node:child_process";
|
|
import { mkdtempSync, mkdirSync, readFileSync, rmSync, 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 { WorkspaceRegistry } from "../src/workspaces/registry.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 }));
|
|
});
|
|
|
|
const validYaml = `workspace:
|
|
schema_version: 4
|
|
id: psd-clinical
|
|
name: Policlinico San Donato
|
|
language: it
|
|
dwh:
|
|
engine: postgres
|
|
database: postgres
|
|
schema: datawarehouse
|
|
supported_transports: [postgres_direct]
|
|
`;
|
|
|
|
async function git(cwd: string, args: string[]): Promise<string> {
|
|
const { stdout } = await runFile("git", args, { cwd });
|
|
return stdout.trim();
|
|
}
|
|
|
|
function config(root: string, remoteUrl: string): WorkspaceRegistryConfig {
|
|
return {
|
|
root,
|
|
remoteUrl,
|
|
branch: "main",
|
|
gitAuthorName: "Registry Annotations Test",
|
|
gitAuthorEmail: "registry-annotations@example.invalid",
|
|
installationId: "test",
|
|
secretRoots: [],
|
|
maxImportBytes: 1024,
|
|
maxImportEntries: 1,
|
|
};
|
|
}
|
|
|
|
type AnnotationsLayout = "absent" | "valid" | "malformed" | "dir";
|
|
|
|
async function fixture(layout: AnnotationsLayout): Promise<{ root: string; remote: string; commit: string }> {
|
|
const root = mkdtempSync(join(tmpdir(), "thoth-registry-annotations-"));
|
|
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", "Registry Annotations Test"]);
|
|
await git(source, ["config", "user.email", "registry-annotations@example.invalid"]);
|
|
writeFileSync(join(source, "thoth-workspaces.yaml"),
|
|
"schema_version: 1\nworkspaces:\n - id: psd-clinical\n name: Policlinico San Donato\n");
|
|
mkdirSync(join(source, "psd-clinical", "schema"), { recursive: true });
|
|
writeFileSync(join(source, "psd-clinical", "workspace.yaml"), validYaml);
|
|
const annotationsPath = join(source, "psd-clinical", "schema", "annotations.yaml");
|
|
if (layout === "valid") writeFileSync(annotationsPath, "tables: {}\n");
|
|
if (layout === "malformed") writeFileSync(annotationsPath, "tables: [not, a, mapping]\n");
|
|
if (layout === "dir") {
|
|
mkdirSync(annotationsPath, { recursive: true });
|
|
writeFileSync(join(annotationsPath, "child.txt"), "nested\n");
|
|
}
|
|
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 };
|
|
}
|
|
|
|
test("activation accepts a valid curated annotation blob", async () => {
|
|
const fixtureValue = await fixture("valid");
|
|
const registry = new WorkspaceRegistry(config(join(fixtureValue.root, "registry"), fixtureValue.remote));
|
|
|
|
await expect(registry.bootstrap()).resolves.toMatchObject({ degraded: false });
|
|
});
|
|
|
|
test("activation accepts an absent annotation blob", async () => {
|
|
const fixtureValue = await fixture("absent");
|
|
const registry = new WorkspaceRegistry(config(join(fixtureValue.root, "registry"), fixtureValue.remote));
|
|
|
|
await expect(registry.bootstrap()).resolves.toMatchObject({ degraded: false });
|
|
});
|
|
|
|
test("activation rejects malformed annotations", async () => {
|
|
const fixtureValue = await fixture("malformed");
|
|
const registry = new WorkspaceRegistry(config(join(fixtureValue.root, "registry"), fixtureValue.remote));
|
|
|
|
await expect(registry.bootstrap()).rejects.toMatchObject({ code: "workspace_invalid" });
|
|
});
|
|
|
|
test("activation rejects a tree at the annotations path", async () => {
|
|
const fixtureValue = await fixture("dir");
|
|
const registry = new WorkspaceRegistry(config(join(fixtureValue.root, "registry"), fixtureValue.remote));
|
|
|
|
await expect(registry.bootstrap()).rejects.toMatchObject({ code: "workspace_invalid" });
|
|
});
|
|
|
|
test("activation syncs the curated annotations to the revision root when a data root is set", async () => {
|
|
const fixtureValue = await fixture("valid");
|
|
const dataRoot = mkdtempSync(join(tmpdir(), "thoth-registry-annotations-data-"));
|
|
temporaryRoots.push(dataRoot);
|
|
const registry = new WorkspaceRegistry({
|
|
...config(join(fixtureValue.root, "registry"), fixtureValue.remote),
|
|
dataRoot,
|
|
});
|
|
|
|
await registry.bootstrap();
|
|
|
|
const annotationsPath = join(dataRoot, "sessions", "psd-clinical", "revisions", fixtureValue.commit, "artifacts", "mschema", "annotations.yaml");
|
|
const manifestPath = join(dataRoot, "sessions", "psd-clinical", "revisions", fixtureValue.commit, "artifacts", "mschema", "annotations.ownership.json");
|
|
expect(readFileSync(annotationsPath, "utf8")).toBe("tables: {}\n");
|
|
const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
|
|
expect(manifest).toMatchObject({ workspace: "psd-clinical", commit: fixtureValue.commit });
|
|
});
|