feat(measurements): add WHOOP source, native score types, and source-priority ladders

Add the WHOOP integration data layer (migration 0111): seven new
MeasurementType values (HRV_RMSSD, DAY_STRAIN, WORKOUT_STRAIN,
SLEEP_PERFORMANCE, SLEEP_EFFICIENCY, SLEEP_CONSISTENCY, SLEEP_NEED,
ENERGY_EXPENDITURE_KJ), a WHOOP MeasurementSource, the WhoopConnection
and WhoopOAuthState models, and the per-user encrypted BYO-key columns
on User. Day-strain takes DAY_STRAIN (not the COMPUTED STRAIN_SCORE) and
RMSSD takes HRV_RMSSD (not the SDNN HEART_RATE_VARIABILITY) so a native
value never shares a bucket with a derived proxy.

Carry the new types through the completeness walls (units, value ranges,
chart tokens, PR direction, categories, list-meta colours/icons/labels,
HealthKit-mapping exclusions) and feed the cross-source picker: recovery
ladder (WHOOP native above the COMPUTED proxy), WHOOP inserted into the
overlapping vital ladders and the workout ladder, plus skinTemperature
and respiratoryRate metric keys.
This commit is contained in:
Marc-André Bombeck committed 2026-06-04 00:49:31 +02:00
1 parent 747a62516a
commit 6db3bef413
21 files changed
+649 -31

No files matched your search

@@ -0,0 +1,121 @@
/**
* v1.11.0 — cross-source priority verification when WHOOP and Apple Health
* (and the COMPUTED recovery engine) populate the same metric for the same
* day. The E-slice oracle: WHOOP feeds the existing two-axis picker via the
* v1.11 ladder additions — no new selection engine.
*
* The recommended defaults (see `src/lib/validations/source-priority.ts`)
* lead the recovery-input ladders with WHOOP (a worn-all-night strap has
* higher-resolution overnight sampling than the iPhone-relayed HealthKit
* summary), keep a real scale ahead of WHOOP's body-measurement estimate for
* weight, and rank WHOOP's device-native Recovery above the COMPUTED proxy.
*/
import { describe, expect, it } from "vitest";
import { pickCanonicalSourceRows } from "@/lib/analytics/source-priority";
function isoDayKey(d: Date): string {
return d.toISOString().slice(0, 10);
}
describe("cross-source priority — WHOOP + Apple Health", () => {
it("picks WHOOP over APPLE_HEALTH for resting heart rate (default)", () => {
const rows = [
{
measuredAt: new Date("2026-06-03T06:00:00Z"),
source: "APPLE_HEALTH" as const,
type: "RESTING_HEART_RATE" as const,
value: 54,
},
{
measuredAt: new Date("2026-06-03T05:30:00Z"),
source: "WHOOP" as const,
type: "RESTING_HEART_RATE" as const,
value: 51,
},
];
const out = pickCanonicalSourceRows(
rows,
"restingHeartRate",
null,
isoDayKey,
);
expect(out.canonicalRows).toHaveLength(1);
expect(out.canonicalRows[0].source).toBe("WHOOP");
expect(out.canonicalRows[0].value).toBe(51);
expect(out.pickedByDay.get("2026-06-03")).toBe("WHOOP");
});
it("picks WHOOP over APPLE_HEALTH for the recovery-input ladders", () => {
// sleep / hrv / respiratoryRate all lead with WHOOP in the default
// ladder. One assertion per metric guards against a future ladder
// tweak that diverges one of them.
for (const { metricKey, type } of [
{ metricKey: "hrv", type: "HEART_RATE_VARIABILITY" },
{ metricKey: "respiratoryRate", type: "RESPIRATORY_RATE" },
] as const) {
const rows = [
{
measuredAt: new Date("2026-06-03T06:00:00Z"),
source: "APPLE_HEALTH" as const,
type,
value: 42,
},
{
measuredAt: new Date("2026-06-03T05:30:00Z"),
source: "WHOOP" as const,
type,
value: 58,
},
];
const out = pickCanonicalSourceRows(rows, metricKey, null, isoDayKey);
expect(out.canonicalRows, metricKey).toHaveLength(1);
expect(out.canonicalRows[0].source, metricKey).toBe("WHOOP");
}
});
it("keeps a real scale ahead of WHOOP for weight", () => {
const rows = [
{
measuredAt: new Date("2026-06-03T07:00:00Z"),
source: "WHOOP" as const,
type: "WEIGHT" as const,
value: 80.5,
},
{
measuredAt: new Date("2026-06-03T07:05:00Z"),
source: "WITHINGS" as const,
type: "WEIGHT" as const,
value: 79.9,
},
];
const out = pickCanonicalSourceRows(rows, "weight", null, isoDayKey);
expect(out.canonicalRows).toHaveLength(1);
expect(out.canonicalRows[0].source).toBe("WITHINGS");
});
it("ranks WHOOP native recovery above the COMPUTED proxy", () => {
// Native-vs-derived: both rows share the RECOVERY_SCORE type and the
// same day, distinguished only by source. The `recovery` ladder
// (["WHOOP", "COMPUTED"]) resolves native-above-proxy with the same
// picker — no second engine.
const rows = [
{
measuredAt: new Date("2026-06-03T05:30:00Z"),
source: "COMPUTED" as const,
type: "RECOVERY_SCORE" as const,
value: 62,
},
{
measuredAt: new Date("2026-06-03T05:30:00Z"),
source: "WHOOP" as const,
type: "RECOVERY_SCORE" as const,
value: 71,
},
];
const out = pickCanonicalSourceRows(rows, "recovery", null, isoDayKey);
expect(out.canonicalRows).toHaveLength(1);
expect(out.canonicalRows[0].source).toBe("WHOOP");
expect(out.canonicalRows[0].value).toBe(71);
});
});
@@ -89,6 +89,11 @@ export interface WorkoutPickerRow {
*/
export const DEFAULT_WORKOUT_SOURCE_PRIORITY: readonly MeasurementSource[] = [
"APPLE_HEALTH",
// v1.11.0 — WHOOP ranks second. Apple Watch GPS + HR is the richest run
// record; a WHOOP strap is the next-best when no watch logged the same
// session. The read-time picker clusters a WHOOP run and an Apple-Health
// run by (activityType, startedAt ± window) and keeps the ladder winner.
"WHOOP",
"WITHINGS",
"MANUAL",
"IMPORT",