Files
RuneStone/browser-runtime/scripts/test-idle.mjs
T

89 lines
4.8 KiB
JavaScript

import { nativeRequest } from './native-support.mjs';
import { chromium } from 'playwright';
import { spawn } from 'node:child_process';
import { cp, mkdir, writeFile } from 'node:fs/promises';
import { resolve, basename, dirname } from 'node:path';
import net from 'node:net';
import assert from 'node:assert/strict';
const listener = net.createServer();
await new Promise(resolve => listener.listen(0, '127.0.0.1', resolve));
const port = listener.address().port;
await new Promise(resolve => listener.close(resolve));
const native = spawn(resolve('../src-tauri/target/debug/runestone.exe'), [], {
windowsHide: true, stdio: 'ignore',
env: { ...process.env, WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS: `--remote-debugging-port=${port}` },
});
let browser, page;
const passed = [];
const idle = () => new Promise(resolve => setTimeout(resolve, 2200));
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)); }
}
assert.ok(browser, 'The isolated test window must start.');
page = browser.contexts()[0].pages()[0];
await page.waitForFunction(() => !!window.__TAURI_INTERNALS__?.invoke);
const invoke = (command, args = {}) => nativeRequest(page, command, args);
let snapshot = await invoke('plugins_snapshot');
assert.equal(basename(dirname(snapshot.root)), 'com.runestone.browser-test');
await page.evaluate(() => window.dispatchEvent(new Event('focus')));
await page.getByRole('button', { name: 'Open browser', exact: true }).click();
const current = () => page.locator('.browser:visible');
const ready = () => page.waitForFunction(() => {
const view = [...document.querySelectorAll('.browser')].find(view => view.getBoundingClientRect().width > 0);
return view?.dataset.browserState === 'ready';
});
await ready();
const original = await current().getAttribute('data-browser-tab');
await idle();
const afterIdle = await invoke('browser_request', { command: 'state', args: {} });
assert.ok(afterIdle.tabs.some(tab => tab.id === original), `An idle connection must keep its original tab: ${JSON.stringify({ original, tabs: afterIdle.tabs, diagnostics: await page.locator('.diagnostics').allTextContents() })}`);
passed.push('the idle connection stays open beyond the authentication timeout');
console.log(`PASS ${passed.at(-1)}`);
for (let index = 0; index < 2; index++) {
await current().getByRole('button', { name: 'New browser tab', exact: true }).click();
await ready();
}
const ids = await page.locator('.browser').evaluateAll(views => views.map(view => view.dataset.browserTab).sort());
assert.equal(ids.length, 3);
for (let index = 0; index < 9; index++) {
await page.locator('.plugin-tab [role="tab"], [role="tab"][data-path^="plugin:"]').nth(index % 3).click();
await ready();
await idle();
const state = await invoke('browser_request', { command: 'state', args: {} });
assert.deepEqual(state.tabs.map(tab => tab.id).sort(), ids, 'Tab switches must not restart the browser.');
assert.equal(await page.getByRole('button', { name: 'Reconnect', exact: true }).count(), 0);
}
passed.push('nine tab switches and idle periods preserve all three runtime tabs');
console.log(`PASS ${passed.at(-1)}`);
const address = current().getByRole('textbox', { name: 'Address or search', exact: true });
await address.fill('https://example.com/');
await address.press('Enter');
await page.waitForFunction(() => [...document.querySelectorAll('.browser input')].some(input => input.value === 'https://example.com/' && !input.disabled));
await current().getByRole('button', { name: 'Bookmark this page', exact: true }).click();
await page.getByRole('button', { name: 'Bookmarks', exact: true }).click();
const bookmark = page.locator('.bookmarks .link[title="https://example.com/"]').first();
await bookmark.waitFor();
await idle();
await bookmark.click();
await ready();
await idle();
const finalState = await invoke('browser_request', { command: 'state', args: {} });
assert.equal(finalState.tabs.length, 4);
assert.ok(ids.every(id => finalState.tabs.some(tab => tab.id === id)));
const opened = await current().getAttribute('data-browser-tab');
assert.equal(finalState.tabs.find(tab => tab.id === opened)?.url, 'https://example.com/');
passed.push('a bookmark opens after idle without replacing existing tabs');
console.log(`PASS ${passed.at(-1)}`);
await mkdir('output', { recursive: true });
await page.screenshot({ path: 'output/browser-idle.png' });
await writeFile('output/browser-idle.json', JSON.stringify({ passed }, null, 2));
} catch (error) {
if (page) console.error(await page.locator('.diagnostics').allTextContents());
throw error;
} finally { await browser?.close().catch(() => {}); native.kill(); }