import { expect, test, vi } from "vitest"; import { runWorkspaceMaintenanceCli, type WorkspaceMaintenanceIo, } from "../src/workspace-maintenance.js"; import type { WorkspaceOperationResult } from "../src/workspaces/preprocessing-service.js"; function io(stdin: string): WorkspaceMaintenanceIo & { stdout: string[]; stderr: string[] } { const stdout: string[] = []; const stderr: string[] = []; return { stdin, stdout, stderr, writeStdout: (value) => void stdout.push(value), writeStderr: (value) => void stderr.push(value), }; } function ok(operation: string): WorkspaceOperationResult { return { schemaVersion: 1, status: "succeeded", code: "ok", workspaceId: "psd-clinical", workspaceRevision: "a".repeat(40), descriptorBlob: "b".repeat(40), operation, completedStages: [], }; } test("entrypoint emits exactly one pristine JSON document and maps success/failure exits", async () => { const service = { inspect: vi.fn(async () => ok("inspect")), run: vi.fn(async () => ({ ...ok("preprocess run"), status: "failed", code: "semantic_index_incompatible" as const })), clear: vi.fn(async () => ok("preprocess clear")), } as any; const inspectIo = io(JSON.stringify({ schemaVersion: 1, workspaceId: "psd-clinical" })); expect(await runWorkspaceMaintenanceCli(["node", "workspace-maintenance", "inspect"], service, inspectIo)).toBe(0); expect(JSON.parse(inspectIo.stdout.join(""))).toMatchObject({ operation: "inspect", code: "ok" }); expect(inspectIo.stderr.join("")).toBe(""); const failedIo = io(JSON.stringify({ schemaVersion: 1, workspaceId: "psd-clinical" })); expect(await runWorkspaceMaintenanceCli(["node", "workspace-maintenance", "preprocess-run"], service, failedIo)).toBe(1); expect(JSON.parse(failedIo.stdout.join(""))).toMatchObject({ code: "semantic_index_incompatible" }); const clearIo = io(JSON.stringify({ schemaVersion: 1, workspaceId: "psd-clinical" })); expect(await runWorkspaceMaintenanceCli(["node", "workspace-maintenance", "preprocess-clear"], service, clearIo)).toBe(0); expect(JSON.parse(clearIo.stdout.join(""))).toMatchObject({ operation: "preprocess clear", code: "ok" }); }); test("malformed stdin, unknown commands, and extra fields fail with exit 2 but still return bounded JSON", async () => { const service = {} as any; const malformedIo = io("not-json"); expect(await runWorkspaceMaintenanceCli(["node", "workspace-maintenance", "inspect"], service, malformedIo)).toBe(2); expect(JSON.parse(malformedIo.stdout.join(""))).toMatchObject({ status: "failed", operation: "inspect" }); const extraFieldIo = io(JSON.stringify({ schemaVersion: 1, workspaceId: "psd-clinical", unexpected: true })); expect(await runWorkspaceMaintenanceCli(["node", "workspace-maintenance", "inspect"], service, extraFieldIo)).toBe(2); expect(JSON.parse(extraFieldIo.stdout.join(""))).toMatchObject({ status: "failed", operation: "inspect" }); const unknownIo = io(JSON.stringify({ schemaVersion: 1, workspaceId: "psd-clinical" })); expect(await runWorkspaceMaintenanceCli(["node", "workspace-maintenance", "explode"], service, unknownIo)).toBe(2); expect(JSON.parse(unknownIo.stdout.join(""))).toMatchObject({ status: "failed", operation: "explode" }); }); test("raw exception text is redacted from stderr and stdout remains within the public result contract", async () => { const service = { inspect: vi.fn(async () => { throw new Error("https://secret.example.invalid?q=token SELECT * FROM sensitive_table"); }), } as any; const captured = io(JSON.stringify({ schemaVersion: 1, workspaceId: "psd-clinical" })); expect(await runWorkspaceMaintenanceCli(["node", "workspace-maintenance", "inspect"], service, captured)).toBe(1); expect(JSON.parse(captured.stdout.join(""))).toMatchObject({ status: "failed", operation: "inspect", }); expect(captured.stderr.join("")).not.toContain("secret.example.invalid"); expect(captured.stderr.join("")).not.toContain("SELECT *"); }); test("retired partial preprocessing commands are rejected without dispatch", async () => { const service = { preprocessDwh: vi.fn(), vectorInspect: vi.fn(), vectorRebuild: vi.fn(), } as any; for (const command of ["preprocess-dwh", "vector-inspect", "vector-rebuild"]) { const captured = io(JSON.stringify({ schemaVersion: 1, workspaceId: "psd-clinical" })); expect(await runWorkspaceMaintenanceCli(["node", "workspace-maintenance", command], service, captured)).toBe(2); expect(JSON.parse(captured.stdout.join(""))).toMatchObject({ status: "failed", operation: command }); } expect(service.preprocessDwh).not.toHaveBeenCalled(); expect(service.vectorInspect).not.toHaveBeenCalled(); expect(service.vectorRebuild).not.toHaveBeenCalled(); });