Files
ruflo/scripts/ci-test-ratchet.mjs
T
Mr. MakandClaude Opus 5 b02d936935 fix(ci): print the failing assertion, not just the filename, on a ratchet failure (#3208)
The ratchet runs vitest with `--reporter=json --outputFile=…`, so nothing
vitest prints reaches the job log and the only record of an unexpected
failure is the file name. Recovering the assertion means downloading the
`test-results-ubuntu-latest` artifact — and a rerun replaces it, so after a
rerun attempt 1's assertion is gone for good.

The failure messages are already in the report this process parsed. Printing
them costs nothing, needs no new artifact, and survives a rerun, because job
logs are per-attempt.

`formatUnexpectedFailures()` is exported and pure so it unit-tests alongside
`evaluateTestReport()`. Every field it reads is optional by design: a
file-level abort ("No test suite found in file") carries `message` and no
assertions, and a report from another reporter version may carry neither. In
both cases the filename still prints, exactly as before, and the PASS path is
untouched.

Verified against a real failing run rather than a fixture: replaying run
35521170139's `vitest.json` through this script turns

      + v3/@claude-flow/cli/__tests__/memory-search-scores-3327.test.ts

into that same line plus

      ✗ memory_search score contract (#3327 Finding B) preserves standard fallback on import failure
        AssertionError: expected [ { key: 'a', …(5) } ] to deeply equal [ { key: 'a', …(4) } ]

which is the line that would have identified the failure in #3208 without a
second run.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KTedGmPKpEdArVBMMn3JaP
2026-09-20 13:49:32 -04:00

155 lines
5.6 KiB
JavaScript

#!/usr/bin/env node
import { spawnSync } from 'node:child_process';
import { existsSync, mkdirSync, readFileSync, rmSync } from 'node:fs';
import { dirname, isAbsolute, relative, resolve, sep } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
const REPO_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..');
function normalizeTestPath(name, repoRoot) {
const normalized = isAbsolute(name) ? relative(repoRoot, name) : name;
return normalized.split(sep).join('/').replace(/^\.\//, '');
}
export function evaluateTestReport(report, baselineEntries, repoRoot = REPO_ROOT) {
if (!report || !Array.isArray(report.testResults)) {
return { ok: false, error: 'Vitest JSON report is missing testResults[]' };
}
if (report.testResults.length === 0 && report.success !== false) {
return { ok: false, error: 'Vitest JSON report contains zero test files' };
}
const baseline = new Set(
baselineEntries.map((entry) => entry.trim()).filter((entry) => entry && !entry.startsWith('#')),
);
const failed = new Set(
report.testResults
.filter((result) => result?.status === 'failed' && typeof result.name === 'string')
.map((result) => normalizeTestPath(result.name, repoRoot)),
);
const unexpected = [...failed].filter((name) => !baseline.has(name)).sort();
const fixed = [...baseline].filter((name) => !failed.has(name)).sort();
if (report.success === false && failed.size === 0) {
return {
ok: false,
error: 'Vitest failed without identifying a failing test file',
failed: [],
unexpected: [],
fixed,
};
}
return {
ok: unexpected.length === 0,
failed: [...failed].sort(),
unexpected,
fixed,
baselineCount: baseline.size,
};
}
/**
* Render each unexpected failure with the assertion that caused it (#3208).
*
* The report is written with --outputFile, so nothing vitest prints reaches
* the job log, and a rerun replaces the artifact — which leaves the filename
* as the only surviving record of an attempt. The failure messages are
* already in the report this process parsed, so putting them in the log
* costs nothing and survives the rerun, because logs are per-attempt.
*
* Every field is optional by design: a file-level abort ("No test suite
* found") has `message` and no assertions, and a report from another
* reporter version may carry neither.
*/
export function formatUnexpectedFailures(report, unexpected, repoRoot = REPO_ROOT) {
const lines = [];
for (const name of unexpected) {
lines.push(` + ${name}`);
const file = (report?.testResults ?? []).find(
(result) => typeof result?.name === 'string' && normalizeTestPath(result.name, repoRoot) === name,
);
const firstLine = (text) => String(text).split('\n')[0].trim();
if (file?.message) lines.push(` ${firstLine(file.message)}`);
for (const assertion of file?.assertionResults ?? []) {
if (assertion?.status !== 'failed') continue;
lines.push(` ✗ ${assertion.fullName || assertion.title || '(unnamed test)'}`);
const [message] = assertion.failureMessages ?? [];
if (message) lines.push(` ${firstLine(message)}`);
}
}
return lines;
}
function parseArgs(argv) {
const args = { report: '', baseline: resolve(REPO_ROOT, 'scripts/ci-test-baseline.txt'), run: true };
for (let i = 0; i < argv.length; i += 1) {
if (argv[i] === '--report' && argv[i + 1]) {
args.report = resolve(argv[++i]);
args.run = false;
} else if (argv[i] === '--baseline' && argv[i + 1]) {
args.baseline = resolve(argv[++i]);
} else {
throw new Error(`Unknown or incomplete argument: ${argv[i]}`);
}
}
return args;
}
function main() {
const args = parseArgs(process.argv.slice(2));
const reportPath = args.report || resolve(REPO_ROOT, '.test-results/vitest.json');
const vitestBin = resolve(REPO_ROOT, 'node_modules/vitest/vitest.mjs');
if (args.run) {
mkdirSync(dirname(reportPath), { recursive: true });
// A killed runner must not accidentally reuse a prior green-enough report.
rmSync(reportPath, { force: true });
const run = spawnSync(process.execPath, [
vitestBin,
'run',
'--reporter=json',
`--outputFile=${reportPath}`,
], {
cwd: REPO_ROOT,
env: { ...process.env, CI: '1' },
stdio: 'inherit',
});
if (run.error) throw run.error;
if (run.signal || (run.status !== 0 && run.status !== 1)) {
console.error(`CI test ratchet: Vitest terminated abnormally (${run.signal ?? run.status})`);
process.exit(1);
}
}
if (!existsSync(reportPath)) {
console.error(`CI test ratchet: report was not produced: ${reportPath}`);
process.exit(1);
}
if (!existsSync(args.baseline)) {
console.error(`CI test ratchet: baseline is missing: ${args.baseline}`);
process.exit(1);
}
const report = JSON.parse(readFileSync(reportPath, 'utf8'));
const baselineEntries = readFileSync(args.baseline, 'utf8').split(/\r?\n/);
const result = evaluateTestReport(report, baselineEntries);
if (!result.ok) {
console.error(`CI test ratchet: FAILED — ${result.unexpected?.length ?? 0} unexpected failing file(s)`);
for (const line of formatUnexpectedFailures(report, result.unexpected ?? [])) console.error(line);
if (result.error) console.error(` ${result.error}`);
process.exit(1);
}
console.log(
`CI test ratchet: PASS — ${result.failed.length}/${result.baselineCount} known failing files remain; `
+ `${result.fixed.length} baseline file(s) are now green`,
);
}
if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) {
main();
}