Files
HealthLog/src/lib/topics/__tests__/build.test.ts
T
Marc-André Bombeck cc04e0944d feat(topics): compute topic overviews and topic pages on the server
GET /api/topics answers one entry per topic the record holds data for:
a headline value, a rule-based sentence and a 30-day sparkline.
GET /api/topics/{topic} answers a topic's signals with their usual
ranges, side-by-side series for a chosen window, the relationships
between its signals, 90-day trends, what it holds nothing for yet, and
its blocks (training load, lab markers with their trend, screener
trends, the cycle day).

Every figure comes from reads the metric pages already make: the local
daily values, the reconstructed nights, the mood score, computeLabTrend,
the training load and the screener trends. No model is involved. A
relationship says nothing below 14 paired days and is only called a
pattern at p < 0.05; a lab trend needs three readings spanning a year.

The preview is the viewer's own opt-in: with it off both routes answer
404 topics.preview_off. Topics, signals and blocks follow the record's
module switches and the sections the viewer's grant opens; the routes
read across sections, so a scoped grant is refused like the timeline.
/api/auth/me publishes the caller's opt-ins as previewFeatures.
2026-10-23 08:18:43 +02:00

162 lines
4.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { shiftDateKey } from "@/lib/tz/format";
import { buildDetail, buildOverview, summarise } from "../build";
import type { TopicInputs } from "../read";
import { TOPICS, type TopicSignalKey } from "../registry";
import type { TopicVisibility } from "../visibility";
const TODAY = "2026-10-23";
function series(n: number, f: (i: number) => number) {
const out = new Map<string, number>();
for (let i = 0; i < n; i++) out.set(shiftDateKey(TODAY, -(n - 1 - i)), f(i));
return out;
}
function inputs(
entries: Array<[TopicSignalKey, Map<string, number>]>,
extra: Partial<TopicInputs> = {},
): TopicInputs {
return {
series: new Map(entries),
labs: null,
training: null,
screeners: null,
cycle: null,
...extra,
};
}
const everything: TopicVisibility = { modules: {}, sees: () => true };
describe("summarise", () => {
it("names the first signal outside its usual range", () => {
const data = inputs([
["RESTING_HEART_RATE", series(30, (i) => (i === 29 ? 75 : 58 + (i % 2)))],
]);
const detail = buildDetail(TOPICS.heart, "30d", data, everything, TODAY);
expect(detail.summary).toMatchObject({
code: "outside",
signal: "RESTING_HEART_RATE",
direction: "above",
});
});
it("is still learning below a week, and empty without anything", () => {
expect(
summarise(
[
{
key: "WEIGHT",
latest: { day: TODAY, value: 80 },
usual: null,
state: "learning",
href: null,
},
],
inputs([]),
).code,
).toBe("learning");
expect(summarise([], inputs([])).code).toBe("empty");
});
});
describe("buildOverview", () => {
it("gives a tile to a topic with data and names the empty and the hidden ones", () => {
const data = inputs([["WEIGHT", series(20, () => 80)]]);
const overview = buildOverview(
["heart", "body"],
["cycle"],
data,
everything,
TODAY,
);
expect(overview.topics.map((t) => t.key)).toEqual(["body"]);
expect(overview.empty).toEqual(["heart"]);
expect(overview.hiddenByModule).toEqual(["cycle"]);
const body = overview.topics[0];
expect(body.headline).toMatchObject({ kind: "signal", key: "WEIGHT" });
expect(body.sparkline).toHaveLength(20);
expect(body.recorded).toBe(1);
expect(body.total).toBe(TOPICS.body.signals.length);
expect(body.tone).toBe("steady");
});
});
describe("buildDetail", () => {
it("lists what it could show but holds nothing for, and says it is partial", () => {
const data = inputs([["RESTING_HEART_RATE", series(20, () => 58)]]);
const detail = buildDetail(TOPICS.heart, "30d", data, everything, TODAY);
expect(detail.status).toBe("partial");
expect(detail.missing).toContain("HEART_RATE_VARIABILITY");
expect(detail.series.map((s) => s.key)).toEqual(["RESTING_HEART_RATE"]);
expect(detail.from).toBe(shiftDateKey(TODAY, -29));
expect(detail.series[0].points).toHaveLength(20);
});
it("never names a pair or a chart row whose module is off", () => {
const sleepOff: TopicVisibility = {
modules: { sleep: false },
sees: () => true,
};
const data = inputs([
["RESTING_HEART_RATE", series(30, () => 58)],
["SLEEP_DURATION", series(30, () => 420)],
]);
const detail = buildDetail(TOPICS.heart, "30d", data, sleepOff, TODAY);
expect(detail.series.map((s) => s.key)).not.toContain("SLEEP_DURATION");
expect(
detail.relationships.some(
(r) => r.x === "SLEEP_DURATION" || r.y === "SLEEP_DURATION",
),
).toBe(false);
});
it("states a below-threshold pair with its count instead of a figure", () => {
const data = inputs([
["SLEEP_DURATION", series(10, () => 400)],
["RESTING_HEART_RATE", series(10, () => 60)],
]);
const detail = buildDetail(TOPICS.heart, "30d", data, everything, TODAY);
const pair = detail.relationships.find(
(r) => r.id === "short-nights-resting-hr",
);
expect(pair).toMatchObject({ status: "insufficient", pairedDays: 10 });
});
it("does not name a pair one side of which holds nothing", () => {
const data = inputs([["ACTIVITY_STEPS", series(30, () => 8000)]]);
const detail = buildDetail(
TOPICS.metabolism,
"30d",
data,
everything,
TODAY,
);
expect(detail.relationships).toEqual([]);
});
it("drops a block the grant does not open", () => {
const noLabs: TopicVisibility = {
modules: {},
sees: (scope) => scope !== "labs",
};
const labs = {
markers: [],
inRange: 0,
withRange: 0,
latestOn: null,
};
const detail = buildDetail(
TOPICS.metabolism,
"30d",
inputs([], { labs }),
noLabs,
TODAY,
);
expect(detail.labs).toBeNull();
});
});