src/programs/snap-negative/seed-cli.test.ts
Linked repository file from this build.
import { expect, test } from "bun:test";
import { missingSeedPolicies, parseArgs } from "../../../scripts/seed-snap-negative";
import { assertProductionConfirmed } from "../../../scripts/db/target";
import { seedPolicy } from "./seed-generator-policy";
import { resetBaselineBatchOptions } from "./seed";
import { generateSeedBatch } from "./seed-generator";
test("seed CLI respects the baseline, complete fiscal years and one-sided ranges", () => {
expect(parseArgs([]).sampleMonths).toEqual(["2026-09"]);
for (const args of [
["--ffy", "2026"],
["--ffy-from", "2026"],
["--ffy-to", "2026"],
]) {
const cli = parseArgs(args);
expect(cli.sampleMonths).toBeNull();
expect(cli.ffyFrom).toBe(2026);
expect(cli.ffyTo).toBe(2026);
const batch = generateSeedBatch(
resetBaselineBatchOptions({
sampleMonths: undefined,
ffyFrom: cli.ffyFrom!,
ffyTo: cli.ffyTo!,
perMonth: 1,
}),
);
expect(new Set(batch.reviews.map((r) => r.sampleMonth)).size).toBe(12);
expect(batch.reviews.length).toBe(24);
}
expect(parseArgs(["--ffy", "2025", "--month", "2026-09"]).sampleMonths).toEqual(["2026-09"]);
});
test("seed CLI rejects missing, fractional, reversed and malformed options", () => {
for (const args of [
["--month"],
["--ffy", "bad"],
["--ffy-from", "2027", "--ffy-to", "2026"],
["--ffy", "2026.5"],
["--seed", "NaN"],
["--per-month", "0"],
["--month", "2026-13"],
["--as-of"],
["--as-of", "2026-02-30"],
["--as-of", "invalid"],
]) {
expect(() => parseArgs(args)).toThrow();
}
});
test("seed CLI accepts an explicit date and policy preflight preserves published tables", () => {
expect(parseArgs(["--as-of", "2026-09-08"]).asOf?.toISOString()).toBe("2026-09-08T12:00:00.000Z");
const policy = seedPolicy(2026);
expect(missingSeedPolicies([policy], [])).toEqual([policy]);
expect(missingSeedPolicies([policy], [structuredClone(policy)])).toEqual([]);
expect(() => missingSeedPolicies([policy], [{ ...policy, errorThreshold: 99 }])).toThrow(
"Policy conflict",
);
expect(() => missingSeedPolicies([policy], [{ ...policy, id: "other-id" }])).toThrow(
"Policy conflict",
);
expect(() => missingSeedPolicies([policy], [{ ...policy, effectiveFrom: "2027-10" }])).toThrow(
"Policy conflict",
);
});
test("CLI dry runs generate complete years and match across host time zones without a database", () => {
const run = (TZ: string) => {
const result = Bun.spawnSync(
[
process.execPath,
"run",
"scripts/seed-snap-negative.ts",
"--ffy-from",
"2025",
"--ffy-to",
"2027",
"--per-month",
"1",
"--dry-run",
],
{
cwd: new URL("../../../", import.meta.url).pathname,
env: { ...process.env, TZ, DATABASE_URL: "" },
stdout: "pipe",
stderr: "pipe",
},
);
expect(result.exitCode).toBe(0);
return result.stdout.toString();
};
const output = run("UTC");
expect(run("America/Los_Angeles")).toBe(output);
expect(output).toContain("Generated 72 reviews");
expect(output).toContain("Reference date: 2027-09-30T12:00:00.000Z");
expect(output).toContain("snap-negative-synthetic-ffy2025-v1");
expect(output).toContain("snap-negative-ffy2026-v1");
expect(output).toContain("snap-negative-synthetic-ffy2027-v1");
});
test("the reported four-FFY seed command generates 4800 cases before any database access", () => {
const result = Bun.spawnSync(
[
process.execPath,
"run",
"scripts/seed-snap-negative.ts",
"--reset",
"--ffy-from",
"2023",
"--ffy-to",
"2026",
"--as-of",
"2026-09-01",
"--dry-run",
],
{
cwd: new URL("../../../", import.meta.url).pathname,
env: { ...process.env, DATABASE_URL: "" },
stdout: "pipe",
stderr: "pipe",
},
);
expect(result.stderr.toString()).toBe("");
expect(result.exitCode).toBe(0);
expect(result.stdout.toString()).toContain("Generated 4800 reviews");
expect(result.stdout.toString()).toContain(
"Dry run complete. No files or database records written.",
);
}, 15_000);
test("seed CLI refuses production unless the operator confirms it", () => {
expect(parseArgs([]).confirmProduction).toBe(false);
expect(parseArgs(["--confirm-production"]).confirmProduction).toBe(true);
expect(() => assertProductionConfirmed("production", false, "Seeding")).toThrow(
"--confirm-production",
);
expect(() => assertProductionConfirmed("production", true, "Seeding")).not.toThrow();
expect(() => assertProductionConfirmed("development", false, "Seeding")).not.toThrow();
});