also looking at this
test: retain bounded browser launch evidence on failure
#70
18 files
+620
-23
| @@ -0,0 +1,59 @@ | ||
| 1 | import { expect, test } from 'bun:test' | |
| 2 | import { Buffer } from 'node:buffer' | |
| 3 | import { mkdtemp, mkdir, readFile, readdir, rm, stat, writeFile } from 'node:fs/promises' | |
| 4 | import { tmpdir } from 'node:os' | |
| 5 | import { join } from 'node:path' | |
| 6 | import { retainBrowserLaunchEvidence } from '../helpers/browser-launch-evidence' | |
| 7 | import type { BrowserLaunchObservation } from '../helpers/browser-launch-evidence' | |
| 8 | ||
| 9 | const observation: BrowserLaunchObservation = { phase: 'startup-unavailable', elapsedMs: 20000, launch: { pid: 12345, exitObserved: true, settledExitCode: 23, signalCode: null, exitObservedBeforeCleanup: true, cleanupRequested: true, exitElapsedMs: 10 }, portProbe: { attempts: 400, successfulReads: 0, failedReads: 400, lastReadError: 'ENOENT', discovered: false } } | |
| 10 | ||
| 11 | test('startup capture retains only a bounded native stderr tail at both evidence destinations', async () => { | |
| 12 | const root = await mkdtemp(join(tmpdir(), 'drone-browser-evidence-')), exported = join(root, 'export') | |
| 13 | try { | |
| 14 | const source = 'unretained-prefix'.repeat(5000) + 'x'.repeat(65500) + 'controlled-tail' | |
| 15 | await writeFile(join(root, 'chrome.log'), source) | |
| 16 | expect(await retainBrowserLaunchEvidence(root, observation, { startupStderr: true, exportDirectory: exported })).toEqual({ local: true, export: true }) | |
| 17 | for (const directory of [root, exported]) { | |
| 18 | const report = JSON.parse(await readFile(join(directory, 'browser-launch.json'), 'utf8')) | |
| 19 | expect(report.stderr.totalBytes).toBe(Buffer.byteLength(source)) | |
| 20 | expect(report.stderr.retainedBytes).toBe(65536) | |
| 21 | expect(report.stderr.truncated).toBe(true) | |
| 22 | const captured = await readFile(join(directory, 'chrome-startup-stderr.log')) | |
| 23 | expect(captured.byteLength).toBe(65536) | |
| 24 | expect(captured.equals(Buffer.from(source).subarray(-65536))).toBe(true) | |
| 25 | expect((await stat(join(directory, 'browser-launch.json'))).mode & 0o777).toBe(0o600) | |
| 26 | } | |
| 27 | expect(await readFile(join(root, 'chrome.log'), 'utf8')).toBe(source) | |
| 28 | } | |
| 29 | finally { await rm(root, { recursive: true, force: true }) } | |
| 30 | }) | |
| 31 | ||
| 32 | test('post-navigation capture withholds private stderr and exports only lifecycle scalars', async () => { | |
| 33 | const root = await mkdtemp(join(tmpdir(), 'drone-browser-evidence-')), exported = join(root, 'export') | |
| 34 | try { | |
| 35 | await writeFile(join(root, 'chrome.log'), 'PRIVATE_FIXTURE_CREDENTIAL_SENTINEL') | |
| 36 | const result = await retainBrowserLaunchEvidence(root, { ...observation, phase: 'closed' }, { startupStderr: false, exportDirectory: exported }) | |
| 37 | expect(result).toEqual({ local: true, export: true }) | |
| 38 | expect(await readdir(exported)).toEqual(['browser-launch.json']) | |
| 39 | const publicText = await readFile(join(exported, 'browser-launch.json'), 'utf8') | |
| 40 | expect(publicText).not.toContain('PRIVATE_FIXTURE_CREDENTIAL_SENTINEL') | |
| 41 | expect(JSON.parse(publicText).stderr.status).toBe('withheld') | |
| 42 | expect(await readFile(join(root, 'chrome.log'), 'utf8')).toBe('PRIVATE_FIXTURE_CREDENTIAL_SENTINEL') | |
| 43 | } | |
| 44 | finally { await rm(root, { recursive: true, force: true }) } | |
| 45 | }) | |
| 46 | ||
| 47 | test('actual refused metadata and export paths report unavailable without replacing the browser failure', async () => { | |
| 48 | const root = await mkdtemp(join(tmpdir(), 'drone-browser-evidence-')), exported = join(root, 'not-a-directory') | |
| 49 | try { | |
| 50 | await writeFile(join(root, 'chrome.log'), 'controlled startup message') | |
| 51 | await mkdir(join(root, 'browser-launch.json')) | |
| 52 | await writeFile(exported, 'owned obstruction') | |
| 53 | const original = new Error('original browser failure') | |
| 54 | const run = async () => { await retainBrowserLaunchEvidence(root, observation, { startupStderr: true, exportDirectory: exported }); throw original } | |
| 55 | await expect(run()).rejects.toBe(original) | |
| 56 | expect(await readFile(exported, 'utf8')).toBe('owned obstruction') | |
| 57 | } | |
| 58 | finally { await rm(root, { recursive: true, force: true }) } | |
| 59 | }) | |