Files
openclaw/test/scripts/bench-web-fetch.test.ts
T
qingminlong 637de7d379 fix(scripts): reject unsafe web-fetch benchmark counts (#119533)
* fix(scripts): reject non-decimal benchmark counts

* fix(scripts): reject unsafe benchmark counts

Punchcard-Session: clear-orchard-lantern-bc

---------

Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
2026-08-05 18:00:32 +08:00

68 lines
2.1 KiB
TypeScript

// Bench Web Fetch tests cover the offline benchmark CLI contract.
import { spawnSync } from "node:child_process";
import { describe, expect, it } from "vitest";
const SCRIPT_PATH = "scripts/bench-web-fetch.ts";
function runBenchWebFetch(...args: string[]) {
return spawnSync(process.execPath, ["--import", "tsx", SCRIPT_PATH, ...args], {
cwd: process.cwd(),
encoding: "utf8",
env: {
...process.env,
FIRECRAWL_API_KEY: "test-firecrawl-key-that-should-be-ignored",
NODE_NO_WARNINGS: "1",
},
});
}
describe("web fetch benchmark script", () => {
it("accepts the package-manager separator documented for pnpm scripts", () => {
const result = runBenchWebFetch(
"--",
"--case",
"tool-create",
"--runs",
"1",
"--warmup",
"0",
"--json",
);
expect(result.status).toBe(0);
expect(result.stderr).toBe("");
const report = JSON.parse(result.stdout) as {
cases: Array<{ id: string; samplesMs: number[] }>;
};
expect(report.cases).toHaveLength(1);
expect(report.cases[0]).toMatchObject({
id: "tool-create",
samplesMs: expect.any(Array),
});
expect(report.cases[0]?.samplesMs).toHaveLength(1);
});
it("rejects duplicate singular flags without a stack trace", () => {
const result = runBenchWebFetch("--runs", "1", "--runs", "2");
expect(result.status).toBe(2);
expect(result.stdout).toBe("");
expect(result.stderr.trim()).toBe("--runs was provided more than once");
expect(result.stderr).not.toContain("\n at ");
});
it.each([
["--runs", "1e3", "--runs must be a positive integer"],
["--warmup", "1e3", "--warmup must be a non-negative integer"],
["--runs", "9007199254740993", "--runs must be a positive integer"],
["--warmup", "9007199254740993", "--warmup must be a non-negative integer"],
])("rejects invalid benchmark count %s %s", (flag, value, expectedError) => {
const result = runBenchWebFetch(flag, value);
expect(result.status).toBe(2);
expect(result.stdout).toBe("");
expect(result.stderr.trim()).toBe(expectedError);
expect(result.stderr).not.toContain("\n at ");
});
});