also looking at this
test: retain bounded browser launch evidence on failure
#70
18 files
+620
-23
| @@ -0,0 +1,48 @@ | ||
| 1 | import { expect, test } from 'bun:test' | |
| 2 | import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' | |
| 3 | import { tmpdir } from 'node:os' | |
| 4 | import { join } from 'node:path' | |
| 5 | import process from 'node:process' | |
| 6 | import { startWorkspaceBrowser } from '../helpers/workspace-browser' | |
| 7 | ||
| 8 | test.skipIf(process.platform === 'win32')('failed native launch retains startup stderr, actual exit and port observations at the original discovery bound', async () => { | |
| 9 | const supplied = process.env.BROWSER_LAUNCH_EVIDENCE | |
| 10 | const root = supplied ? (await mkdir(supplied), supplied) : await mkdtemp(join(tmpdir(), 'drone-browser-launch-')) | |
| 11 | const executable = join(root, 'launch.ts'), pidPath = join(root, 'launch.pid') | |
| 12 | let passed = false | |
| 13 | const original = process.env.BROWSE_BROWSER | |
| 14 | try { | |
| 15 | await writeFile(executable, `#!${process.execPath}\nimport { writeFileSync } from 'node:fs'\nwriteFileSync(${JSON.stringify(pidPath)}, String(process.pid))\nconsole.error('controlled native browser startup refusal')\nprocess.exit(23)\n`, { mode: 0o700 }) | |
| 16 | process.env.BROWSE_BROWSER = executable | |
| 17 | await expect(startWorkspaceBrowser(root)).rejects.toThrow('Fresh browser must start') | |
| 18 | const actualPid = Number(await readFile(pidPath, 'utf8')) | |
| 19 | const report = JSON.parse(await readFile(join(root, 'browser-launch.json'), 'utf8')) | |
| 20 | expect(report.phase).toBe('startup-unavailable') | |
| 21 | expect(report.launch.pid).toBe(actualPid) | |
| 22 | expect(report.launch.exitObserved).toBe(true) | |
| 23 | expect(report.launch.settledExitCode).toBe(23) | |
| 24 | expect(report.launch.exitObservedBeforeCleanup).toBe(true) | |
| 25 | expect(report.launch.cleanupRequested).toBe(true) | |
| 26 | expect(report.portProbe.attempts).toBe(400) | |
| 27 | expect(report.portProbe.lastReadError).toBe('ENOENT') | |
| 28 | expect(report.stderr.status).toBe('retained') | |
| 29 | expect(report.stderr.retainedBytes).toBeLessThanOrEqual(65536) | |
| 30 | expect(await readFile(join(root, 'chrome-startup-stderr.log'), 'utf8')).toContain('controlled native browser startup refusal') | |
| 31 | expect(() => process.kill(actualPid, 0)).toThrow() | |
| 32 | passed = true | |
| 33 | } | |
| 34 | finally { | |
| 35 | if (original === undefined) delete process.env.BROWSE_BROWSER | |
| 36 | else process.env.BROWSE_BROWSER = original | |
| 37 | // The owned native program exits23 before discovery completes; never delete a live fixture. | |
| 38 | const actualPid = Number(await readFile(pidPath, 'utf8').catch(() => '0')) | |
| 39 | if (actualPid > 1) { | |
| 40 | let gone = false | |
| 41 | try { process.kill(actualPid, 0) } | |
| 42 | catch (error) { gone = (error as NodeJS.ErrnoException).code === 'ESRCH' } | |
| 43 | expect(gone).toBe(true) | |
| 44 | } | |
| 45 | if (passed && !supplied) await rm(root, { recursive: true, force: true }) | |
| 46 | else console.log(JSON.stringify({ evidence: 'controlled-native-launch', retainedRoot: root, passed })) | |
| 47 | } | |
| 48 | }, 30000) | |