Files
HealthLog/src/lib/analytics/__tests__/glucose-panel-window.test.ts
T
Marc-André Bombeck 1f4ba5939d style: apply prettier across the tree
One-shot prettier pass so the new format:check gate starts from a clean
baseline. Formatting only — no behaviour change. Generator-owned and
vendored artifacts stay untouched via .prettierignore.
2026-06-19 22:20:31 +02:00

56 lines
2.1 KiB
TypeScript

/**
* v1.17.0 — the glucose clinical panel window is one exported constant.
*
* `GLUCOSE_PANEL_WINDOW_DAYS` (30) is the single source of truth for the
* window the TIR / GMI / eA1C / CV panel covers wherever it renders outside an
* ad-hoc report period: the analytics route, the dashboard snapshot, and the
* Coach snapshot. This test pins the value AND asserts all three consumers
* import the constant rather than re-typing a bare `30`, so the "same number
* everywhere" guarantee can't silently drift. The doctor PDF deliberately uses
* the report period and is excluded.
*/
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import {
DEFAULT_WINDOW_DAYS,
GLUCOSE_PANEL_WINDOW_DAYS,
} from "../glucose-metrics";
const ROOT = join(__dirname, "..", "..", "..", "..");
const CONSUMERS = [
"src/app/api/analytics/route.ts",
"src/lib/dashboard/snapshot.ts",
"src/lib/ai/coach/snapshot.ts",
];
describe("glucose panel window constant", () => {
it("is 30 days and distinct from the Battelino default", () => {
expect(GLUCOSE_PANEL_WINDOW_DAYS).toBe(30);
expect(GLUCOSE_PANEL_WINDOW_DAYS).not.toBe(DEFAULT_WINDOW_DAYS);
});
it.each(CONSUMERS)("%s imports the exported constant", (relPath) => {
const src = readFileSync(join(ROOT, relPath), "utf8");
expect(src).toContain("GLUCOSE_PANEL_WINDOW_DAYS");
});
it.each(CONSUMERS)(
"%s derives the glucose window from the exported constant",
(relPath) => {
const src = readFileSync(join(ROOT, relPath), "utf8");
// The window must derive from the exported constant — either passed
// straight to `windowDays`/the `Date.now()` math, or via the file's
// local alias `GLUCOSE_CLINICAL_WINDOW_DAYS = GLUCOSE_PANEL_WINDOW_DAYS`.
// It must NOT be a bare `windowDays: 30` glucose literal.
const usesConstantDirectly = /GLUCOSE_PANEL_WINDOW_DAYS/.test(src);
const aliasesConstant =
/GLUCOSE_CLINICAL_WINDOW_DAYS\s*=\s*GLUCOSE_PANEL_WINDOW_DAYS/.test(
src,
);
expect(usesConstantDirectly || aliasesConstant).toBe(true);
},
);
});