src/programs/snap-negative/rule-versions.test.ts
Linked repository file from this build.
import { describe, expect, test } from "bun:test";
import { execute } from "@/features/reviews/commands";
import { bindStoredRules } from "@/features/reviews/review-store";
import { createSeed } from "./seed";
import { calculate, selectFinal } from "./calculation";
import { validateReview } from "./validation";
import {
CURRENT_CALC_VERSION,
CURRENT_CHECK_VERSION,
LEGACY_CALC_VERSION,
PREVIOUS_CALC_VERSION,
} from "./rule-versions";
import { demoActor } from "@/lib/auth/roster";
const meta = { id: "rules", at: "2026-09-08T12:00:00Z" };
describe("review rule bindings", () => {
test("new reviews bind rules; unknown bound rules fail instead of falling through", () => {
const review = createSeed();
expect(review.calcVersion).toBe(CURRENT_CALC_VERSION);
expect(review.checkVersion).toBe(CURRENT_CHECK_VERSION);
expect(CURRENT_CHECK_VERSION).not.toBe("snap-negative-basic-v1");
review.calcVersion = "future";
expect(() => selectFinal(review)).toThrow("Unsupported calculation version");
review.checkVersion = "future";
expect(() => validateReview(review)).toThrow("Unsupported edit-check version");
});
test("only admin and statistics can rebind, invalidating prior results and checks", () => {
const review = createSeed();
review.facts.final = selectFinal(review);
review.validation = { revision: 0, findings: [], createdAt: meta.at };
for (const role of ["examiner", "pec", "supervisor", "program_manager"] as const)
expect(() =>
execute(review, { type: "apply_rules" }, { ...demoActor(role), name: role }, meta),
).toThrow();
const next = execute(
review,
{ type: "apply_rules" },
{ ...demoActor("admin"), name: "Avery" },
meta,
);
expect(next.facts.final).toBeUndefined();
expect(next.validation).toBeUndefined();
expect(review.facts.final).toBeDefined();
});
test("legacy reads use fixed IDs and preserve explicit old bindings", () => {
const review = createSeed();
const stored = {
review,
version: 1,
requestId: "old",
savedAt: meta.at,
actor: { ...demoActor("supervisor"), name: "Sam" },
};
delete (review as Partial<typeof review>).calcVersion;
expect(bindStoredRules(stored)?.review.calcVersion).toBe(LEGACY_CALC_VERSION);
review.calcVersion = "archived-v0";
expect(bindStoredRules(stored)?.review.calcVersion).toBe("archived-v0");
});
});
test("old calculations stay bound until an authorized rule update", () => {
const review = createSeed();
review.calcVersion = LEGACY_CALC_VERSION;
for (let householdSize = 1; householdSize <= 8; householdSize++) {
const scenario = { ...review.facts.scenarios.compI, householdSize };
const { policyId: oldId, ...oldResult } = calculate(
scenario,
review.sampleMonth,
LEGACY_CALC_VERSION,
);
const { policyId: newId, ...newResult } = calculate(
scenario,
review.sampleMonth,
PREVIOUS_CALC_VERSION,
);
expect(newId).not.toBe(oldId);
expect(newResult).toEqual(oldResult);
}
review.facts.scenarios.compI.householdSize = 20;
expect(() => selectFinal(review)).toThrow("apply current rules");
const next = execute(
review,
{ type: "apply_rules" },
{ ...demoActor("admin"), name: "Admin" },
meta,
);
expect(() => selectFinal(next)).not.toThrow();
});