import { describe, it } from "node:test"; import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { readFileSync } from "node:fs"; import path from "node:path"; // CI runs `test:coverage`, developers run `test`. A test file neither glob reaches never runs anywhere, // and one only `test` reaches passes locally but never in CI (#3715). const ROOT = path.resolve(import.meta.dirname, "..", ".."); const SCRIPTS: Record = JSON.parse(readFileSync(path.join(ROOT, "package.json"), "utf8")).scripts; const TEST_FILE_NAME = /^test_[^/]*\.ts$/; const testGlobs = (command: string | undefined): string[] => (command ?? "") .split(/\s+/) .filter((word) => word.endsWith("test_*.ts")) .sort(); // A glob here only uses `*` within one path segment, so each segment matches by its literal pieces in order. const segmentMatches = (pattern: string, name: string): boolean => { const [head = "", ...rest] = pattern.split("*"); if (!name.startsWith(head)) return false; let offset = head.length; for (const [index, piece] of rest.entries()) { const isLast = index === rest.length - 1; const found = isLast ? (name.endsWith(piece) && name.length - piece.length >= offset ? name.length - piece.length : -1) : name.indexOf(piece, offset); if (found < 0) return false; offset = found + piece.length; } return rest.length > 0 || offset === name.length; }; const globMatches = (glob: string, file: string): boolean => { const patternSegments = glob.replace(/^\.\//, "").split("/"); const fileSegments = file.split("/"); return patternSegments.length === fileSegments.length && patternSegments.every((segment, index) => segmentMatches(segment, fileSegments[index] ?? "")); }; const trackedTestFiles = (): string[] => execFileSync("git", ["ls-files", "test", "packages/mulmoclaude/test"], { cwd: ROOT, encoding: "utf8" }) .split("\n") .filter((file) => TEST_FILE_NAME.test(path.posix.basename(file))); describe("root test script globs", () => { it("finds globs in both scripts", () => { assert.ok(testGlobs(SCRIPTS.test).length > 0, "no test_*.ts glob in `test` — the parser or the script changed shape"); }); it("runs the same test files under `test` and `test:coverage`", () => { assert.deepEqual(testGlobs(SCRIPTS["test:coverage"]), testGlobs(SCRIPTS.test)); }); it("reaches every tracked test file", () => { const globs = testGlobs(SCRIPTS.test); const files = trackedTestFiles(); assert.ok(files.length > 0, "no tracked test files found — the listing changed shape"); assert.deepEqual( files.filter((file) => !globs.some((glob) => globMatches(glob, file))), [], "test files no glob in `test` / `test:coverage` runs", ); }); });