From 7065009feb2c3c1cb3b6ced65f533e56c3cbb009 Mon Sep 17 00:00:00 2001 From: ndycode <405533+ndycode@users.noreply.github.com> Date: Sun, 22 Mar 2026 14:02:03 +0800 Subject: [PATCH 1/3] test: cover runtime benchmark script --- test/benchmark-runtime-path-script.test.ts | 93 ++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 test/benchmark-runtime-path-script.test.ts diff --git a/test/benchmark-runtime-path-script.test.ts b/test/benchmark-runtime-path-script.test.ts new file mode 100644 index 00000000..241d7d02 --- /dev/null +++ b/test/benchmark-runtime-path-script.test.ts @@ -0,0 +1,93 @@ +import { spawnSync } from "node:child_process"; +import { + copyFileSync, + mkdirSync, + mkdtempSync, + readFileSync, + rmSync, + writeFileSync, +} from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; + +const tempRoots: string[] = []; + +afterEach(() => { + while (tempRoots.length > 0) { + const root = tempRoots.pop(); + if (root) rmSync(root, { recursive: true, force: true }); + } +}); + +function createFixture(): { root: string; scriptCopy: string } { + const root = mkdtempSync(path.join(tmpdir(), "runtime-bench-")); + tempRoots.push(root); + + const scriptsDir = path.join(root, "scripts"); + const distRequestDir = path.join(root, "dist", "lib", "request"); + const distRequestHelpersDir = path.join(distRequestDir, "helpers"); + const distLibDir = path.join(root, "dist", "lib"); + + mkdirSync(scriptsDir, { recursive: true }); + mkdirSync(distRequestHelpersDir, { recursive: true }); + mkdirSync(distLibDir, { recursive: true }); + + const scriptCopy = path.join(scriptsDir, "benchmark-runtime-path.mjs"); + copyFileSync( + path.join(process.cwd(), "scripts", "benchmark-runtime-path.mjs"), + scriptCopy, + ); + + writeFileSync( + path.join(distRequestDir, "request-transformer.js"), + "export function filterInput(input) { return Array.isArray(input) ? input : []; }\n", + "utf8", + ); + writeFileSync( + path.join(distRequestHelpersDir, "tool-utils.js"), + "export function cleanupToolDefinitions(tools) { return Array.isArray(tools) ? tools : []; }\n", + "utf8", + ); + writeFileSync( + path.join(distLibDir, "accounts.js"), + [ + "export class AccountManager {", + " constructor(_, storage) { this.storage = storage; }", + " getCurrentOrNextForFamilyHybrid() { return this.storage.accounts[0] ?? null; }", + "}", + ].join("\n"), + "utf8", + ); + + return { root, scriptCopy }; +} + +describe("benchmark runtime path script", () => { + it("writes a benchmark payload with expected result names", () => { + const { root, scriptCopy } = createFixture(); + const outputPath = path.join(root, "runtime-benchmark.json"); + + const result = spawnSync( + process.execPath, + [scriptCopy, "--iterations=1", `--output=${outputPath}`], + { cwd: root, encoding: "utf8" }, + ); + + expect(result.status).toBe(0); + expect(result.stdout).toContain("Runtime benchmark written:"); + + const payload = JSON.parse(readFileSync(outputPath, "utf8")) as { + iterations: number; + results: Array<{ name: string }>; + }; + expect(payload.iterations).toBe(1); + expect(payload.results.map((entry) => entry.name)).toEqual([ + "filterInput_small", + "filterInput_large", + "cleanupToolDefinitions_medium", + "cleanupToolDefinitions_large", + "accountHybridSelection_200", + ]); + }); +}); From 7d44c75525ea4fbc61a884277d110dc9ca5d1295 Mon Sep 17 00:00:00 2001 From: ndycode <405533+ndycode@users.noreply.github.com> Date: Sun, 22 Mar 2026 14:18:09 +0800 Subject: [PATCH 2/3] Use retry-safe benchmark cleanup --- test/benchmark-runtime-path-script.test.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/benchmark-runtime-path-script.test.ts b/test/benchmark-runtime-path-script.test.ts index 241d7d02..2fbdee61 100644 --- a/test/benchmark-runtime-path-script.test.ts +++ b/test/benchmark-runtime-path-script.test.ts @@ -4,19 +4,21 @@ import { mkdirSync, mkdtempSync, readFileSync, - rmSync, writeFileSync, } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it } from "vitest"; +import { removeWithRetry } from "./helpers/remove-with-retry.js"; const tempRoots: string[] = []; -afterEach(() => { +afterEach(async () => { while (tempRoots.length > 0) { const root = tempRoots.pop(); - if (root) rmSync(root, { recursive: true, force: true }); + if (root) { + await removeWithRetry(root, { recursive: true, force: true }); + } } }); From 6de014444da6defb2037183badc9ccf1a4b6250c Mon Sep 17 00:00:00 2001 From: ndycode <405533+ndycode@users.noreply.github.com> Date: Sun, 22 Mar 2026 14:31:28 +0800 Subject: [PATCH 3/3] Fail fast on benchmark test hangs --- test/benchmark-runtime-path-script.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/benchmark-runtime-path-script.test.ts b/test/benchmark-runtime-path-script.test.ts index 2fbdee61..19d65891 100644 --- a/test/benchmark-runtime-path-script.test.ts +++ b/test/benchmark-runtime-path-script.test.ts @@ -73,7 +73,7 @@ describe("benchmark runtime path script", () => { const result = spawnSync( process.execPath, [scriptCopy, "--iterations=1", `--output=${outputPath}`], - { cwd: root, encoding: "utf8" }, + { cwd: root, encoding: "utf8", timeout: 15_000 }, ); expect(result.status).toBe(0);