|
| 1 | +import { randomBytes } from "node:crypto"; |
| 2 | +import { expect } from "@effect/vitest"; |
| 3 | +import { Effect } from "effect"; |
| 4 | +import { composePluginApi } from "@executor-js/api/server"; |
| 5 | +import { mcpHttpPlugin } from "@executor-js/plugin-mcp/api"; |
| 6 | +import { makeElicitationMcpServer, serveMcpServer } from "@executor-js/plugin-mcp/testing"; |
| 7 | +import { AuthTemplateSlug, ConnectionName, IntegrationSlug } from "@executor-js/sdk/shared"; |
| 8 | + |
| 9 | +import { scenario } from "../src/scenario"; |
| 10 | +import { Api, Browser, Mcp, Target } from "../src/services"; |
| 11 | +import { parseBrowserApproval } from "../src/surfaces/mcp"; |
| 12 | +import { visit } from "../src/surfaces/browser"; |
| 13 | + |
| 14 | +const api = composePluginApi([mcpHttpPlugin()] as const); |
| 15 | + |
| 16 | +scenario( |
| 17 | + "MCP · browser approval preserves the chosen lifetime and defaults to once", |
| 18 | + { timeout: 180_000 }, |
| 19 | + Effect.scoped( |
| 20 | + Effect.gen(function* () { |
| 21 | + const target = yield* Target; |
| 22 | + const browser = yield* Browser; |
| 23 | + const mcp = yield* Mcp; |
| 24 | + const { client: makeClient } = yield* Api; |
| 25 | + const identity = yield* target.newIdentity(); |
| 26 | + const client = yield* makeClient(api, identity); |
| 27 | + const slug = IntegrationSlug.make(`approval_terms_${randomBytes(4).toString("hex")}`); |
| 28 | + const server = yield* serveMcpServer(makeElicitationMcpServer); |
| 29 | + yield* client.mcp.addServer({ |
| 30 | + payload: { |
| 31 | + transport: "remote", |
| 32 | + name: "Approval terms", |
| 33 | + endpoint: server.url, |
| 34 | + slug, |
| 35 | + remoteTransport: "streamable-http", |
| 36 | + }, |
| 37 | + }); |
| 38 | + yield* Effect.gen(function* () { |
| 39 | + yield* client.connections.create({ |
| 40 | + payload: { |
| 41 | + owner: "org", |
| 42 | + name: ConnectionName.make("main"), |
| 43 | + integration: slug, |
| 44 | + template: AuthTemplateSlug.make("none"), |
| 45 | + value: "", |
| 46 | + }, |
| 47 | + }); |
| 48 | + const session = mcp.session(identity, { elicitationMode: "browser" }); |
| 49 | + yield* session.listTools(); |
| 50 | + for (const scope of ["session", "always", ""] as const) { |
| 51 | + const paused = yield* session.call("execute", { |
| 52 | + code: `return await tools.${slug}.org.main.remembered_echo({value:"browser"});`, |
| 53 | + }); |
| 54 | + const approval = parseBrowserApproval(paused); |
| 55 | + const [resumed] = yield* Effect.all( |
| 56 | + [ |
| 57 | + session.awaitResume(approval.executionId), |
| 58 | + browser.session(identity, async ({ page, step }) => { |
| 59 | + await step(`Approve ${scope || "once"} through the console`, async () => { |
| 60 | + await visit(page, approval.approvalUrl); |
| 61 | + const choice = page.getByLabel("Remember this approval"); |
| 62 | + await choice.waitFor(); |
| 63 | + expect(await choice.inputValue(), "every approval starts as one-time").toBe(""); |
| 64 | + if (scope !== "") await choice.selectOption(scope); |
| 65 | + await page.getByRole("button", { name: "Approve", exact: true }).click(); |
| 66 | + await page.getByText("Approve sent").waitFor(); |
| 67 | + }); |
| 68 | + }), |
| 69 | + ], |
| 70 | + { concurrency: "unbounded" }, |
| 71 | + ); |
| 72 | + expect(resumed.ok).toBe(true); |
| 73 | + expect(resumed.text, "the chosen lifetime reaches the upstream MCP server").toContain( |
| 74 | + `approved:browser:${scope || "once"}`, |
| 75 | + ); |
| 76 | + } |
| 77 | + }).pipe(Effect.ensuring(client.mcp.removeServer({ params: { slug } }).pipe(Effect.orDie))); |
| 78 | + }), |
| 79 | + ), |
| 80 | +); |
0 commit comments