// tests/uat/spike-diagnose-offscreen-target.ts — Plan-04-04 debug // session-2 disambiguation helper. // // Single-purpose diagnostic script: launch the harness, prime the // recording via assertA2 (so the offscreen exists with active // MediaRecorder), then enumerate `browser.targets()` and print each // target's `type()` + `url()`. Used to discover why the production // offscreen target predicate in `launch.ts:registerOffscreenConsoleAttach` // fails to match: empirical inspection of what Puppeteer actually // reports for an MV3 offscreen document. // // Operation: `tsx tests/uat/spike-diagnose-offscreen-target.ts`. // Wall-clock: ~5s (no idle, no SW kill). Exits 0 if the offscreen // target is discoverable; 1 if no target with the offscreen URL // is found. import { launchHarnessBrowser } from './lib/launch'; async function main(): Promise { process.stdout.write('\nMokosh debug session-2 — offscreen target diagnostic\n'); process.stdout.write('='.repeat(72) + '\n'); const handles = await launchHarnessBrowser(); process.stdout.write(`Diagnostic: extensionId=${handles.extensionId}\n\n`); // Prime recording (offscreen comes alive here). process.stdout.write('Diagnostic: priming via assertA2 to spawn offscreen\n'); const a2Result = await handles.harnessPage.evaluate(async () => { const harness = ( window as unknown as { __mokoshHarness: { assertA2: () => Promise<{ passed: boolean; error?: string }> }; } ).__mokoshHarness; return harness.assertA2(); }); process.stdout.write(`Diagnostic: assertA2.passed=${a2Result.passed}\n\n`); // Wait a moment for the offscreen target to materialize fully. await new Promise((res) => setTimeout(res, 1_000)); // Enumerate every target. const allTargets = handles.browser.targets(); process.stdout.write(`Diagnostic: browser.targets() count = ${allTargets.length}\n`); process.stdout.write('-'.repeat(72) + '\n'); for (const target of allTargets) { const targetType = target.type(); const targetUrl = target.url(); process.stdout.write(` type=${targetType.padEnd(20)} url=${targetUrl}\n`); } process.stdout.write('-'.repeat(72) + '\n'); // Identify any target that matches the offscreen URL pattern. const offscreenCandidates = allTargets.filter((t) => t.url().includes('/src/offscreen/')); process.stdout.write(`\nDiagnostic: targets matching '/src/offscreen/' = ${offscreenCandidates.length}\n`); for (const candidate of offscreenCandidates) { process.stdout.write( ` CANDIDATE: type=${candidate.type()} url=${candidate.url()}\n`, ); } await handles.browser.close(); return offscreenCandidates.length > 0 ? 0 : 1; } const code = await main(); process.exit(code);