refactor: make browser workspace writes bootstrap-only

This commit is contained in:
2026-08-11 15:36:09 +02:00
parent d9fd902d08
commit 1790d2449f
14 changed files with 844 additions and 1509 deletions
+136 -216
View File
@@ -4,17 +4,53 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { http, HttpResponse } from "msw";
import { afterEach, beforeEach, expect, test, vi } from "vitest";
import { server } from "../test/msw";
import { workspaceRevisionFixture, workspaceSummaryFixture } from "../test/workspace-fixtures";
import { WorkspaceManager } from "./WorkspaceManager";
const workspace = {
workspace: { schema_version: 3, id: "psd-clinical", name: "PSD Clinical", language: "en" },
dwh: { engine: "postgres", database: "clinical", schema: "datawarehouse", port: 5432, supported_transports: ["postgres_direct"] },
semantic_index: {
vector_store: { engine: "qdrant", collection: "clinical", dimensions: 1024, distance: "cosine" },
embedding: { provider: "ollama_internal", model: "qwen3-embedding:0.6b", dimensions: 1024 },
const readyWorkspace = {
workspace: {
schema_version: 3,
id: "psd-clinical",
name: "PSD Clinical",
description: "Clinical data",
language: "en" as const,
},
llm_policy: { default: "zai/glm-5.2", allowed: ["zai/glm-5.2"] },
} as const;
dwh: {
engine: "postgres" as const,
database: "clinical",
schema: "datawarehouse",
port: 5432,
supported_transports: ["postgres_direct"] as const,
},
semantic_index: {
vector_store: { engine: "qdrant" as const, collection: "clinical", dimensions: 1024 as const, distance: "cosine" as const },
embedding: { provider: "ollama_internal" as const, model: "qwen3-embedding:0.6b" as const, dimensions: 1024 as const },
},
llm_policy: { default: "zai/glm-5.2" as const, allowed: ["zai/glm-5.2"] as const },
evidence: {
source: {
type: "filesystem" as const,
uri: "psd-clinical/evidence",
patterns: ["documents/**/*.pdf"],
max_bytes: 12_000_000,
},
policy: { max_chunk_chars: 8_000, retain_published_generations: 5 },
},
};
const bootstrapWorkspace = {
...readyWorkspace,
workspace: {
...readyWorkspace.workspace,
id: "bootstrap-slot",
name: "Bootstrap slot",
description: "Needs configuration",
},
semantic_index: {
...readyWorkspace.semantic_index,
vector_store: { ...readyWorkspace.semantic_index.vector_store, collection: "bootstrap_slot" },
},
};
function renderManager() {
const client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
@@ -26,58 +62,108 @@ beforeEach(() => {
server.use(
http.get("/api/workspace-registry/status", () =>
HttpResponse.json({ branch: "main", head: "a".repeat(40), ahead: 0, behind: 0, degraded: false })),
http.get("/api/workspaces", () => HttpResponse.json([{
id: "psd-clinical", name: "psd-clinical", displayName: "PSD Clinical", description: "Clinical data",
language: "en", file: "psd-clinical.yaml",
revision: { id: "psd-clinical", commit: "a".repeat(40), blob: "b".repeat(40), snapshotPath: "/tmp/psd" },
}])),
http.get("/api/workspaces", () => HttpResponse.json([
workspaceSummaryFixture("bootstrap-slot", {
displayName: "Bootstrap slot",
description: "Needs configuration",
configurationState: "configuration_required",
}),
workspaceSummaryFixture("psd-clinical", {
displayName: "PSD Clinical",
description: "Clinical data",
revision: workspaceRevisionFixture("psd-clinical"),
}),
])),
http.get("/api/workspaces/psd-clinical", () => HttpResponse.json({
workspace,
revision: { id: "psd-clinical", commit: "a".repeat(40), blob: "b".repeat(40), snapshotPath: "/tmp/psd" },
workspace: readyWorkspace,
revision: workspaceRevisionFixture("psd-clinical"),
})),
);
});
afterEach(() => vi.unstubAllGlobals());
test("lists registry workspaces and saves a new workspace only as a browser draft", async () => {
test("renders catalog slots in order and opens a bootstrap form for configuration_required entries", async () => {
const user = userEvent.setup();
renderManager();
expect(await screen.findByRole("heading", { name: "Workspace management" })).toBeVisible();
expect(await screen.findByRole("button", { name: "PSD Clinical" })).toBeVisible();
expect(screen.getByText(`Commit ${"a".repeat(12)}`)).toBeVisible();
await user.click(screen.getByRole("button", { name: "New workspace" }));
await user.clear(screen.getByLabelText("Workspace ID"));
await user.type(screen.getByLabelText("Workspace ID"), "trial-registry");
expect(screen.getByRole("button", { name: "Bootstrap slot" })).toBeVisible();
expect(screen.getByRole("button", { name: "PSD Clinical" })).toBeVisible();
await user.click(screen.getByRole("button", { name: "Bootstrap slot" }));
expect(await screen.findByLabelText("Workspace ID")).toHaveValue("bootstrap-slot");
expect(screen.getByLabelText("Workspace ID")).toBeDisabled();
expect(screen.getByLabelText("Workspace name")).toHaveValue("Bootstrap slot");
expect(screen.getByLabelText("Workspace name")).toBeDisabled();
expect(screen.getByLabelText("Description")).toHaveValue("Needs configuration");
expect(screen.getByRole("button", { name: "Save draft" })).toBeVisible();
expect(screen.getByRole("button", { name: "Create workspace" })).toBeVisible();
});
test("saves a bootstrap draft locally for a configuration_required slot", async () => {
const user = userEvent.setup();
renderManager();
await user.click(await screen.findByRole("button", { name: "Bootstrap slot" }));
await user.clear(screen.getByLabelText("Vector collection"));
await user.type(screen.getByLabelText("Vector collection"), "bootstrap_docs");
await user.click(screen.getByRole("button", { name: "Save draft" }));
await waitFor(() => expect(screen.getByText("Draft saved in this browser.")).toBeVisible());
expect(localStorage.getItem("thothii.workspace-registry.v1.draft.trial-registry")).not.toBeNull();
expect(localStorage.getItem("thothii.workspace-registry.v2.bootstrap.bootstrap-slot")).toContain('"baseCommit"');
expect(localStorage.getItem("thothii.workspace-registry.v2.bootstrap.bootstrap-slot")).not.toContain("baseBlob");
});
test("imports a bundle as a local draft and never publishes it automatically", async () => {
test("successful create discards the bootstrap draft and reloads the workspace as read-only", async () => {
const user = userEvent.setup();
const publishSpy = vi.fn();
let workspacesCalls = 0;
server.use(
http.post("/api/workspaces/import", () => HttpResponse.json({ draft: { workspace, contract: {} } })),
http.post("/api/workspaces/publish", () => {
publishSpy();
return HttpResponse.json({});
http.get("/api/workspaces", () => {
workspacesCalls += 1;
return HttpResponse.json(workspacesCalls === 1 ? [
workspaceSummaryFixture("bootstrap-slot", {
displayName: "Bootstrap slot",
description: "Needs configuration",
configurationState: "configuration_required",
}),
] : [
workspaceSummaryFixture("bootstrap-slot", {
displayName: "Bootstrap slot",
description: "Needs configuration",
revision: workspaceRevisionFixture("bootstrap-slot"),
}),
]);
}),
http.get("/api/workspaces/bootstrap-slot", () => HttpResponse.json({
workspace: bootstrapWorkspace,
revision: workspaceRevisionFixture("bootstrap-slot"),
})),
http.post("/api/workspaces/validate", () => HttpResponse.json({ workspace: bootstrapWorkspace, contract: {} })),
http.post("/api/workspaces/publish", () => HttpResponse.json({ revision: workspaceRevisionFixture("bootstrap-slot") })),
);
localStorage.setItem("thothii.workspace-registry.v2.bootstrap.bootstrap-slot", JSON.stringify({
workspaceId: "bootstrap-slot",
baseCommit: "a".repeat(40),
workspace: bootstrapWorkspace,
updatedAt: "2026-08-04T10:00:00.000Z",
}));
renderManager();
await user.upload(screen.getByLabelText("Import workspace bundle"), new File(["bundle"], "workspace.zip", { type: "application/zip" }));
await user.click(await screen.findByRole("button", { name: "Bootstrap slot" }));
await user.click(screen.getByRole("button", { name: "Create workspace" }));
await user.click(screen.getByRole("button", { name: "Validate draft" }));
await user.click(await screen.findByRole("button", { name: "Create workspace" }));
await user.click(screen.getByRole("button", { name: "Confirm create" }));
expect(await screen.findByText("Imported draft saved in this browser. Validate it before publishing.")).toBeVisible();
expect(publishSpy).not.toHaveBeenCalled();
expect(localStorage.getItem("thothii.workspace-registry.v1.draft.psd-clinical")).not.toBeNull();
await waitFor(() => expect(localStorage.getItem("thothii.workspace-registry.v2.bootstrap.bootstrap-slot")).toBeNull());
expect(await screen.findByText(/edit bootstrap-slot\/workspace\.yaml, commit\/push, then Pull/i)).toBeVisible();
expect(screen.queryByRole("button", { name: "Save draft" })).not.toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Create workspace" })).not.toBeInTheDocument();
});
test("pulls and exports only when the manager explicitly requests each action", async () => {
test("ready workspaces stay read-only while keeping pull, export, validate, and installation test actions", async () => {
const user = userEvent.setup();
const publishSpy = vi.fn();
const createObjectURL = vi.fn(() => "blob:workspace-bundle");
const revokeObjectURL = vi.fn();
class DownloadUrl extends URL {
@@ -89,123 +175,22 @@ test("pulls and exports only when the manager explicitly requests each action",
server.use(
http.post("/api/workspace-registry/pull", () => HttpResponse.json({ branch: "main", head: "c".repeat(40), ahead: 0, behind: 0, degraded: false })),
http.get("/api/workspaces/psd-clinical/export", () => new HttpResponse(new Blob(["bundle"], { type: "application/zip" }))),
http.post("/api/workspaces/publish", () => {
publishSpy();
return HttpResponse.json({});
}),
);
renderManager();
await user.click(await screen.findByRole("button", { name: "PSD Clinical" }));
await user.click(screen.getByRole("button", { name: "Pull latest registry" }));
expect(await screen.findByText("Registry updated. Reload a workspace to review its latest revision.")).toBeVisible();
await user.click(screen.getByRole("button", { name: "Export workspace bundle" }));
await waitFor(() => expect(createObjectURL).toHaveBeenCalledTimes(1));
expect(revokeObjectURL).toHaveBeenCalledWith("blob:workspace-bundle");
expect(publishSpy).not.toHaveBeenCalled();
});
test("stages duplicate and delete operations without publishing", async () => {
const user = userEvent.setup();
let published = false;
server.use(http.post("/api/workspaces/publish", () => {
published = true;
return HttpResponse.json({});
}));
renderManager();
await user.click(await screen.findByRole("button", { name: "PSD Clinical" }));
await user.click(screen.getByRole("button", { name: "Duplicate workspace" }));
expect(screen.getByLabelText("Workspace ID")).not.toBeDisabled();
await user.click(screen.getByRole("button", { name: "PSD Clinical" }));
await user.click(screen.getByRole("button", { name: "Delete workspace" }));
expect(screen.getByText("Deletion draft staged locally.")).toBeVisible();
expect(published).toBe(false);
});
test("saves resolved conflict choices as a rebased browser draft without publishing again", async () => {
const user = userEvent.setup();
let publishCalls = 0;
const local = { ...workspace, semantic_index: { ...workspace.semantic_index, vector_store: { ...workspace.semantic_index.vector_store, collection: "local_collection" } } };
const remote = { ...workspace, semantic_index: { ...workspace.semantic_index, vector_store: { ...workspace.semantic_index.vector_store, collection: "remote_collection" } } };
server.use(
http.post("/api/workspaces/validate", () => HttpResponse.json({ workspace: local, contract: {} })),
http.post("/api/workspaces/publish", () => {
publishCalls += 1;
return HttpResponse.json({
code: "workspace_conflict", message: "Workspace changed in the registry.", fields: ["semantic_index.vector_store.collection"],
expected: { commit: "a".repeat(40), blob: "b".repeat(40) }, actual: { commit: "c".repeat(40), blob: "d".repeat(40) },
base: workspace, local, remote,
}, { status: 409 });
}),
);
renderManager();
await user.click(await screen.findByRole("button", { name: "PSD Clinical" }));
await user.click(screen.getByRole("button", { name: "Publish draft" }));
await user.click(screen.getByRole("button", { name: "Validate draft" }));
await user.click(await screen.findByRole("button", { name: "Publish" }));
await user.click(screen.getByRole("button", { name: "Confirm publish" }));
await user.click(await screen.findByRole("radio", { name: "Use your draft for semantic_index.vector_store.collection" }));
await user.click(screen.getByRole("button", { name: "Save revised draft" }));
expect(await screen.findByText("Revised draft saved with registry revision cccccccccccc. Validate it before publishing.")).toBeVisible();
expect(localStorage.getItem("thothii.workspace-registry.v1.draft.psd-clinical")).toContain('"baseCommit":"cccccccccccccccccccccccccccccccccccccccc"');
expect(localStorage.getItem("thothii.workspace-registry.v1.draft.psd-clinical")).toContain('"baseBlob":"dddddddddddddddddddddddddddddddddddddddd"');
expect(publishCalls).toBe(1);
});
test("proposes a different valid ID when duplicating a 63-character workspace ID", async () => {
const user = userEvent.setup();
const maxId = `w${"a".repeat(62)}`;
const maxWorkspace = { ...workspace, workspace: { ...workspace.workspace, id: maxId } };
server.use(
http.get("/api/workspaces", () => HttpResponse.json([{
id: maxId, name: maxId, displayName: "Maximum", language: "en", file: `${maxId}.yaml`,
revision: { id: maxId, commit: "a".repeat(40), blob: "b".repeat(40), snapshotPath: "/tmp/maximum" },
}])),
http.get(`/api/workspaces/${maxId}`, () => HttpResponse.json({
workspace: maxWorkspace,
revision: { id: maxId, commit: "a".repeat(40), blob: "b".repeat(40), snapshotPath: "/tmp/maximum" },
})),
);
renderManager();
await user.click(await screen.findByRole("button", { name: "Maximum" }));
await user.click(screen.getByRole("button", { name: "Duplicate workspace" }));
const proposed = screen.getByLabelText("Workspace ID") as HTMLInputElement;
expect(proposed.value).toMatch(/^[a-z][a-z0-9-]{2,62}$/);
expect(proposed).not.toHaveValue(maxId);
});
test("does not show a saved-draft toast when manager validation rejects a DWH timeout", async () => {
const user = userEvent.setup();
renderManager();
await user.click(await screen.findByRole("button", { name: "PSD Clinical" }));
await user.clear(screen.getByLabelText("DWH timeout (ms)"));
await user.type(screen.getByLabelText("DWH timeout (ms)"), "0");
await user.click(screen.getByRole("button", { name: "Save draft" }));
expect(screen.getByRole("alert")).toHaveTextContent("DWH timeout must be a positive whole number");
expect(screen.queryByText("Draft saved in this browser.")).not.toBeInTheDocument();
});
test("runs validation and installation test with only sanitized messages", async () => {
const user = userEvent.setup();
server.use(
http.post("/api/workspaces/validate", () => HttpResponse.json({ workspace, contract: {} })),
http.post("/api/workspaces/validate", () => HttpResponse.json({ workspace: readyWorkspace, contract: {} })),
http.post("/api/workspaces/psd-clinical/test", () => HttpResponse.json({
activatable: false,
diagnostics: [{ level: "warning", code: "binding_missing", field: "dwh", message: "DWH binding is not configured" }],
})),
);
localStorage.setItem("thothii.workspace-registry.v1.draft.psd-clinical", JSON.stringify({ foo: "bar" }));
renderManager();
await user.click(await screen.findByRole("button", { name: "PSD Clinical" }));
expect(await screen.findByText(/edit psd-clinical\/workspace\.yaml, commit\/push, then Pull/i)).toBeVisible();
expect(screen.queryByRole("button", { name: /duplicate workspace|delete workspace|publish draft/i })).not.toBeInTheDocument();
await user.click(screen.getByRole("button", { name: "Pull latest registry" }));
expect(await screen.findByText("Registry updated. Reload a workspace to review its latest revision.")).toBeVisible();
await user.click(screen.getByRole("button", { name: "Export workspace bundle" }));
await waitFor(() => expect(createObjectURL).toHaveBeenCalledTimes(1));
await user.click(screen.getByRole("button", { name: "Validate workspace" }));
expect(await screen.findByText("Workspace definition is valid.")).toBeVisible();
await user.click(screen.getByRole("button", { name: "Test on this installation" }));
@@ -213,81 +198,16 @@ test("runs validation and installation test with only sanitized messages", async
expect(within(screen.getByTestId("workspace-diagnostics")).queryByText(/password|token|secret/i)).not.toBeInTheDocument();
});
test("loads a state-free registry revision and displays only its commit", async () => {
test("import populates only a matching configuration_required slot and refuses existing workspaces", async () => {
const user = userEvent.setup();
renderManager();
await user.click(await screen.findByRole("button", { name: "PSD Clinical" }));
server.use(http.post("/api/workspaces/import", () => HttpResponse.json({ draft: { workspace: readyWorkspace, contract: {} } })));
await user.upload(screen.getByLabelText("Import workspace bundle"), new File(["bundle"], "workspace.zip", { type: "application/zip" }));
expect(await screen.findByText(/workspace_invalid: Imported bundle can only bootstrap a matching catalog slot/i)).toBeVisible();
expect(await screen.findByLabelText("Vector collection")).toBeVisible();
expect(screen.getByText(`Revision ${"a".repeat(12)}`)).toBeVisible();
expect(screen.queryByText(/migration required/i)).not.toBeInTheDocument();
});
test("shows an actionable unavailable message when a workspace summary omits its canonical revision", async () => {
const user = userEvent.setup();
server.use(
http.get("/api/workspaces", () => HttpResponse.json([
{
id: "broken-workspace", name: "broken-workspace", displayName: "Broken workspace", description: "Broken data",
language: "en", file: "broken-workspace.yaml",
},
])),
);
renderManager();
await user.click(await screen.findByRole("button", { name: "Broken workspace" }));
expect(await screen.findByText("This workspace summary is incomplete. Refresh the registry or choose another workspace before creating sessions or editing drafts.")).toBeVisible();
expect(screen.queryByLabelText("Vector collection")).not.toBeInTheDocument();
});
test("shows an accessible retry instead of a loading status when the registry status query fails", async () => {
const user = userEvent.setup();
let calls = 0;
server.use(http.get("/api/workspace-registry/status", () => {
calls += 1;
return calls === 1 ? new HttpResponse(null, { status: 503 }) : HttpResponse.json({ branch: "main", ahead: 0, behind: 0, degraded: false });
}));
renderManager();
expect(await screen.findByRole("alert", { name: "Workspace registry status failed" })).toHaveTextContent("Could not load registry status.");
await user.click(screen.getByRole("button", { name: "Retry registry status" }));
expect(await screen.findByText("main")).toBeVisible();
expect(calls).toBe(2);
});
test("shows an accessible retry instead of an empty list when the workspace list query fails", async () => {
const user = userEvent.setup();
let calls = 0;
server.use(http.get("/api/workspaces", () => {
calls += 1;
return calls === 1 ? new HttpResponse(null, { status: 503 }) : HttpResponse.json([]);
}));
renderManager();
expect(await screen.findByRole("alert", { name: "Workspace list failed" })).toHaveTextContent("Could not load workspaces.");
expect(screen.queryByText("No published workspaces.")).not.toBeInTheDocument();
await user.click(screen.getByRole("button", { name: "Retry workspace list" }));
expect(await screen.findByText("No published workspaces.")).toBeVisible();
expect(calls).toBe(2);
});
test("shows an accessible retry when the selected workspace detail query fails", async () => {
const user = userEvent.setup();
let calls = 0;
server.use(http.get("/api/workspaces/psd-clinical", () => {
calls += 1;
return calls === 1 ? new HttpResponse(null, { status: 503 }) : HttpResponse.json({
workspace,
revision: { id: "psd-clinical", commit: "a".repeat(40), blob: "b".repeat(40), snapshotPath: "/tmp/psd" },
});
}));
renderManager();
await user.click(await screen.findByRole("button", { name: "PSD Clinical" }));
expect(await screen.findByRole("alert", { name: "Workspace details failed" })).toHaveTextContent("Could not load workspace details.");
await user.click(screen.getByRole("button", { name: "Retry workspace details" }));
expect(await screen.findByRole("heading", { name: "PSD Clinical" })).toBeVisible();
expect(calls).toBe(2);
server.use(http.post("/api/workspaces/import", () => HttpResponse.json({ draft: { workspace: bootstrapWorkspace, contract: {} } })));
await user.upload(screen.getByLabelText("Import workspace bundle"), new File(["bundle"], "workspace.zip", { type: "application/zip" }));
expect(await screen.findByText("Imported bootstrap draft saved in this browser. Validate it before creating the descriptor.")).toBeVisible();
expect(localStorage.getItem("thothii.workspace-registry.v2.bootstrap.bootstrap-slot")).not.toBeNull();
});