import { expect, test } from "vitest"; // The shipped operator client is plain ESM, shared by the embedded command and these API-boundary tests. // @ts-expect-error JavaScript operator module has no separate declaration file. import { prepareWorkspace, importBinding } from "../../tools/tht/internal/installapply/import-bindings.mjs"; const revision = "a".repeat(40); test("a stranded preprocessing state requests guarded recovery, then verifies fresh readiness", async () => { let state = "running"; const request = async (path: string, body?: unknown): Promise => { if (path === "/catalog/databases") return Response.json([{ id: "database-id", workspaceId: "chinook", version: 2, schemaSyncedVersion: 2, binding: { transport: "postgres_direct" }, workspaceRevision: { commit: revision } }]); if (path.endsWith("/sync-runs")) return Response.json([]); if (path.endsWith("/preprocessing")) { if (body) { expect(body).toEqual({ recoverInterrupted: true }); state = "ready"; } return Response.json({ state }); } throw Error("unexpected operation"); }; expect(await prepareWorkspace(request, "chinook", revision, async () => {})).toEqual({ workspaceId: "chinook", state: "ready" }); }); test("retry reaches a pending human schema review even when its reservation blocks binding import", async () => { const request = async (path: string) => { if (path === "/installation/bindings") return Response.json({ code: "installation_operation_pending" }, { status: 409 }); if (path === "/catalog/databases") return Response.json([{ workspaceId: "chinook", activeSyncRun: { id: "run-id", state: "awaiting_confirmation" } }]); throw Error("unexpected operation"); }; await expect(importBinding(request, { configuration: { workspaceId: "chinook" } }, revision, async () => {})).rejects.toMatchObject({ code: "schema_review_required", runId: "run-id" }); }); test.each(["blocked", "failed"])("configured Evidence/index failure (%s) remains a real stop", async state => { const request = async (path: string, body?: unknown): Promise => { if (path === "/catalog/databases") return Response.json([{ id: "database-id", workspaceId: "chinook", version: 2, schemaSyncedVersion: 2, binding: { transport: "postgres_direct" }, workspaceRevision: { commit: revision } }]); if (path.endsWith("/sync-runs")) return Response.json([]); if (path.endsWith("/preprocessing")) return Response.json({ state }, { status: body === undefined ? 200 : 422 }); throw Error("unexpected destructive operation"); }; await expect(prepareWorkspace(request, "chinook", revision, async () => {})).rejects.toMatchObject({ code: `preprocessing_${state}` }); }); test("SSH metadata connectivity cannot be mistaken for session transport support", async () => { const calls: string[] = []; const request = async (path: string) => { calls.push(path); return Response.json([{ id: "database-id", workspaceId: "chinook", version: 2, binding: { transport: "ssh_tunnel" }, workspaceRevision: { commit: revision } }]); }; await expect(prepareWorkspace(request, "chinook", revision)).rejects.toMatchObject({ code: "session_transport_unsupported" }); expect(calls).toEqual(["/catalog/databases"]); }); test("a destructive synchronization stops at the existing domain gate without confirmation", async () => { const calls: string[] = []; const request = async (path: string) => { calls.push(path); if (path === "/catalog/databases") return Response.json([{ id: "database-id", workspaceId: "chinook", version: 2, binding: { transport: "postgres_direct" }, workspaceRevision: { commit: revision } }]); if (path.endsWith("/sync-runs")) return Response.json([{ id: "run-id", state: "awaiting_confirmation", scope: "all" }]); throw Error("unexpected operation"); }; await expect(prepareWorkspace(request, "chinook", revision)).rejects.toMatchObject({ code: "schema_review_required", runId: "run-id" }); expect(calls).toEqual(["/catalog/databases", "/catalog/databases/database-id/sync-runs"]); }); test("fresh additive sync and preprocessing use existing APIs once, then repeat as a no-op", async () => { const writes: string[] = []; let synchronized = false, preprocessed = false; const request = async (path: string, body?: unknown) => { if (body !== undefined) writes.push(path); if (path === "/catalog/databases") return Response.json([{ id: "database-id", workspaceId: "chinook", version: 2, schemaSyncedVersion: synchronized ? 2 : undefined, binding: { transport: "postgres_direct" }, workspaceRevision: { commit: revision } }]); if (path === "/catalog/databases/database-id") return Response.json({ version: 2, schemaSyncedVersion: 2 }); if (path.endsWith("/sync-runs")) { if (body) { expect(body).toEqual({ version: 2, scope: "all", tableIds: [] }); return Response.json({ id: "run-id", state: "queued", scope: "all" }); } return Response.json(synchronized ? [{ id: "run-id", state: "succeeded", scope: "all" }] : []); } if (path === "/catalog/sync-runs/run-id") { synchronized = true; return Response.json({ id: "run-id", state: "succeeded", scope: "all" }); } if (path === "/workspaces/chinook/preprocessing") { if (body) preprocessed = true; return Response.json({ state: preprocessed ? "ready" : "required" }); } throw Error("unexpected operation"); }; expect(await prepareWorkspace(request, "chinook", revision, async () => {})).toEqual({ workspaceId: "chinook", state: "ready" }); expect(await prepareWorkspace(request, "chinook", revision, async () => {})).toEqual({ workspaceId: "chinook", state: "ready" }); expect(writes).toEqual(["/catalog/databases/database-id/sync-runs", "/workspaces/chinook/preprocessing"]); });