import { createRequire } from 'node:module'; import { mkdir } 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.launch({ headless: true, ...(process.env.CHROME_PATH ? { executablePath: process.env.CHROME_PATH } : {}) }); const page = await browser.newPage({ viewport: { width: 1100, height: 850 } }); const errors = []; page.on('pageerror', error => errors.push(error.message)); await page.addInitScript(() => { window.isTauri = true; const snapshot = { profiles: [{ id: 'user', name: 'Personal' }, { id: 'work', name: 'Work' }], tabs: [], bookmarks: [], settings: { search: 'duckduckgo', adblock: true, linkDestination: 'internal', personalRules: '', homepage: '', filterLists: [] }, }; window.browserMenuTest = { snapshot, calls: [] }; let sequence = 0; window.__TAURI_INTERNALS__ = { metadata: { currentWindow: { label: 'settings-main' }, currentWebview: { label: 'settings-main' } }, transformCallback: () => ++sequence, unregisterCallback: () => {}, invoke: async (command, args = {}) => { if (command === 'plugin:event|listen') return args.handler; if (command === 'plugin:event|unlisten') return; if (command === 'browser_connect') return 'menu-session'; if (command === 'browser_stop' || command === 'plugins_release') return; if (command === 'plugins_snapshot') return { root: 'test', plugins: [] }; if (command.startsWith('plugin:window|')) return false; if (command !== 'browser_request') throw Error('Unhandled IPC: ' + command); const details = args.args || {}; window.browserMenuTest.calls.push({ command: args.command, ...details }); if (args.command === 'webData') return { profileId: details.profileId, cookies: [], history: [] }; if (args.command === 'settings') Object.assign(snapshot.settings, details); return structuredClone(snapshot); }, }; window.__TAURI_EVENT_PLUGIN_INTERNALS__ = { unregisterListener: () => {} }; }); const menu = name => page.getByRole('menu', { name, exact: true }); const trigger = name => page.getByRole('button', { name, exact: true }); try { await mkdir('output/todo-menus', { recursive: true }); await page.goto((process.env.SMOKE_URL || 'http://127.0.0.1:1420') + '/?settingsWindow=true'); await trigger('Browser').click(); await trigger('Search engine').waitFor(); 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); await trigger('Search engine').focus(); await page.keyboard.press('Enter'); const engines = menu('Search engine'); await engines.waitFor(); assert.equal(await engines.getByRole('menuitemradio', { name: 'DuckDuckGo' }).getAttribute('aria-checked'), 'true'); assert.equal(await engines.evaluate(node => getComputedStyle(node).borderColor), 'rgba(0, 0, 0, 0)'); await page.screenshot({ path: `output/todo-menus/search-${mode}.png` }); await page.keyboard.press('End'); await page.keyboard.press('Space'); assert.equal(await trigger('Search engine').innerText(), 'Bing'); assert.equal(await trigger('Search engine').evaluate(node => node === document.activeElement), true); await trigger('Search engine').click(); await page.keyboard.press('Home'); await page.keyboard.press('Enter'); await trigger('Profile').focus(); await page.keyboard.press('Space'); await menu('Profile').waitFor(); await page.screenshot({ path: `output/todo-menus/profile-${mode}.png` }); await page.keyboard.press('ArrowDown'); await page.keyboard.press('Enter'); await page.waitForFunction(() => document.querySelector('button[aria-label="Profile"]')?.textContent === 'Work'); await page.waitForFunction(() => window.browserMenuTest.calls.some(call => call.command === 'webData' && call.profileId === 'work')); await trigger('Profile').click(); await page.keyboard.press('Home'); await page.keyboard.press('Enter'); await page.setViewportSize({ width: 520, height: 700 }); await trigger('Search engine').click(); const bounds = await engines.boundingBox(); assert.ok(bounds.x >= 8 && bounds.x + bounds.width <= 512); await page.screenshot({ path: `output/todo-menus/narrow-${mode}.png` }); await page.keyboard.press('Escape'); assert.equal(await trigger('Search engine').evaluate(node => node === document.activeElement), true); await page.setViewportSize({ width: 1100, height: 850 }); } await trigger('Search engine').click(); await menu('Search engine').getByRole('menuitemradio', { name: 'Google', exact: true }).click(); assert.equal(await page.evaluate(() => window.browserMenuTest.calls.filter(call => call.command === 'settings').length), 0, 'A choice does not submit the form'); await trigger('Save browser settings').click(); await page.waitForFunction(() => window.browserMenuTest.snapshot.settings.search === 'google'); await page.evaluate(async () => { const { state } = await import('/src/lib/browser/client.ts'); const snapshot = window.browserMenuTest.snapshot; snapshot.profiles.push(...Array.from({ length: 60 }, (_, i) => ({ id: 'p' + i, name: 'Profile ' + i }))); state.set(structuredClone(snapshot)); }); await page.setViewportSize({ width: 520, height: 700 }); await trigger('Profile').click(); const profiles = menu('Profile'); assert.equal(await profiles.evaluate(node => node.scrollHeight > node.clientHeight), true, 'Long profile lists scroll'); await page.keyboard.press('End'); assert.equal(await profiles.getByRole('menuitemradio', { name: 'Profile 59', exact: true }).evaluate(node => node === document.activeElement), true); await page.screenshot({ path: 'output/todo-menus/profile-long-narrow.png' }); await page.keyboard.press('Escape'); assert.equal(await trigger('Profile').evaluate(node => node === document.activeElement), true); await trigger('Profile').click(); await trigger('Profile').evaluate(node => { let parent = node.parentElement; while (parent && parent.scrollHeight <= parent.clientHeight) parent = parent.parentElement; if (!parent) throw Error('Settings need a scroll area'); parent.scrollTop -= 40; }); await profiles.waitFor({ state: 'detached' }); await trigger('Search engine').click(); await trigger('General').click(); assert.equal(await page.locator('.rs-menu').count(), 0, 'Leaving Browser closes the menu'); assert.deepEqual(errors, []); console.log('PASS: browser choices, persistence, keyboard selection and dismissal, long profiles, both themes, narrow layout, and cleanup.'); } finally { await browser.close(); }