Documentation for test stabilization was finalized by updating the test audit report with 2 additional lines. Fusion-Task-Id: FN-3293
553 lines
19 KiB
JavaScript
553 lines
19 KiB
JavaScript
/**
|
|
* 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}`);
|
|
});
|