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:
Fusion
2026-05-04 11:38:40 -07:00
committed by gsxdsm
parent 4b6c11ff0a
commit c9e776cb34
15 changed files with 1314 additions and 51 deletions

View File

@@ -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);