Files
cmu-meal-estimator/src/sanity.test.ts
T
2026-09-18 16:47:15 -04:00

85 lines
2.9 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { loadConfig } from "./configLoader";
import { estimate } from "./engine";
// Read the actual shipped config that gets served at runtime.
const configPath = fileURLToPath(new URL("../public/config.json", import.meta.url));
const rawConfig = JSON.parse(readFileSync(configPath, "utf-8"));
describe("real config sanity", () => {
const result = loadConfig(rawConfig);
it("loads the shipped config without errors", () => {
expect(result.ok).toBe(true);
});
it("produces sensible first-plan numbers partway through the term", () => {
if (!result.ok) throw new Error("config failed to load");
const config = result.config;
const plan = config.plans[0];
// Target a date roughly in the middle of the meal window.
const midMs =
(config.mealStartDate.getTime() + config.mealEndDate.getTime()) / 2;
const midDate = new Date(midMs);
const r = estimate(config, {
selectedPlanId: plan.id,
targetDate: midDate,
activeMissingPeriodIds: new Set(),
extraExcludedDays: 0,
actualMealBlocks: null,
actualFlexDollars: null,
});
// Per-week rate matches the plan total spread over the active days.
expect(r.perWeekMealBlocks).toBeCloseTo(
(plan.totalMealBlocks / r.totalActiveDays) * 7,
2
);
// Consumed + remaining conserves the plan totals.
expect(r.consumedMealBlocks + r.remainingMealBlocks).toBeCloseTo(
plan.totalMealBlocks
);
expect(r.consumedFlexDollars + r.remainingFlexDollars).toBeCloseTo(
plan.totalFlexDollars
);
// Partway through, consumption is strictly between 0 and the total.
expect(r.consumedMealBlocks).toBeGreaterThan(0);
expect(r.consumedMealBlocks).toBeLessThan(plan.totalMealBlocks);
});
it("checking a break reduces total active days", () => {
if (!result.ok) throw new Error("config failed to load");
const config = result.config;
if (config.missingPeriods.length === 0) return; // nothing to check
const plan = config.plans[0];
const period = config.missingPeriods[0];
const endDate = config.mealEndDate;
const base = estimate(config, {
selectedPlanId: plan.id,
targetDate: endDate,
activeMissingPeriodIds: new Set(),
extraExcludedDays: 0,
actualMealBlocks: null,
actualFlexDollars: null,
});
const withBreak = estimate(config, {
selectedPlanId: plan.id,
targetDate: endDate,
activeMissingPeriodIds: new Set([period.id]),
extraExcludedDays: 0,
actualMealBlocks: null,
actualFlexDollars: null,
});
expect(withBreak.totalActiveDays).toBeLessThan(base.totalActiveDays);
// Per-week rate rises because the same total spreads over fewer days.
expect(withBreak.perWeekMealBlocks).toBeGreaterThan(base.perWeekMealBlocks);
});
});