feat: publish and synchronize workspace drafts

This commit is contained in:
2026-08-04 06:47:10 +02:00
parent 6b40fa0cc5
commit b75b3af28e
9 changed files with 545 additions and 16 deletions
@@ -0,0 +1,94 @@
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { http, HttpResponse } from "msw";
import { beforeEach, expect, test, vi } from "vitest";
import type { CanonicalWorkspace, PublishWorkspaceRequest, WorkspaceConflict } from "../api/workspaces";
import { server } from "../test/msw";
import { WorkspacePublishDialog } from "./WorkspacePublishDialog";
const workspace: CanonicalWorkspace = {
workspace: { schema_version: 2, id: "psd-clinical", name: "PSD Clinical", language: "en" },
dwh: { engine: "postgres", database: "clinical", schema: "datawarehouse", supported_transports: ["postgres_direct"] },
semantic_index: {
vector_store: { engine: "pgvector", database: "vectors", schema: "public", collection: "clinical", dimensions: 768, distance: "cosine", supported_transports: ["pgvector_direct"] },
embedding: { provider: "ollama_compatible", model: "nomic-embed-text", dimensions: 768 },
},
llm_policy: { allowed: ["zai/glm-5.2"] },
};
const request: PublishWorkspaceRequest = {
action: "update", workspace, baseCommit: "a".repeat(40), baseBlob: "b".repeat(40),
};
const conflict: WorkspaceConflict = {
code: "workspace_conflict",
fields: ["semantic_index.embedding.model"],
base: workspace,
local: { ...workspace, semantic_index: { ...workspace.semantic_index, embedding: { ...workspace.semantic_index.embedding, model: "local-model" } } },
remote: { ...workspace, semantic_index: { ...workspace.semantic_index, embedding: { ...workspace.semantic_index.embedding, model: "remote-model" } } },
};
beforeEach(() => {
server.use(http.post("http://localhost:8787/workspaces/validate", () => HttpResponse.json({ workspace, contract: {} })));
});
test("validates a draft and requires a separate confirmation before publishing", async () => {
const user = userEvent.setup();
const published = vi.fn();
let publishCalls = 0;
server.use(http.post("http://localhost:8787/workspaces/publish", () => {
publishCalls += 1;
return HttpResponse.json({ revision: { id: "psd-clinical", commit: "c".repeat(40), blob: "d".repeat(40), snapshotPath: "/safe", state: "operational" } });
}));
render(<WorkspacePublishDialog open request={request} onOpenChange={vi.fn()} onPublished={published} onPull={vi.fn()} onReload={vi.fn()} />);
expect(screen.getByRole("button", { name: "Publish" })).toBeDisabled();
await user.click(screen.getByRole("button", { name: "Validate draft" }));
expect(await screen.findByText("Workspace definition is valid." )).toBeVisible();
await user.click(screen.getByRole("button", { name: "Publish" }));
expect(screen.getByRole("heading", { name: "Confirm publication" })).toBeVisible();
expect(publishCalls).toBe(0);
await user.click(screen.getByRole("button", { name: "Confirm publish" }));
await waitFor(() => expect(published).toHaveBeenCalledTimes(1));
expect(publishCalls).toBe(1);
});
test("shows a field-level conflict and never overwrites the remote workspace", async () => {
const user = userEvent.setup();
let published = false;
server.use(http.post("http://localhost:8787/workspaces/publish", () => {
published = true;
return HttpResponse.json({ ...conflict, message: "Workspace changed in the registry." }, { status: 409 });
}));
render(<WorkspacePublishDialog open request={request} onOpenChange={vi.fn()} onPublished={vi.fn()} onPull={vi.fn()} onReload={vi.fn()} />);
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" }));
expect(await screen.findByText("semantic_index.embedding.model")).toBeVisible();
expect(screen.getByText("local-model")).toBeVisible();
expect(screen.getByText("remote-model")).toBeVisible();
expect(screen.getByRole("button", { name: "Pull latest registry" })).toBeVisible();
expect(screen.getByRole("button", { name: "Reload workspace" })).toBeVisible();
expect(screen.queryByRole("button", { name: /use local|use remote|confirm publish/i })).not.toBeInTheDocument();
expect(published).toBe(true);
});
test("keeps a conflict open and redacts a failed registry pull", async () => {
const user = userEvent.setup();
server.use(http.post("http://localhost:8787/workspaces/publish", () => HttpResponse.json({
...conflict, message: "Workspace changed in the registry.",
}, { status: 409 })));
render(<WorkspacePublishDialog open request={request} onOpenChange={vi.fn()} onPublished={vi.fn()} onPull={vi.fn().mockRejectedValue(new Error("token=secret"))} onReload={vi.fn()} />);
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("button", { name: "Pull latest registry" }));
expect(await screen.findByText("Registry pull could not be completed. Try again or reload the workspace.")).toBeVisible();
expect(screen.queryByText(/token=secret/)).not.toBeInTheDocument();
expect(screen.getByRole("button", { name: "Reload workspace" })).toBeVisible();
});