From 560de528a35ced86c4b719e62091011c43a4e193 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sat, 21 Nov 2026 11:42:37 -0500 Subject: [PATCH] test(web): consolidate scenario deadline and diagnostic checks (#13149) --- .../PlaywrightExecutionBoundary.test.ts | 124 ++++++++---------- src/browser/PlaywrightScenarioSession.test.ts | 62 ++++----- 2 files changed, 80 insertions(+), 106 deletions(-) diff --git a/src/browser/PlaywrightExecutionBoundary.test.ts b/src/browser/PlaywrightExecutionBoundary.test.ts index 741e5a9126..d2acfe3803 100644 --- a/src/browser/PlaywrightExecutionBoundary.test.ts +++ b/src/browser/PlaywrightExecutionBoundary.test.ts @@ -14,33 +14,36 @@ describe("withPlaywrightExecutionBoundary", () => { vi.restoreAllMocks(); }); - it("rejects work that exceeds its absolute remaining duration", async () => { - vi.useFakeTimers(); - const result = withPlaywrightExecutionBoundary( - () => new Promise(() => undefined), - 25, - ); - const rejection = - expect(result).rejects.toBeInstanceOf(AnalysisTimeoutError); - await vi.advanceTimersByTimeAsync(25); - await rejection; - }); - - it("uses the owning policy's timeout error when its deadline expires", async () => { - vi.useFakeTimers(); - const timeout = new AnalysisTimeoutError("capture_browser_scenario", 25, { - userMessage: "Fixed service-worker policy deadline.", - }); - const result = withPlaywrightExecutionBoundary( - () => new Promise(() => undefined), - 25, - undefined, - () => timeout, - ); - const rejection = expect(result).rejects.toBe(timeout); - await vi.advanceTimersByTimeAsync(25); - await rejection; - }); + it.each([false, true])( + "expires at the monotonic deadline despite a wall-clock jump (policy error: %s)", + async (customTimeout) => { + vi.useFakeTimers(); + const timeout = new AnalysisTimeoutError("capture_browser_scenario", 25, { + userMessage: "Fixed service-worker policy deadline.", + }); + const observe = vi.fn(); + const result = withPlaywrightExecutionBoundary( + () => new Promise(() => {}), + 25, + undefined, + customTimeout ? () => timeout : undefined, + ); + void result.then(observe, observe); + vi.setSystemTime(Date.now() - 10_000_000); + await vi.advanceTimersByTimeAsync(24); + expect(observe).not.toHaveBeenCalled(); + await vi.advanceTimersByTimeAsync(1); + expect(observe).toHaveBeenCalledOnce(); + const error: unknown = observe.mock.calls[0]![0]; + if (customTimeout) expect(error).toBe(timeout); + else + expect(error).toMatchObject({ + _tag: "AnalysisTimeoutError", + operation: "capture_browser_scenario", + timeoutMs: 25, + }); + }, + ); it("does not admit work or attach cancellation after synchronous expiry", async () => { vi.spyOn(performance, "now") @@ -76,50 +79,27 @@ describe("withPlaywrightExecutionBoundary", () => { expect(vi.getTimerCount()).toBe(0); }); - it("rejects when the caller cancels active work", async () => { - const controller = new AbortController(); - const result = withPlaywrightExecutionBoundary( - () => new Promise(() => undefined), - 1_000, - controller.signal, - ); - controller.abort(); - await expect(result).rejects.toBeInstanceOf(AnalysisCancelledError); - }); - - it("keeps its timeout stable across a wall-clock jump", async () => { - vi.spyOn(Date, "now") - .mockReturnValueOnce(1_000) - .mockReturnValue(10_000_000); - - await expect( - withPlaywrightExecutionBoundary( - () => - new Promise((resolve) => - setTimeout(() => resolve("done"), 5), - ), + it.each(["before", "during"] as const)( + "keeps typed caller reasons opaque when cancelled %s work", + async (phase) => { + vi.useFakeTimers(); + const caller = new AbortController(); + const reason = new AnalysisTimeoutError("capture_browser_scenario", 123); + if (phase === "before") caller.abort(reason); + const work = vi.fn(() => new Promise(() => {})); + const pending = withPlaywrightExecutionBoundary( + work, 1_000, - ), - ).resolves.toBe("done"); - }); + caller.signal, + ); + if (phase === "during") caller.abort(reason); + const error: unknown = await pending.catch((cause: unknown) => cause); + if (!(error instanceof AnalysisCancelledError)) + throw new Error("Expected typed cancellation"); + expect(error.cause).toBe(reason); + expect(work).toHaveBeenCalledTimes(phase === "before" ? 0 : 1); + expect(getEventListeners(caller.signal, "abort")).toHaveLength(0); + expect(vi.getTimerCount()).toBe(0); + }, + ); }); - -it.each(["before", "during"] as const)( - "keeps typed caller reasons opaque when cancelled %s work", - async (phase) => { - const caller = new AbortController(); - const reason = new AnalysisTimeoutError("capture_browser_scenario", 123); - if (phase === "before") caller.abort(reason); - const pending = withPlaywrightExecutionBoundary( - () => new Promise(() => {}), - undefined, - caller.signal, - ); - if (phase === "during") caller.abort(reason); - const error: unknown = await pending.catch((cause: unknown) => cause); - expect(error).toBeInstanceOf(AnalysisCancelledError); - if (!(error instanceof AnalysisCancelledError)) - throw new Error("Expected typed cancellation"); - expect(error.cause).toBe(reason); - }, -); diff --git a/src/browser/PlaywrightScenarioSession.test.ts b/src/browser/PlaywrightScenarioSession.test.ts index 4b5bf927c4..ccfd5d39c9 100644 --- a/src/browser/PlaywrightScenarioSession.test.ts +++ b/src/browser/PlaywrightScenarioSession.test.ts @@ -11,6 +11,7 @@ import { EventEmitter } from "node:events"; import { beforeEach, expect, it, vi, type Mock } from "vitest"; import type { PlaywrightScenarioStorage } from "./PlaywrightScenarioStorage.js"; +import type { Page } from "playwright-core"; const mocks = vi.hoisted(() => ({ openBrowser: vi.fn(), @@ -1523,10 +1524,7 @@ it("expires the response at the cleanup deadline while retaining the same releas }); it("retains late opening failure cleanup after cancellation until the exact profile lease recovers", async () => { - let profileStarted: () => void = () => {}; - const profileAttempt = new Promise((resolve) => { - profileStarted = resolve; - }); + const { promise: profileAttempt, resolve: profileStarted } = deferred(); let denied = true; const lease = new AnalysisCleanupLease(async () => { profileStarted(); @@ -1561,7 +1559,11 @@ it("retains late opening failure cleanup after cancellation until the exact prof caller.abort(reason); const result = await pending; if (result.ok) throw new Error("Expected cancelled opening"); - expect(result.error.cleanupIncomplete).toBe(true); + expect(result.error).toMatchObject({ + cleanupIncomplete: true, + cleanupPrimary: { _tag: "AnalysisCancelledError" }, + }); + expect(result.error.cleanupPrimary?.cause).toBe(reason); launch.release(); await profileAttempt; await expect(provider.close()).rejects.toMatchObject({ @@ -1676,7 +1678,10 @@ it.each([false, true])( details: { diagnostics: { collected_artifacts: { - dom: { value: { text: "" } }, + dom: { + state: "captured", + value: { text: "" }, + }, }, }, }, @@ -1684,11 +1689,6 @@ it.each([false, true])( expect(result.error).toMatchObject({ reason: "service_worker_blocked", cleanupIncomplete: false, - diagnostics: { - collected_artifacts: { - dom: { state: "captured", value: { text: "" } }, - }, - }, partialObservation: { capture: { browser: { cleanup: "disconnected-external" }, @@ -1759,9 +1759,6 @@ it.each(["absent", "undefined", "inherited", "empty"] as const)( ], }, }); - expect(projected.message).toContain( - "Set environment variable REQUIRED_SECRET", - ); expect(JSON.stringify(projected)).not.toContain( environment.AVAILABLE_SECRET, ); @@ -1791,12 +1788,9 @@ it("keeps declared secrets out of final cleanup and retry failure diagnostics", vi.spyOn(cleanup, "cleanupResources", "get").mockImplementation(() => cleanup.browserClosed ? [] : [`browser_transport:${secret}`], ); - const originalClose = cleanup.close.getMockImplementation()!; - let blocked = true; - cleanup.close.mockImplementation(async (...args) => { - if (blocked) throw new Error(`Close failed after ${secret}`); - return originalClose(...args); - }); + const refusal = new Error(`Close failed after ${secret}`); + const redacted = "[REDACTED:credential]"; + cleanup.close.mockRejectedValueOnce(refusal); const provider = new PlaywrightBrowserScenarioProvider((scenario, options) => PlaywrightScenarioSession.open( scenario, @@ -1815,24 +1809,23 @@ it("keeps declared secrets out of final cleanup and retry failure diagnostics", steps: [{ action: "goto_start" }, { action: "wait_for_timeout" }], }, }); - expect(JSON.stringify(projectAnalysisError(result.error))).not.toContain( - secret, - ); - let retried: unknown; - try { - await provider.close(); - } catch (error) { - retried = error; - } + cleanup.close.mockRejectedValueOnce(refusal); + const retried = await provider.close().catch((cause: unknown) => cause); if (!(retried instanceof ProviderCleanupError)) throw new Error("Expected retained cleanup failure"); - expect(JSON.stringify(projectAnalysisError(retried))).not.toContain(secret); - blocked = false; + const initial = projectAnalysisError(result.error); + expect(initial.message).toContain(`Close failed after ${redacted}`); + for (const projected of [initial, projectAnalysisError(retried)]) { + expect(projected.details).toMatchObject({ + cleanup: "incomplete", + resources: [`browser_transport:${redacted}`], + }); + expect(JSON.stringify(projected)).not.toContain(secret); + } await provider.close(); expect(cleanup.browserClosed).toBe(true); expect(result.error.cleanupIncomplete).toBe(false); } finally { - blocked = false; await provider.close(); } }); @@ -1840,7 +1833,7 @@ it("keeps declared secrets out of final cleanup and retry failure diagnostics", it("projects both request and route cleanup failures with declared secrets redacted", async () => { const { page } = fixture(); const secret = "private-request-diagnostic-value"; - const route = vi.fn(async () => undefined); + const route = vi.fn(); const unroute = vi.fn(async () => { throw new Error(`page.unroute: closed ${secret}`); }); @@ -1876,7 +1869,8 @@ it("projects both request and route cleanup failures with declared secrets redac expect(step?.error).toContain("page.evaluate: closed"); expect(step?.error).toContain("page.unroute: closed"); expect(JSON.stringify(result.value)).not.toContain(secret); - expect(unroute).toHaveBeenCalledOnce(); + expect(route).toHaveBeenCalledOnce(); + expect(unroute).toHaveBeenCalledExactlyOnceWith(...route.mock.calls[0]!); } finally { await provider.close(); }