import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { expect, test, vi } from "vitest"; import type { CanonicalWorkspace } from "../api/workspaces"; import type { WorkspaceDraft } from "../workspaces/drafts"; import { WorkspaceEditor } from "./WorkspaceEditor"; const workspace: CanonicalWorkspace = { workspace: { schema_version: 2, 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: "pgvector", database: "vectors", schema: "public", collection: "clinical", dimensions: 768, distance: "cosine", port: 5432, supported_transports: ["pgvector_direct"], }, embedding: { provider: "ollama_compatible", model: "nomic-embed-text", dimensions: 768 }, }, llm_policy: { default: "zai/glm-5.2", allowed: ["zai/glm-5.2"] }, }; const draft: WorkspaceDraft = { workspaceId: "psd-clinical", baseCommit: "a".repeat(40), baseBlob: "b".repeat(40), workspace, updatedAt: "2026-08-04T10:00:00.000Z", }; test("uses closed choices for transport and rejects an invalid free-form port before save", async () => { const user = userEvent.setup(); render(); expect(screen.getByRole("listbox", { name: "DWH transport" })).toHaveTextContent("postgres_direct"); await user.clear(screen.getByLabelText("DWH port")); await user.type(screen.getByLabelText("DWH port"), "70000"); await user.click(screen.getByRole("button", { name: "Save draft" })); expect(screen.getByRole("alert")).toHaveTextContent("Port must be between 1 and 65535"); expect(screen.getByLabelText("DWH port")).toHaveAttribute("aria-invalid", "true"); }); test("keeps vector dimensions and embedding dimensions atomic in a draft", async () => { const user = userEvent.setup(); const onSaveDraft = vi.fn(); render(); await user.clear(screen.getByLabelText("Semantic index dimensions")); await user.type(screen.getByLabelText("Semantic index dimensions"), "1024"); await user.click(screen.getByRole("button", { name: "Save draft" })); expect(onSaveDraft).toHaveBeenCalledWith(expect.objectContaining({ workspace: expect.objectContaining({ semantic_index: expect.objectContaining({ vector_store: expect.objectContaining({ dimensions: 1024 }), embedding: expect.objectContaining({ dimensions: 1024 }), }), }), })); }); test("uses native closed selects for each workspace enum and embedding provider", () => { render(); expect(screen.getByRole("combobox", { name: "Workspace language" })).toHaveValue("en"); expect(screen.getByRole("combobox", { name: "Vector distance" })).toHaveValue("cosine"); expect(screen.getByRole("combobox", { name: "Embedding provider" })).toHaveValue("ollama_compatible"); expect(screen.getByRole("listbox", { name: "DWH transport" })).toHaveProperty("multiple", true); expect(screen.getByRole("listbox", { name: "Vector transport" })).toHaveProperty("multiple", true); });