// Use only an isolated com.runestone.spelling-test debug window. import { createRequire } from 'node:module'; import { mkdir, writeFile } from 'node:fs/promises'; import assert from 'node:assert/strict'; const { chromium } = createRequire(import.meta.url)(process.env.PLAYWRIGHT_MODULE || 'playwright'); const browser = await chromium.connectOverCDP(process.env.SPELLING_CDP_URL || 'http://127.0.0.1:9237'); const page = browser.contexts()[0].pages()[0]; page.setDefaultTimeout(15000); const captures = []; try { await page.waitForFunction(() => !!window.__TAURI_INTERNALS__); await page.getByRole('button', { name: 'Settings (Ctrl+,)', exact: true }).waitFor(); const identity = await page.evaluate(async () => (await import('/node_modules/@tauri-apps/api/path.js')).appDataDir()); assert.match(identity, /[\\/]com\.runestone\.spelling-test[\\/]?$/); await page.evaluate(async () => { const { listen } = await import('/node_modules/@tauri-apps/api/event.js'); window.nativeMenuPayload = null; await listen('spelling-menu', ({ payload }) => window.nativeMenuPayload = payload); await (await import('/node_modules/@tauri-apps/api/core.js')).invoke('spelling_menu_install'); }); await mkdir('output/todo-menus', { recursive: true }); const menu = page.getByRole('menu', { name: 'Spelling actions', exact: true }); for (const mode of ['dark', 'light']) { await page.evaluate(async mode => { const { saveAppearance } = await import('/src/lib/theme/appearance.ts'); saveAppearance({ theme: 'default', mode }); }, mode); for (const kind of ['input', 'textarea', 'dialog']) { await page.evaluate(kind => { document.getElementById('native-menu-field')?.remove(); document.getElementById('native-menu-dialog')?.remove(); const field = document.createElement(kind === 'input' ? 'input' : 'textarea'); field.id = 'native-menu-field'; field.spellcheck = true; field.value = 'I recieved a letter.'; field.style.cssText = 'position:fixed;left:100px;top:100px;width:500px;height:100px;font:16px/24px Arial;padding:10px;z-index:100'; if (kind === 'dialog') { const dialog = document.createElement('dialog'); dialog.id = 'native-menu-dialog'; dialog.append(field); document.body.append(dialog); dialog.showModal(); } else document.body.append(field); field.focus(); }, kind); const field = page.locator('#native-menu-field'); await field.press('End'); await field.pressSequentially(' TEST'); await field.click({ button: 'right', position: { x: 300, y: kind === 'input' ? 50 : 22 } }); await menu.waitFor(); const payload = await page.evaluate(() => window.nativeMenuPayload); captures.push({ mode, kind, value: await field.inputValue(), payload }); await writeFile('output/todo-menus/native-filtered-items.json', JSON.stringify(captures, null, 2)); const flatten = items => items.flatMap(item => [item, ...flatten(item.children)]); const entries = flatten(payload.items); assert.ok(entries.every(item => item.kind === 3 || !['share', 'writingDirection', 'voiceTyping', 'other'].includes(item.name))); assert.ok(entries.some(item => item.name === 'undo' && item.enabled), mode + ' ' + kind + ' keeps native Undo enabled'); assert.ok(entries.some(item => item.name === 'emoji')); assert.ok(entries.some(item => item.name === 'copy')); assert.equal(await menu.getByRole('menuitem', { name: /More tools|Writing direction|Voice typing|Share/ }).count(), 0); assert.equal(await menu.evaluate(node => getComputedStyle(node).borderColor), 'rgba(0, 0, 0, 0)'); if (kind === 'textarea') await page.screenshot({ path: `output/todo-menus/native-${mode}.png` }); await menu.getByRole('menuitem', { name: 'Undo Ctrl+Z', exact: true }).click(); await page.waitForFunction(() => document.getElementById('native-menu-field').value === 'I recieved a letter.').catch(async error => { console.error('Undo result', await field.inputValue()); throw error; }); await field.click({ button: 'right', position: { x: 48, y: kind === 'input' ? 50 : 22 } }); await menu.getByRole('menuitem', { name: 'received', exact: true }).click(); assert.equal(await field.inputValue(), 'I received a letter.'); await field.press('Control+z'); assert.equal(await field.inputValue(), 'I recieved a letter.'); await field.click({ button: 'right', position: { x: 48, y: kind === 'input' ? 50 : 22 } }); await menu.waitFor(); await page.keyboard.press('Escape'); assert.equal(await field.evaluate(node => document.activeElement === node), true); } } await writeFile('output/todo-menus/native-filtered-items.json', JSON.stringify(captures, null, 2)); console.log('PASS: real WebView2 menus omit unsupported commands, retain editing and spelling, execute native Undo, correct spelling with undo, and restore focus in fields and dialogs in both themes.'); } finally { await page.evaluate(() => { document.getElementById('native-menu-field')?.remove(); document.getElementById('native-menu-dialog')?.remove(); }).catch(() => {}); await browser.close(); }