|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Playwright fixtures that put the real webview bundle in a real browser, talking to the real host. |
| 5 | + * |
| 6 | + * The bridge is deliberately thin — `acquireVsCodeApi()` in the page forwards straight to the host's |
| 7 | + * `onDidReceiveMessage`, and everything the host posts is replayed as a `window.postMessage`. Nothing |
| 8 | + * between the two is stubbed, so a broken message contract on either side fails these tests. |
| 9 | + */ |
| 10 | + |
| 11 | +const fs = require('fs'); |
| 12 | +const path = require('path'); |
| 13 | +const { test: base, expect } = require('@playwright/test'); |
| 14 | + |
| 15 | +const { startHarnessServer } = require('./harness/server'); |
| 16 | +const { createObjModHost } = require('./harness/objmodHost'); |
| 17 | +const { createW3iHost, createWpmHost } = require('./harness/mapEditorHosts'); |
| 18 | +const { root } = require('./harness/tsLoader'); |
| 19 | + |
| 20 | +/** Mirrors the webview API surface the shipped code uses. State lives in sessionStorage so it |
| 21 | + * survives a reload the same way VS Code's per-webview state does — that's what the persistence |
| 22 | + * tests reload against. */ |
| 23 | +const VSCODE_API_SHIM = ` |
| 24 | +window.__e2eOutbox = []; |
| 25 | +window.acquireVsCodeApi = function () { |
| 26 | + return { |
| 27 | + postMessage: function (message) { |
| 28 | + var plain; |
| 29 | + try { plain = JSON.parse(JSON.stringify(message)); } catch (e) { plain = { type: message && message.type }; } |
| 30 | + window.__e2eOutbox.push(plain); |
| 31 | + window.__e2eToHost(plain); |
| 32 | + }, |
| 33 | + getState: function () { |
| 34 | + try { return JSON.parse(sessionStorage.getItem('__wv_state') || 'null'); } catch (e) { return null; } |
| 35 | + }, |
| 36 | + setState: function (state) { |
| 37 | + try { sessionStorage.setItem('__wv_state', JSON.stringify(state)); } catch (e) { /* quota */ } |
| 38 | + return state; |
| 39 | + }, |
| 40 | + }; |
| 41 | +}; |
| 42 | +`; |
| 43 | + |
| 44 | +/** |
| 45 | + * Wires a page to a host and navigates to its HTML. |
| 46 | + * @returns {Promise<{ pageErrors: Error[], consoleErrors: string[], gotoHtml: (html: string) => Promise<void> }>} |
| 47 | + */ |
| 48 | +async function attachPageToHost(page, server, host) { |
| 49 | + const pageErrors = []; |
| 50 | + const consoleErrors = []; |
| 51 | + page.on('pageerror', (err) => pageErrors.push(err)); |
| 52 | + page.on('console', (msg) => { if (msg.type() === 'error') consoleErrors.push(msg.text()); }); |
| 53 | + |
| 54 | + await page.exposeFunction('__e2eToHost', (message) => { host.receive(message); }); |
| 55 | + await page.addInitScript(VSCODE_API_SHIM); |
| 56 | + |
| 57 | + // Serialize host->page delivery: several posts can land in the same tick (details + icons), and |
| 58 | + // the webview's handlers are order-sensitive. |
| 59 | + let chain = Promise.resolve(); |
| 60 | + host.onPost((message) => { |
| 61 | + chain = chain.then(async () => { |
| 62 | + try { |
| 63 | + await page.evaluate((m) => window.postMessage(m, '*'), JSON.parse(JSON.stringify(message))); |
| 64 | + } catch { |
| 65 | + // Page closed or navigating — the real webview drops these too. |
| 66 | + } |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + const gotoHtml = async (html) => { |
| 71 | + await page.goto(server.publish(html), { waitUntil: 'domcontentloaded' }); |
| 72 | + }; |
| 73 | + await gotoHtml(host.html); |
| 74 | + |
| 75 | + return { pageErrors, consoleErrors, gotoHtml, flush: () => chain }; |
| 76 | +} |
| 77 | + |
| 78 | +const test = base.extend({ |
| 79 | + // One server per worker: starting/stopping an http listener per test is pure overhead. |
| 80 | + // eslint-disable-next-line no-empty-pattern -- Playwright requires the fixture argument to be a destructuring pattern, even when nothing is used. |
| 81 | + server: [async ({}, use) => { |
| 82 | + const server = await startHarnessServer(); |
| 83 | + await use(server); |
| 84 | + await server.close(); |
| 85 | + }, { scope: 'worker' }], |
| 86 | + |
| 87 | + /** Opens the object editor. `openObjMod({ config, fixtureDir, fileName })` -> { host, ... }. */ |
| 88 | + openObjMod: async ({ page, server }, use) => { |
| 89 | + const opened = []; |
| 90 | + await use(async (options = {}) => { |
| 91 | + const bundle = path.join(root, 'dist', 'webview', 'objModEditorWebview.js'); |
| 92 | + if (!fs.existsSync(bundle)) { |
| 93 | + throw new Error(`Missing ${path.relative(root, bundle)} — run "npm run compile-web" before the e2e suite.`); |
| 94 | + } |
| 95 | + const host = await createObjModHost({ origin: server.origin, ...options }); |
| 96 | + opened.push(host); |
| 97 | + const wiring = await attachPageToHost(page, server, host); |
| 98 | + const handle = { host, page, ...wiring }; |
| 99 | + // The tree/details panel paint from a reactive effect during bundle evaluation, so by the |
| 100 | + // time #tree has rows the editor is genuinely interactive. |
| 101 | + await page.waitForSelector('#object-editor', { state: 'attached' }); |
| 102 | + return handle; |
| 103 | + }); |
| 104 | + for (const host of opened) host.dispose(); |
| 105 | + }, |
| 106 | + |
| 107 | + /** Opens the editable .w3i map-info editor. */ |
| 108 | + openW3i: async ({ page, server }, use) => { |
| 109 | + const opened = []; |
| 110 | + await use(async (options = {}) => { |
| 111 | + const host = await createW3iHost({ origin: server.origin, ...options }); |
| 112 | + opened.push(host); |
| 113 | + const wiring = await attachPageToHost(page, server, host); |
| 114 | + return { host, page, ...wiring }; |
| 115 | + }); |
| 116 | + for (const host of opened) host.dispose(); |
| 117 | + }, |
| 118 | + |
| 119 | + /** Opens the editable .wpm pathing-map editor. */ |
| 120 | + openWpm: async ({ page, server }, use) => { |
| 121 | + const opened = []; |
| 122 | + await use(async (options = {}) => { |
| 123 | + const host = await createWpmHost({ origin: server.origin, ...options }); |
| 124 | + opened.push(host); |
| 125 | + const wiring = await attachPageToHost(page, server, host); |
| 126 | + return { host, page, ...wiring }; |
| 127 | + }); |
| 128 | + for (const host of opened) host.dispose(); |
| 129 | + }, |
| 130 | +}); |
| 131 | + |
| 132 | +module.exports = { test, expect, root }; |
0 commit comments