import { nativeRequest } from './native-support.mjs'; import { chromium } from 'playwright'; import { spawn, execFileSync } from 'node:child_process'; import { mkdir } from 'node:fs/promises'; import { resolve, basename, dirname } from 'node:path'; import net from 'node:net'; import assert from 'node:assert/strict'; const host = resolve(process.env.RUNESTONE_ROOT || '..'); const portServer = net.createServer(); await new Promise(resolve => portServer.listen(0, '127.0.0.1', resolve)); const port = portServer.address().port; await new Promise(resolve => portServer.close(resolve)); const native = spawn(resolve(host, 'src-tauri/target/debug/runestone.exe'), [], { windowsHide: true, stdio: 'ignore', env: { ...process.env, WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS: `--remote-debugging-port=${port}` } }); let browser, page; try { for (let attempt = 0; attempt < 60; attempt++) { try { browser = await chromium.connectOverCDP(`http://127.0.0.1:${port}`); break; } catch { await new Promise(resolve => setTimeout(resolve, 500)); } } if (!browser) throw Error('The isolated host did not start.'); page = browser.contexts()[0].pages()[0]; await page.waitForFunction(() => !!window.__TAURI_INTERNALS__?.invoke); const invoke = (command, args = {}) => nativeRequest(page, command, args); const snapshot = await invoke('plugins_snapshot'); assert.equal(basename(snapshot.root), 'plugins'); assert.equal(basename(dirname(snapshot.root)), 'com.runestone.browser-test', 'The test must use its isolated profile.'); await page.evaluate(() => window.dispatchEvent(new Event('focus'))); await page.getByRole('button', { name: 'Open browser', exact: true }).click(); await page.locator('[data-browser-state="ready"]').waitFor(); const address = page.getByRole('textbox', { name: 'Address or search', exact: true }); const fixture = '
Rich text
'; await address.fill(`https://httpbin.org/base64/${Buffer.from(fixture).toString('base64')}`); await address.press('Enter'); await page.waitForTimeout(2500); await mkdir(resolve(host, '.audit/browser-fields-native'), { recursive: true }); await page.screenshot({ path: resolve(host, '.audit/browser-fields-native/before-input.png') }); const surface = page.getByRole('application', { name: 'Web page', exact: true }); const box = await surface.boundingBox(); await page.mouse.click(box.x + 100, box.y + 60); const expectCaret = () => page.waitForFunction(() => { const image = document.querySelector('.surface img'); if (!image?.complete || !image.naturalWidth) return false; const ratio = image.naturalWidth / document.querySelector('.surface').clientWidth; const canvas = document.createElement('canvas'); canvas.width = 12; canvas.height = 28; const context = canvas.getContext('2d'); context.drawImage(image, 42 * ratio, 46 * ratio, 12 * ratio, 28 * ratio, 0, 0, 12, 28); const pixels = context.getImageData(0, 0, 12, 28).data; let dark = 0; for (let offset = 0; offset < pixels.length; offset += 4) if (pixels[offset] < 100 && pixels[offset + 1] < 100 && pixels[offset + 2] < 100) dark++; return dark >= 8; }, null, { timeout: 5000, polling: 50 }); await expectCaret(); await page.screenshot({ path: resolve(host, '.audit/browser-fields-native/visible-caret.png') }); const clipboard = () => execFileSync('powershell.exe', ['-NoProfile', '-Command', 'Get-Clipboard -Raw'], { encoding: 'utf8', windowsHide: true }).trim(); for (const [name, y, value] of [['input', 60, 'RuneStone input'], ['textarea', 170, 'RuneStone textarea'], ['contenteditable', 310, 'RuneStone rich text']]) { await page.mouse.click(box.x + 100, box.y + y); const focused = await page.evaluate(() => document.activeElement?.getAttribute('aria-label')); assert.equal(focused, 'Web page text input', 'The native host keeps its input bridge focused.'); await page.keyboard.press('Control+a'); await page.keyboard.type(value); await page.waitForTimeout(800); await page.keyboard.press('Control+a'); await page.keyboard.press('Control+c'); await page.waitForTimeout(300); await page.screenshot({ path: resolve(host, `.audit/browser-fields-native/${name}.png`) }); assert.equal(clipboard(), value, `The native ${name} accepts text and selection.`); console.log(`PASS: native ${name} accepts focus, typing and selection`); } for (const mode of ['dark', 'light']) { await page.evaluate(mode => { localStorage.setItem('runestone.appearance', JSON.stringify({ theme: 'default', mode })); window.dispatchEvent(new StorageEvent('storage', { key: 'runestone.appearance' })); }, mode); await page.waitForFunction(mode => document.documentElement.dataset.theme === mode, mode); await page.setViewportSize({ width: 880, height: 650 }); await page.waitForTimeout(500); const narrowBox = await surface.boundingBox(); await page.mouse.click(narrowBox.x + 60, narrowBox.y + 60); await page.keyboard.press('Control+a'); await page.keyboard.press('Backspace'); await expectCaret(); await page.screenshot(); await page.waitForTimeout(100); await page.screenshot({ path: resolve(host, `.audit/browser-fields-native/caret-${mode}-880.png`) }); } console.log('PASS: native caret pixels in dark and light appearances at 880px'); } finally { if (page) await nativeRequest(page, 'browser_stop').catch(() => {}); await browser?.close(); native.kill(); }