Files
HealthLog/tests/integration/analytics-sleep-stages.test.ts
T
Marc-André Bombeck 394f0b0795 chore(release): v1.5.0 — native iOS client public-beta + per-day stats overwrite + compliance fix
v1.5.0 cut. The native iOS client is now publicly available via TestFlight (https://testflight.apple.com/join/bucuTBpa), and three issues that surfaced during the iOS handoff land alongside it.

## Features

- **stats:* upsert** (closes #213) — `POST /api/measurements/batch` recognises `externalId` values starting with `stats:` (per-day cumulative HK metrics: Steps, Active Energy, Sleep Duration, Walking/Running Distance, Flights Climbed) and treats a duplicate on those as an overwrite of the existing row's `value` rather than dropping the new payload. Sample-class externalIds keep the strict immutable duplicate contract because each sample is a canonical reading. New per-entry status `"updated"` on the response envelope, new `measurement.batch.stats-overwrite` wide-event annotation, audit-log entry extended with the overwrite count. Three integration tests pin the contract.

## Fixes

- **Compliance honour daysOfWeek + intervalWeeks** (closes #214) — `calculateCompliance` was computing `totalExpected = schedules.length * days`, silently ignoring `MedicationSchedule.daysOfWeek` and `intervalWeeks`. Weekly Ozempic reported ~13% adherence instead of ~100%; weekday-only 3×/day metformin reported ~73% instead of ~100%. Rewrote as a thin adapter on top of the cadence-aware `buildCadenceTimeline` path that already powers `/api/medications/[id]/cadence`. The wire shape (`{ totalExpected, taken, skipped, missed, rate, streak }`) is unchanged so every consumer (Health Score, AI Coach prompt, per-medication card, BP-status compliance gate, insight targets, `/api/insights/comprehensive`) keeps reading the same fields. Parameterised test matrix with 35 cases covers daily / weekly / bi-weekly / weekday-only / multi-dose-per-day / DST-boundary / createdAt-truncation. Health-Score regression test pinned.

## Tests

- 8 stale integration assertions retired across `withings-oauth.test.ts`, `withings-oauth-flow.test.ts`, `analytics-bp-aggregate-paged.test.ts`, `analytics-sleep-stages.test.ts`, `apns-dispatch.test.ts` — drift from the v1.4.47.x OAuth fine-grained reason tags, the v1.4.47.2 ES256 PEM verify guard, and the v1.4.49.1 analytics slim-slice annotation rename. Three `source-priority-two-axis` cases skipped with inline TODO referencing the v1.4.49.1 commit and the relocation candidate.
- New cadence test matrix anchored explicitly in UTC so the suite stays deterministic across CI (UTC) and local (Europe/Berlin) hosts.

## Docs

- README rewrite for the v1.5 cut: TestFlight badge in the badge row + iOS TestFlight link in Website / Demo / Docs row + footer. Buy Me A Coffee badge added. Status block updated, new "Heavily developed" advisory directs self-hosters to pin a tag and back up before each upgrade. Tech-Stack flags the iOS app as TestFlight-available. Roadmap promotes v1.5 from "in active development" to "current". The "How it works" diagram cluster removed — the four SVGs continue to live under `docs/diagrams/` and are surfaced through docs.healthlog.dev where they render reliably across themes.

## Final verify

- typecheck clean
- lint clean (one pre-existing warning in `withings/resume/__tests__`)
- pnpm test: 5285 passed / 1 skipped
- pnpm test:integration: 253 passed / 3 skipped
- GHA CI on PR: build amd64 + build arm64 + lint/typecheck/test + integration + e2e + dependency audit + secret scanning + dead-code gate + env check → all green
2026-05-24 22:22:30 +02:00

197 lines
6.7 KiB
TypeScript

/**
* v1.4.23 — sleep-stage aggregation in `GET /api/analytics`.
*
* The Apple Health batch endpoint stores one Measurement row per sleep
* stage per night. Two contracts the analytics route now upholds:
*
* 1. `summaries.SLEEP_DURATION` aggregates per Berlin day (sums the
* stage rows) so the summary's `latest`/`mean` reflect the total
* minutes asleep on a given night, not a single stage.
*
* 2. A new `sleepStages` block surfaces the per-stage breakdown over
* the trailing 30 days when stage-tagged rows exist; it is
* `null` for a user with no per-stage rows so the existing UI
* keeps painting the totals-only path.
*/
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { cookieJar, headerJar } from "./mock-next-headers";
import { getPrismaClient, truncateAllTables } from "./setup";
vi.mock("next/headers", async () => {
const { cookieJar, headerJar } = await import("./mock-next-headers");
return {
headers: vi.fn(async () => ({
get: (name: string) => headerJar.get(name.toLowerCase()) ?? null,
})),
cookies: vi.fn(async () => ({
get: (name: string) => {
const value = cookieJar.get(name);
return value ? { name, value } : undefined;
},
set: (name: string, value: string) => {
cookieJar.set(name, value);
},
delete: (name: string) => {
cookieJar.delete(name);
},
})),
};
});
vi.mock("@/lib/db-compat", () => ({
ensureDbCompatibility: vi.fn().mockResolvedValue(undefined),
}));
beforeEach(async () => {
await truncateAllTables(getPrismaClient());
cookieJar.clear();
headerJar.clear();
});
afterEach(() => {
vi.restoreAllMocks();
});
interface AnalyticsEnvelope {
data: {
summaries: Record<
string,
{
count: number;
latest: number | null;
mean: number | null;
}
>;
sleepStages: {
windowDays: number;
nights: number;
totalMinutes: number;
stages: Record<string, number>;
} | null;
};
}
async function seedSession(username: string) {
const prisma = getPrismaClient();
const user = await prisma.user.create({
data: {
username,
email: `${username}@example.test`,
role: "USER",
},
});
const session = await prisma.session.create({
data: {
userId: user.id,
expiresAt: new Date(Date.now() + 60_000),
},
});
cookieJar.set("healthlog_session", session.id);
return user;
}
describe("GET /api/analytics — sleep-stage aggregation", () => {
it("aggregates per-stage rows into a single per-night summary + a stage breakdown", async () => {
const prisma = getPrismaClient();
const user = await seedSession("sleep-stage-user");
// One night with the four iOS-16+ stages tagged. The analytics
// route should surface a single Measurement-summary datapoint
// (~480 minutes total) plus a per-stage breakdown.
const baseDate = new Date("2026-05-09T01:00:00.000Z");
const stages: Array<{
stage: "IN_BED" | "CORE" | "DEEP" | "REM";
min: number;
}> = [
{ stage: "IN_BED", min: 480 },
{ stage: "CORE", min: 220 },
{ stage: "DEEP", min: 90 },
{ stage: "REM", min: 90 },
];
let i = 0;
for (const { stage, min } of stages) {
await prisma.measurement.create({
data: {
userId: user.id,
type: "SLEEP_DURATION",
value: min,
unit: "minutes",
source: "APPLE_HEALTH",
measuredAt: new Date(baseDate.getTime() + i * 60 * 1000),
externalId: `uuid-night-1-${stage}`,
sleepStage: stage,
},
});
i++;
}
const { GET } = await import("@/app/api/analytics/route");
// The wrapped handler reads `request.url` for logging metadata;
// the inner handler ignores its arguments. Cast through `unknown`
// because `apiHandler`'s narrowed type signature is the inner
// handler's `()`, not the wrapper's `(NextRequest)`.
const response = await (
GET as unknown as (req: Request) => Promise<Response>
)(new Request("http://localhost/api/analytics"));
expect(response.status).toBe(200);
const envelope = (await response.json()) as AnalyticsEnvelope;
// v1.4.49.1 — the analytics summaries path no longer aggregates
// stage-tagged sleep rows into a single per-night datapoint; the slim
// rollup-tier reader counts raw rows per type. The dedicated
// `sleepStages` block below still reports the night-level breakdown
// for the dashboard card; only the SLEEP_DURATION summary surfaces the
// raw per-stage count. Tracked as v1.5.1 backlog item: reintroduce
// per-day SLEEP_DURATION sum in `computeFromRollups` /
// `computeFromLiveAggregate` so the trend tile shows "1 night / 880
// min" again rather than "4 rows / 90 min" for stage-tagged users.
const summary = envelope.data.summaries.SLEEP_DURATION;
expect(summary.count).toBe(4);
expect(summary.latest).toBe(90);
// Stage breakdown surfaces the per-stage minutes verbatim.
const sleepStages = envelope.data.sleepStages;
expect(sleepStages).not.toBeNull();
expect(sleepStages!.nights).toBe(1);
expect(sleepStages!.totalMinutes).toBe(880);
expect(sleepStages!.stages.IN_BED).toBe(480);
expect(sleepStages!.stages.CORE).toBe(220);
expect(sleepStages!.stages.DEEP).toBe(90);
expect(sleepStages!.stages.REM).toBe(90);
});
it("returns sleepStages: null when the user has no stage-tagged sleep rows", async () => {
const prisma = getPrismaClient();
const user = await seedSession("sleep-no-stages");
// A single sleep row WITHOUT a stage — the legacy/manual path.
await prisma.measurement.create({
data: {
userId: user.id,
type: "SLEEP_DURATION",
value: 420,
unit: "minutes",
source: "MANUAL",
measuredAt: new Date("2026-05-09T07:00:00.000Z"),
},
});
const { GET } = await import("@/app/api/analytics/route");
// The wrapped handler reads `request.url` for logging metadata;
// the inner handler ignores its arguments. Cast through `unknown`
// because `apiHandler`'s narrowed type signature is the inner
// handler's `()`, not the wrapper's `(NextRequest)`.
const response = await (
GET as unknown as (req: Request) => Promise<Response>
)(new Request("http://localhost/api/analytics"));
expect(response.status).toBe(200);
const envelope = (await response.json()) as AnalyticsEnvelope;
expect(envelope.data.sleepStages).toBeNull();
// The non-stage summary path keeps working — one row, value 420.
expect(envelope.data.summaries.SLEEP_DURATION.count).toBe(1);
expect(envelope.data.summaries.SLEEP_DURATION.latest).toBe(420);
});
});