import { createRequire } from "node:module"; import assert from "node:assert/strict"; import { mkdir } from "node:fs/promises"; const require = createRequire(import.meta.url); const { chromium } = require(process.env.PLAYWRIGHT_MODULE || "playwright"); const browser = await chromium.launch({ headless: true, ...(process.env.CHROME_PATH ? { executablePath: process.env.CHROME_PATH } : {}) }); const page = await browser.newPage({ viewport: { width: 1440, height: 1000 } }); page.setDefaultTimeout(15000); const errors = []; page.on("pageerror", error => errors.push(error.message)); await mkdir(".audit", { recursive: true }); await page.addInitScript(() => { window.isTauri = true; const root = "C:\\tab-menu-workspace"; const files = new Map(["a.md", "b.md", "c.md", "d.md"].map(name => [name, `# ${name}\n\nOriginal text.\n\n\`\`\`js\nconst answer = 42;\n\`\`\`\n\n| Name | Value |\n| --- | --- |\n| Alpha | One |\n`])); files.set("sample.txt", "const answer = 42;\n"); window.tabMenuTest = { prompts: [], answer: "Cancel", revealed: null, clipboard: null }; Object.defineProperty(navigator, "clipboard", { value: { writeText: async text => { window.tabMenuTest.clipboard = text; } } }); let sequence = 0; const callbacks = new Map(), listeners = new Map(); window.spellingTest = { commands: [], emit: payload => callbacks.get(listeners.get("spelling-menu"))?.({ event: "spelling-menu", id: 1, payload }) }; window.__TAURI_INTERNALS__ = { metadata: { currentWindow: { label: "main" }, currentWebview: { label: "main" } }, transformCallback: callback => { callbacks.set(++sequence, callback); return sequence; }, unregisterCallback: id => callbacks.delete(id), invoke: async (command, args = {}) => { if (command === "plugin:event|listen") { listeners.set(args.event, args.handler); return ++sequence; } if (command === "plugin:event|unlisten") return; if (command.startsWith("spelling_menu_")) { window.spellingTest.commands.push({ method: command, ...args }); return; } if (command === "spelling_suggestions") return { language: "en-US", words: ["Corrected"], misspelled: args.word === "Original" }; if (command === "workspace_last_path") return root; if (command === "workspace_open") return { root, name: "tab-menu-workspace", watch_mode: "native" }; if (command === "workspace_read") { if (!files.has(args.rel)) throw new Error("missing"); return files.get(args.rel); } if (command === "workspace_list") return args.rel ? [] : [{ name: "notes", rel_path: "notes", kind: "folder" }, ...[...files.keys()].map(name => ({ name, rel_path: name, kind: "file" }))]; if (command === "workspace_search") return []; if (command === "workspace_write") throw new Error("Keep unsaved edits for the close test"); if (command === "workspace_rename") { files.set(args.to, files.get(args.from)); files.delete(args.from); return; } if (command === "workspace_abs_path") return `${root}\\${args.rel}`; if (command === "workspace_reveal") { window.tabMenuTest.revealed = args.rel; return; } if (command === "plugin:dialog|message") { window.tabMenuTest.prompts.push(args.message); return window.tabMenuTest.answer; } throw new Error(`Unhandled IPC: ${command}`); }, }; window.__TAURI_EVENT_PLUGIN_INTERNALS__ = { unregisterListener: () => {} }; }); const menu = name => page.getByRole('menu', { name, exact: true }); const item = name => page.getByRole('menuitem', { name, exact: true }); const output = '.audit/editor-surfaces'; await mkdir(output, { recursive: true }); const report = []; const dismiss = async () => { await page.keyboard.press('Escape'); await page.locator('.rs-menu').waitFor({ state: 'detached' }); }; const styles = panel => panel.evaluate(node => { const row = node.querySelector(':scope > button'); const a = getComputedStyle(node), b = getComputedStyle(row); return [a.backgroundColor, a.borderRadius, a.padding, b.minHeight, b.padding, b.font]; }); try { await page.goto(process.env.SMOKE_URL || 'http://127.0.0.1:1420'); await page.locator('[data-tree-rel="a.md"]').click(); await page.locator('.cm-content').waitFor(); for (const theme of ['dark', 'light']) { await page.setViewportSize({ width: 1440, height: 1000 }); await page.evaluate(async mode => { const { saveAppearance } = await import('/src/lib/theme/appearance.ts'); const canvas = document.createElement('canvas'); canvas.width = 800; canvas.height = 600; const c = canvas.getContext('2d'); const g = c.createLinearGradient(0, 0, 800, 600); g.addColorStop(0, '#628ba8'); g.addColorStop(1, '#b47796'); c.fillStyle = g; c.fillRect(0, 0, 800, 600); saveAppearance({ theme: 'default', mode, newTabBackground: { image: canvas.toDataURL(), overlay: .15, position: 'center', size: 'cover' } }); }, theme); await page.locator('[data-tree-rel="a.md"]').click({ button: 'right' }); const reference = await styles(menu('File actions')); await dismiss(); const text = page.getByText('Original text.', { exact: true }); await text.click({ button: 'right', position: { x: 20, y: 8 } }); const writer = menu('Markdown text actions'); assert.deepEqual(await styles(writer), reference); for (const name of ['Add link', 'Add external link', 'Format', 'Paragraph', 'Insert', 'Paste', 'Select all']) assert.equal(await item(name).count(), 1); await item('Corrected').waitFor(); assert.equal(await item('Spelling').count(), 0, 'Corrections appear directly in the main menu'); assert.equal(await writer.locator(':scope > button').first().textContent(), 'Corrected'); await page.screenshot({ path: `${output}/writer-menu-${theme}.png` }); await item('Corrected').focus(); await page.keyboard.press('Enter'); await page.getByText('Corrected text.', { exact: true }).waitFor(); await page.keyboard.press('Control+z'); await text.waitFor(); await page.getByRole('button', { name: 'Files', exact: true }).click(); await page.getByRole('button', { name: 'Hide outline', exact: true }).click(); await page.setViewportSize({ width: 560, height: 620 }); await text.click({ button: 'right', position: { x: 20, y: 8 } }); await item('Corrected').waitFor(); await page.keyboard.press('Home'); assert.equal(await item('Corrected').evaluate(node => node === document.activeElement), true); const spellingBounds = await writer.boundingBox(); assert.ok(spellingBounds.x >= 8 && spellingBounds.x + spellingBounds.width <= 552); await page.screenshot({ path: `${output}/spelling-narrow-${theme}.png` }); await page.keyboard.press('Enter'); await page.getByText('Corrected text.', { exact: true }).waitFor(); await page.keyboard.press('Control+z'); await text.waitFor(); await page.setViewportSize({ width: 1440, height: 1000 }); await page.getByRole('button', { name: 'Files', exact: true }).click(); await page.getByRole('button', { name: 'Show outline', exact: true }).click(); await page.locator('.cm-content').click({ button: 'right', position: { x: 600, y: 150 } }); assert.deepEqual(await styles(writer), reference, 'Blank space uses the same actions'); await item('Format').focus(); await page.keyboard.press('ArrowRight'); await item('Bold').click(); await page.locator('.cm-content').click({ button: 'right', position: { x: 600, y: 150 } }); await page.getByRole('menuitem', { name: 'Undo Ctrl+Z', exact: true }).click(); assert.equal(await page.locator('.cm-content').textContent().then(s => s.includes('**')), false, 'Undo reverses the menu edit'); const tint = await page.locator('.writer-code-line').first().evaluate(node => getComputedStyle(node).backgroundColor); assert.ok(tint.includes('/ 0.82') || /rgba\(.+, 0\.82\)/.test(tint), `Writer code tint: ${tint}`); await page.screenshot({ path: `${output}/writer-code-${theme}.png` }); await page.getByRole('group', { name: 'Document view', exact: true }).hover(); await page.getByRole('button', { name: 'Read', exact: true }).click(); const readingTint = await page.locator('pre.md-code').evaluate(node => getComputedStyle(node).backgroundColor); assert.equal(readingTint, tint, 'Reader and Writer use the same code tint'); await page.screenshot({ path: `${output}/reader-code-${theme}.png` }); await page.getByRole('button', { name: 'Edit', exact: true }).click(); await page.getByRole('group', { name: 'Editor layout', exact: true }).hover(); await page.getByRole('button', { name: 'IDE', exact: true }).click(); await page.locator('.monaco-editor').waitFor(); await page.locator('.monaco-host').click({ button: 'right', position: { x: 500, y: 400 } }); assert.deepEqual(await styles(writer), reference, 'Markdown IDE retains the same menu'); await dismiss(); await page.locator('[data-tree-rel="sample.txt"]').click(); await page.locator('.monaco-host').click({ button: 'right', position: { x: 500, y: 400 } }); const code = menu('Code text actions'); assert.deepEqual(await styles(code), reference, 'Code IDE uses the host menu'); assert.equal(await page.locator('.monaco-menu').count(), 0); await page.screenshot({ path: `${output}/ide-menu-${theme}.png` }); await page.getByRole('menuitem', { name: 'Command palette F1', exact: true }).click(); const palette = page.locator('.quick-input-widget'); await palette.waitFor({ state: 'visible' }); const paletteMetrics = () => palette.evaluate(node => { const row = node.querySelector('.monaco-list-row.focused'); const field = node.querySelector('.monaco-inputbox'); const a = getComputedStyle(row), b = getComputedStyle(field); const probe = document.createElement('div'); probe.style.background = 'var(--rs-item-selection-bg)'; probe.style.color = 'var(--rs-item-selection-border)'; node.append(probe); const expected = getComputedStyle(probe); const result = { fill: a.backgroundColor, expectedFill: expected.backgroundColor, outline: a.outlineColor, expectedOutline: expected.color, radius: a.borderRadius, inputRadius: b.borderRadius, inputOutline: b.outlineColor, inputOutlineWidth: b.outlineWidth }; probe.remove(); return result; }); let p = await paletteMetrics(); assert.equal(p.fill, p.expectedFill); assert.equal(p.outline, p.expectedOutline); assert.equal(p.radius, '5px'); assert.equal(p.inputRadius, '5px'); assert.equal(p.inputOutline, p.expectedOutline); assert.equal(p.inputOutlineWidth, '2px'); await page.keyboard.press('ArrowDown'); p = await paletteMetrics(); assert.equal(p.fill, p.expectedFill); await page.screenshot({ path: `${output}/palette-${theme}.png` }); await page.keyboard.press('Escape'); await page.getByRole('button', { name: 'Files', exact: true }).click(); await page.getByRole('button', { name: 'Hide outline', exact: true }).click(); await page.setViewportSize({ width: 560, height: 620 }); await page.locator('.monaco-host').click({ button: 'right', position: { x: 100, y: 300 } }); await code.waitFor(); const bounds = await code.boundingBox(); assert.ok(bounds.x >= 0 && bounds.x + bounds.width <= 560); await page.keyboard.press('End'); assert.equal(await page.getByRole('menuitem', { name: 'Command palette F1', exact: true }).evaluate(n => n === document.activeElement), true); await page.screenshot({ path: `${output}/ide-narrow-${theme}.png` }); await page.keyboard.press('Enter'); await palette.waitFor({ state: 'visible' }); await page.screenshot({ path: `${output}/palette-narrow-${theme}.png` }); await page.keyboard.press('Escape'); await page.setViewportSize({ width: 1440, height: 1000 }); await page.getByRole('button', { name: 'Files', exact: true }).click(); await page.getByRole('button', { name: 'Show outline', exact: true }).click(); await page.locator('[data-tree-rel="a.md"]').click(); await page.getByRole('group', { name: 'Editor layout', exact: true }).hover(); await page.getByRole('button', { name: 'Writer', exact: true }).click(); report.push({ theme, passed: ['word and blank menu parity', 'spelling correction and undo', 'Writer/IDE parity', 'undo menu action', 'code menu host styles', 'transparent code in Writer/Reader', 'palette selection/focus', 'narrow keyboard menu/palette'], nativeDesktop: 'not tested' }); } assert.deepEqual(errors, []); console.log(JSON.stringify(report, null, 2)); } catch (error) { await page.screenshot({path: `${output}/failure.png`}); console.log(await page.locator(".rs-menu").allTextContents()); throw error; } finally { await browser.close(); }