test(web): consolidate browser opening deadlines and late release checks (#13158)

This commit is contained in:
morluto authored and GitHub committed 2026-11-21 17:12:52 +00:00
1 parent 6961a8c47c
commit 52437cd431
2 files changed
+270 -289

No files matched your search

+270 -285
View File
@@ -349,102 +349,73 @@ it("keeps attached-target ownership after the cleanup response timeout", async (
}
});
it("does not configure a late attached connection after cancellation", async () => {
const connection = phaseGate<
void,
Pick<Browser, "contexts" | "close" | "isConnected" | "version">
>();
const connectedBrowser = {
close: vi.fn(async () => undefined),
contexts: vi.fn(() => []),
isConnected: vi.fn(() => true),
version: () => "fixture",
};
const controller = new AbortController();
const launcher = {
connectOverCDP: () => connection.hold(),
launchPersistentContext: async () => {
throw new Error("Unexpected launch");
},
};
const scenario = browserScenarioSchema.parse({
browser: {
mode: "connect",
cdp_endpoint: "http://127.0.0.1:9222",
target_id: "page-1",
},
start_url: { url: "https://example.test" },
actions: [{ step_id: "wait", action: "wait_for_timeout", duration_ms: 1 }],
});
const opening = openPlaywrightScenarioBrowser(
scenario,
{},
{
launcher,
signal: controller.signal,
},
);
try {
await connection.reached(opening, "CDP connection");
controller.abort();
connection.release(connectedBrowser);
await expect(opening).rejects.toMatchObject({
_tag: "BrowserObservationError",
reason: "cancelled",
it.each(["handshake", "target discovery"] as const)(
"closes the attached browser on cancellation during $0",
async (stage) => {
const caller = new AbortController();
const reason = new Error(`cancel during ${stage}`);
const handshake = phaseGate();
const browser = {
close: vi.fn(async () => undefined),
contexts: vi.fn(() => {
caller.abort(reason);
return [];
}),
isConnected: () => true,
version: () => "fixture",
};
const scenario = browserScenarioSchema.parse({
browser: {
mode: "connect",
cdp_endpoint: "http://127.0.0.1:9222",
target_id: "page-1",
},
start_url: { url: "https://example.test" },
actions: scenarioActions,
});
expect(connectedBrowser.contexts).not.toHaveBeenCalled();
expect(connectedBrowser.close).toHaveBeenCalledOnce();
} finally {
controller.abort();
connection.release(connectedBrowser);
const opened = await opening.catch(() => undefined);
await opened?.cleanup.close();
}
});
it("closes an attached connection when cancellation starts target discovery", async () => {
const controller = new AbortController();
const connectedBrowser = {
close: vi.fn(async () => undefined),
contexts: vi.fn(() => {
controller.abort();
return [];
}),
isConnected: vi.fn(() => true),
version: () => "fixture",
};
const launcher = {
connectOverCDP: async () => connectedBrowser,
launchPersistentContext: async () => {
throw new Error("Unexpected launch");
},
};
const scenario = browserScenarioSchema.parse({
browser: {
mode: "connect",
cdp_endpoint: "http://127.0.0.1:9222",
target_id: "page-1",
},
start_url: { url: "https://example.test" },
actions: [{ step_id: "wait", action: "wait_for_timeout", duration_ms: 1 }],
});
await expect(
openPlaywrightScenarioBrowser(
const opening = openPlaywrightScenarioBrowser(
scenario,
{},
{
launcher,
signal: controller.signal,
signal: caller.signal,
launcher: {
connectOverCDP: async () => {
if (stage === "handshake") await handshake.hold();
return browser;
},
launchPersistentContext: async () => {
throw new Error("Unexpected launch");
},
},
},
),
).rejects.toMatchObject({
_tag: "AnalysisCancelledError",
});
expect(connectedBrowser.contexts).toHaveBeenCalledOnce();
expect(connectedBrowser.close).toHaveBeenCalledOnce();
});
);
try {
if (stage === "handshake") {
await handshake.reached(opening, "browser handshake");
caller.abort(reason);
handshake.release();
}
await expect(opening).rejects.toMatchObject(
stage === "handshake"
? {
_tag: "BrowserObservationError",
reason: "cancelled",
cause: reason,
}
: { _tag: "AnalysisCancelledError", cause: reason },
);
expect(browser.contexts).toHaveBeenCalledTimes(
stage === "handshake" ? 0 : 1,
);
expect(browser.close).toHaveBeenCalledOnce();
} finally {
caller.abort(reason);
handshake.release();
const opened = await opening.catch(() => undefined);
await opened?.cleanup.close();
}
},
);
const attachedBrowserFixture = (
callbacks: {
@@ -879,51 +850,54 @@ it("cancels same-target queued connection before acquiring a browser", async ()
environment,
{ launcher },
);
await firstConnection.reached(first, "first CDP connection");
expect(connectOverCDP).toHaveBeenCalledTimes(1);
const firstRefusal = new Error("first connection failed");
try {
await firstConnection.reached(first, "first CDP connection");
expect(connectOverCDP).toHaveBeenCalledTimes(1);
const controller = new AbortController();
const removeAbort = vi.spyOn(controller.signal, "removeEventListener");
const queued = openPlaywrightScenarioBrowser(
makeScenario("page-1"),
environment,
{ launcher, signal: controller.signal },
);
controller.abort();
await expect(queued).rejects.toMatchObject({
_tag: "BrowserObservationError",
reason: "cancelled",
});
expect(connectOverCDP).toHaveBeenCalledTimes(1);
expect(removeAbort).toHaveBeenCalledWith("abort", expect.any(Function));
const controller = new AbortController();
const removeAbort = vi.spyOn(controller.signal, "removeEventListener");
const queued = openPlaywrightScenarioBrowser(
makeScenario("page-1"),
environment,
{ launcher, signal: controller.signal },
);
const reason = new Error("cancel queued attach");
controller.abort(reason);
await expect(queued).rejects.toMatchObject({
_tag: "BrowserObservationError",
reason: "cancelled",
cause: reason,
});
expect(connectOverCDP).toHaveBeenCalledTimes(1);
expect(removeAbort).toHaveBeenCalledWith("abort", expect.any(Function));
await expect(
openPlaywrightScenarioBrowser(makeScenario("page-2"), environment, {
launcher,
}),
).rejects.toThrow("connection failed");
expect(connectOverCDP).toHaveBeenCalledTimes(2);
await expect(
openPlaywrightScenarioBrowser(makeScenario("page-2"), environment, {
launcher,
}),
).rejects.toThrow("connection failed");
expect(connectOverCDP).toHaveBeenCalledTimes(2);
const launch = browserScenarioSchema.parse({
browser: { mode: "launch", executable_path: process.execPath },
start_url: { url: "https://example.test" },
actions: [{ step_id: "wait", action: "wait_for_timeout", duration_ms: 1 }],
});
await expect(
openPlaywrightScenarioBrowser(launch, environment, { launcher }),
).rejects.toMatchObject({
diagnostics: { startup_error: "launch boundary reached" },
});
expect(launchPersistentContext).toHaveBeenCalledTimes(1);
await expect(
openPlaywrightScenarioBrowser(launchScenario, environment, { launcher }),
).rejects.toMatchObject({
diagnostics: { startup_error: "launch boundary reached" },
});
expect(launchPersistentContext).toHaveBeenCalledTimes(1);
firstConnection.reject(new Error("first connection failed"));
await expect(first).rejects.toThrow("first connection failed");
await expect(
openPlaywrightScenarioBrowser(makeScenario("page-1"), environment, {
launcher,
}),
).rejects.toThrow("connection failed");
expect(connectOverCDP).toHaveBeenCalledTimes(3);
firstConnection.reject(firstRefusal);
await expect(first).rejects.toThrow("first connection failed");
await expect(
openPlaywrightScenarioBrowser(makeScenario("page-1"), environment, {
launcher,
}),
).rejects.toThrow("connection failed");
expect(connectOverCDP).toHaveBeenCalledTimes(3);
} finally {
firstConnection.reject(firstRefusal);
await first.catch(() => undefined);
}
});
it("serializes concurrent attach across loopback spellings of one browser", async () => {
@@ -1131,40 +1105,46 @@ it.each([false, true])(
it("bounds an opening failure response while retaining the same late cleanup lease", async () => {
vi.useFakeTimers();
let acknowledge: () => void = () => {};
const late = new Promise<void>((resolve) => {
acknowledge = resolve;
});
const lease = new AnalysisCleanupLease(() => late, ["browser_profile"]);
const reason = { caller: "cancelled opening" };
const late = phaseGate();
const lease = new AnalysisCleanupLease(
() => late.hold(),
["browser_profile"],
);
const primary = new AnalysisCancelledError("capture_browser_scenario", {
cause: reason,
cause: new Error("cancelled opening"),
});
const settled = vi.fn<(cause: unknown) => void>();
const opening = failBrowserScenarioOperation(lease, primary).catch(settled);
try {
const opening = failBrowserScenarioOperation(lease, primary).catch(
(cause: unknown) => cause,
);
await vi.advanceTimersByTimeAsync(30_000);
const failure = await opening;
await late.reached(opening, "opening failure cleanup");
await vi.advanceTimersByTimeAsync(29_999);
expect(settled).not.toHaveBeenCalled();
await vi.advanceTimersByTimeAsync(1);
expect(settled).toHaveBeenCalledOnce();
const failure = settled.mock.calls[0]![0];
if (!(failure instanceof AnalysisError))
throw new Error("Expected typed cleanup failure");
expect(failure.cleanupPrimary).toBe(primary);
expect(failure).toMatchObject({
cleanupIncomplete: true,
cleanupResources: ["browser_profile"],
});
expect(lease.released).toBe(false);
acknowledge();
late.release();
await lease.release();
expect(failure).toMatchObject({
cleanupIncomplete: false,
cleanupResources: [],
});
if (!(failure instanceof AnalysisError))
throw new Error("Expected typed cleanup failure");
expect(projectAnalysisError(failure)).toMatchObject({ code: "cancelled" });
expect(primary.cause).toBe(reason);
expect(projectAnalysisError(failure).code).toBe("cancelled");
} finally {
acknowledge();
await lease.release();
vi.useRealTimers();
late.release();
try {
await opening;
await lease.release();
} finally {
vi.useRealTimers();
}
}
});
@@ -1186,63 +1166,15 @@ it.each([
},
);
it("applies command_timeout_ms to the primary connect handshake when service workers are allowed", async () => {
let timeout: number | undefined;
const started = performance.now();
const scenario = browserScenarioSchema.parse({
browser: {
mode: "connect",
cdp_endpoint: "http://127.0.0.1:9222",
target_id: "primary-handshake",
},
start_url: { url: "https://example.test" },
environment: { service_workers: "allow" },
command_timeout_ms: 50,
actions: scenarioActions,
});
const opening = openPlaywrightScenarioBrowser(
scenario,
{},
{
launcher: {
connectOverCDP: async (_endpoint, options) => {
timeout = options?.timeout;
return new Promise(() => undefined);
},
launchPersistentContext: async () => {
throw new Error("Unexpected launch");
},
},
},
);
const failure = await opening.then(
() => undefined,
(cause: unknown) => cause,
);
expect(failure).toMatchObject({
reason: "timeout",
userMessage: expect.stringContaining("50 ms command_timeout_ms"),
remediationAction: expect.stringContaining("capture-browser-scenario JSON"),
diagnostics: { phase: "handshake", command_timeout_ms: 50 },
});
expect(
(failure as { remediationAction?: string }).remediationAction,
).not.toContain("--command-timeout-ms");
expect(timeout).toBe(50);
// The fake ignores Playwright's timeout; only REA's own 50 ms deadline can
// stop the handshake before the default it would otherwise wait for.
expect(performance.now() - started).toBeLessThan(
DEFAULT_CDP_COMMAND_TIMEOUT_MS,
);
});
it("applies command_timeout_ms to primary target lookup when service workers are allowed", async () => {
const started = performance.now();
const lookup = phaseGate();
const detach = vi.fn(async () => undefined);
const targetId = "primary-target-info";
const scenario = browserScenarioSchema.parse({
browser: {
mode: "connect",
cdp_endpoint: "http://127.0.0.1:9222",
target_id: "primary-target-info",
target_id: targetId,
},
start_url: { url: "https://example.test" },
environment: { service_workers: "allow" },
@@ -1250,97 +1182,137 @@ it("applies command_timeout_ms to primary target lookup when service workers are
actions: scenarioActions,
});
const browser = {
close: async () => undefined,
close: vi.fn(async () => undefined),
contexts: () => [
{
pages: () => [{} as Page],
newCDPSession: async () =>
({
send: () => new Promise(() => undefined),
detach: async () => undefined,
send: async () => {
await lookup.hold();
return { targetInfo: { targetId } };
},
detach,
}) as unknown as CDPSession,
} as unknown as BrowserContext,
],
isConnected: () => true,
version: () => "fixture",
};
await expect(
openPlaywrightScenarioBrowser(
scenario,
{},
{
launcher: {
connectOverCDP: async (_endpoint, options) => {
expect(options?.timeout).toBe(50);
return browser;
},
launchPersistentContext: async () => {
throw new Error("Unexpected launch");
},
},
},
),
).rejects.toMatchObject({
reason: "timeout",
userMessage: expect.stringContaining("Target.getTargetInfo"),
});
// A lookup bounded by the default instead of command_timeout_ms ends later.
expect(performance.now() - started).toBeLessThan(
DEFAULT_CDP_COMMAND_TIMEOUT_MS,
);
});
it("closes a context that launches after its deadline before removing the profile", async () => {
const order: string[] = [];
let profilePath = "";
const scenario = browserScenarioSchema.parse({
browser: { mode: "launch", executable_path: process.execPath },
start_url: { url: "https://example.test" },
command_timeout_ms: 40,
actions: scenarioActions,
});
const failure = await openPlaywrightScenarioBrowser(
const connectOverCDP = vi.fn(async () => browser);
vi.useFakeTimers();
const opening = openPlaywrightScenarioBrowser(
scenario,
{},
{
launcher: {
connectOverCDP: async () => {
throw new Error("Unexpected connection");
},
launchPersistentContext: async (profile) => {
profilePath = profile;
await new Promise((resolve) => setTimeout(resolve, 80));
order.push("launched");
return {
close: async () => {
order.push("close");
},
} as unknown as BrowserContext;
connectOverCDP,
launchPersistentContext: async () => {
throw new Error("Unexpected launch");
},
},
},
).then(
() => undefined,
(cause: unknown) => cause,
);
expect(failure).toMatchObject({
reason: "timeout",
userMessage: expect.stringContaining(
"Browser launch and persistent-context startup",
),
diagnostics: { phase: "launch", command_timeout_ms: 40 },
});
expect(
(failure as { remediationAction?: string }).remediationAction,
).not.toContain("--command-timeout-ms");
expect(order).toEqual(["launched", "close"]);
await expect(access(profilePath)).rejects.toThrow();
const settled = vi.fn();
void opening.then(settled, settled);
try {
await lookup.reached(opening, "target lookup");
expect(connectOverCDP).toHaveBeenCalledExactlyOnceWith(
"http://127.0.0.1:9222",
{ timeout: 50, noDefaults: true },
);
await vi.advanceTimersByTimeAsync(49);
expect(settled).not.toHaveBeenCalled();
expect(detach).not.toHaveBeenCalled();
await vi.advanceTimersByTimeAsync(1);
expect(settled).toHaveBeenCalledExactlyOnceWith(
expect.objectContaining({
reason: "timeout",
userMessage: expect.stringContaining("Target.getTargetInfo"),
diagnostics: {
phase: "command",
command_timeout_ms: 50,
method: "Target.getTargetInfo",
},
}),
);
expect(detach).toHaveBeenCalledOnce();
expect(browser.close).toHaveBeenCalledOnce();
} finally {
lookup.release();
try {
const opened = await opening.catch(() => undefined);
await opened?.cleanup.close();
} finally {
vi.useRealTimers();
}
}
});
it("closes a context that launches after its deadline before removing the profile", async () => {
const launch = phaseGate<string, BrowserContext>();
const close = phaseGate();
const scenario = { ...launchScenario, command_timeout_ms: 40 };
vi.useFakeTimers();
const opening = openPlaywrightScenarioBrowser(
scenario,
{},
{
launcher: {
launchPersistentContext: (profile) => launch.hold(profile),
connectOverCDP: async () => {
throw new Error("Unexpected connection");
},
},
},
);
const lateContext = {
close: () => close.hold(),
} as unknown as BrowserContext;
try {
const profile = await launch.reached(opening, "browser startup");
await vi.advanceTimersByTimeAsync(40);
launch.release(lateContext);
await close.reached(opening, "late browser context release");
await expect(access(profile)).resolves.toBeUndefined();
close.release();
const failure = await opening.catch((cause: unknown) => cause);
expect(failure).toMatchObject({
reason: "timeout",
userMessage: expect.stringContaining(
"Browser launch and persistent-context startup",
),
diagnostics: { phase: "launch", command_timeout_ms: 40 },
});
expect(
(failure as { remediationAction?: string }).remediationAction,
).not.toContain("--command-timeout-ms");
await expect(access(profile)).rejects.toMatchObject({ code: "ENOENT" });
} finally {
launch.release(lateContext);
close.release();
try {
const opened = await opening.catch(() => undefined);
await opened?.cleanup.close();
} finally {
vi.useRealTimers();
}
}
});
it("closes a browser that connects after its deadline without waiting to report the timeout", async () => {
vi.useFakeTimers();
const handshake = phaseGate();
const closed = vi.fn(async () => undefined);
const browser = {
close: vi.fn(async () => undefined),
contexts: vi.fn(() => []),
isConnected: () => true,
version: () => "late-connect",
};
const connectOverCDP = vi.fn(async () => {
await handshake.hold();
return browser;
});
const leases: AnalysisCleanupLease[] = [];
const scenario = browserScenarioSchema.parse({
browser: {
@@ -1360,15 +1332,7 @@ it("closes a browser that connects after its deadline without waiting to report
{
retainCleanup: (lease) => leases.push(lease),
launcher: {
connectOverCDP: async () => {
await handshake.hold();
return {
close: closed,
contexts: () => [],
isConnected: () => true,
version: () => "late-connect",
};
},
connectOverCDP,
launchPersistentContext: async () => {
throw new Error("Unexpected launch");
},
@@ -1381,15 +1345,36 @@ it("closes a browser that connects after its deadline without waiting to report
});
try {
await handshake.reached(opening, "late CDP handshake");
await vi.advanceTimersByTimeAsync(40);
expect(connectOverCDP).toHaveBeenCalledExactlyOnceWith(
"http://127.0.0.1:9222",
{ timeout: 40, noDefaults: true },
);
await vi.advanceTimersByTimeAsync(39);
expect(settled).toBe(false);
await vi.advanceTimersByTimeAsync(1);
expect(settled).toBe(true);
expect(closed).not.toHaveBeenCalled();
expect(await opening).toMatchObject({ reason: "timeout" });
expect(browser.close).not.toHaveBeenCalled();
const failure = await opening;
expect(failure).toMatchObject({
reason: "timeout",
userMessage: expect.stringContaining("40 ms command_timeout_ms"),
remediationAction: expect.stringContaining(
"capture-browser-scenario JSON",
),
diagnostics: { phase: "handshake", command_timeout_ms: 40 },
});
expect(
(failure as { remediationAction?: string }).remediationAction,
).not.toContain("--command-timeout-ms");
expect(leases).toHaveLength(1);
expect(leases[0]?.released).toBe(false);
expect(leases[0]).toMatchObject({
released: false,
cleanupResources: ["browser_transport"],
});
handshake.release();
await leases[0]!.release();
expect(closed).toHaveBeenCalledTimes(1);
expect(browser.close).toHaveBeenCalledOnce();
expect(browser.contexts).not.toHaveBeenCalled();
expect(leases[0]?.released).toBe(true);
} finally {
handshake.release();
@@ -1550,10 +1550,6 @@ export const PROVIDER_LIFECYCLE_COVERAGE: Readonly<
),
),
handshake: covered(
test(
"src/browser/PlaywrightScenarioBrowser.test.ts",
"applies command_timeout_ms to the primary connect handshake when service workers are allowed",
),
test(
"src/browser/PlaywrightScenarioBrowser.test.ts",
"closes a browser that connects after its deadline without waiting to report the timeout",