fix(sleep): label Polar nights reconstructed and emit their awake time

Polar exposes only per-stage duration totals, so its mapper already
reconstructs a contiguous CORE-DEEP-REM order exactly like WHOOP and flags
each segment reconstructed. The night reader's honesty allow-list only held
WHOOP, so a Polar-won night was served with reconstructed:false — presenting
a synthetic stage order to web and iOS as if it were the device's measured
timing. Add POLAR to the allow-list so the layout is labelled approximate.

Polar's total_interruption_duration carried the night's awake time but was
dropped, so every Polar night read as fully consolidated (awakeMinutes:null,
awakenings:0) with overstated asleep-vs-in-bed efficiency. Lay it as a
leading AWAKE segment so the asleep stages partition the remaining window and
the reader surfaces real awake time.

WHOOP and Polar independently implemented the same contiguous-timeline walk.
Extract a shared reconstructContiguousSleepTimeline helper under src/lib/sleep
and call it from both; each mapper keeps only its field normalisation. Oura's
measured 5-min hypnogram is a different algorithm and is left untouched.
This commit is contained in:
Marc-André Bombeck committed 2026-06-15 01:19:25 +02:00
1 parent a27247f93c
commit 290f46fcda
7 files changed
+351 -86

No files matched your search

@@ -399,6 +399,22 @@ describe("reconstructSleepSessions", () => {
expect(new Set(ends).size).toBe(ends.length);
});
it("flags a Polar-won night as reconstructed (synthetic stage order)", () => {
// Polar exposes only per-stage duration totals, so its mapper reconstructs
// a contiguous CORE→DEEP→REM order exactly like WHOOP. The reader must label
// such a night reconstructed so the UI never presents the synthetic order as
// measured timing.
const rows: SleepStageRow[] = [
srcRow("2026-06-04T01:00:00.000Z", "CORE", 240, "POLAR"),
srcRow("2026-06-04T03:00:00.000Z", "DEEP", 120, "POLAR"),
srcRow("2026-06-04T05:00:00.000Z", "REM", 120, "POLAR"),
];
const sessions = reconstructSleepSessions(rows, "UTC");
expect(sessions).toHaveLength(1);
expect(sessions[0].source).toBe("POLAR");
expect(sessions[0].reconstructed).toBe(true);
});
it("does NOT flag an Apple-Health night as reconstructed (measured timeline)", () => {
const rows: SleepStageRow[] = [
srcRow("2026-06-04T01:00:00.000Z", "CORE", 240, "APPLE_HEALTH"),
+6 -4
View File
@@ -285,13 +285,15 @@ const NO_SOURCE = "__none__";
* Sources whose per-segment stage ORDER is synthesised, not measured. WHOOP's
* v2 API exposes only per-stage duration totals (no onset timestamps), so the
* ingest mapper reconstructs a contiguous timeline in a fixed physiological
* order (`whoop/client.ts` `mapSleep`). A session won by such a source carries
* `reconstructed: true` so the UI labels it as an approximate layout. Apple
* Health / Withings / Fitbit all store a real per-segment series and stay off
* this set.
* order (`whoop/client.ts` + `polar/client.ts` `mapSleep`, both via the shared
* `reconstructContiguousSleepTimeline` builder). A session won by such a source
* carries `reconstructed: true` so the UI labels it as an approximate layout.
* Apple Health / Oura / Withings / Fitbit all store a real per-segment series
* and stay off this set.
*/
const RECONSTRUCTED_TIMELINE_SOURCES: ReadonlySet<string> = new Set<string>([
"WHOOP",
"POLAR",
]);
/**