refactor(test): dedupe shared test helpers

This commit is contained in:
Peter Steinberger
2026-03-21 23:07:16 +00:00
parent 29b165e456
commit a622eecd3b
45 changed files with 898 additions and 1107 deletions

View File

@@ -0,0 +1,67 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { collectFilesSync, isCodeFile, relativeToCwd } from "../../scripts/check-file-utils.js";
const tempDirs: string[] = [];
afterEach(() => {
for (const dir of tempDirs.splice(0)) {
fs.rmSync(dir, { recursive: true, force: true });
}
});
function makeTempDir(): string {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-check-file-utils-"));
tempDirs.push(dir);
return dir;
}
describe("scripts/check-file-utils isCodeFile", () => {
it("accepts source files and skips declarations", () => {
expect(isCodeFile("example.ts")).toBe(true);
expect(isCodeFile("example.mjs")).toBe(true);
expect(isCodeFile("example.d.ts")).toBe(false);
});
});
describe("scripts/check-file-utils collectFilesSync", () => {
it("collects matching files while skipping common generated dirs", () => {
const rootDir = makeTempDir();
fs.mkdirSync(path.join(rootDir, "src", "nested"), { recursive: true });
fs.mkdirSync(path.join(rootDir, "dist"), { recursive: true });
fs.writeFileSync(path.join(rootDir, "src", "keep.ts"), "");
fs.writeFileSync(path.join(rootDir, "src", "nested", "keep.test.ts"), "");
fs.writeFileSync(path.join(rootDir, "dist", "skip.ts"), "");
const files = collectFilesSync(rootDir, {
includeFile: (filePath) => filePath.endsWith(".ts"),
}).map((filePath) => path.relative(rootDir, filePath));
expect(files.toSorted()).toEqual(["src/keep.ts", "src/nested/keep.test.ts"]);
});
it("supports custom skipped directories", () => {
const rootDir = makeTempDir();
fs.mkdirSync(path.join(rootDir, "fixtures"), { recursive: true });
fs.mkdirSync(path.join(rootDir, "src"), { recursive: true });
fs.writeFileSync(path.join(rootDir, "fixtures", "skip.ts"), "");
fs.writeFileSync(path.join(rootDir, "src", "keep.ts"), "");
const files = collectFilesSync(rootDir, {
includeFile: (filePath) => filePath.endsWith(".ts"),
skipDirNames: new Set(["fixtures"]),
}).map((filePath) => path.relative(rootDir, filePath));
expect(files).toEqual(["src/keep.ts"]);
});
});
describe("scripts/check-file-utils relativeToCwd", () => {
it("renders repo-relative paths when possible", () => {
expect(relativeToCwd(path.join(process.cwd(), "scripts", "check-file-utils.ts"))).toBe(
"scripts/check-file-utils.ts",
);
});
});

View File

@@ -0,0 +1,71 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import {
collectVitestFileDurations,
normalizeTrackedRepoPath,
tryReadJsonFile,
} from "../../scripts/test-report-utils.mjs";
describe("scripts/test-report-utils normalizeTrackedRepoPath", () => {
it("normalizes repo-local absolute paths to repo-relative slash paths", () => {
const absoluteFile = path.join(process.cwd(), "src", "tools", "example.test.ts");
expect(normalizeTrackedRepoPath(absoluteFile)).toBe("src/tools/example.test.ts");
});
it("preserves external absolute paths as normalized absolute paths", () => {
const externalFile = path.join(path.parse(process.cwd()).root, "tmp", "outside.test.ts");
expect(normalizeTrackedRepoPath(externalFile)).toBe(externalFile.split(path.sep).join("/"));
});
});
describe("scripts/test-report-utils collectVitestFileDurations", () => {
it("extracts per-file durations and applies file normalization", () => {
const report = {
testResults: [
{
name: path.join(process.cwd(), "src", "alpha.test.ts"),
startTime: 100,
endTime: 460,
assertionResults: [{}, {}],
},
{
name: "src/zero.test.ts",
startTime: 300,
endTime: 300,
assertionResults: [{}],
},
],
};
expect(collectVitestFileDurations(report, normalizeTrackedRepoPath)).toEqual([
{
file: "src/alpha.test.ts",
durationMs: 360,
testCount: 2,
},
]);
});
});
describe("scripts/test-report-utils tryReadJsonFile", () => {
it("returns the fallback when the file is missing", () => {
const missingPath = path.join(os.tmpdir(), `openclaw-missing-${Date.now()}.json`);
expect(tryReadJsonFile(missingPath, { ok: true })).toEqual({ ok: true });
});
it("reads valid JSON files", () => {
const tempPath = path.join(os.tmpdir(), `openclaw-json-${Date.now()}.json`);
fs.writeFileSync(tempPath, JSON.stringify({ ok: true }));
try {
expect(tryReadJsonFile(tempPath, null)).toEqual({ ok: true });
} finally {
fs.unlinkSync(tempPath);
}
});
});