test(web): consolidate script metadata admission and projection proofs (#13091)

This commit is contained in:
morluto authored and GitHub committed 2026-11-21 12:29:47 +00:00
1 parent cc24a0b4c0
commit 6878e24cc7
2 files changed
+221 -226

No files matched your search

@@ -7,6 +7,25 @@ import { CDP_CAPTURE_SCRIPT_METADATA_LIMITS } from "../../../src/browser/CdpCapt
import { receiveCdpEvent } from "../../../src/browser/CdpConnection.js";
describe("CDP script metadata admission", () => {
const origin = "https://app.example";
const emit = (
events: CdpCaptureEvents,
scriptId: string,
sourceMapURL?: unknown,
) =>
events.ingest(
receiveCdpEvent({
method: "Debugger.scriptParsed",
params: { scriptId, url: `${origin}/app.js`, sourceMapURL },
}),
);
const mapPath = (size: number) => `/maps/${"x".repeat(size)}.map`;
const mebibyte = 1024 * 1024;
const resourceExclusions = (events: CdpCaptureEvents) =>
events.completeness
.snapshot()
.excluded.filter(({ reason }) => reason === "resource_budget_exhausted");
it("retains an invalid source-map declaration as unknown", () => {
const events = captureEvents();
events.beginAuthorizedFrame(new CdpTargetDocument({ frameId: "frame-1" }));
@@ -15,16 +34,7 @@ describe("CDP script metadata admission", () => {
["absent", undefined],
["empty", ""],
] as const) {
events.ingest(
receiveCdpEvent({
method: "Debugger.scriptParsed",
params: {
scriptId,
url: "https://app.example/app.js",
sourceMapURL,
},
}),
);
emit(events, scriptId, sourceMapURL);
expect(events.scripts.get(scriptId)?.sourceMapUrl).toBeNull();
}
expect(events.completeness.snapshot().excluded).toContainEqual({
@@ -34,112 +44,69 @@ describe("CDP script metadata admission", () => {
});
});
it("releases a replaced script ID's prior bytes and resets admission on a new document", () => {
it("reclaims replaced map bytes and starts each document with a fresh budget", () => {
const events = captureEvents();
const origin = "https://app.example";
events.beginAuthorizedFrame(new CdpTargetDocument({ frameId: "frame-1" }));
const emit = (scriptId: string, bytes: number): void => {
events.ingest(
receiveCdpEvent({
method: "Debugger.scriptParsed",
params: {
scriptId,
url: `${origin}/app.js`,
sourceMapURL: `/maps/${"x".repeat(bytes)}.map`,
},
}),
);
const expectMap = (id: string, payloadBytes: number) => {
const url = events.scripts.get(id)?.sourceMapUrl;
expect(url?.length).toBe(`${origin}/maps/.map`.length + payloadBytes);
expect(url?.endsWith(".map")).toBe(true);
};
emit("same-id", 3 * 1024 * 1024);
emit("same-id", 2 * 1024 * 1024);
emit("next-id", 1024 * 1024);
expect(events.scripts.get("same-id")?.sourceMapUrl).toContain(
"x".repeat(2 * 1024 * 1024),
);
expect(events.scripts.get("next-id")?.sourceMapUrl).toContain(
"x".repeat(1024 * 1024),
);
emit(events, "same-id", mapPath(3 * mebibyte));
emit(events, "same-id", mapPath(2 * mebibyte));
emit(events, "next-id", mapPath(mebibyte));
expectMap("same-id", 2 * mebibyte);
expectMap("next-id", mebibyte);
expect(events.retainedScriptMetadataBytes).toBeLessThanOrEqual(
CDP_CAPTURE_SCRIPT_METADATA_LIMITS.retainedBytes,
);
const normalizedAway = `./`.repeat(1_200_000) + "map.js.map";
events.ingest(
receiveCdpEvent({
method: "Debugger.scriptParsed",
params: {
scriptId: "normalized-url",
url: `${origin}/app.js`,
sourceMapURL: normalizedAway,
},
}),
);
emit(events, "normalized-url", `${"./".repeat(1_200_000)}map.js.map`);
expect(events.scripts.get("normalized-url")?.sourceMapUrl).toBe(
`${origin}/map.js.map`,
);
events.beginAuthorizedFrame(new CdpTargetDocument({ frameId: "frame-2" }));
emit("new-document", 5 * 1024 * 1024);
emit("new-document", 1024 * 1024);
emit(events, "new-document", mapPath(5 * mebibyte));
expect(events.scripts.get("new-document")?.sourceMapUrl).toBeNull();
emit(events, "new-document", mapPath(mebibyte));
expectMap("new-document", mebibyte);
events.recordScriptMetadataBudgetExclusions();
expect(
events.completeness
.snapshot()
.excluded.find(
({ section, reason }) =>
section === "source_maps" && reason === "resource_budget_exhausted",
),
).toBeUndefined();
expect(resourceExclusions(events)).toEqual([]);
events.beginAuthorizedFrame(new CdpTargetDocument({ frameId: "frame-3" }));
emit("new-document", 3 * 1024 * 1024);
expect(events.scripts.get("new-document")?.sourceMapUrl).toContain(
"x".repeat(3 * 1024 * 1024),
);
emit(events, "new-document", mapPath(3 * mebibyte));
expectMap("new-document", 3 * mebibyte);
emit(events, "map-overflow", mapPath(2 * mebibyte));
expect(events.scripts.get("map-overflow")?.sourceMapUrl).toBeNull();
events.recordScriptMetadataBudgetExclusions();
expect(resourceExclusions(events)).toEqual([
{ section: "source_maps", reason: "resource_budget_exhausted", count: 1 },
]);
});
it("does not retain IDs for scripts rejected by byte or record limits", () => {
it("rejects oversized metadata and excess records at independent boundaries", () => {
const events = captureEvents();
const origin = "https://app.example";
events.beginAuthorizedFrame(new CdpTargetDocument({ frameId: "frame-1" }));
const emit = (scriptId: string): void => {
events.ingest(
receiveCdpEvent({
method: "Debugger.scriptParsed",
params: { scriptId, url: `${origin}/app.js` },
}),
);
};
const { retainedBytes, scripts } = CDP_CAPTURE_SCRIPT_METADATA_LIMITS;
const oversized = "x".repeat(retainedBytes + 1);
emit(events, oversized);
expect(events.scripts.has(oversized)).toBe(false);
emit("x".repeat(CDP_CAPTURE_SCRIPT_METADATA_LIMITS.retainedBytes + 1));
for (
let index = 0;
index < CDP_CAPTURE_SCRIPT_METADATA_LIMITS.scripts;
index += 1
)
emit(`script-${String(index)}`);
for (let index = 0; index < 10; index += 1)
emit(`rejected-${String(index)}`);
expect(events.scripts.size).toBe(
CDP_CAPTURE_SCRIPT_METADATA_LIMITS.scripts,
);
for (let index = 0; index < scripts; index += 1)
emit(events, `script-${String(index)}`);
expect(events.scripts.size).toBe(scripts);
expect(events.scripts.has(`script-${String(scripts - 1)}`)).toBe(true);
expect(events.retainedScriptMetadataBytes).toBeLessThan(retainedBytes);
emit(events, "record-overflow", "/maps/overflow.map");
expect(events.scripts.has("record-overflow")).toBe(false);
expect(events.scriptMetadataBudgetOmissionsById.size).toBe(0);
expect(events.rejectedScriptMetadataBudgetCount).toBe(11);
events.recordScriptMetadataBudgetExclusions();
expect(
events.completeness
.snapshot()
.excluded.find(
({ section, reason }) =>
section === "scripts" && reason === "resource_budget_exhausted",
),
).toEqual({
section: "scripts",
reason: "resource_budget_exhausted",
count: 11,
});
expect(resourceExclusions(events)).toEqual([
{ section: "scripts", reason: "resource_budget_exhausted", count: 2 },
{ section: "source_maps", reason: "resource_budget_exhausted", count: 1 },
]);
});
});
+160 -132
View File
@@ -1,5 +1,5 @@
import { createHash } from "node:crypto";
import { readFile, rm, writeFile } from "node:fs/promises";
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { pathToFileURL } from "node:url";
@@ -10,6 +10,7 @@ import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js";
import { CdpElectronProvider } from "../../../src/browser/CdpElectronProvider.js";
import { publishWebScripts } from "../../../src/application/PublishWebScripts.js";
import { ArtifactResourceScope } from "../../../src/resources/ArtifactResourceScope.js";
import { webScriptExportManifestSchema } from "../../../src/domain/webScriptExport.js";
import { selectScriptCapture } from "../../../src/domain/webScriptCaptureAdapters.js";
import { inspectWebPageInputSchema } from "../../../src/domain/browserObservation.js";
import { inspectElectronPageInputSchema } from "../../../src/domain/javascript/electronObservation.js";
@@ -53,155 +54,182 @@ afterEach(async () => {
for (const close of resources.splice(0).reverse()) await close();
});
const fixtureRoot = async (): Promise<string> => {
const root = await createTestTempDirectory("rea-cdp-script-metadata-");
resources.push(() => rm(root, { force: true, recursive: true }));
await Promise.all([
writeFile(join(root, "index.html"), "<html></html>"),
writeFile(join(root, "app.js"), "export const observed = true;"),
]);
return root;
const createMetadataDirectory = () =>
createTestTempDirectory("rea-cdp-script-metadata-");
const metadataFile = (index: number): string => `metadata-${String(index)}.js`;
const writeMetadataFiles = async (root: string): Promise<void> => {
await Promise.all(
metadataCases.map((_, index) =>
writeFile(
join(root, metadataFile(index)),
`export const observed = ${String(index)};`,
),
),
);
};
const metadataEvents = (location: (index: number) => string) =>
metadataCases.map(({ reported }, index) => ({
method: "Debugger.scriptParsed",
params: {
scriptId: metadataFile(index),
url: location(index),
executionContextId: 1,
...reported,
},
}));
type MetadataExpectation = (typeof metadataCases)[number]["expected"];
const expectMetadataCases = (
items: readonly unknown[],
expectedRecord: (index: number, metadata: MetadataExpectation) => object,
): void => {
expect(items).toHaveLength(metadataCases.length);
for (const [index, { name, expected }] of metadataCases.entries())
expect(items, name).toContainEqual(
expect.objectContaining(expectedRecord(index, expected)),
);
};
describe.each(["browser", "electron"] as const)(
"%s script metadata",
(kind) => {
it.each(metadataCases)(
"$name through capture and consumers",
async ({ reported, expected }) => {
const root = await fixtureRoot();
const artifactResources = new ArtifactResourceScope();
resources.push(() => artifactResources.close());
const browser = await startFakeCdpBrowser({
...(kind === "electron"
? { electronFileUrl: pathToFileURL(join(root, "index.html")).href }
: {}),
commandEvents: ({ method }, origin) =>
method === "Debugger.enable"
? [
{
method: "Debugger.scriptParsed",
sessionId: "session-1",
params: {
scriptId: "captured-script",
url:
kind === "browser"
? `${origin}/app.js`
: pathToFileURL(join(root, "app.js")).href,
executionContextId: 1,
scriptLanguage: "JavaScript",
...reported,
},
},
]
: undefined,
});
resources.push(() => browser.close());
if (kind === "electron") {
const result = await new CdpElectronProvider().inspectPage(
inspectElectronPageInputSchema.parse({
cdp_endpoint: browser.endpoint,
target_id: "electron-page",
observation_ms: 0,
include_script_sources: true,
}),
);
if (!result.ok) throw result.error;
expect(result.value.scripts.items).toHaveLength(1);
expect(result.value.scripts.items[0]).toMatchObject(expected);
return;
}
const result = await new CdpBrowserProvider().inspectPage(
inspectWebPageInputSchema.parse({
cdp_endpoint: browser.endpoint,
target_id: "allowed-page",
observation_ms: 0,
include_script_sources: true,
it("retains each reported metadata variant through capture and consumers", async () => {
const root = await createMetadataDirectory();
if (kind === "electron")
await Promise.all([
writeFile(join(root, "index.html"), "<html></html>"),
writeMetadataFiles(root),
]);
const file = (index: number) => join(root, metadataFile(index));
const browserUrl = (origin: string, index: number) =>
`${origin}/${metadataFile(index)}`;
const browser = await startFakeCdpBrowser({
...(kind === "electron"
? { electronFileUrl: pathToFileURL(join(root, "index.html")).href }
: {}),
commandEvents: ({ method }, origin) =>
method === "Debugger.enable"
? metadataEvents((index) =>
kind === "browser"
? browserUrl(origin, index)
: pathToFileURL(file(index)).href,
).map((event) => ({
...event,
sessionId: "session-1",
params: { ...event.params, scriptLanguage: "JavaScript" },
}))
: undefined,
});
resources.push(() => browser.close());
const input = {
cdp_endpoint: browser.endpoint,
observation_ms: 0,
include_script_sources: true,
};
if (kind === "electron") {
const result = await new CdpElectronProvider().inspectPage(
inspectElectronPageInputSchema.parse({
...input,
target_id: "electron-page",
}),
);
if (!result.ok) throw result.error;
expect(result.value.scripts.items).toHaveLength(1);
expect(result.value.scripts.items[0]).toMatchObject(expected);
const capturePath = join(root, "capture.json");
const captureBytes = JSON.stringify(result.value);
await writeFile(capturePath, captureBytes);
const exported = await publishWebScripts(
{
capture_path: capturePath,
output_directory: join(root, "exported"),
},
selectScriptCapture(result.value),
createHash("sha256").update(captureBytes).digest("hex"),
{ resources: artifactResources },
);
expect(exported.scripts[0]?.source).toMatchObject({
is_module: expected.is_module,
});
expect(exported.scripts[0]?.content.state).toBe("exported");
const manifest: unknown = JSON.parse(
await readFile(exported.manifest.path, "utf8"),
);
expect(manifest).toMatchObject({
scripts: [{ source: { is_module: expected.is_module } }],
});
},
);
expectMetadataCases(result.value.scripts.items, (index, expected) => ({
file_path: file(index),
...expected,
}));
return;
}
const result = await new CdpBrowserProvider().inspectPage(
inspectWebPageInputSchema.parse({
...input,
target_id: "allowed-page",
}),
);
if (!result.ok) throw result.error;
const artifactResources = new ArtifactResourceScope();
resources.push(() => artifactResources.close());
const capturePath = join(root, "capture.json");
const captureBytes = JSON.stringify(result.value);
await writeFile(capturePath, captureBytes);
const exported = await publishWebScripts(
{ capture_path: capturePath, output_directory: join(root, "exported") },
selectScriptCapture(result.value),
createHash("sha256").update(captureBytes).digest("hex"),
{ resources: artifactResources },
);
const manifest = webScriptExportManifestSchema.parse(
JSON.parse(await readFile(exported.manifest.path, "utf8")),
);
const url = (index: number) => browserUrl(browser.allowedOrigin, index);
expectMetadataCases(result.value.scripts.items, (index, expected) => ({
url: url(index),
...expected,
}));
for (const records of [exported.scripts, manifest.scripts])
expectMetadataCases(records, (index, expected) => ({
url: url(index),
source: expect.objectContaining({ is_module: expected.is_module }),
content: expect.objectContaining({ state: "exported" }),
}));
});
},
);
describe("Inspector script metadata", () => {
it.each(metadataCases)(
"$name through reconciliation",
async ({ reported, expected }) => {
const root = await fixtureRoot();
const scriptUrl = pathToFileURL(join(root, "app.js")).href;
const inspector = await startFakeV8Inspector({
targetUrl: scriptUrl,
scriptEvents: [
{
method: "Debugger.scriptParsed",
params: {
scriptId: "captured-script",
url: scriptUrl,
executionContextId: 1,
...reported,
},
},
],
});
resources.push(() => inspector.close());
const input = {
inspector_endpoint: inspector.endpoint,
target_id: inspector.targetId,
observation_ms: 50,
};
const provider = new V8InspectorProvider();
const result = await provider.observe(input);
if (!result.ok) throw result.error;
expect(result.value.scripts.items).toHaveLength(1);
expect(result.value.scripts.items[0]).toMatchObject(expected);
const evidence = createJavaScriptRuntimeObservationEvidence(
"observe_javascript_runtime",
input,
result.value,
provider.identity(),
);
const projected = projectRuntimeCaptures([
parseRuntimeCapture(evidence, ["runtime_observations", 0]),
]);
const scriptNode = projected.nodes.find(
({ kind }) => kind === "runtime-script-instance",
);
expect(scriptNode?.observations[0]?.properties).toMatchObject(expected);
},
it("retains Inspector metadata variants across runtime reconciliation", async () => {
const root = await createMetadataDirectory();
await writeMetadataFiles(root);
const file = (index: number) => join(root, metadataFile(index));
const inspector = await startFakeV8Inspector({
targetUrl: pathToFileURL(file(0)).href,
scriptEvents: metadataEvents((index) => pathToFileURL(file(index)).href),
});
resources.push(() => inspector.close());
const input = {
inspector_endpoint: inspector.endpoint,
target_id: inspector.targetId,
observation_ms: 50,
};
const provider = new V8InspectorProvider();
const result = await provider.observe(input);
if (!result.ok) throw result.error;
const evidence = createJavaScriptRuntimeObservationEvidence(
"observe_javascript_runtime",
input,
result.value,
provider.identity(),
);
const projected = projectRuntimeCaptures([
parseRuntimeCapture(evidence, ["runtime_observations", 0]),
]);
const scriptNodes = projected.nodes.filter(
({ kind }) => kind === "runtime-script-instance",
);
const properties = scriptNodes.map(
({ observations }) => observations[0]?.properties,
);
expectMetadataCases(result.value.scripts.items, (index, expected) => ({
location: { kind: "file", file_path: file(index) },
...expected,
}));
expectMetadataCases(properties, (index, expected) => ({
location: file(index),
...expected,
}));
});
describe("Inspector script exclusions", () => {
const reconcileV8Scripts = async (
scriptEvents: readonly { method: string; params: object }[],
) => {
const root = await fixtureRoot();
const root = await createMetadataDirectory();
await writeFile(join(root, "app.js"), "export const observed = true;");
const inspector = await startFakeV8Inspector({
targetUrl: pathToFileURL(join(root, "app.js")).href,
scriptEvents,