59 lines
4.0 KiB
JavaScript
59 lines
4.0 KiB
JavaScript
import { createRequire } from 'node:module';
|
|
import path from 'node:path';
|
|
import assert from 'node:assert/strict';
|
|
import { writeFile } from 'node:fs/promises';
|
|
const require = createRequire(path.resolve('browser-runtime/package.json'));
|
|
const { chromium } = require('playwright');
|
|
const browser = await chromium.connectOverCDP(process.env.BROWSER_NATIVE_CDP || 'http://127.0.0.1:9224');
|
|
const page = browser.contexts()[0].pages()[0];
|
|
const raw = (command, args = {}) => page.evaluate(({ command, args }) => window.__TAURI_INTERNALS__.invoke(command, args), { command, args });
|
|
const profile = await raw('plugins_snapshot');
|
|
assert.ok(/[\\/]com\.runestone\.(?:browser|spelling)-test[\\/]plugins$/.test(profile.root), 'Native acceptance requires an isolated test profile.');
|
|
const session = await raw('browser_connect');
|
|
const request = (command, args = {}) => raw('browser_request', { command, args, session });
|
|
const initialTabs = (await request('state')).tabs.map(tab => tab.id);
|
|
let tabId;
|
|
try {
|
|
await page.keyboard.press('Control+t');
|
|
await page.getByRole('button', { name: 'Open browser', exact: true }).click();
|
|
const surface = page.getByRole('application', { name: 'Web page', exact: true }).last();
|
|
await page.locator('.browser[data-browser-state="ready"]').last().waitFor();
|
|
tabId = await page.locator('.browser[data-browser-state="ready"]').last().getAttribute('data-browser-tab');
|
|
const address = page.getByRole('textbox', { name: 'Address or search', exact: true }).last();
|
|
await address.fill('https://example.com/'); await address.press('Enter');
|
|
for (let attempt = 0; attempt < 60; attempt++) {
|
|
const snapshot = await request('agentSnapshot', { tabId });
|
|
if (snapshot.text.includes('documentation examples')) break;
|
|
if (attempt === 59) throw Error(`Public test page did not load: ${snapshot.text}`);
|
|
await page.waitForTimeout(250);
|
|
}
|
|
await page.waitForFunction(() => [...document.querySelectorAll('.surface img')].some(image => image.complete && image.naturalWidth > 0));
|
|
const dimensions = await surface.locator('img').evaluate(image => ({ width: image.naturalWidth, height: image.naturalHeight,
|
|
css: image.getBoundingClientRect().width, scale: devicePixelRatio, source: image.src.slice(0, 30) }));
|
|
assert.ok(dimensions.width >= dimensions.css * dimensions.scale - 2, JSON.stringify(dimensions));
|
|
const bounds = await surface.boundingBox();
|
|
await page.mouse.click(bounds.x + 20, bounds.y + 20, { button: 'right' });
|
|
const menu = page.getByRole('menu', { name: 'Browser page actions', exact: true });
|
|
await menu.waitFor();
|
|
assert.equal(await menu.getByRole('menuitem', { name: 'Reload', exact: true }).count(), 1);
|
|
await page.keyboard.press('Escape'); await menu.waitFor({ state: 'detached' });
|
|
await request('edit', { tabId, action: 'selectAll' });
|
|
await page.waitForTimeout(100);
|
|
await request('edit', { tabId, action: 'copy' });
|
|
await page.waitForTimeout(100);
|
|
await address.focus(); await page.keyboard.press('Control+a'); await page.keyboard.press('Control+v');
|
|
assert.match(await address.inputValue(), /documentation examples/);
|
|
await address.fill('https://example.com/');
|
|
await assert.rejects(raw('browser_request', { command: 'edit', args: { tabId, action: 'copy' }, session: 'invalid-session' }), /session/i);
|
|
await assert.rejects(request('contextAction', { tabId, token: 'stale-menu', action: 'saveImage' }), /no longer available/);
|
|
await page.screenshot({ path: 'output/browser-native-acceptance.png' });
|
|
await writeFile('output/browser-native-acceptance.json', JSON.stringify({ dimensions, passed: [
|
|
'Native Tauri-to-Electron page rendering and high-DPI frame delivery', 'Native page context event opens the shared host menu',
|
|
'Native editing commands copy real webpage text', 'Session and stale-menu checks reject invalid requests',
|
|
] }, null, 2));
|
|
console.log('PASS native browser transport, rendering, context menu, editing and session protection');
|
|
} finally {
|
|
if (tabId && !initialTabs.includes(tabId)) { await page.keyboard.press('Escape'); await page.keyboard.press('Control+w'); }
|
|
await browser.close();
|
|
}
|