In Decide Which Prompt Ships, your code turned recorded runs into a decision. Soon a pull request check will run that code on every change, and nobody will reread it. If its pass rate is wrong, every decision built on it is too, and no eval will notice.
In this lesson, you’ll test the pass rate and comparison code with node:test, the built-in Node.js test runner, and use a failing report to fix a real bug. Start by moving that code where a test can reach it.
Write Your First Test
A test reaches code only by importing it, and importing rerun.js would run it and call the model. So move the pass rate line from rerun.js and the pairing step from compare.js into a module that only defines them:
evals/stats.js
export function passRate(passed, total) {
return (100 * passed / total).toFixed(1);
}
export function shareChanges(a, b) {
const ids = Object.keys(a.passes);
const same = ids.length === Object.keys(b.passes).length
&& ids.every((id) => id in b.passes);
if (!same) throw new Error("results hold different cases");
const changes = {};
for (const id of ids) {
changes[id] = b.passes[id] / b.runs - a.passes[id] / a.runs;
}
return changes;
}
shareChanges() checks that both results objects hold the same cases, then returns each case’s change in pass share. In compare.js, it replaces the case check and the loop’s change calculation, while the runs check and the reporting stay:
evals/compare.js
import { shareChanges } from "./stats.js";
// ...
const changes = shareChanges(a, b);
const ids = Object.keys(changes);
// The runs check and the per-run means stay here.
for (const id of ids) {
const change = changes[id];
const shareA = a.passes[id] / a.runs;
const shareB = b.passes[id] / b.runs;
// The rest of the loop stays as it was.
}
In rerun.js, call passRate() for each run’s rate.