test(web): consolidate CDP discovery and HTTP response contracts (#13173)

This commit is contained in:
morluto authored and GitHub committed 2026-11-21 13:05:05 -05:00
1 parent 6818b01d5a
commit b95bd814a7
3 files changed
+68 -111

No files matched your search

+12 -16
View File
@@ -115,20 +115,18 @@ it.each([0, 1, 2])(
},
);
it.each(
(["browser", "electron", "inspector"] as const).flatMap((providerKind) => [
{ providerKind, malformedRows: 0 },
{ providerKind, malformedRows: 1 },
{ providerKind, malformedRows: 2 },
]),
)(
"$providerKind rejects ambiguous HTTP target identity before authorizing its location or opening a socket (malformed duplicate rows: $malformedRows)",
async ({ providerKind, malformedRows }) => {
it.each(["browser", "electron", "inspector"] as const)(
"%s rejects ambiguous HTTP target identity before authorizing its location or opening a socket",
async (providerKind) => {
let upgrades = 0;
const server = createServer((request, response) => {
const address = server.address();
if (address === null || typeof address === "string")
throw new Error("Missing TCP address");
if (request.url !== "/json/version" && request.url !== "/json/list") {
response.writeHead(404).end();
return;
}
response.setHeader("Content-Type", "application/json");
response.end(
JSON.stringify(
@@ -145,16 +143,11 @@ it.each(
{
id: "duplicate",
type: "page",
...(malformedRows === 2
? {}
: { url: "https://allowed.test/" }),
url: "https://allowed.test/",
},
{
id: "duplicate",
type: "node",
...(malformedRows > 0
? {}
: { url: "file:///missing/foreign.js" }),
},
{
id: "sibling",
@@ -207,7 +200,10 @@ it.each(
});
if (result.ok) throw new Error("Expected ambiguous target rejection");
expect(result.error).toBeInstanceOf(BrowserObservationError);
expect(result.error.userMessage).toContain("ambiguous");
expect(result.error).toMatchObject({
reason: "invalid_endpoint_response",
diagnostics: { target_id: "duplicate" },
});
expect(upgrades).toBe(0);
} finally {
await provider.close();
-27
View File
@@ -138,33 +138,6 @@ describe("CDP discovery cancellation ownership", () => {
expect(requests).toBe(0);
});
it("cancels an in-flight discovery at the HTTP producer with the original reason", async () => {
let requestedPath: string | undefined;
let requestSeen!: () => void;
const seen = new Promise<void>((resolve) => {
requestSeen = resolve;
});
const { endpoint } = await listen((request) => {
requestedPath = request.url;
requestSeen();
});
const reason = new Error("operator stopped discovery");
const controller = new AbortController();
const discovery = discoverCdpEndpoint(
endpoint,
"inspect_web_page",
controller.signal,
).catch((error: unknown) => error);
await seen;
controller.abort(reason);
const failure = await discovery;
expect(requestedPath).toBe("/json/version");
expect(failure).toBeInstanceOf(AnalysisCancelledError);
expect((failure as AnalysisCancelledError).cause).toBe(reason);
});
it("keeps typed and plain discovery failures produced before a caller abort", async () => {
const failures = [
new BrowserObservationError("inspect_web_page", "protocol_error"),
@@ -1,8 +1,9 @@
import { createServer } from "node:http";
import type { RequestListener, Server } from "node:http";
import { afterEach, describe, expect, it } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";
import { phaseArrival } from "../../support/lifecycleHarness.js";
import { AnalysisCancelledError } from "../../../src/domain/analysisErrorCore.js";
import { withVerifierCleanup } from "../../../scripts/lib/verifier-cleanup.mjs";
import {
@@ -149,74 +150,61 @@ describe("CDP discovery response bounds", () => {
await expect.poll(() => responseClosed).toBe(true);
});
it("ends a response that exceeds the total elapsed deadline", async () => {
let responseClosed = false;
const cdpRequest = phaseArrival();
const server = await startServer((_request, response) => {
response.once("close", () => {
responseClosed = true;
it.each(["deadline", "cancellation"] as const)(
"closes an in-flight discovery response on %s before reading the target list",
async (end) => {
let responseClosed = false;
const requestedPaths: Array<string | undefined> = [];
const cdpRequest = phaseArrival();
const server = await startServer((request, response) => {
requestedPaths.push(request.url);
response.once("close", () => {
responseClosed = true;
});
response.flushHeaders();
cdpRequest.arrive();
});
cdpRequest.arrive();
});
const controller = new AbortController();
const pending = readCdpJson(
server.url,
"list_browser_targets",
controller.signal,
{
bytes: 16,
timeoutMs: 50,
},
);
await withVerifierCleanup(async () => {
await cdpRequest.reached(pending, "CDP request");
await expect(pending).rejects.toMatchObject({
_tag: "BrowserObservationError",
reason: "timeout",
});
await expect.poll(() => responseClosed).toBe(true);
}, [
() => controller.abort(),
async () => {
await pending.catch(() => undefined);
},
]);
});
it("preserves caller cancellation and closes the response", async () => {
let responseClosed = false;
const cdpRequest = phaseArrival();
const server = await startServer((_request, response) => {
response.once("close", () => {
responseClosed = true;
});
cdpRequest.arrive();
});
const controller = new AbortController();
const pending = readCdpJson(
server.url,
"list_browser_targets",
controller.signal,
{
bytes: 16,
timeoutMs: 1_000,
},
);
await withVerifierCleanup(async () => {
await cdpRequest.reached(pending, "CDP request");
controller.abort();
await expect(pending).rejects.toMatchObject({
_tag: "AnalysisCancelledError",
operation: "list_browser_targets",
});
await expect.poll(() => responseClosed).toBe(true);
}, [
() => controller.abort(),
async () => {
await pending.catch(() => undefined);
},
]);
});
const controller = new AbortController();
const reason = { caller: "stopped discovery" };
const timeoutMs = 50;
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
const pending = discoverCdpEndpoint(
server.url.href,
"list_browser_targets",
controller.signal,
timeoutMs,
);
await withVerifierCleanup(async () => {
await cdpRequest.reached(pending, "CDP version request");
if (end === "deadline") await vi.advanceTimersByTimeAsync(timeoutMs);
else controller.abort(reason);
const error: unknown = await pending.catch(
(failure: unknown) => failure,
);
if (end === "deadline") {
expect(error).toMatchObject({
_tag: "BrowserObservationError",
operation: "list_browser_targets",
reason: "timeout",
});
} else {
if (!(error instanceof AnalysisCancelledError))
throw new Error("Expected caller cancellation", { cause: error });
expect(error.operation).toBe("list_browser_targets");
expect(error.cause).toBe(reason);
}
vi.useRealTimers();
await expect.poll(() => responseClosed).toBe(true);
expect(requestedPaths).toEqual(["/json/version"]);
}, [
() => controller.abort(),
async () => {
await pending.catch(() => undefined);
},
() => vi.useRealTimers(),
]);
},
);
it("destroys an unsuccessful HTTP response", async () => {
let responseClosed = false;