Files
reverse-engineering-anything/src/domain/webDeadlineControls.test.ts
T

268 lines
8.6 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
cdpCleanupGraceExpired,
cdpCommandDeadlineRecovery,
cdpDiscoveryTimeoutRecovery,
} from "./browserDeadline.js";
import { listBrowserTargetsInputSchema } from "./browserObservation.js";
import {
analyzeWebBundleInputSchema,
webBundleHeapExhaustedReason,
webBundleHeapExhaustedRemediation,
webBundleHeapLimit,
webBundleMemoryExhaustion,
webBundleFixedByteBudget,
webBundleResultAdmissionFailure,
webBundleScriptTimeoutRecovery,
} from "./webBundleAnalysis.js";
import {
inspectWebNetworkCaptureInputSchema,
webNetworkCaptureTimeoutRecovery,
} from "./webNetworkCapture.js";
import {
webSourceLocationInputSchema,
webSourceMapDecodeTimeoutRecovery,
} from "./webSourceLocation.js";
const bundle = {
cdp_endpoint: "http://127.0.0.1:9222",
target_id: "page",
};
describe("web deadline controls", () => {
it("rejects a per-script deadline above the total", () => {
const parsed = analyzeWebBundleInputSchema.safeParse({
...bundle,
analysis_timeout_ms: 40_000,
total_analysis_timeout_ms: 30_000,
});
expect(parsed.success).toBe(false);
if (parsed.success) return;
expect(
parsed.error.issues.map((issue) => issue.message).join(" "),
).toContain("analysis_timeout_ms");
});
it("keeps omitted web-bundle deadlines at today's defaults", () => {
const parsed = analyzeWebBundleInputSchema.parse(bundle);
expect(parsed.analysis_timeout_ms).toBeUndefined();
expect(parsed.total_analysis_timeout_ms).toBeUndefined();
expect(parsed.max_heap_mb).toBeUndefined();
expect(parsed.source_map_fetch_timeout_ms).toBeUndefined();
expect(parsed.discovery_timeout_ms).toBeUndefined();
expect(parsed.command_timeout_ms).toBeUndefined();
});
it("accepts the promoted deadlines inside 1..2147483647", () => {
expect(
analyzeWebBundleInputSchema.safeParse({
...bundle,
discovery_timeout_ms: 2_147_483_647,
command_timeout_ms: 2_147_483_647,
}).success,
).toBe(true);
expect(
listBrowserTargetsInputSchema.safeParse({
cdp_endpoint: "http://127.0.0.1:9222",
discovery_timeout_ms: 15_000,
}).success,
).toBe(true);
expect(
listBrowserTargetsInputSchema.safeParse({
cdp_endpoint: "http://127.0.0.1:9222",
discovery_timeout_ms: 0,
}).success,
).toBe(false);
expect(
listBrowserTargetsInputSchema.safeParse({
cdp_endpoint: "http://127.0.0.1:9222",
command_timeout_ms: 1_500,
}).success,
).toBe(false);
expect(
inspectWebNetworkCaptureInputSchema.safeParse({
capture_path: "/tmp/capture.har",
format: "har",
capture_timeout_ms: 0,
}).success,
).toBe(false);
expect(
webSourceLocationInputSchema.safeParse({
manifest_path: "/tmp/manifest.json",
script_index: 0,
source_map: {
path: "/tmp/app.js.map",
url: "https://example.test/app.js.map",
},
generated_position: { line: 1, column: 0 },
decode_timeout_ms: 20_000,
}).success,
).toBe(true);
});
it("names each deadline and the independent budgets it does not raise", () => {
expect(webBundleScriptTimeoutRecovery(30_000).userMessage).toContain(
"analysis_timeout_ms",
);
const captureRecovery = webNetworkCaptureTimeoutRecovery(30_000);
for (const control of [
"max_input_bytes",
"max_reply_bytes",
"max_nesting_depth",
]) {
expect(captureRecovery.remediationAction).toContain(control);
}
expect(captureRecovery.remediationAction).toContain(
"capture_timeout_ms does not raise them",
);
expect(webSourceMapDecodeTimeoutRecovery(20_000).userMessage).toContain(
"decode_timeout_ms",
);
expect(
cdpCommandDeadlineRecovery(20_000, "handshake").userMessage,
).toContain("command_timeout_ms");
expect(cdpCleanupGraceExpired()).toContain("5000 ms");
expect(cdpCleanupGraceExpired()).toContain("no runtime override");
const inspector = cdpCommandDeadlineRecovery(
20_000,
"handshake",
undefined,
false,
);
expect(inspector.userMessage).not.toContain("command_timeout_ms");
expect(inspector.remediationAction).not.toContain("--command-timeout-ms");
// A fixed deadline binds no per-call control.
expect(inspector.limit).toBeUndefined();
expect(inspector.remediationAction).toContain("no runtime override");
const selectableDiscovery = cdpDiscoveryTimeoutRecovery(
"inspect_web_page",
15_000,
"browser-socket",
"The 67108864-byte browser-socket message budget",
);
expect(selectableDiscovery.userMessage).toContain(
"15000 ms discovery_timeout_ms",
);
expect(selectableDiscovery.remediationAction).toContain(
"Increase discovery_timeout_ms above 15000",
);
expect(selectableDiscovery.remediationAction).not.toContain("--");
expect(selectableDiscovery.limit).toMatchObject({
control_kind: "parameter",
parameter: "discovery_timeout_ms",
effective_value: 15_000,
});
expect(selectableDiscovery.remediationAction).toContain(
"command_timeout_ms does not raise it",
);
const fixedDiscovery = cdpDiscoveryTimeoutRecovery(
"observe_web_execution",
10_000,
"response",
"The 8388608-byte discovery document budget",
);
expect(fixedDiscovery.userMessage).not.toContain("discovery_timeout_ms");
expect(fixedDiscovery.remediationAction).not.toContain(
"--discovery-timeout-ms",
);
expect(fixedDiscovery.limit).toBeUndefined();
expect(fixedDiscovery.remediationAction).toContain("no runtime override");
});
});
it("attributes web-bundle exhaustion to the enforced memory control", () => {
const heapMb = 1024;
const commitBytes = 1024 * 1024 * 1024 + 512 * 1024 * 1024;
const node = webBundleMemoryExhaustion({
runtime: "node",
heapMb,
memoryLimitReached: true,
processMemoryLimitBytes: 99,
});
expect(node).toEqual({ kind: "v8-heap", heapMb });
expect(webBundleHeapExhaustedReason(node)).toContain("V8 old-space heap");
expect(webBundleHeapExhaustedRemediation(node)).toContain(
"Increase max_heap_mb above 1024",
);
const windows = webBundleMemoryExhaustion({
runtime: "bun",
heapMb,
memoryLimitReached: true,
processMemoryLimitBytes: commitBytes,
});
expect(windows).toEqual({
kind: "process-commit",
heapMb,
limitBytes: commitBytes,
});
expect(webBundleHeapExhaustedReason(windows)).toContain(
`${String(commitBytes)}-byte process commit limit`,
);
expect(webBundleHeapExhaustedRemediation(windows)).toContain(
"Windows process commit limit",
);
for (const adjustable of [node, windows])
expect(webBundleHeapLimit(adjustable)).toMatchObject({
control_kind: "parameter",
parameter: "max_heap_mb",
effective_value: heapMb,
unit: "MiB",
});
for (const processMemoryLimitBytes of [null, commitBytes]) {
const allocation = webBundleMemoryExhaustion({
runtime: "bun",
heapMb,
memoryLimitReached: false,
processMemoryLimitBytes,
});
expect(allocation).toEqual({
kind: "bun-allocation",
heapMb,
processMemoryLimitBytes,
});
expect(webBundleHeapLimit(allocation)).toBeUndefined();
expect(webBundleHeapExhaustedRemediation(allocation)).toContain(
"Reduce the analysis instead of increasing max_heap_mb",
);
expect(webBundleHeapExhaustedReason(allocation)).toContain(
processMemoryLimitBytes === null
? "enforces no analyzer process commit limit"
: `${String(commitBytes)}-byte process commit limit was reported as reached`,
);
}
expect(
webBundleHeapExhaustedRemediation({ kind: "v8-heap", heapMb: 16_384 }),
).toContain("already at the maximum 16384 MiB");
});
it("names host heap pressure only while the fixed result budget still has room", () => {
const host = webBundleResultAdmissionFailure({
fixedBudgetBytes: 8_388_608,
fixedRemainingBytes: 8_388_608,
hostCapacityBytes: 0,
});
expect(host).toMatchObject({
kind: "host-capacity",
remediationAction: expect.stringContaining("NODE_OPTIONS"),
reportedLimits: {
result_bytes: 8_388_608,
result_budget_remaining_bytes: 8_388_608,
host_result_capacity_bytes: 0,
},
});
expect(
webBundleResultAdmissionFailure({
fixedBudgetBytes: 8_388_608,
fixedRemainingBytes: 0,
hostCapacityBytes: 0,
}),
).toEqual({
kind: "fixed-budget",
reason: webBundleFixedByteBudget(8_388_608, "result budget"),
reportedLimits: { result_bytes: 8_388_608 },
});
});