Files
HealthLog/src/components/cycle/__tests__/predictions-panel.test.tsx
T
Marc-André Bombeck d9a31524a2 feat(cycle): serve the cycle forecast as a subscribable calendar
GET /api/cycle/feed/<hlf_token>[.ics] answers any calendar app with the
record's forecast as RFC 5545: all-day events for the next few periods
over their widening windows, optionally past periods, and for a
conception goal that asks for it the next fertile window. Dates and a
title only; discreet titles and the calendar name "HealthLog" by default.
The body is built live on every fetch in the record's own zone and
language, never the request's. ETag is the content hash and a matching
If-None-Match answers 304; DTSTAMP moves only when the content does.

The feed is anonymous, so every gate is read per fetch: the auth-surface
limit before any lookup, the token's shape, the HMAC lookup of a live
feed, a per-token bucket, then the record and carrier not pending
deletion, the operator's external-access switch, cycle tracking for the
record, and for a feed a delegate minted, a live MANAGE grant covering
cycle. Every miss is the same 404.

/api/cycle/feed-subscription (status, turn on, options, turn off) and
its rotate are cookie-only through requireRecordAuth("manage", "cycle"),
with a fresh proof on turning on and rotating and a per-actor mint
bucket. The URL is returned once; the status carries the prefix, the
options, the fetch statistics as a calendar-app family and the
projection state.

The hlf_ family is registered with the path and token redactors, the
secret-shape refusal, the proxy's public paths and edge headers, the
URL-credential, sharing, recent-proof, step-up and module-gate guards,
the error-code catalogue, the activity feed and the destructive-control
registry. The cycle settings tab gains the subscription card and the
predictions panel lists the later cycles.
2026-11-06 18:03:37 +01:00

163 lines
4.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, it, expect } from "vitest";
import { renderToStaticMarkup } from "react-dom/server";
import { PredictionsPanel } from "../predictions-panel";
import { I18nProvider } from "@/lib/i18n/context";
import type { CyclePrediction, CycleHistoryResponse } from "../types";
function render(node: React.ReactNode) {
return renderToStaticMarkup(
<I18nProvider initialLocale="en">{node}</I18nProvider>,
);
}
const basePrediction: CyclePrediction = {
method: "CALENDAR",
nextPeriodStart: "2026-07-01",
nextPeriodStartLow: "2026-06-29",
nextPeriodStartHigh: "2026-07-03",
fertileWindowStart: null,
fertileWindowEnd: null,
predictedOvulation: null,
ovulationConfirmed: false,
confidence: 0.8,
cyclesObserved: 6,
stillLearning: false,
disclaimer: "Estimates only, not medical advice.",
};
const emptyHistory: CycleHistoryResponse = {
cycles: [],
stats: {
avgLengthDays: null,
lengthVariabilityDays: null,
avgPeriodLengthDays: null,
regularity: "LEARNING",
},
};
describe("<PredictionsPanel>", () => {
it("renders the next-period range without repeating the non-medical disclaimer", () => {
// The disclaimer now lives once at the cycle-tracking on-ramp, never beneath
// the prediction surfaces, so the panel must not render it for any goal.
const html = render(
<PredictionsPanel
prediction={basePrediction}
rawChartMode={false}
history={emptyHistory}
/>,
);
expect(html).not.toContain('data-slot="cycle-disclaimer"');
expect(html).not.toContain("Estimates only, not medical advice.");
// The range (not a single date) renders.
expect(html).toContain("–");
});
it("renders the fertile window only when the prediction carries one", () => {
const withFertile: CyclePrediction = {
...basePrediction,
fertileWindowStart: "2026-06-14",
fertileWindowEnd: "2026-06-19",
predictedOvulation: "2026-06-17",
};
const html = render(
<PredictionsPanel
prediction={withFertile}
rawChartMode={false}
history={emptyHistory}
/>,
);
expect(html).toContain("Fertile window");
expect(html).toContain("Estimated ovulation");
});
it("suppresses fertile-window language when the prediction nulls it (goal-gated server-side)", () => {
const html = render(
<PredictionsPanel
prediction={basePrediction}
rawChartMode={false}
history={emptyHistory}
/>,
);
expect(html).not.toContain("Fertile window");
});
it("shows the still-learning state for an under-3-cycle prediction", () => {
const html = render(
<PredictionsPanel
prediction={{ ...basePrediction, stillLearning: true }}
rawChartMode={false}
history={emptyHistory}
/>,
);
expect(html).toContain("Still learning");
});
it("shows the raw-chart-mode state when predictions are off", () => {
const html = render(
<PredictionsPanel
prediction={null}
rawChartMode={true}
history={emptyHistory}
/>,
);
expect(html).toContain("Prediction is off");
});
});
describe("<PredictionsPanel> later cycles (#1350)", () => {
const projection = [
{
k: 1,
start: "2026-07-01",
periodLength: 5,
bandStart: "2026-06-29",
bandEnd: "2026-07-03",
},
{
k: 2,
start: "2026-07-29",
periodLength: 5,
bandStart: "2026-07-26",
bandEnd: "2026-08-01",
},
{
k: 3,
start: "2026-08-26",
periodLength: 5,
bandStart: "2026-08-22",
bandEnd: "2026-08-30",
},
];
it("lists the cycles after the next one, as the server's windows", () => {
const html = render(
<PredictionsPanel
prediction={basePrediction}
rawChartMode={false}
history={emptyHistory}
projection={projection}
projectionState="OK"
/>,
);
expect(html).toContain('data-slot="cycle-projection"');
expect(html).toContain("Later cycles");
// Two later rows; cycle 1 is the next-period row above them.
expect(html.match(/Around /g)).toHaveLength(2);
});
it("states a late period instead of listing later cycles", () => {
const html = render(
<PredictionsPanel
prediction={basePrediction}
rawChartMode={false}
history={emptyHistory}
projection={[]}
projectionState="LATE"
/>,
);
expect(html).toContain('data-slot="cycle-projection-late"');
expect(html).not.toContain('data-slot="cycle-projection"');
});
});