mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 08:02:12 +00:00
chore(release): v1.28.23 — sleep disagreement on the dashboard; Apple Health medication groundwork
The sleep source-discrepancy annotation now reaches the surfaces: SleepNight carries the MAIN session's annotation (naps never cross-compare), the analytics slim slice / dashboard snapshot / iOS summary thread it additively, and the dashboard sleep tile renders the shared discreet marker (extracted from the sleep panel — one implementation, two surfaces). Served totals unchanged, pinned by tests. Apple Health medications groundwork (server side of the medication-import request): IntakeSource gains APPLE_HEALTH; medications accept an external mirror identity (source-exclusive by policy) and dose imports carry the HK dose-event UUID — both idempotent per stable key, with the concurrent-create race resolved to the winner. Migration 0239, additive only. Entries without the new fields behave byte-identically. Hardening: doctor-report aggregation + PDF charts compute ranges with loops instead of spread calls — a report over a sample-dense year cannot overflow the call stack (the class fixed for the Google Health full sync).
This commit is contained in:
1 parent
755da41019
commit
c400e2aa88
29 files changed
+1585
-276
No files matched your search
@@ -572,6 +572,14 @@ describe("GET /api/dashboard/summary", () => {
|
||||
latestValue: number | null;
|
||||
unit: string | null;
|
||||
sleepStages: Record<string, number> | null;
|
||||
sleepSourceDiscrepancy: {
|
||||
deltaMinutes: number;
|
||||
sources: Array<{
|
||||
source: string;
|
||||
deviceType: string | null;
|
||||
asleepMinutes: number;
|
||||
}>;
|
||||
} | null;
|
||||
}>;
|
||||
};
|
||||
};
|
||||
@@ -585,6 +593,82 @@ describe("GET /api/dashboard/summary", () => {
|
||||
expect(sleep?.sleepStages?.CORE).toBeCloseTo(4, 2);
|
||||
expect(sleep?.sleepStages?.DEEP).toBeCloseTo(1.5, 2);
|
||||
expect(sleep?.sleepStages?.REM).toBeCloseTo(80 / 60, 2);
|
||||
// v1.28.x — the additive discrepancy field is present and null-safe:
|
||||
// source-less single-writer rows carry no annotation.
|
||||
expect(sleep?.sleepSourceDiscrepancy).toBeNull();
|
||||
// Every non-sleep kind pins the additive field to null.
|
||||
for (const m of body.data.metrics) {
|
||||
if (m.id !== "sleep") expect(m.sleepSourceDiscrepancy).toBeNull();
|
||||
}
|
||||
});
|
||||
|
||||
it("carries the latest night's source-discrepancy annotation on the sleep tile (v1.28.x)", async () => {
|
||||
// Two writers disagree on the night: granular Apple (410 asleep) next
|
||||
// to a bare Withings aggregate claiming 615. Apple wins the served
|
||||
// total; the additive annotation surfaces the disagreement.
|
||||
vi.mocked(getSession).mockResolvedValue(SESSION_OK as never);
|
||||
const wake = new Date("2026-06-04T06:00:00.000Z");
|
||||
vi.mocked(prisma.measurement.groupBy).mockResolvedValue([
|
||||
{
|
||||
type: "SLEEP_DURATION",
|
||||
_count: { _all: 4 },
|
||||
_max: { measuredAt: wake },
|
||||
},
|
||||
] as never);
|
||||
vi.mocked(prisma.measurement.findMany).mockResolvedValue([
|
||||
{
|
||||
value: 240,
|
||||
measuredAt: new Date("2026-06-04T00:00:00.000Z"),
|
||||
sleepStage: "CORE",
|
||||
source: "APPLE_HEALTH",
|
||||
},
|
||||
{
|
||||
value: 90,
|
||||
measuredAt: new Date("2026-06-04T02:00:00.000Z"),
|
||||
sleepStage: "DEEP",
|
||||
source: "APPLE_HEALTH",
|
||||
},
|
||||
{
|
||||
value: 80,
|
||||
measuredAt: new Date("2026-06-04T04:00:00.000Z"),
|
||||
sleepStage: "REM",
|
||||
source: "APPLE_HEALTH",
|
||||
},
|
||||
{
|
||||
value: 615,
|
||||
measuredAt: wake,
|
||||
sleepStage: "ASLEEP",
|
||||
source: "WITHINGS",
|
||||
},
|
||||
] as never);
|
||||
const res = await callGet(makeReq());
|
||||
const body = (await res.json()) as {
|
||||
data: {
|
||||
metrics: Array<{
|
||||
id: string;
|
||||
latestValue: number | null;
|
||||
sleepSourceDiscrepancy: {
|
||||
deltaMinutes: number;
|
||||
sources: Array<{
|
||||
source: string;
|
||||
deviceType: string | null;
|
||||
asleepMinutes: number;
|
||||
}>;
|
||||
} | null;
|
||||
}>;
|
||||
};
|
||||
};
|
||||
const sleep = body.data.metrics.find((m) => m.id === "sleep");
|
||||
expect(sleep, "sleep tile must be emitted").toBeDefined();
|
||||
// Served total stays the winning writer's number (410 min → 6.83 h).
|
||||
expect(sleep?.latestValue).toBeCloseTo(410 / 60, 2);
|
||||
expect(sleep?.sleepSourceDiscrepancy).toEqual({
|
||||
deltaMinutes: 205,
|
||||
sources: [
|
||||
{ source: "WITHINGS", deviceType: null, asleepMinutes: 615 },
|
||||
{ source: "APPLE_HEALTH", deviceType: null, asleepMinutes: 410 },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("emits the sleep-rhythm DTO (sleep-debt + chronotype) computed from the foundation modules (v1.17.0)", async () => {
|
||||
|
||||
@@ -60,6 +60,7 @@ import { buildMedsTodayBlock } from "@/lib/dashboard/meds-today";
|
||||
import {
|
||||
summarizeSleepNights,
|
||||
reconstructSleepNights,
|
||||
type SleepSourceDiscrepancy,
|
||||
type SleepStageRow,
|
||||
} from "@/lib/analytics/sleep-night";
|
||||
import { loadUserSourcePriority } from "@/lib/rollups/measurement-read";
|
||||
@@ -130,6 +131,16 @@ interface MetricCard {
|
||||
* headline `latestValue`.
|
||||
*/
|
||||
sleepStages: Partial<Record<SleepStage, number>> | null;
|
||||
/**
|
||||
* v1.28.x — source-discrepancy annotation for the headline night's MAIN
|
||||
* session, sleep tile only; `null` for every other kind and for a night
|
||||
* whose writers agree. Additive (the established wire pattern for the
|
||||
* iOS-locked shape): observational only — `latestValue` stays the
|
||||
* winning writer's total; clients render a discreet "sources disagree"
|
||||
* hint next to the number. Minutes are whole (rounded), matching the
|
||||
* `/api/sleep/night` serialisation.
|
||||
*/
|
||||
sleepSourceDiscrepancy: SleepSourceDiscrepancy | null;
|
||||
trend: "up" | "down" | "flat" | "unknown";
|
||||
sparkline: number[];
|
||||
updatedAt: string | null;
|
||||
@@ -707,6 +718,7 @@ async function buildDashboardSummary(
|
||||
unitKey: METRIC_UNIT_KEYS.weight,
|
||||
unit: null,
|
||||
sleepStages: null,
|
||||
sleepSourceDiscrepancy: null,
|
||||
trend: trendOf(spark),
|
||||
sparkline: spark,
|
||||
updatedAt: latest?.at?.toISOString() ?? meta.lastSeenAt,
|
||||
@@ -747,6 +759,7 @@ async function buildDashboardSummary(
|
||||
unitKey: METRIC_UNIT_KEYS.bloodPressure,
|
||||
unit: null,
|
||||
sleepStages: null,
|
||||
sleepSourceDiscrepancy: null,
|
||||
trend: trendOf(sysSpark),
|
||||
sparkline: sysSpark,
|
||||
updatedAt:
|
||||
@@ -776,6 +789,7 @@ async function buildDashboardSummary(
|
||||
unitKey: METRIC_UNIT_KEYS.pulse,
|
||||
unit: null,
|
||||
sleepStages: null,
|
||||
sleepSourceDiscrepancy: null,
|
||||
trend: trendOf(spark),
|
||||
sparkline: spark,
|
||||
updatedAt: latest?.at?.toISOString() ?? meta.lastSeenAt,
|
||||
@@ -803,6 +817,7 @@ async function buildDashboardSummary(
|
||||
unitKey: METRIC_UNIT_KEYS.bodyFat,
|
||||
unit: null,
|
||||
sleepStages: null,
|
||||
sleepSourceDiscrepancy: null,
|
||||
trend: trendOf(spark),
|
||||
sparkline: spark,
|
||||
updatedAt: latest?.at.toISOString() ?? meta.lastSeenAt,
|
||||
@@ -862,6 +877,19 @@ async function buildDashboardSummary(
|
||||
unit: "h",
|
||||
sleepStages:
|
||||
stageHours && Object.keys(stageHours).length > 0 ? stageHours : null,
|
||||
// Additive, observational: the headline night's MAIN session saw
|
||||
// two writer buckets with clearly different asleep totals. Round
|
||||
// to whole minutes, matching the `/api/sleep/night` serialisation.
|
||||
sleepSourceDiscrepancy: night?.sourceDiscrepancy
|
||||
? {
|
||||
deltaMinutes: Math.round(night.sourceDiscrepancy.deltaMinutes),
|
||||
sources: night.sourceDiscrepancy.sources.map((b) => ({
|
||||
source: b.source,
|
||||
deviceType: b.deviceType,
|
||||
asleepMinutes: Math.round(b.asleepMinutes),
|
||||
})),
|
||||
}
|
||||
: null,
|
||||
trend: trendOf(nightSpark),
|
||||
sparkline: nightSpark,
|
||||
updatedAt: night?.measuredAt.toISOString() ?? meta.lastSeenAt,
|
||||
@@ -881,6 +909,7 @@ async function buildDashboardSummary(
|
||||
unitKey: METRIC_UNIT_KEYS[kind],
|
||||
unit: null,
|
||||
sleepStages: null,
|
||||
sleepSourceDiscrepancy: null,
|
||||
trend: trendOf(spark),
|
||||
sparkline: spark,
|
||||
updatedAt: latest?.at.toISOString() ?? meta.lastSeenAt,
|
||||
|
||||
Reference in new issue
Block a user