diff --git a/tests/composition/browserScenarioStartupRedaction.test.ts b/tests/composition/browserScenarioStartupRedaction.test.ts index f20bce1141..0dd870dfaa 100644 --- a/tests/composition/browserScenarioStartupRedaction.test.ts +++ b/tests/composition/browserScenarioStartupRedaction.test.ts @@ -3,7 +3,7 @@ import { PlaywrightScenarioSession } from "../../src/browser/PlaywrightScenarioS import { chromium, type BrowserContext } from "playwright-core"; import { expect, it, vi } from "vitest"; import { withVerifierCleanup } from "../../scripts/lib/verifier-cleanup.mjs"; -import { awaitAcknowledgement } from "../fixtures/lifecycleAcknowledgement.js"; +import { controlledCleanup, phaseGate } from "../support/lifecycleHarness.js"; import { captureBrowserScenario } from "../../src/application/BrowserScenarioCaptureService.js"; import { createBrowserScenarioProvider } from "../../src/composition/browserScenario.js"; import { browserScenarioSchema } from "../../src/domain/browserScenario.js"; @@ -13,6 +13,16 @@ import { primaryAnalysisError, } from "../../src/domain/analysisErrorBase.js"; +const startupScenario = ( + secrets: readonly { secret_id: string; environment_variable: string }[], +) => + browserScenarioSchema.parse({ + browser: { mode: "launch", executable_path: process.execPath }, + start_url: { url: "https://app.test/" }, + actions: [{ step_id: "wait", action: "wait_for_timeout", duration_ms: 1 }], + secrets, + }); + it.each([ { late: false, secret: "browser_profile" }, { late: false, secret: "startup private/value" }, @@ -20,46 +30,27 @@ it.each([ ])( "excludes declared startup secrets across opening cleanup and retries: %j", async ({ late, secret }) => { - const scenario = browserScenarioSchema.parse({ - browser: { mode: "launch", executable_path: process.execPath }, - start_url: { url: "https://app.test/" }, - actions: [ - { step_id: "wait", action: "wait_for_timeout", duration_ms: 1 }, - ], - secrets: [ - { secret_id: "credential", environment_variable: "REA_STARTUP_SECRET" }, - ], - }); + const scenario = startupScenario([ + { secret_id: "credential", environment_variable: "REA_STARTUP_SECRET" }, + ]); const diagnosticSecret = encodeURIComponent(secret).replace( /%[0-9A-F]{2}/gu, (escape) => escape.toLowerCase(), ); - let blocked = true; - let closeCalls = 0; + const cleanup = controlledCleanup({ + refused: true, + failure: new Error(`startup cleanup failed ${diagnosticSecret}`), + }); + // An unusable launch reply fails before a PlaywrightScenarioSession exists. const context = { - // An unusable launch reply fails before a PlaywrightScenarioSession exists. browser: () => null, - close: async () => { - closeCalls += 1; - if (blocked) - throw new Error(`startup cleanup failed ${diagnosticSecret}`); - }, + close: cleanup.close, } as unknown as BrowserContext; - let finishOpening: () => void = () => undefined; - const opening = new Promise((resolve) => { - finishOpening = resolve; - }); - let launchStarted: () => void = () => undefined; - const started = new Promise((resolve) => { - launchStarted = resolve; - }); + const opening = phaseGate(); + if (!late) opening.release(context); const launch = vi .spyOn(chromium, "launchPersistentContext") - .mockImplementation(async () => { - launchStarted(); - if (late) await opening; - return context; - }); + .mockImplementation(() => opening.hold()); const provider = createBrowserScenarioProvider({ REA_STARTUP_SECRET: secret, }); @@ -73,7 +64,7 @@ it.each([ capturing = captureBrowserScenario(provider, scenario, { signal: cancellation.signal, }); - await awaitAcknowledgement(started, capturing, "browser launch"); + await opening.reached(capturing, "browser launch"); if (late) cancellation.abort(reason); const result = await capturing; if (result.ok) throw new Error("Expected startup failure"); @@ -92,7 +83,7 @@ it.each([ expect(projectAnalysisError(result.error).code).toBe( "cleanup_incomplete", ); - finishOpening(); + opening.release(context); } let retry: unknown; try { @@ -103,21 +94,26 @@ it.each([ if (!(retry instanceof AnalysisError)) throw new Error("Expected retained startup cleanup failure"); expect(retry.cleanupIncomplete).toBe(true); - const retryProjection = JSON.stringify(projectAnalysisError(retry)); + const projected = projectAnalysisError(retry); + expect(projected).toMatchObject({ + code: "cleanup_incomplete", + details: { resources: expect.arrayContaining(["browser_transport"]) }, + }); + const retryProjection = JSON.stringify(projected); expect(retryProjection).not.toContain(secret); expect(retryProjection).not.toContain(diagnosticSecret); if (secret === "browser_profile") expect(retry.cleanupResources).toContain("[REDACTED:credential]"); - expect(closeCalls).toBeGreaterThan(0); - blocked = false; + expect(cleanup.attempts).toBeGreaterThan(0); + cleanup.allow(); await closeProvider(); expect(result.error.cleanupIncomplete).toBe(false); expect(retry.cleanupIncomplete).toBe(false); expect(result.error.cleanupResources).toEqual([]); }, [ () => { - blocked = false; - finishOpening(); + cleanup.allow(); + opening.release(context); }, async () => { await capturing; @@ -131,16 +127,11 @@ it.each([ it.each([false, true])( "preserves the original startup failure when no effective secret is declared: %s", async (emptyDeclaration) => { - const scenario = browserScenarioSchema.parse({ - browser: { mode: "launch", executable_path: process.execPath }, - start_url: { url: "https://app.test/" }, - actions: [ - { step_id: "wait", action: "wait_for_timeout", duration_ms: 1 }, - ], - secrets: emptyDeclaration + const scenario = startupScenario( + emptyDeclaration ? [{ secret_id: "empty", environment_variable: "REA_EMPTY_SECRET" }] : [], - }); + ); const original = new BrowserObservationError( "capture_browser_scenario", "protocol_error",