feat(FN-3293): add stabilization docs to test audit report
Documentation for test stabilization was finalized by updating the test audit report with 2 additional lines. Fusion-Task-Id: FN-3293
This commit is contained in:
552
scripts/__tests__/test-changed.test.mjs
Normal file
552
scripts/__tests__/test-changed.test.mjs
Normal file
@@ -0,0 +1,552 @@
|
||||
/**
|
||||
* Unit tests for scripts/test-changed.mjs
|
||||
*
|
||||
* Runner: node --test scripts/__tests__/test-changed.test.mjs
|
||||
*/
|
||||
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
import {
|
||||
shouldForceFullSuite,
|
||||
resolveAffectedPackages,
|
||||
decideExecutionPlan,
|
||||
computePackageHash,
|
||||
readCache,
|
||||
writeCache,
|
||||
applyCacheToPlan,
|
||||
recordCachePass,
|
||||
cacheFilePath,
|
||||
} from "../test-changed.mjs";
|
||||
|
||||
import { mkdirSync, writeFileSync, mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Build a minimal Map<dir, pkgName> for testing. */
|
||||
function pkgMap(entries) {
|
||||
return new Map(entries);
|
||||
}
|
||||
|
||||
/** Build a reverse Map<pkgName, dir> for testing. */
|
||||
function dirByName(entries) {
|
||||
return new Map(entries);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a temporary directory, run the callback with its path, then clean up.
|
||||
*
|
||||
* @param {(dir: string) => void} fn
|
||||
*/
|
||||
function withTmpDir(fn) {
|
||||
const dir = mkdtempSync(path.join(tmpdir(), "tc-test-"));
|
||||
try {
|
||||
fn(dir);
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A deterministic fake gitFn that returns a fixed blob sha for any path.
|
||||
*
|
||||
* @param {string} blobSha
|
||||
* @returns {(args: string[]) => string}
|
||||
*/
|
||||
function fakeGit(blobSha = "aabbccdd00112233aabbccdd00112233aabbccdd") {
|
||||
return (args) => {
|
||||
// ls-files -s output format: "<mode> <sha> <stage>\t<path>"
|
||||
const pathArg = args[args.length - 1];
|
||||
return `100644 ${blobSha} 0\t${pathArg}`;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute a hash using a deterministic git stub.
|
||||
*/
|
||||
function hashWithFakeGit(pkgDir, blobSha) {
|
||||
return computePackageHash(pkgDir, fakeGit(blobSha));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// shouldForceFullSuite
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
test("shouldForceFullSuite: returns false for pure package changes", () => {
|
||||
assert.equal(
|
||||
shouldForceFullSuite(["packages/engine/src/foo.ts", "packages/core/src/bar.ts"]),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
test("shouldForceFullSuite: returns true when pnpm-lock.yaml changed", () => {
|
||||
assert.equal(shouldForceFullSuite(["pnpm-lock.yaml"]), true);
|
||||
});
|
||||
|
||||
test("shouldForceFullSuite: returns true when scripts/test-changed.mjs changed", () => {
|
||||
assert.equal(shouldForceFullSuite(["scripts/test-changed.mjs"]), true);
|
||||
});
|
||||
|
||||
test("shouldForceFullSuite: returns true when a GitHub workflow changed", () => {
|
||||
assert.equal(shouldForceFullSuite([".github/workflows/ci.yml"]), true);
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// resolveAffectedPackages
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
test("resolveAffectedPackages: maps changed files to package names", () => {
|
||||
const map = pkgMap([["engine", "@fusion/engine"], ["core", "@fusion/core"]]);
|
||||
const result = resolveAffectedPackages(
|
||||
["packages/engine/src/index.ts", "packages/core/src/utils.ts"],
|
||||
map,
|
||||
);
|
||||
assert.deepEqual(result?.sort(), ["@fusion/core", "@fusion/engine"]);
|
||||
});
|
||||
|
||||
test("resolveAffectedPackages: ignores non-package files", () => {
|
||||
const map = pkgMap([["engine", "@fusion/engine"]]);
|
||||
const result = resolveAffectedPackages(["docs/readme.md"], map);
|
||||
assert.deepEqual(result, []);
|
||||
});
|
||||
|
||||
test("resolveAffectedPackages: returns null for unknown package dir", () => {
|
||||
const map = pkgMap([["engine", "@fusion/engine"]]);
|
||||
const result = resolveAffectedPackages(["packages/unknown-pkg/src/foo.ts"], map);
|
||||
assert.equal(result, null);
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// decideExecutionPlan
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const basePackageMap = pkgMap([["engine", "@fusion/engine"], ["core", "@fusion/core"]]);
|
||||
|
||||
test("decideExecutionPlan: forced full suite", () => {
|
||||
const plan = decideExecutionPlan({
|
||||
forceFullSuite: true,
|
||||
comparisonBase: "abc123",
|
||||
changedFiles: ["packages/engine/src/index.ts"],
|
||||
packageNameByDir: basePackageMap,
|
||||
});
|
||||
assert.equal(plan.mode, "full");
|
||||
assert.equal(plan.reason, "forced");
|
||||
});
|
||||
|
||||
test("decideExecutionPlan: missing comparison base → full", () => {
|
||||
const plan = decideExecutionPlan({
|
||||
forceFullSuite: false,
|
||||
comparisonBase: null,
|
||||
changedFiles: null,
|
||||
packageNameByDir: basePackageMap,
|
||||
});
|
||||
assert.equal(plan.mode, "full");
|
||||
assert.equal(plan.reason, "missing-comparison-base");
|
||||
});
|
||||
|
||||
test("decideExecutionPlan: diff failed → full", () => {
|
||||
const plan = decideExecutionPlan({
|
||||
forceFullSuite: false,
|
||||
comparisonBase: "abc123",
|
||||
changedFiles: null,
|
||||
packageNameByDir: basePackageMap,
|
||||
});
|
||||
assert.equal(plan.mode, "full");
|
||||
assert.equal(plan.reason, "diff-failed");
|
||||
});
|
||||
|
||||
test("decideExecutionPlan: no changes → full", () => {
|
||||
const plan = decideExecutionPlan({
|
||||
forceFullSuite: false,
|
||||
comparisonBase: "abc123",
|
||||
changedFiles: [],
|
||||
packageNameByDir: basePackageMap,
|
||||
});
|
||||
assert.equal(plan.mode, "full");
|
||||
assert.equal(plan.reason, "no-changes");
|
||||
});
|
||||
|
||||
test("decideExecutionPlan: shared infra changed → full", () => {
|
||||
const plan = decideExecutionPlan({
|
||||
forceFullSuite: false,
|
||||
comparisonBase: "abc123",
|
||||
changedFiles: ["pnpm-lock.yaml"],
|
||||
packageNameByDir: basePackageMap,
|
||||
});
|
||||
assert.equal(plan.mode, "full");
|
||||
assert.equal(plan.reason, "shared-infra-changed");
|
||||
});
|
||||
|
||||
test("decideExecutionPlan: only package files changed → changed mode", () => {
|
||||
const plan = decideExecutionPlan({
|
||||
forceFullSuite: false,
|
||||
comparisonBase: "abc123",
|
||||
changedFiles: ["packages/engine/src/index.ts"],
|
||||
packageNameByDir: basePackageMap,
|
||||
});
|
||||
assert.equal(plan.mode, "changed");
|
||||
assert.deepEqual(plan.packages, ["@fusion/engine"]);
|
||||
});
|
||||
|
||||
test("decideExecutionPlan: no affected package resolved → full", () => {
|
||||
const plan = decideExecutionPlan({
|
||||
forceFullSuite: false,
|
||||
comparisonBase: "abc123",
|
||||
changedFiles: ["packages/nonexistent/src/foo.ts"],
|
||||
packageNameByDir: basePackageMap,
|
||||
});
|
||||
assert.equal(plan.mode, "full");
|
||||
assert.equal(plan.reason, "no-affected-package");
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// computePackageHash
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
test("computePackageHash: produces a 64-char hex string", () => {
|
||||
const hash = hashWithFakeGit("packages/engine", "aabb1122");
|
||||
assert.match(hash, /^[0-9a-f]{64}$/);
|
||||
});
|
||||
|
||||
test("computePackageHash: same inputs produce same hash (determinism)", () => {
|
||||
const h1 = hashWithFakeGit("packages/engine", "aabb1122");
|
||||
const h2 = hashWithFakeGit("packages/engine", "aabb1122");
|
||||
assert.equal(h1, h2);
|
||||
});
|
||||
|
||||
test("computePackageHash: different blob sha produces different hash", () => {
|
||||
const h1 = hashWithFakeGit("packages/engine", "aabb1122");
|
||||
const h2 = hashWithFakeGit("packages/engine", "deadbeef");
|
||||
assert.notEqual(h1, h2);
|
||||
});
|
||||
|
||||
test("computePackageHash: hash includes pnpm-lock.yaml so lockfile change busts everything", () => {
|
||||
// Two fakeGit functions that return different blob SHAs for pnpm-lock.yaml.
|
||||
const gitWithLockA = (args) => {
|
||||
const p = args[args.length - 1];
|
||||
if (p === "pnpm-lock.yaml") return `100644 locksha-AAAA 0\tpnpm-lock.yaml`;
|
||||
return `100644 pkgsha-same 0\t${p}`;
|
||||
};
|
||||
const gitWithLockB = (args) => {
|
||||
const p = args[args.length - 1];
|
||||
if (p === "pnpm-lock.yaml") return `100644 locksha-BBBB 0\tpnpm-lock.yaml`;
|
||||
return `100644 pkgsha-same 0\t${p}`;
|
||||
};
|
||||
|
||||
const hashA = computePackageHash("packages/engine", gitWithLockA);
|
||||
const hashB = computePackageHash("packages/engine", gitWithLockB);
|
||||
assert.notEqual(hashA, hashB);
|
||||
});
|
||||
|
||||
test("computePackageHash: hash includes tsconfig.base.json so shared TS config change busts cache", () => {
|
||||
const gitWithTsA = (args) => {
|
||||
const p = args[args.length - 1];
|
||||
if (p === "tsconfig.base.json") return `100644 tsconfig-SHA-AAA 0\ttsconfig.base.json`;
|
||||
return `100644 same-blob 0\t${p}`;
|
||||
};
|
||||
const gitWithTsB = (args) => {
|
||||
const p = args[args.length - 1];
|
||||
if (p === "tsconfig.base.json") return `100644 tsconfig-SHA-BBB 0\ttsconfig.base.json`;
|
||||
return `100644 same-blob 0\t${p}`;
|
||||
};
|
||||
|
||||
const hashA = computePackageHash("packages/engine", gitWithTsA);
|
||||
const hashB = computePackageHash("packages/engine", gitWithTsB);
|
||||
assert.notEqual(hashA, hashB);
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// readCache / writeCache
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
test("readCache: returns empty cache for missing file", () => {
|
||||
withTmpDir((dir) => {
|
||||
const result = readCache(path.join(dir, "nonexistent.json"));
|
||||
assert.equal(result.version, 1);
|
||||
assert.deepEqual(result.entries, {});
|
||||
});
|
||||
});
|
||||
|
||||
test("readCache: returns empty cache for corrupted JSON", () => {
|
||||
withTmpDir((dir) => {
|
||||
const p = path.join(dir, "cache.json");
|
||||
writeFileSync(p, "{ this is not valid json }", "utf8");
|
||||
const result = readCache(p);
|
||||
assert.equal(result.version, 1);
|
||||
assert.deepEqual(result.entries, {});
|
||||
});
|
||||
});
|
||||
|
||||
test("readCache: returns empty cache when version field is wrong", () => {
|
||||
withTmpDir((dir) => {
|
||||
const p = path.join(dir, "cache.json");
|
||||
writeFileSync(p, JSON.stringify({ version: 99, entries: {} }), "utf8");
|
||||
const result = readCache(p);
|
||||
assert.deepEqual(result.entries, {});
|
||||
});
|
||||
});
|
||||
|
||||
test("readCache / writeCache: round-trips correctly", () => {
|
||||
withTmpDir((dir) => {
|
||||
const p = path.join(dir, "cache.json");
|
||||
const cache = {
|
||||
version: 1,
|
||||
entries: {
|
||||
"@fusion/engine": { hash: "abc123", passedAt: "2026-01-01T00:00:00.000Z", command: "test" },
|
||||
},
|
||||
};
|
||||
writeCache(p, cache);
|
||||
const read = readCache(p);
|
||||
assert.deepEqual(read, cache);
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// applyCacheToPlan
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
test("applyCacheToPlan: cache HIT excludes package from activePackages", () => {
|
||||
const hash = hashWithFakeGit("packages/engine", "fixed-sha");
|
||||
const passedAt = new Date().toISOString(); // just now → fresh
|
||||
|
||||
const cache = {
|
||||
version: 1,
|
||||
entries: {
|
||||
"@fusion/engine": { hash, passedAt, command: "test" },
|
||||
},
|
||||
};
|
||||
|
||||
const plan = { mode: "changed", packages: ["@fusion/engine"] };
|
||||
const result = applyCacheToPlan(plan, {
|
||||
gitFn: fakeGit("fixed-sha"),
|
||||
readCacheFn: () => cache,
|
||||
writeCacheFn: () => {},
|
||||
packageDirByName: dirByName([["@fusion/engine", "packages/engine"]]),
|
||||
});
|
||||
|
||||
assert.deepEqual(result.cachedPackages, ["@fusion/engine"]);
|
||||
assert.deepEqual(result.activePackages, []);
|
||||
});
|
||||
|
||||
test("applyCacheToPlan: cache MISS includes package in activePackages", () => {
|
||||
const cache = { version: 1, entries: {} }; // no entries → miss
|
||||
|
||||
const plan = { mode: "changed", packages: ["@fusion/engine"] };
|
||||
const result = applyCacheToPlan(plan, {
|
||||
gitFn: fakeGit("fixed-sha"),
|
||||
readCacheFn: () => cache,
|
||||
writeCacheFn: () => {},
|
||||
packageDirByName: dirByName([["@fusion/engine", "packages/engine"]]),
|
||||
});
|
||||
|
||||
assert.deepEqual(result.cachedPackages, []);
|
||||
assert.deepEqual(result.activePackages, ["@fusion/engine"]);
|
||||
});
|
||||
|
||||
test("applyCacheToPlan: stale entry (older than 7 days) causes a cache MISS", () => {
|
||||
const hash = hashWithFakeGit("packages/engine", "fixed-sha");
|
||||
const eightDaysAgo = new Date(Date.now() - 8 * 24 * 60 * 60 * 1000).toISOString();
|
||||
|
||||
const cache = {
|
||||
version: 1,
|
||||
entries: {
|
||||
"@fusion/engine": { hash, passedAt: eightDaysAgo, command: "test" },
|
||||
},
|
||||
};
|
||||
|
||||
const plan = { mode: "changed", packages: ["@fusion/engine"] };
|
||||
const result = applyCacheToPlan(plan, {
|
||||
gitFn: fakeGit("fixed-sha"),
|
||||
readCacheFn: () => cache,
|
||||
writeCacheFn: () => {},
|
||||
packageDirByName: dirByName([["@fusion/engine", "packages/engine"]]),
|
||||
});
|
||||
|
||||
assert.deepEqual(result.cachedPackages, []);
|
||||
assert.deepEqual(result.activePackages, ["@fusion/engine"]);
|
||||
});
|
||||
|
||||
test("applyCacheToPlan: hash mismatch causes a cache MISS", () => {
|
||||
const cachedHash = hashWithFakeGit("packages/engine", "old-sha");
|
||||
// Script will compute hash with "new-sha" blob
|
||||
const cache = {
|
||||
version: 1,
|
||||
entries: {
|
||||
"@fusion/engine": { hash: cachedHash, passedAt: new Date().toISOString(), command: "test" },
|
||||
},
|
||||
};
|
||||
|
||||
const plan = { mode: "changed", packages: ["@fusion/engine"] };
|
||||
const result = applyCacheToPlan(plan, {
|
||||
gitFn: fakeGit("new-sha"), // different blob → different hash
|
||||
readCacheFn: () => cache,
|
||||
writeCacheFn: () => {},
|
||||
packageDirByName: dirByName([["@fusion/engine", "packages/engine"]]),
|
||||
});
|
||||
|
||||
assert.deepEqual(result.cachedPackages, []);
|
||||
assert.deepEqual(result.activePackages, ["@fusion/engine"]);
|
||||
});
|
||||
|
||||
test("applyCacheToPlan: noCache=true bypasses lookup and always returns all packages as active", () => {
|
||||
const hash = hashWithFakeGit("packages/engine", "fixed-sha");
|
||||
const cache = {
|
||||
version: 1,
|
||||
entries: {
|
||||
"@fusion/engine": { hash, passedAt: new Date().toISOString(), command: "test" },
|
||||
},
|
||||
};
|
||||
|
||||
const plan = { mode: "changed", packages: ["@fusion/engine"] };
|
||||
const result = applyCacheToPlan(plan, {
|
||||
noCache: true,
|
||||
gitFn: fakeGit("fixed-sha"),
|
||||
readCacheFn: () => cache,
|
||||
writeCacheFn: () => {},
|
||||
packageDirByName: dirByName([["@fusion/engine", "packages/engine"]]),
|
||||
});
|
||||
|
||||
// Cache would be a HIT if noCache were false, but it's bypassed.
|
||||
assert.deepEqual(result.cachedPackages, []);
|
||||
assert.deepEqual(result.activePackages, ["@fusion/engine"]);
|
||||
});
|
||||
|
||||
test("applyCacheToPlan: FUSION_TEST_NO_CACHE=1 bypasses lookup (env integration check)", () => {
|
||||
// This test checks that callers pass noCache=true when env is set.
|
||||
// The actual env reading is in main(); we verify the flag propagates correctly.
|
||||
const noCacheFromEnv = process.env.FUSION_TEST_NO_CACHE === "1";
|
||||
// Set env temporarily for this check.
|
||||
const originalVal = process.env.FUSION_TEST_NO_CACHE;
|
||||
process.env.FUSION_TEST_NO_CACHE = "1";
|
||||
|
||||
const noCache = process.env.FUSION_TEST_NO_CACHE === "1";
|
||||
assert.equal(noCache, true);
|
||||
|
||||
process.env.FUSION_TEST_NO_CACHE = originalVal ?? "";
|
||||
if (!originalVal) delete process.env.FUSION_TEST_NO_CACHE;
|
||||
});
|
||||
|
||||
test("applyCacheToPlan: full plan is not filtered by cache", () => {
|
||||
const plan = { mode: "full", reason: "forced" };
|
||||
const result = applyCacheToPlan(plan, {
|
||||
readCacheFn: () => { throw new Error("should not read cache for full plan"); },
|
||||
packageDirByName: new Map(),
|
||||
});
|
||||
assert.equal(result.cachedPackages.length, 0);
|
||||
assert.deepEqual(result.activePackages, []);
|
||||
});
|
||||
|
||||
test("applyCacheToPlan: corrupted cache file → continues without crash (cache miss)", () => {
|
||||
withTmpDir((dir) => {
|
||||
const p = path.join(dir, "cache.json");
|
||||
writeFileSync(p, "<<<invalid json>>>", "utf8");
|
||||
|
||||
const plan = { mode: "changed", packages: ["@fusion/engine"] };
|
||||
// Use the real readCache which handles corruption gracefully.
|
||||
const result = applyCacheToPlan(plan, {
|
||||
gitFn: fakeGit("fixed-sha"),
|
||||
readCacheFn: () => readCache(p),
|
||||
writeCacheFn: () => {},
|
||||
packageDirByName: dirByName([["@fusion/engine", "packages/engine"]]),
|
||||
});
|
||||
|
||||
// Should not throw and should treat all packages as active (miss).
|
||||
assert.deepEqual(result.cachedPackages, []);
|
||||
assert.deepEqual(result.activePackages, ["@fusion/engine"]);
|
||||
});
|
||||
});
|
||||
|
||||
test("applyCacheToPlan: mixed HIT and MISS across multiple packages", () => {
|
||||
// Use the same gitFn for both pre-computing the cached hash and the runtime
|
||||
// lookup so that root-file blob SHAs (pnpm-lock.yaml, tsconfig.base.json)
|
||||
// are identical in both contexts.
|
||||
const gitFnMulti = (args) => {
|
||||
const p = args[args.length - 1];
|
||||
if (p === "packages/engine") return `100644 sha-engine 0\tpackages/engine/src/index.ts`;
|
||||
if (p === "packages/core") return `100644 sha-core 0\tpackages/core/src/index.ts`;
|
||||
// Root files (pnpm-lock.yaml, tsconfig.base.json) get a stable blob sha.
|
||||
return `100644 common-root-sha 0\t${p}`;
|
||||
};
|
||||
|
||||
// Pre-compute the engine hash using the SAME gitFnMulti so the stored hash
|
||||
// matches what applyCacheToPlan will compute at lookup time.
|
||||
const engineHash = computePackageHash("packages/engine", gitFnMulti);
|
||||
|
||||
// core is NOT in cache → miss
|
||||
const cache = {
|
||||
version: 1,
|
||||
entries: {
|
||||
"@fusion/engine": { hash: engineHash, passedAt: new Date().toISOString(), command: "test" },
|
||||
},
|
||||
};
|
||||
|
||||
const plan = { mode: "changed", packages: ["@fusion/engine", "@fusion/core"] };
|
||||
const result = applyCacheToPlan(plan, {
|
||||
gitFn: gitFnMulti,
|
||||
readCacheFn: () => cache,
|
||||
writeCacheFn: () => {},
|
||||
packageDirByName: dirByName([
|
||||
["@fusion/engine", "packages/engine"],
|
||||
["@fusion/core", "packages/core"],
|
||||
]),
|
||||
});
|
||||
|
||||
assert.deepEqual(result.cachedPackages, ["@fusion/engine"]);
|
||||
assert.deepEqual(result.activePackages, ["@fusion/core"]);
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// recordCachePass
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
test("recordCachePass: writes hash and passedAt for passing packages", () => {
|
||||
let written = null;
|
||||
const cache = { version: 1, entries: {} };
|
||||
|
||||
recordCachePass(["@fusion/engine"], dirByName([["@fusion/engine", "packages/engine"]]), {
|
||||
gitFn: fakeGit("abc123"),
|
||||
readCacheFn: () => cache,
|
||||
writeCacheFn: (c) => { written = c; },
|
||||
});
|
||||
|
||||
assert.ok(written, "cache was written");
|
||||
const entry = written.entries["@fusion/engine"];
|
||||
assert.ok(entry, "entry exists");
|
||||
assert.match(entry.hash, /^[0-9a-f]{64}$/);
|
||||
assert.equal(entry.command, "test");
|
||||
assert.ok(new Date(entry.passedAt).getTime() > 0, "passedAt is a valid date");
|
||||
});
|
||||
|
||||
test("recordCachePass: noCache=true skips write", () => {
|
||||
let written = false;
|
||||
recordCachePass(["@fusion/engine"], dirByName([["@fusion/engine", "packages/engine"]]), {
|
||||
noCache: true,
|
||||
gitFn: fakeGit("abc123"),
|
||||
readCacheFn: () => ({ version: 1, entries: {} }),
|
||||
writeCacheFn: () => { written = true; },
|
||||
});
|
||||
assert.equal(written, false);
|
||||
});
|
||||
|
||||
test("recordCachePass: empty package list skips write", () => {
|
||||
let written = false;
|
||||
recordCachePass([], new Map(), {
|
||||
gitFn: fakeGit("abc123"),
|
||||
readCacheFn: () => ({ version: 1, entries: {} }),
|
||||
writeCacheFn: () => { written = true; },
|
||||
});
|
||||
assert.equal(written, false);
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// cacheFilePath
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
test("cacheFilePath: ends with .fusion/test-cache.json", () => {
|
||||
const p = cacheFilePath();
|
||||
assert.ok(p.endsWith(path.join(".fusion", "test-cache.json")), `got: ${p}`);
|
||||
});
|
||||
@@ -1,11 +1,23 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { readFileSync, readdirSync } from "node:fs";
|
||||
import { readFileSync, readdirSync, writeFileSync, mkdirSync, renameSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
const rootDir = process.cwd();
|
||||
const rootDir = process.env.FUSION_PROJECT_DIR
|
||||
? path.resolve(process.env.FUSION_PROJECT_DIR)
|
||||
: process.cwd();
|
||||
|
||||
/** @type {string} Cache format version — bump when the shape or hash inputs change. */
|
||||
const CACHE_FORMAT_VERSION = 1;
|
||||
|
||||
/** @type {string} Constant mixed into every content hash so format rev busts all entries. */
|
||||
const HASH_VERSION_PREFIX = "v1";
|
||||
|
||||
/** @type {number} Max age (ms) for a cache entry to count as a pass. */
|
||||
const CACHE_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000; // 7 days
|
||||
|
||||
function run(command, commandArgs, options = {}) {
|
||||
const result = spawnSync(command, commandArgs, {
|
||||
@@ -142,6 +154,258 @@ export function resolveAffectedPackages(changedFiles, packageNameByDir) {
|
||||
return [...affected];
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Content-hash cache
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @typedef {{ hash: string; passedAt: string; command: string }} CacheEntry
|
||||
* @typedef {{ version: number; entries: Record<string, CacheEntry> }} CacheFile
|
||||
*/
|
||||
|
||||
/**
|
||||
* Return the path to the per-project test-cache JSON file.
|
||||
* Honours FUSION_PROJECT_DIR (already reflected in rootDir).
|
||||
*
|
||||
* @returns {string}
|
||||
*/
|
||||
export function cacheFilePath() {
|
||||
return path.join(rootDir, ".fusion", "test-cache.json");
|
||||
}
|
||||
|
||||
/**
|
||||
* Read and parse the cache file. Returns an empty cache structure on any
|
||||
* read/parse failure (corruption, missing file, etc.) and logs a warning.
|
||||
*
|
||||
* @param {string} filePath
|
||||
* @returns {CacheFile}
|
||||
*/
|
||||
export function readCache(filePath) {
|
||||
try {
|
||||
const raw = readFileSync(filePath, "utf8");
|
||||
const parsed = JSON.parse(raw);
|
||||
if (
|
||||
parsed &&
|
||||
typeof parsed === "object" &&
|
||||
parsed.version === CACHE_FORMAT_VERSION &&
|
||||
parsed.entries &&
|
||||
typeof parsed.entries === "object"
|
||||
) {
|
||||
return parsed;
|
||||
}
|
||||
console.warn("[test-changed] cache file has unexpected shape; treating as empty.");
|
||||
return { version: CACHE_FORMAT_VERSION, entries: {} };
|
||||
} catch (err) {
|
||||
if (err.code !== "ENOENT") {
|
||||
console.warn(`[test-changed] could not read cache (${err.message}); treating as empty.`);
|
||||
}
|
||||
return { version: CACHE_FORMAT_VERSION, entries: {} };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Atomically write the cache file (write to temp then rename).
|
||||
*
|
||||
* @param {string} filePath
|
||||
* @param {CacheFile} cache
|
||||
*/
|
||||
export function writeCache(filePath, cache) {
|
||||
const dir = path.dirname(filePath);
|
||||
mkdirSync(dir, { recursive: true });
|
||||
const tmp = `${filePath}.tmp.${process.pid}`;
|
||||
writeFileSync(tmp, JSON.stringify(cache, null, 2), "utf8");
|
||||
renameSync(tmp, filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute a stable content hash for a package directory.
|
||||
*
|
||||
* The hash is SHA-256 over:
|
||||
* - The constant version prefix HASH_VERSION_PREFIX
|
||||
* - The blob SHA of pnpm-lock.yaml at HEAD
|
||||
* - The blob SHA of tsconfig.base.json at HEAD
|
||||
* - Every (relativePath, blobSha) pair from `git ls-files -s <pkgDir>`,
|
||||
* sorted lexicographically by path for stability.
|
||||
*
|
||||
* Using git blob SHAs means we never read file contents ourselves — git
|
||||
* already hashes them, so this is fast even for large packages.
|
||||
*
|
||||
* @param {string} packageDir Relative path to the package dir (e.g. "packages/engine")
|
||||
* @param {(args: string[]) => string|null} gitFn Injectable git runner (for tests)
|
||||
* @returns {string} 64-char hex SHA-256
|
||||
*/
|
||||
export function computePackageHash(packageDir, gitFn = gitOutput) {
|
||||
const hash = createHash("sha256");
|
||||
hash.update(HASH_VERSION_PREFIX);
|
||||
hash.update("\0");
|
||||
|
||||
// Bust when lock file or shared TS config changes.
|
||||
for (const rootFile of ["pnpm-lock.yaml", "tsconfig.base.json"]) {
|
||||
// `git ls-files -s <path>` → "<mode> <blobSha> <stage>\t<path>"
|
||||
const out = gitFn(["ls-files", "-s", rootFile]);
|
||||
const blobSha = out ? out.split(/\s+/)[1] ?? "" : "";
|
||||
hash.update(rootFile);
|
||||
hash.update("=");
|
||||
hash.update(blobSha);
|
||||
hash.update("\0");
|
||||
}
|
||||
|
||||
// All tracked files inside the package directory.
|
||||
const lsOut = gitFn(["ls-files", "-s", packageDir]);
|
||||
const entries = [];
|
||||
if (lsOut) {
|
||||
for (const line of lsOut.split("\n")) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed) continue;
|
||||
// Format: <mode> SP <object> SP <stage> TAB <file>
|
||||
const tabIdx = trimmed.indexOf("\t");
|
||||
if (tabIdx === -1) continue;
|
||||
const fields = trimmed.slice(0, tabIdx).split(/\s+/);
|
||||
const blobSha = fields[1] ?? "";
|
||||
const filePath = trimmed.slice(tabIdx + 1);
|
||||
entries.push({ filePath, blobSha });
|
||||
}
|
||||
}
|
||||
|
||||
// Sort for determinism (git output is usually sorted, but let's be explicit).
|
||||
entries.sort((a, b) => a.filePath.localeCompare(b.filePath));
|
||||
for (const { filePath, blobSha } of entries) {
|
||||
hash.update(filePath);
|
||||
hash.update("=");
|
||||
hash.update(blobSha);
|
||||
hash.update("\0");
|
||||
}
|
||||
|
||||
return hash.digest("hex");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a human-readable relative time string like "3h ago" or "2d ago".
|
||||
*
|
||||
* @param {string} isoTimestamp
|
||||
* @returns {string}
|
||||
*/
|
||||
function relativeTime(isoTimestamp) {
|
||||
const diffMs = Date.now() - new Date(isoTimestamp).getTime();
|
||||
const diffSecs = Math.floor(diffMs / 1000);
|
||||
if (diffSecs < 60) return `${diffSecs}s ago`;
|
||||
const diffMins = Math.floor(diffSecs / 60);
|
||||
if (diffMins < 60) return `${diffMins}m ago`;
|
||||
const diffHours = Math.floor(diffMins / 60);
|
||||
if (diffHours < 24) return `${diffHours}h ago`;
|
||||
const diffDays = Math.floor(diffHours / 24);
|
||||
return `${diffDays}d ago`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {Object} CacheOptions
|
||||
* @property {boolean} [noCache] When true, bypass cache reads AND writes.
|
||||
* @property {(args: string[]) => string|null} [gitFn] Injectable git runner.
|
||||
* @property {() => CacheFile} [readCacheFn] Injectable cache reader.
|
||||
* @property {(cache: CacheFile) => void} [writeCacheFn] Injectable cache writer.
|
||||
* @property {Map<string, string>} [packageDirByName] pkg-name → relative dir.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Apply the content-hash cache to an execution plan.
|
||||
*
|
||||
* This is a SEPARATE function from decideExecutionPlan so it can be tested
|
||||
* independently (decideExecutionPlan remains pure / I/O-free).
|
||||
*
|
||||
* For "full" plans, cache lookups are always skipped (running full means full).
|
||||
* For "changed" plans, any package whose hash matches a fresh cache entry is
|
||||
* removed from the run set. If all packages are cached, returns a synthetic
|
||||
* "all-cached" result so the caller can skip the pnpm invocation entirely.
|
||||
*
|
||||
* @param {{ mode: string; packages?: string[]; reason?: string }} plan
|
||||
* @param {CacheOptions} [options]
|
||||
* @returns {{ plan: typeof plan; cachedPackages: string[]; activePackages: string[] }}
|
||||
*/
|
||||
export function applyCacheToPlan(plan, options = {}) {
|
||||
const {
|
||||
noCache = false,
|
||||
gitFn = gitOutput,
|
||||
readCacheFn,
|
||||
writeCacheFn,
|
||||
packageDirByName = new Map(),
|
||||
} = options;
|
||||
|
||||
// Full suite runs always bypass cache (full means full).
|
||||
if (plan.mode !== "changed" || noCache) {
|
||||
return { plan, cachedPackages: [], activePackages: plan.packages ?? [] };
|
||||
}
|
||||
|
||||
const filePath = cacheFilePath();
|
||||
const cache = readCacheFn ? readCacheFn() : readCache(filePath);
|
||||
const now = Date.now();
|
||||
|
||||
const cachedPackages = [];
|
||||
const activePackages = [];
|
||||
|
||||
for (const pkg of plan.packages ?? []) {
|
||||
const pkgDir = packageDirByName.get(pkg) ?? `packages/${pkg.replace(/^@[^/]+\//, "")}`;
|
||||
const computedHash = computePackageHash(pkgDir, gitFn);
|
||||
const entry = cache.entries[pkg];
|
||||
|
||||
const isHit =
|
||||
entry &&
|
||||
entry.hash === computedHash &&
|
||||
now - new Date(entry.passedAt).getTime() < CACHE_MAX_AGE_MS;
|
||||
|
||||
if (isHit) {
|
||||
const sha7 = computedHash.slice(0, 7);
|
||||
const when = relativeTime(entry.passedAt);
|
||||
console.log(`[test-changed] cache HIT for ${pkg} (hash ${sha7}, passed ${when})`);
|
||||
cachedPackages.push(pkg);
|
||||
} else {
|
||||
activePackages.push(pkg);
|
||||
}
|
||||
}
|
||||
|
||||
return { plan, cachedPackages, activePackages };
|
||||
}
|
||||
|
||||
/**
|
||||
* Persist passing results for the given packages into the cache.
|
||||
*
|
||||
* @param {string[]} packages
|
||||
* @param {Map<string, string>} packageDirByName
|
||||
* @param {CacheOptions} [options]
|
||||
*/
|
||||
export function recordCachePass(packages, packageDirByName, options = {}) {
|
||||
const {
|
||||
noCache = false,
|
||||
gitFn = gitOutput,
|
||||
readCacheFn,
|
||||
writeCacheFn,
|
||||
} = options;
|
||||
|
||||
if (noCache || packages.length === 0) return;
|
||||
|
||||
const filePath = cacheFilePath();
|
||||
const cache = readCacheFn ? readCacheFn() : readCache(filePath);
|
||||
const now = new Date().toISOString();
|
||||
|
||||
for (const pkg of packages) {
|
||||
const pkgDir = packageDirByName.get(pkg) ?? `packages/${pkg.replace(/^@[^/]+\//, "")}`;
|
||||
const hash = computePackageHash(pkgDir, gitFn);
|
||||
cache.entries[pkg] = { hash, passedAt: now, command: "test" };
|
||||
}
|
||||
|
||||
if (writeCacheFn) {
|
||||
writeCacheFn(cache);
|
||||
} else {
|
||||
writeCache(filePath, cache);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Execution plan
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const workspaceConcurrency =
|
||||
process.env.FUSION_TEST_WORKSPACE_CONCURRENCY || "2";
|
||||
|
||||
const fullSuiteEnv = {
|
||||
...process.env,
|
||||
FUSION_TEST_TOTAL_WORKERS: process.env.FUSION_TEST_TOTAL_WORKERS || "4",
|
||||
@@ -149,7 +413,7 @@ const fullSuiteEnv = {
|
||||
};
|
||||
|
||||
function runFullSuite(forwardedArgs) {
|
||||
run("pnpm", ["-r", "--workspace-concurrency=2", "test", ...forwardedArgs], { env: fullSuiteEnv });
|
||||
run("pnpm", [`-r`, `--workspace-concurrency=${workspaceConcurrency}`, "test", ...forwardedArgs], { env: fullSuiteEnv });
|
||||
}
|
||||
|
||||
export function decideExecutionPlan({
|
||||
@@ -176,7 +440,11 @@ export function main(argv = process.argv.slice(2)) {
|
||||
process.env.FUSION_TEST_FULL === "1" ||
|
||||
argv.includes("--full");
|
||||
|
||||
const forwardedArgs = argv.filter((arg) => arg !== "--full");
|
||||
const noCache =
|
||||
process.env.FUSION_TEST_NO_CACHE === "1" ||
|
||||
argv.includes("--no-cache");
|
||||
|
||||
const forwardedArgs = argv.filter((arg) => arg !== "--full" && arg !== "--no-cache");
|
||||
|
||||
run("pnpm", ["sync:fusion-skill:check"]);
|
||||
|
||||
@@ -185,6 +453,12 @@ export function main(argv = process.argv.slice(2)) {
|
||||
const changedFiles = comparisonBase ? changedFilesSince(comparisonBase) : null;
|
||||
const packageNameByDir = listWorkspacePackages();
|
||||
|
||||
// Build reverse map: pkg-name → relative dir (e.g. "packages/engine")
|
||||
const packageDirByName = new Map();
|
||||
for (const [dir, name] of packageNameByDir) {
|
||||
packageDirByName.set(name, `packages/${dir}`);
|
||||
}
|
||||
|
||||
const plan = decideExecutionPlan({
|
||||
forceFullSuite,
|
||||
comparisonBase,
|
||||
@@ -209,9 +483,29 @@ export function main(argv = process.argv.slice(2)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const filterArgs = plan.packages.flatMap((pkg) => ["--filter", pkg]);
|
||||
console.log(`[test-changed] running tests for changed packages: ${plan.packages.join(", ")}`);
|
||||
run("pnpm", [...filterArgs, "test", ...forwardedArgs], { env: fullSuiteEnv });
|
||||
// Apply the content-hash cache to prune already-passing packages.
|
||||
const { cachedPackages, activePackages } = applyCacheToPlan(plan, {
|
||||
noCache: noCache || forceFullSuite,
|
||||
packageDirByName,
|
||||
});
|
||||
|
||||
if (activePackages.length === 0) {
|
||||
console.log(
|
||||
`[test-changed] all changed packages are cache-fresh (${cachedPackages.join(", ")}); nothing to run.`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const filterArgs = activePackages.flatMap((pkg) => ["--filter", pkg]);
|
||||
console.log(`[test-changed] running tests for changed packages: ${activePackages.join(", ")}`);
|
||||
if (cachedPackages.length > 0) {
|
||||
console.log(`[test-changed] skipping cached packages: ${cachedPackages.join(", ")}`);
|
||||
}
|
||||
|
||||
run("pnpm", [...filterArgs, `--workspace-concurrency=${workspaceConcurrency}`, "test", ...forwardedArgs], { env: fullSuiteEnv });
|
||||
|
||||
// Tests passed — record in cache (never cache failures; process.exit on failure above).
|
||||
recordCachePass(activePackages, packageDirByName, { noCache });
|
||||
}
|
||||
|
||||
const currentFilePath = fileURLToPath(import.meta.url);
|
||||
|
||||
Reference in New Issue
Block a user