mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-25 03:45:46 -06:00
30530ae2a3
* test(tooling): delete routing inventory reassertions * test(tooling): preserve full-suite ownership guard * test(tooling): remove dead fs scan helper --------- Co-authored-by: Amp <amp@ampcode.com>
86 lines
2.5 KiB
TypeScript
86 lines
2.5 KiB
TypeScript
// Test assertions for scanning repository files and filesystem fixtures.
|
|
import fs from "node:fs";
|
|
import { expect, vi } from "vitest";
|
|
import { spawnNodeEvalSync } from "./node-process.js";
|
|
|
|
type FsScanCounter = "existsSync" | "readdirSync" | "statSync";
|
|
|
|
type NodeFsScanResult<T> = {
|
|
counts: Partial<Record<FsScanCounter, number>>;
|
|
result: T;
|
|
};
|
|
|
|
/** Asserts a synchronous block did not enumerate directories. */
|
|
export function expectNoReaddirSyncDuring<T>(run: () => T): T {
|
|
return expectNoFsSyncDuring(run, ["readdirSync"]);
|
|
}
|
|
|
|
export function expectNoFsSyncDuring<T>(run: () => T, counters: FsScanCounter[]): T {
|
|
const spies = counters.map((counter) => {
|
|
switch (counter) {
|
|
case "existsSync":
|
|
return vi.spyOn(fs, "existsSync");
|
|
case "readdirSync":
|
|
return vi.spyOn(fs, "readdirSync");
|
|
case "statSync":
|
|
return vi.spyOn(fs, "statSync");
|
|
default: {
|
|
counter satisfies never;
|
|
throw new Error("Unsupported fs scan counter");
|
|
}
|
|
}
|
|
});
|
|
try {
|
|
const result = run();
|
|
for (const spy of spies) {
|
|
expect(spy).not.toHaveBeenCalled();
|
|
}
|
|
return result;
|
|
} finally {
|
|
for (const spy of spies) {
|
|
spy.mockRestore();
|
|
}
|
|
}
|
|
}
|
|
|
|
export function expectNoNodeFsScans<T>(
|
|
body: string,
|
|
options?: {
|
|
counters?: FsScanCounter[];
|
|
parseResult?: (result: unknown) => T;
|
|
},
|
|
): T {
|
|
const counters = options?.counters ?? ["existsSync", "readdirSync"];
|
|
const result = spawnNodeEvalSync(
|
|
`
|
|
import fs from "node:fs";
|
|
import { syncBuiltinESMExports } from "node:module";
|
|
const counts = ${JSON.stringify(Object.fromEntries(counters.map((name) => [name, 0])))};
|
|
${counters
|
|
.map(
|
|
(name) => `
|
|
const ${name}Original = fs.${name};
|
|
fs.${name} = (...args) => {
|
|
counts.${name} += 1;
|
|
return ${name}Original(...args);
|
|
};`,
|
|
)
|
|
.join("\n")}
|
|
syncBuiltinESMExports();
|
|
const result = await (async () => {
|
|
${body}
|
|
})();
|
|
console.log(JSON.stringify({ counts, result }));
|
|
`,
|
|
{
|
|
cwd: process.cwd(),
|
|
stdio: ["ignore", "pipe", "pipe"],
|
|
},
|
|
);
|
|
|
|
expect(result.status, result.stderr).toBe(0);
|
|
const payload = JSON.parse(result.stdout) as NodeFsScanResult<unknown>;
|
|
expect(payload.counts).toEqual(Object.fromEntries(counters.map((name) => [name, 0])));
|
|
return options?.parseResult ? options.parseResult(payload.result) : (payload.result as T);
|
|
}
|