|
1 | | -import { test, expect, type Page } from "@playwright/test"; |
| 1 | +import { test, expect, type Page, type ConsoleMessage } from "@playwright/test"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Helper to get the app frame locator (nested: sandbox > app) |
| 5 | + */ |
| 6 | +function getAppFrame(page: Page) { |
| 7 | + return page.frameLocator("iframe").first().frameLocator("iframe").first(); |
| 8 | +} |
| 9 | + |
| 10 | +/** |
| 11 | + * Collect console messages with [HOST] prefix |
| 12 | + */ |
| 13 | +function captureHostLogs(page: Page): string[] { |
| 14 | + const logs: string[] = []; |
| 15 | + page.on("console", (msg: ConsoleMessage) => { |
| 16 | + const text = msg.text(); |
| 17 | + if (text.includes("[HOST]")) { |
| 18 | + logs.push(text); |
| 19 | + } |
| 20 | + }); |
| 21 | + return logs; |
| 22 | +} |
2 | 23 |
|
3 | 24 | // Server configurations |
4 | 25 | const SERVERS = [ |
@@ -67,3 +88,53 @@ SERVERS.forEach((server) => { |
67 | 88 | }); |
68 | 89 | }); |
69 | 90 | }); |
| 91 | + |
| 92 | +// Interaction tests for basic servers (React and Vanilla JS have identical UIs) |
| 93 | +const BASIC_SERVERS = SERVERS.filter( |
| 94 | + (s) => s.key === "basic-react" || s.key === "basic-vanillajs", |
| 95 | +); |
| 96 | + |
| 97 | +BASIC_SERVERS.forEach((server) => { |
| 98 | + test.describe(`${server.name} - Interactions`, () => { |
| 99 | + test("Send Message button triggers host callback", async ({ page }) => { |
| 100 | + const logs = captureHostLogs(page); |
| 101 | + await loadServer(page, server.index); |
| 102 | + |
| 103 | + const appFrame = getAppFrame(page); |
| 104 | + await appFrame.locator('button:has-text("Send Message")').click(); |
| 105 | + |
| 106 | + // Wait for the async message to be processed |
| 107 | + await page.waitForTimeout(500); |
| 108 | + |
| 109 | + expect(logs.some((log) => log.includes("Message from MCP App"))).toBe( |
| 110 | + true, |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + test("Send Log button triggers host callback", async ({ page }) => { |
| 115 | + const logs = captureHostLogs(page); |
| 116 | + await loadServer(page, server.index); |
| 117 | + |
| 118 | + const appFrame = getAppFrame(page); |
| 119 | + await appFrame.locator('button:has-text("Send Log")').click(); |
| 120 | + |
| 121 | + await page.waitForTimeout(500); |
| 122 | + |
| 123 | + expect(logs.some((log) => log.includes("Log message from MCP App"))).toBe( |
| 124 | + true, |
| 125 | + ); |
| 126 | + }); |
| 127 | + |
| 128 | + test("Open Link button triggers host callback", async ({ page }) => { |
| 129 | + const logs = captureHostLogs(page); |
| 130 | + await loadServer(page, server.index); |
| 131 | + |
| 132 | + const appFrame = getAppFrame(page); |
| 133 | + await appFrame.locator('button:has-text("Open Link")').click(); |
| 134 | + |
| 135 | + await page.waitForTimeout(500); |
| 136 | + |
| 137 | + expect(logs.some((log) => log.includes("Open link request"))).toBe(true); |
| 138 | + }); |
| 139 | + }); |
| 140 | +}); |
0 commit comments