also looking at this
test: retain bounded browser launch evidence on failure
#70
18 files
+620
-23
| @@ -2,6 +2,8 @@ import assert from 'node:assert/strict' | ||
| 2 | 2 | import { mkdir, readFile } from 'node:fs/promises' |
| 3 | 3 | import { join } from 'node:path' |
| 4 | 4 | import { observeBrowserOperation } from './browser-operation' |
| 5 | import { browserFileErrorCode, retainBrowserLaunchEvidence } from './browser-launch-evidence' | |
| 6 | import type { BrowserLaunchObservation } from './browser-launch-evidence' | |
| 5 | 7 | |
| 6 | 8 | /** Dependency-free CDP, with a fresh profile. Credentials never enter command arguments. */ |
| 7 | 9 | async function openSocket(url: string): Promise<WebSocket> { |
| @@ -19,15 +21,24 @@ export async function startWorkspaceBrowser(root: string) { | ||
| 19 | 21 | await mkdir(profile) |
| 20 | 22 | const child = Bun.spawn([process.env.BROWSE_BROWSER ?? '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', '--headless=new', '--disable-gpu', '--disable-background-timer-throttling', '--disable-renderer-backgrounding', '--disable-backgrounding-occluded-windows', '--disable-features=CalculateNativeWinOcclusion', '--disable-dev-shm-usage', '--disable-extensions', '--disable-component-update', '--no-first-run', '--no-default-browser-check', '--disable-background-networking', '--remote-debugging-port=0', `--user-data-dir=${profile}`, 'about:blank'], { stdout: 'ignore', stderr: Bun.file(join(root, 'chrome.log')) }) |
| 21 | 23 | const started = Date.now() |
| 24 | const began = performance.now() | |
| 22 | 25 | let closing = false |
| 23 | void child.exited.then(code => { if (!closing) console.log(`QA isolated Chrome exited unexpectedly: ${code}, elapsedMs:${Date.now() - started}`) }) | |
| 26 | const launch: BrowserLaunchObservation['launch'] = { pid: child.pid, exitObserved: false, settledExitCode: null, signalCode: null, exitObservedBeforeCleanup: false, cleanupRequested: false, exitElapsedMs: null } | |
| 27 | const portProbe: BrowserLaunchObservation['portProbe'] = { attempts: 0, successfulReads: 0, failedReads: 0, lastReadError: null, discovered: false } | |
| 28 | let pageVisited = false | |
| 29 | const retain = (phase: BrowserLaunchObservation['phase']) => retainBrowserLaunchEvidence(root, { phase, elapsedMs: performance.now() - began, launch: { ...launch }, portProbe: { ...portProbe } }, { startupStderr: !pageVisited, exportDirectory: process.env.WORKSPACE_BROWSER_EVIDENCE ?? process.env.EXECUTION_BROWSER_EVIDENCE }) | |
| 30 | void child.exited.then(code => { | |
| 31 | launch.exitObserved = true; launch.settledExitCode = code; launch.signalCode = child.signalCode; launch.exitObservedBeforeCleanup = !closing; launch.exitElapsedMs = performance.now() - began | |
| 32 | if (!closing) console.log(`QA isolated Chrome exited unexpectedly: ${code}, elapsedMs:${Date.now() - started}`) | |
| 33 | }) | |
| 24 | 34 | let port = '' |
| 25 | 35 | for (let attempt = 0; attempt < 400; attempt++) { |
| 26 | port = await readFile(join(profile, 'DevToolsActivePort'), 'utf8').then(value => value.split('\n')[0]!).catch(() => '') | |
| 27 | if (port) break | |
| 36 | portProbe.attempts++ | |
| 37 | port = await readFile(join(profile, 'DevToolsActivePort'), 'utf8').then(value => { portProbe.successfulReads++; portProbe.lastReadError = null; return value.split('\n')[0]! }).catch(error => { portProbe.failedReads++; portProbe.lastReadError = browserFileErrorCode(error); return '' }) | |
| 38 | if (port) { portProbe.discovered = true; break } | |
| 28 | 39 | await Bun.sleep(50) |
| 29 | 40 | } |
| 30 | if (!port) { closing = true; if (child.exitCode === null) child.kill(); await Promise.race([child.exited, Bun.sleep(2000)]); throw new Error('Fresh browser must start') } | |
| 41 | if (!port) { closing = true; launch.cleanupRequested = true; if (child.exitCode === null) child.kill(); await Promise.race([child.exited, Bun.sleep(2000)]); await retain('startup-unavailable'); throw new Error('Fresh browser must start') } | |
| 31 | 42 | // Keep a browser-level session, as well as page sessions, for the owned lifecycle. |
| 32 | 43 | let browserSocket: WebSocket |
| 33 | 44 | try { |
| @@ -35,13 +46,14 @@ export async function startWorkspaceBrowser(root: string) { | ||
| 35 | 46 | const version = await observeBrowserOperation('version-json', () => response.json()) as { webSocketDebuggerUrl: string } |
| 36 | 47 | browserSocket = await observeBrowserOperation('browser-socket', () => openSocket(version.webSocketDebuggerUrl)) |
| 37 | 48 | } |
| 38 | catch (error) { closing = true; if (child.exitCode === null) child.kill(); await Promise.race([child.exited, Bun.sleep(2000)]); throw error } | |
| 49 | catch (error) { closing = true; launch.cleanupRequested = true; if (child.exitCode === null) child.kill(); await Promise.race([child.exited, Bun.sleep(2000)]); await retain('discovery-failed'); throw error } | |
| 39 | 50 | let browserSequence = 0 |
| 40 | 51 | browserSocket.onmessage = () => {} |
| 41 | 52 | const keepAlive = setInterval(() => { if (browserSocket.readyState === WebSocket.OPEN) browserSocket.send(JSON.stringify({ id: ++browserSequence, method: 'Browser.getVersion' })) }, 5000) |
| 42 | 53 | const pages: Array<{ close: () => void }> = [] |
| 43 | 54 | return { |
| 44 | 55 | async page(url: string, javascript = true) { |
| 56 | pageVisited = true | |
| 45 | 57 | const response = await observeBrowserOperation('target-fetch', () => fetch(`http://127.0.0.1:${port}/json/new?about:blank`, { method: 'PUT', signal: AbortSignal.timeout(5000) })) |
| 46 | 58 | const target = await observeBrowserOperation('target-json', () => response.json()) as { webSocketDebuggerUrl: string } |
| 47 | 59 | const socket = await observeBrowserOperation('page-socket', () => openSocket(target.webSocketDebuggerUrl)) |
| @@ -73,6 +85,7 @@ export async function startWorkspaceBrowser(root: string) { | ||
| 73 | 85 | while (Date.now() < end) { if (await evaluate(expression)) return; await Bun.sleep(50) } |
| 74 | 86 | throw new Error(message) |
| 75 | 87 | }, |
| 88 | async checkLiveness() { phase = 'post-renewal liveness'; assert.equal(await evaluate('1'), 1) }, | |
| 76 | 89 | async fill(selector: string, value: string) { phase = `field ${selector}`; await evaluate(`(() => { const element = document.querySelector(${JSON.stringify(selector)}); if (!element) throw Error('Missing form field'); element.value = ${JSON.stringify(value)}; element.dispatchEvent(new Event('input',{bubbles:true})); element.dispatchEvent(new Event('change',{bubbles:true})); })()`) }, |
| 77 | 90 | async click(text: string) { phase = `action ${text}`; await evaluate(`(() => { const element = [...document.querySelectorAll('button')].find(value => value.textContent.trim() === ${JSON.stringify(text)} && !value.disabled); if (!element) throw Error('Missing enabled action'); element.click(); })()`) }, |
| 78 | 91 | async screenshot(path: string, width = 1280, height = 1000) { await send('Emulation.setDeviceMetricsOverride', { width, height, deviceScaleFactor: 1, mobile: width < 500 }); await Bun.sleep(100); const shot = await send('Page.captureScreenshot', { format: 'png', captureBeyondViewport: false }); await Bun.write(path, Buffer.from(shot.data, 'base64')) }, |
| @@ -83,6 +96,6 @@ export async function startWorkspaceBrowser(root: string) { | ||
| 83 | 96 | await page.wait(javascript ? "document.querySelector('#incident-key') !== null && document.querySelector('[data-workspace-status]')?.textContent.trim() === 'disconnected'" : "document.querySelector('#incident-key') !== null", 'Compiled workspace must initialize') |
| 84 | 97 | return page |
| 85 | 98 | }, |
| 86 | async close() { console.log(`QA browser.close requested, elapsedMs:${Date.now() - started}`); closing = true; clearInterval(keepAlive); browserSocket.close(); for (const page of pages) page.close(); if (child.exitCode === null) child.kill(); await Promise.race([child.exited, Bun.sleep(2000)]) }, | |
| 99 | async close() { console.log(`QA browser.close requested, elapsedMs:${Date.now() - started}`); closing = true; launch.cleanupRequested = true; clearInterval(keepAlive); browserSocket.close(); for (const page of pages) page.close(); if (child.exitCode === null) child.kill(); await Promise.race([child.exited, Bun.sleep(2000)]); await retain('closed') }, | |
| 87 | 100 | } |
| 88 | 101 | } |