orbit/dev/cdp-chrome.mjs

42 lines
1.4 KiB
JavaScript

// Run a saves-ui-test page in headless Chrome (CDP) and print the result.
// node dev/cdp-chrome.mjs <url> [timeoutMs=240000]
import { launch, CDP } from './cdpshot.mjs';
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const [url, timeoutMs = '240000'] = process.argv.slice(2);
const t0 = Date.now();
const { url: wsUrl, kill } = await launch(url);
const cdp = new CDP(wsUrl);
try {
await cdp.open();
await cdp.send('Runtime.enable');
await cdp.send('Page.enable');
let out = null;
const poll = async () => {
try {
const r = await cdp.send('Runtime.evaluate', { expression: 'window.__SAVES_UI__', returnByValue: true });
if (r.result && r.result.value) out = r.result.value;
} catch { /* page still booting */ }
};
while (Date.now() - t0 < Number(timeoutMs)) {
await poll();
if (out) break;
await sleep(500);
}
if (!out) {
// Dump captured console errors to help diagnose.
try {
const r = await cdp.send('Runtime.evaluate', { expression: 'JSON.stringify({errors: window.__CAPTURED_ERRORS__, logs: (window.__CAPTURED_LOGS__||[]).slice(-30)})', returnByValue: true });
console.log('PAGE STATE:\n', r.result && r.result.value);
} catch {}
console.log('TIMEOUT — __SAVES_UI__ never set');
process.exitCode = 2;
} else {
console.log(JSON.stringify(out, null, 2));
if (!out.pass) process.exitCode = 1;
}
} finally {
cdp.close();
kill();
}