Files
HealthLog/src/components/cycle/__tests__/wheel-state.test.ts
T
Marc-André BombeckandClaude Opus 4.8 31a14ce7bf feat(cycle): premium cycle ring, BBT chart, and full read-state coverage
Finish the cycle web vertical's signature surfaces:

- Rebuild the cycle wheel to the wellness-ring premium signature — a
  clockwise clock-wipe that draws the phase arcs on from the 12-o'clock
  day-0 origin, a phase-tinted bloom on the active arc, the shared
  specular sheen, a day-number count-up, and a soft current-day marker
  halo. All on the wellness-tile surface, gated once per session and
  zeroed under prefers-reduced-motion.
- Add the basal-body-temperature chart to the predictions tab: the
  current-cycle BBT series with phase-tinted points, fertility signs in
  the tooltip, and an estimated-ovulation reference line (suppressed in
  raw-chart mode). Surface basalBodyTempC + ovulation-test + mucus on the
  calendar day DTO so the chart reads from the existing calendar load.
- Cover the read states the prediction/disclaimer surfaces were missing:
  pass the disclaimer to the predictions panel, render it on the calendar
  tab, add per-tab error + retry, and gate the settings spinner.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-06 13:09:00 +02:00

85 lines
2.7 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { deriveWheelState, currentCycleStartDate } from "../wheel-state";
import type { CalendarDay } from "../types";
function day(date: string, phase: CalendarDay["phase"]): CalendarDay {
return {
date,
phase,
isPredictedPeriod: false,
isFertileWindow: false,
isPredictedOvulation: false,
isPeriodLogged: phase === "MENSTRUAL",
flow: null,
hasSymptoms: false,
confidence: 1,
basalBodyTempC: null,
ovulationTest: null,
cervicalMucus: null,
};
}
describe("deriveWheelState", () => {
it("returns a null state when today carries no phase", () => {
const days = [day("2026-06-01", null), day("2026-06-02", null)];
const w = deriveWheelState(days, "2026-06-02");
expect(w.dayOfCycle).toBeNull();
expect(w.phase).toBeNull();
expect(w.spans).toEqual([]);
});
it("counts the day-of-cycle from the most recent menstrual start", () => {
// A run starting 2026-06-01 (menstrual) → today 2026-06-05 is cycle day 5.
const days = [
day("2026-05-30", "LUTEAL"),
day("2026-06-01", "MENSTRUAL"),
day("2026-06-02", "MENSTRUAL"),
day("2026-06-03", "FOLLICULAR"),
day("2026-06-04", "FOLLICULAR"),
day("2026-06-05", "FOLLICULAR"),
];
const w = deriveWheelState(days, "2026-06-05");
expect(w.dayOfCycle).toBe(5);
expect(w.phase).toBe("FOLLICULAR");
});
it("builds proportional phase spans summing to ~1", () => {
const days = [
day("2026-06-01", "MENSTRUAL"),
day("2026-06-02", "MENSTRUAL"),
day("2026-06-03", "FOLLICULAR"),
day("2026-06-04", "OVULATORY"),
day("2026-06-05", "LUTEAL"),
];
const w = deriveWheelState(days, "2026-06-03");
const total = w.spans.reduce((s, x) => s + x.fraction, 0);
expect(total).toBeCloseTo(1, 5);
// Only phases present in the run are emitted.
expect(w.spans.map((s) => s.phase)).toContain("MENSTRUAL");
});
});
describe("currentCycleStartDate", () => {
it("returns the most recent menstrual run-start at/before today", () => {
const days = [
day("2026-05-30", "LUTEAL"),
day("2026-06-01", "MENSTRUAL"),
day("2026-06-02", "MENSTRUAL"),
day("2026-06-03", "FOLLICULAR"),
day("2026-06-04", "FOLLICULAR"),
];
expect(currentCycleStartDate(days, "2026-06-04")).toBe("2026-06-01");
});
it("returns null when today carries no phase", () => {
const days = [day("2026-06-01", null), day("2026-06-02", null)];
expect(currentCycleStartDate(days, "2026-06-02")).toBeNull();
});
it("returns null when today is absent from the calendar", () => {
const days = [day("2026-06-01", "MENSTRUAL")];
expect(currentCycleStartDate(days, "2026-06-09")).toBeNull();
});
});