// @vitest-environment jsdom import { createRoot, type Root } from "react-dom/client"; import { flushSync } from "react-dom"; import { afterEach, describe, expect, it, vi } from "vitest"; import type { AgentPermissions } from "@paperclipai/shared"; import { TrustPresetSection } from "./TrustPresetSection"; import { TooltipProvider } from "@/components/ui/tooltip"; (globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; let root: Root | null = null; let container: HTMLDivElement | null = null; function renderSection(permissions: Partial, withProjectEditor = false) { container = document.createElement("div"); document.body.appendChild(container); const onChange = vi.fn(); const createdRoot = createRoot(container); root = createdRoot; flushSync(() => { createdRoot.render( Allowed projects : undefined} permissions={permissions} onChange={onChange} companyId="company-1" projectCandidates={[{ id: "project-1", label: "Paperclip App" }]} issueCandidates={[{ id: "issue-1", label: "PAP-1 ยท Review PR" }]} /> , ); }); return { onChange, text: () => container?.textContent ?? "" }; } afterEach(() => { if (root) { flushSync(() => root?.unmount()); } container?.remove(); root = null; container = null; }); describe("TrustPresetSection", () => { it("hides the CE boundary editor under Standard", () => { const view = renderSection({ canCreateAgents: false, trustPreset: "standard" }); expect(view.text()).toContain("Trust preset"); expect(view.text()).not.toContain("Boundary type"); expect(view.text()).not.toContain("Get Paperclip EE."); }); it("shows a selectable CE boundary editor for low-trust review", () => { const view = renderSection({ canCreateAgents: false, trustPreset: "low_trust_review", authorizationPolicy: { trustPreset: "low_trust_review", trustBoundary: { mode: "low_trust_review", companyId: "company-1", projectIds: ["project-1"], }, }, }); expect(view.text()).toContain("Containment active"); expect(view.text()).toContain("Boundary type"); expect(view.text()).toContain("Paperclip App"); expect(view.text()).toContain("Get Paperclip EE."); expect(view.text()).not.toContain("Managed by EE/API"); }); it("keeps the single-boundary controls when the allowed-project editor is present", () => { const view = renderSection({ trustPreset: "low_trust_review", authorizationPolicy: { trustPreset: "low_trust_review", trustBoundary: { mode: "low_trust_review", companyId: "company-1", projectIds: ["project-1"], }, } }, true); expect(view.text()).toContain("Allowed projects"); expect(view.text()).toContain("Boundary type"); const clear = [...container!.querySelectorAll("button")].find(button => button.textContent === "Clear boundary"); expect(clear).toBeDefined(); flushSync(() => clear!.click()); const changed = view.onChange.mock.calls[0][0]; expect(changed.trustPreset).toBe("low_trust_review"); expect(changed.authorizationPolicy.trustBoundary.projectIds).toBeUndefined(); }); it("renders multi-boundary policies as read-only", () => { const view = renderSection({ canCreateAgents: false, trustPreset: "low_trust_review", authorizationPolicy: { trustPreset: "low_trust_review", trustBoundary: { mode: "low_trust_review", companyId: "company-1", projectIds: ["project-1", "project-2"], }, }, }); expect(view.text()).toContain("Managed by EE/API"); expect(view.text()).toContain("2 boundaries"); expect(view.text()).not.toContain("Clear boundary"); }); });