fix(evidence): preserve literal keys and response body ownership

Retain exact empty and computed literal keys in JavaScript observations. Bind CDP response bodies to request occurrences, account for late responses, and validate the target after final progress callbacks.
This commit is contained in:
morluto
2026-10-12 16:17:29 +08:00
parent 79156e5bed
commit d1dcebce2c
10 changed files with 575 additions and 210 deletions
@@ -131,6 +131,203 @@ it("preserves unavailable response observations and captures completed peers", a
).toEqual(["missing-body", "available"]);
});
it("marks JSON response bodies unavailable when their events arrive during body capture", async () => {
const browser = await startFakeCdpBrowser({
commandEvents(command, origin) {
const session =
command.sessionId === undefined ? {} : { sessionId: command.sessionId };
if (command.method === "Network.enable") {
const url = `${origin}/initial.json`;
return [
{
...session,
method: "Network.requestWillBeSent",
params: {
requestId: "initial",
request: { url, method: "GET" },
initiator: { type: "other" },
},
},
{
...session,
method: "Network.responseReceived",
params: {
requestId: "initial",
response: { url, status: 200, mimeType: "application/json" },
},
},
{
...session,
method: "Network.loadingFinished",
params: { requestId: "initial", encodedDataLength: 16 },
},
];
}
if (command.method !== "Network.getResponseBody") return undefined;
const url = `${origin}/late.json`;
return [
{
...session,
method: "Network.requestWillBeSent",
params: {
requestId: "late",
request: { url, method: "GET" },
initiator: { type: "other" },
},
},
{
...session,
method: "Network.responseReceived",
params: {
requestId: "late",
response: { url, status: 200, mimeType: "application/json" },
},
},
{
...session,
method: "Network.loadingFinished",
params: { requestId: "late", encodedDataLength: 14 },
},
];
},
commandResult(command) {
return command.method === "Network.getResponseBody"
? { body: '{"initial":true}', base64Encoded: false }
: undefined;
},
});
trackBrowser(browser);
const result = await new CdpBrowserProvider().inspectPage(
inspectWebPageInputSchema.parse({
cdp_endpoint: browser.endpoint,
allowed_origins: [browser.allowedOrigin],
target_id: "allowed-page",
observation_ms: 20,
include_json_body_shapes: true,
}),
);
if (!result.ok) throw result.error;
expect(result.value.network.requests).toEqual([
expect.objectContaining({
request_id: "initial",
body_shapes: {
status: "included",
request: null,
response: expect.any(Object),
},
}),
expect.objectContaining({
request_id: "late",
body_shapes: { status: "unavailable", request: null, response: null },
}),
]);
expect(result.value.completeness.unavailable_sections).toContain(
"json_body_shapes",
);
});
it("does not attach a redirect response body to the reused request ID", async () => {
const browser = await startFakeCdpBrowser({
commandEvents(command, origin) {
const session =
command.sessionId === undefined ? {} : { sessionId: command.sessionId };
if (command.method === "Network.enable") {
const url = `${origin}/before-redirect.json`;
return [
{
...session,
method: "Network.requestWillBeSent",
params: {
requestId: "reused",
request: { url, method: "GET" },
initiator: { type: "other" },
},
},
{
...session,
method: "Network.responseReceived",
params: {
requestId: "reused",
response: { url, status: 302, mimeType: "application/json" },
},
},
{
...session,
method: "Network.loadingFinished",
params: { requestId: "reused", encodedDataLength: 12 },
},
];
}
if (command.method !== "Network.getResponseBody") return undefined;
const previousUrl = `${origin}/before-redirect.json`;
const nextUrl = `${origin}/after-redirect.json`;
return [
{
...session,
method: "Network.requestWillBeSent",
params: {
requestId: "reused",
request: { url: nextUrl, method: "GET" },
initiator: { type: "other" },
redirectResponse: {
url: previousUrl,
status: 302,
mimeType: "application/json",
encodedDataLength: 12,
},
},
},
{
...session,
method: "Network.responseReceived",
params: {
requestId: "reused",
response: {
url: nextUrl,
status: 200,
mimeType: "application/json",
},
},
},
{
...session,
method: "Network.loadingFinished",
params: { requestId: "reused", encodedDataLength: 14 },
},
];
},
commandResult(command) {
return command.method === "Network.getResponseBody"
? { body: '{"belongs":"redirect"}', base64Encoded: false }
: undefined;
},
});
trackBrowser(browser);
const result = await new CdpBrowserProvider().inspectPage(
inspectWebPageInputSchema.parse({
cdp_endpoint: browser.endpoint,
allowed_origins: [browser.allowedOrigin],
target_id: "allowed-page",
observation_ms: 20,
include_json_body_shapes: true,
}),
);
if (!result.ok) throw result.error;
expect(result.value.network.requests).toEqual([
expect.objectContaining({
request_id: "reused",
url: `${browser.allowedOrigin}/after-redirect.json`,
redirects: [expect.objectContaining({ status: 302 })],
body_shapes: { status: "unavailable", request: null, response: null },
}),
]);
expect(result.value.completeness.unavailable_sections).toContain(
"json_body_shapes",
);
});
it("marks malformed JSON body shapes unavailable while preserving partial request shapes", async () => {
const browser = await startFakeCdpBrowser({
commandEvents(command, origin) {
@@ -301,6 +498,12 @@ it("rejects malformed response-body envelopes and preserves request shapes", asy
reason: "invalid_protocol_value",
count: 2,
});
expect(result.value.completeness.excluded).not.toContainEqual(
expect.objectContaining({
section: "json_body_shapes",
reason: "provider_unavailable",
}),
);
});
it.each([