feat: manage workspace Git checkout and snapshots
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { mkdtempSync, mkdirSync, 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 { GitWorkspaceRepository } from "../src/workspaces/git-repository.js";
|
||||
import type { WorkspaceRegistryConfig } from "../src/workspaces/types.js";
|
||||
|
||||
const validYaml = `workspace:
|
||||
schema_version: 1
|
||||
id: psd-clinical
|
||||
name: Policlinico San Donato
|
||||
language: it
|
||||
dwh:
|
||||
engine: postgres
|
||||
database: postgres
|
||||
schema: datawarehouse
|
||||
supported_transports: [postgres_direct]
|
||||
semantic_index:
|
||||
vector_store:
|
||||
engine: pgvector
|
||||
collection: clinical_documents
|
||||
dimensions: 768
|
||||
distance: cosine
|
||||
supported_transports: [pgvector_direct]
|
||||
embedding:
|
||||
provider: ollama_compatible
|
||||
model: nomic-embed-text-v2-moe
|
||||
dimensions: 768
|
||||
llm_policy:
|
||||
allowed: [zai/glm-5.2]
|
||||
`;
|
||||
|
||||
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<void> {
|
||||
await runFile("git", args, { cwd });
|
||||
}
|
||||
|
||||
async function temporaryRemote(): Promise<{ root: string; remote: string; initialCommit: string }> {
|
||||
const root = mkdtempSync(join(tmpdir(), "thoth-workspace-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", "Workspace Registry Test"]);
|
||||
await git(source, ["config", "user.email", "workspace-registry@example.invalid"]);
|
||||
mkdirSync(join(source, "workspaces"));
|
||||
writeFileSync(join(source, "workspaces", "psd-clinical.yaml"), validYaml);
|
||||
await git(source, ["add", "workspaces/psd-clinical.yaml"]);
|
||||
await git(source, ["commit", "-m", "Initial workspace"]);
|
||||
await git(source, ["remote", "add", "origin", remote]);
|
||||
await git(source, ["push", "origin", "main"]);
|
||||
const { stdout } = await runFile("git", ["rev-parse", "HEAD"], { cwd: source });
|
||||
return { root, remote, initialCommit: stdout.trim() };
|
||||
}
|
||||
|
||||
function config(root: string, remoteUrl: string): WorkspaceRegistryConfig {
|
||||
return {
|
||||
root,
|
||||
remoteUrl,
|
||||
branch: "main",
|
||||
gitAuthorName: "Workspace Registry Test",
|
||||
gitAuthorEmail: "workspace-registry@example.invalid",
|
||||
installationId: "test",
|
||||
secretRoots: [],
|
||||
maxImportBytes: 1024,
|
||||
maxImportEntries: 1,
|
||||
};
|
||||
}
|
||||
|
||||
test("bootstraps a persistent checkout from a local bare repository", async () => {
|
||||
const fixture = await temporaryRemote();
|
||||
const repository = new GitWorkspaceRepository(config(join(fixture.root, "registry"), fixture.remote));
|
||||
|
||||
const status = await repository.bootstrap();
|
||||
|
||||
expect(status).toMatchObject({
|
||||
branch: "main",
|
||||
head: fixture.initialCommit,
|
||||
ahead: 0,
|
||||
behind: 0,
|
||||
degraded: false,
|
||||
});
|
||||
});
|
||||
|
||||
test("redacts failed Git checkout details behind a stable error code", async () => {
|
||||
const root = mkdtempSync(join(tmpdir(), "thoth-workspace-git-missing-"));
|
||||
temporaryRoots.push(root);
|
||||
const remote = join(root, "missing.git");
|
||||
const repository = new GitWorkspaceRepository(config(join(root, "registry"), remote));
|
||||
|
||||
const error = await repository.bootstrap().catch((error: unknown) => error);
|
||||
expect(error).toMatchObject({
|
||||
code: "git_unavailable",
|
||||
message: "Workspace Git operation failed",
|
||||
});
|
||||
expect((error as Error).message).not.toContain(remote);
|
||||
});
|
||||
Reference in New Issue
Block a user