Files
HealthLog/src/lib/analytics/__tests__/window-confidence.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

75 lines
2.0 KiB
TypeScript

import { describe, it, expect } from "vitest";
import {
computeWindowConfidence,
MIN_READINGS_FOR_CONFIDENCE,
} from "../window-confidence";
const NOW = new Date("2026-06-14T12:00:00.000Z");
const daysAgo = (n: number) =>
new Date(NOW.getTime() - n * 24 * 60 * 60 * 1000);
describe("computeWindowConfidence", () => {
it("flags a sample below the floor as insufficient", () => {
for (const count of [0, 1, 4]) {
const c = computeWindowConfidence({
windowDays: 90,
readingCount: count,
earliestReadingAt: count === 0 ? null : daysAgo(10),
now: NOW,
});
expect(c.sufficient).toBe(false);
}
});
it("flags a sample at or above the floor as sufficient", () => {
const c = computeWindowConfidence({
windowDays: 90,
readingCount: MIN_READINGS_FOR_CONFIDENCE,
earliestReadingAt: daysAgo(10),
now: NOW,
});
expect(c.sufficient).toBe(true);
});
it("returns a null span when the window is empty", () => {
const c = computeWindowConfidence({
windowDays: 90,
readingCount: 0,
earliestReadingAt: null,
now: NOW,
});
expect(c.effectiveSpanDays).toBeNull();
});
it("reports the real span until history reaches the window", () => {
const c = computeWindowConfidence({
windowDays: 90,
readingCount: 8,
earliestReadingAt: daysAgo(23),
now: NOW,
});
expect(c.effectiveSpanDays).toBe(23);
});
it("caps the span at the window once history exceeds it", () => {
const c = computeWindowConfidence({
windowDays: 90,
readingCount: 120,
earliestReadingAt: daysAgo(400),
now: NOW,
});
expect(c.effectiveSpanDays).toBe(90);
});
it("rounds a same-day-only sample up to one day", () => {
const c = computeWindowConfidence({
windowDays: 90,
readingCount: 5,
earliestReadingAt: new Date(NOW.getTime() - 60 * 1000),
now: NOW,
});
expect(c.effectiveSpanDays).toBe(1);
});
});