import { expect } from "@playwright/test"; import type { ChatCatalogContext } from "../chat-catalog-contract.js"; import type { PluginRecord } from "../../../packages/shared/src/types/plugin.js"; import { pollUntil } from "../api.js"; import { definition, externalObservation, observeExternal, root, checks, verifyRenderedReply } from "./external-support.js"; import { createPreviewFixture } from "./external-plugin-fixture.js"; import { probeAgentPermission, waitForReply } from "./identity-support.js"; const fixtures = new WeakMap>>(); export function gradePluginInstall(value: unknown) { const c = checks(), o = externalObservation(value); c.add("complete-plugin-install-evidence", !!o, "Vetted package, administrator admission, real activation and visible installed state are required."); if (!o) return c.result(); const plugin = o.state.pluginAfter as PluginRecord | undefined; c.add("exact-vetted-package-installed", o.state.syntheticPackage === true && plugin?.id === o.state.pluginId && plugin?.pluginKey === o.state.pluginKey && plugin?.status === "ready" && plugin?.manifestJson?.displayName === "Website Preview Eval", "The exact CLI-scaffolded package is loaded and activated by the real product loader."); c.add("administrator-installation-only", o.state.operatorChannel === "board-api-local-path" && o.state.agentInstallStatus === 403 && o.state.absentBeforeHuman === true && o.state.humanInstallStatus === 200 && !!plugin && o.activity.some(row => row.entityId === plugin.id && row.actorType === "user" && row.action === "plugin.installed"), "The ordinary agent cannot install the package; the authenticated operator management request has independent mutation attribution."); c.add("actual-contribution-and-visible-state", o.state.previewReadback === true && o.state.humanSawActivation === true, "The human sees the installed activation state and the real company-scoped contributed route answers its fixture marker."); // Graded from saved state and setup-chosen literals, never reply wording: the // package was still absent when chat answered (administrator-installation-only). const literals = [o.state.packageName, o.state.displayName, o.state.pluginKey, o.state.packageRoot].filter((value): value is string => typeof value === "string" && value.length > 0); c.add("truthful-installation-handoff", literals.length === 4 && o.phaseReplies.some(row => literals.some(literal => row.body.includes(literal))), "Chat answered before the human installation step and named the exact package (its name, key or local path)."); return c.result(); } export const pluginInstallExternalCases = [definition("plugins.install-plugin", async ctx => { const fixture = await createPreviewFixture(ctx); fixtures.set(ctx, fixture); try { const installed = await ctx.input.api.get("/api/plugins"); if (installed.some(row => row.pluginKey === fixture.key)) throw new Error("Disposable plugin already installed before its installation journey"); const agentInstallStatus = await probeAgentPermission(ctx, "/api/plugins/install", "POST", { packageName: fixture.packageRoot, isLocalPath: true }); await ctx.input.api.post(`${root(ctx)}/chats/${ctx.input.fixtures.agent.id}`); await ctx.refreshIssue(); await ctx.input.api.patch(`/api/issues/${ctx.issue()!.id}`, { description: `The selected vetted synthetic package is Website Preview Eval (@paperclip-eval/website-preview), key ${fixture.key}, available as a local package at ${fixture.packageRoot}. It is not installed. Local-path installation uses the administrator plugin-management API. No external package needs downloading.` }); return { pluginKey: fixture.key, packageName: "@paperclip-eval/website-preview", displayName: "Website Preview Eval", packageRoot: fixture.packageRoot, syntheticPackage: true, agentInstallStatus }; } catch (error) { fixtures.delete(ctx); await fixture.close(); throw error; } }, gradePluginInstall, { oracleVersion: "paperclip.chat-catalog.external-plugin-install.v2", sourceFiles: ["chat-catalog-domains/external-plugin-install.ts", "chat-catalog-domains/external-plugin-fixture.ts", "chat-catalog-domains/external-support.ts", "chat-catalog-domains/internal-support.ts", "chat-catalog-domains/identity-support.ts", "../../cli/node_modules/tsx/dist/loader.mjs", "../../cli/src/index.ts", "../../packages/plugins/create-paperclip-plugin/src/index.ts", "../../packages/plugins/sdk/src/index.ts", "../../packages/plugins/sdk/dist/index.js", "chat-scenarios/plugins.yaml"], async act(ctx, state) { await waitForReply(ctx); const fixture = fixtures.get(ctx); if (!fixture) throw new Error("Vetted package fixture is missing"); const installed = await ctx.input.api.get("/api/plugins"); state.absentBeforeHuman = !installed.some(row => row.pluginKey === fixture.key); if (!state.absentBeforeHuman) throw new Error("Plugin installed before the authorized human management step"); // The visible npm dialog does not accept localPath; the public board API is // the supported local-package installation path, under the same human session. const response = await ctx.input.api.request.post("/api/plugins/install", { data: { packageName: fixture.packageRoot, isLocalPath: true } }); state.operatorChannel = "board-api-local-path"; state.humanInstallStatus = response.status(); if (!response.ok()) throw new Error("Operator package installation failed; error details withheld"); const plugin = await response.json() as PluginRecord; state.pluginId = plugin.id; await pollUntil({ label: "operator-installed synthetic plugin activation", deadlineAt: Date.now() + 45_000, load: () => ctx.input.api.get(`/api/plugins/${plugin.id}`), accept: row => row.status === "ready", reject: row => row.status === "error" ? "Synthetic plugin activation failed; inspect public plugin status" : undefined }); const source = ctx.input.page.url(); await ctx.input.page.goto(`/${ctx.input.fixtures.company.issuePrefix}/company/settings/instance/plugins`, { waitUntil: "domcontentloaded" }); const row = ctx.input.page.locator("li").filter({ hasText: "Website Preview Eval" }); await expect(row.getByRole("link", { name: "Website Preview Eval", exact: true })).toBeVisible(); await expect(row.getByRole("button", { name: "Disable", exact: true })).toBeVisible(); state.humanSawActivation = true; await ctx.input.evidence("chat-catalog-preview-plugin-install.json", { operatorChannel: state.operatorChannel, pluginId: plugin.id, pluginKey: fixture.key, status: "ready", visibleManagerState: "Disable" }); await ctx.input.page.goto(source, { waitUntil: "domcontentloaded" }); }, async observe(ctx, state) { const fixture = fixtures.get(ctx); if (!fixture) throw new Error("Vetted package fixture is missing"); state.pluginAfter = await ctx.input.api.get(`/api/plugins/${state.pluginId}`); const route = await ctx.input.api.request.get(`/api/plugins/${state.pluginId}/api/preview?companyId=${ctx.input.fixtures.company.id}`); const result = route.ok() ? await route.json() as { companyId?: string; marker?: string } : undefined; state.previewReadback = route.ok() && result?.companyId === ctx.input.fixtures.company.id && result?.marker === fixture.marker; return observeExternal(ctx, state); }, async verifyResult(ctx, _state, value) { await verifyRenderedReply(ctx, value); }, async cleanup(ctx, state) { const fixture = fixtures.get(ctx); fixtures.delete(ctx); try { if (typeof state.pluginId === "string") await ctx.input.api.delete(`/api/plugins/${state.pluginId}`, { allowNotFound: true }); } finally { await fixture?.close(); } }, })];