|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest" |
| 2 | + |
| 3 | +// Mock the mcp-client module before importing browser-eval-manager |
| 4 | +vi.mock("../../src/_internal/mcp-client.js", () => ({ |
| 5 | + connectToMCPServer: vi.fn(), |
| 6 | + callServerTool: vi.fn(), |
| 7 | + listServerTools: vi.fn(), |
| 8 | +})) |
| 9 | + |
| 10 | +// Mock the exec function to skip installation check |
| 11 | +vi.mock("child_process", () => ({ |
| 12 | + exec: vi.fn((cmd, callback) => { |
| 13 | + callback(null, { stdout: "@playwright/[email protected]" }) |
| 14 | + }), |
| 15 | +})) |
| 16 | + |
| 17 | +import { connectToMCPServer, callServerTool } from "../../src/_internal/mcp-client.js" |
| 18 | +import { |
| 19 | + startBrowserEvalMCP, |
| 20 | + stopBrowserEvalMCP, |
| 21 | +} from "../../src/_internal/browser-eval-manager.js" |
| 22 | +import { handler } from "../../src/tools/browser-eval.js" |
| 23 | + |
| 24 | +describe("browser-eval playwright-mcp screenshot tool", () => { |
| 25 | + beforeEach(() => { |
| 26 | + vi.clearAllMocks() |
| 27 | + vi.mocked(connectToMCPServer).mockResolvedValue({ |
| 28 | + client: { close: vi.fn() }, |
| 29 | + transport: { close: vi.fn() }, |
| 30 | + } as never) |
| 31 | + }) |
| 32 | + |
| 33 | + afterEach(async () => { |
| 34 | + await stopBrowserEvalMCP() |
| 35 | + }) |
| 36 | + |
| 37 | + it("should pass --image-responses omit flag to playwright-mcp", async () => { |
| 38 | + await startBrowserEvalMCP() |
| 39 | + |
| 40 | + expect(connectToMCPServer).toHaveBeenCalledWith( |
| 41 | + "npx", |
| 42 | + expect.arrayContaining(["--image-responses", "omit"]), |
| 43 | + expect.any(Object) |
| 44 | + ) |
| 45 | + }) |
| 46 | + |
| 47 | + it("should return screenshot file path without base64 image data", async () => { |
| 48 | + await startBrowserEvalMCP() |
| 49 | + |
| 50 | + const screenshotPath = "/var/folders/tmp/screenshot-1234.png" |
| 51 | + vi.mocked(callServerTool).mockResolvedValue({ |
| 52 | + content: [{ type: "text", text: `Screenshot saved to ${screenshotPath}` }], |
| 53 | + }) |
| 54 | + |
| 55 | + const result = await handler({ action: "screenshot" }) |
| 56 | + const parsed = JSON.parse(result) |
| 57 | + |
| 58 | + expect(parsed.success).toBe(true) |
| 59 | + expect(parsed.action).toBe("screenshot") |
| 60 | + |
| 61 | + // Verify response contains file path |
| 62 | + const textContent = parsed.result.content.find( |
| 63 | + (block: { type: string }) => block.type === "text" |
| 64 | + ) |
| 65 | + expect(textContent.text).toContain(screenshotPath) |
| 66 | + |
| 67 | + // Verify no base64 image data in response |
| 68 | + const imageContent = parsed.result.content.find( |
| 69 | + (block: { type: string }) => block.type === "image" |
| 70 | + ) |
| 71 | + expect(imageContent).toBeUndefined() |
| 72 | + }) |
| 73 | +}) |
0 commit comments