|
| 1 | +// Integration tests for createBrowserToolServer. These exercise the real |
| 2 | +// @playwright/mcp embed with a real BrowserContext. Opt-in: |
| 3 | +// |
| 4 | +// PHANTOM_INTEGRATION=1 bun test src/ui/__tests__/browser-mcp.integration.test.ts |
| 5 | +// |
| 6 | +// Skipped by default so `bun test` stays hermetic. |
| 7 | +// |
| 8 | +// Two load-bearing invariants are enforced here: |
| 9 | +// |
| 10 | +// 1. The embed exposes exactly 21 tools. @playwright/mcp@0.0.70 is pinned |
| 11 | +// specifically so the tool surface cannot drift silently; this assertion |
| 12 | +// is the drift detector the pin was meant to anchor. |
| 13 | +// |
| 14 | +// 2. A real `browser_navigate` call succeeds against a BrowserContext |
| 15 | +// minted by the preview tool. This is the end-to-end verification of |
| 16 | +// the cross-version playwright-core boundary documented in |
| 17 | +// src/ui/browser-mcp.ts note 3: the context is an instance from |
| 18 | +// playwright-core@1.59.1 consumed by @playwright/mcp's hoisted |
| 19 | +// playwright-core@1.60.0-alpha SimpleBrowser wrapper. |
| 20 | + |
| 21 | +import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 22 | +import { Client } from "@modelcontextprotocol/sdk/client/index.js"; |
| 23 | +import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js"; |
| 24 | +import { createBrowserToolServer } from "../browser-mcp.ts"; |
| 25 | +import { __resetPreviewStateForTesting, closePreviewResources, getOrCreatePreviewContext } from "../preview.ts"; |
| 26 | +import { revokeAllSessions } from "../session.ts"; |
| 27 | + |
| 28 | +const ENABLED = process.env.PHANTOM_INTEGRATION === "1"; |
| 29 | +const suite = ENABLED ? describe : describe.skip; |
| 30 | + |
| 31 | +const EXPECTED_TOOL_NAMES = [ |
| 32 | + "browser_click", |
| 33 | + "browser_close", |
| 34 | + "browser_console_messages", |
| 35 | + "browser_drag", |
| 36 | + "browser_evaluate", |
| 37 | + "browser_file_upload", |
| 38 | + "browser_fill_form", |
| 39 | + "browser_handle_dialog", |
| 40 | + "browser_hover", |
| 41 | + "browser_navigate", |
| 42 | + "browser_navigate_back", |
| 43 | + "browser_network_requests", |
| 44 | + "browser_press_key", |
| 45 | + "browser_resize", |
| 46 | + "browser_run_code", |
| 47 | + "browser_select_option", |
| 48 | + "browser_snapshot", |
| 49 | + "browser_tabs", |
| 50 | + "browser_take_screenshot", |
| 51 | + "browser_type", |
| 52 | + "browser_wait_for", |
| 53 | +]; |
| 54 | + |
| 55 | +type CallResult = { isError?: boolean; content: unknown }; |
| 56 | + |
| 57 | +suite("createBrowserToolServer (integration)", () => { |
| 58 | + let server: ReturnType<typeof Bun.serve> | null = null; |
| 59 | + let port = 0; |
| 60 | + let client: Client | null = null; |
| 61 | + let embed: Awaited<ReturnType<typeof createBrowserToolServer>> | null = null; |
| 62 | + |
| 63 | + beforeAll(async () => { |
| 64 | + // Reset module-level preview state so running this file after any |
| 65 | + // other test file that called closePreviewResources() still starts |
| 66 | + // from a pristine state. Bun shares module instances across test |
| 67 | + // files inside the same process. |
| 68 | + __resetPreviewStateForTesting(); |
| 69 | + server = Bun.serve({ |
| 70 | + port: 0, |
| 71 | + fetch(req) { |
| 72 | + const url = new URL(req.url); |
| 73 | + if (url.pathname === "/ui/test.html") { |
| 74 | + return new Response( |
| 75 | + "<!DOCTYPE html><html><head><title>Browser MCP Integration</title></head>" + |
| 76 | + "<body><h1>Hello</h1></body></html>", |
| 77 | + { headers: { "content-type": "text/html" } }, |
| 78 | + ); |
| 79 | + } |
| 80 | + return new Response("not found", { status: 404 }); |
| 81 | + }, |
| 82 | + }); |
| 83 | + port = server.port ?? 0; |
| 84 | + |
| 85 | + embed = await createBrowserToolServer(() => getOrCreatePreviewContext()); |
| 86 | + const [serverTransport, clientTransport] = InMemoryTransport.createLinkedPair(); |
| 87 | + const serverInstance = embed.instance as unknown as { |
| 88 | + connect: (t: typeof serverTransport) => Promise<void>; |
| 89 | + close: () => Promise<void>; |
| 90 | + }; |
| 91 | + await serverInstance.connect(serverTransport); |
| 92 | + client = new Client({ name: "phantom-browser-integration", version: "1.0" }, { capabilities: {} }); |
| 93 | + await client.connect(clientTransport); |
| 94 | + }); |
| 95 | + |
| 96 | + afterAll(async () => { |
| 97 | + await client?.close(); |
| 98 | + if (embed) { |
| 99 | + const inst = embed.instance as unknown as { close: () => Promise<void> }; |
| 100 | + await inst.close(); |
| 101 | + } |
| 102 | + await closePreviewResources(); |
| 103 | + revokeAllSessions(); |
| 104 | + server?.stop(true); |
| 105 | + }); |
| 106 | + |
| 107 | + test("listTools returns exactly the 21-tool @playwright/mcp surface", async () => { |
| 108 | + if (!client) throw new Error("client not initialized"); |
| 109 | + const { tools } = await client.listTools(); |
| 110 | + expect(tools).toHaveLength(21); |
| 111 | + const names = tools.map((t) => t.name).sort(); |
| 112 | + expect(names).toEqual([...EXPECTED_TOOL_NAMES].sort()); |
| 113 | + }); |
| 114 | + |
| 115 | + test("browser_navigate succeeds across the cross-version BrowserContext boundary", async () => { |
| 116 | + if (!client) throw new Error("client not initialized"); |
| 117 | + const result = (await client.callTool({ |
| 118 | + name: "browser_navigate", |
| 119 | + arguments: { url: `http://localhost:${port}/ui/test.html` }, |
| 120 | + })) as CallResult; |
| 121 | + // A successful navigate returns content with no isError flag set. |
| 122 | + // The exact content shape is @playwright/mcp's concern; we care only |
| 123 | + // that the call did not land in the error branch. |
| 124 | + expect(result.isError).toBeFalsy(); |
| 125 | + expect(result.content).toBeDefined(); |
| 126 | + }); |
| 127 | +}); |
0 commit comments