test(sources): integration tests for two-axis canonical-source resolution

Lock the GET /api/analytics wire-up to the new two-axis picker with a
fixture-driven test that drives a real Postgres testcontainer + the
production handler. Four scenarios on the same Berlin day:

  1. Plain default user (single source, no override) — picker passes
     through; v1.4.25-today behaviour stays a regression sentinel.
  2. Per-metric override (sleep: [WITHINGS, APPLE_HEALTH]) — flips
     the default APPLE_HEALTH-first ladder; Withings row wins.
  3. Per-device default (no user override) — Apple Watch row beats
     iPhone row under same source on default ladder (watch first).
  4. User flips device ladder to [phone, watch] — iPhone row wins.

Wire-up: analytics' fetchMeasurementSeriesChunked now selects
deviceType + type on every chunk so the picker can read both axes.
ChunkedRow gains two fields; all existing aggregation paths continue
to compile and the integration suite stays green (6/6 passing across
analytics-sleep-stages.test.ts + source-priority-two-axis.test.ts).

Unit tests already cover the picker's internal edge cases (45 unit
across helper + algorithm); this file is the end-to-end guard.
This commit is contained in:
Marc-André Bombeck committed 2026-05-14 15:36:42 +02:00
1 parent 024845e5bd
commit 53278ccd87
2 files changed
+281

No files matched your search

+18
View File
@@ -525,6 +525,18 @@ interface ChunkedRow {
* cumulative aggregators can pick ONE source per day when more than
* one ingest path contributes to the same metric. */
source: MeasurementSource;
/** v1.4.25 W8c — second axis of the canonical picker. Nullable until
* the iOS app starts shipping HKDevice.model with each sample;
* legacy / Withings rows stay NULL and the picker treats them as
* `unknown`. Carried on every type's read so the cumulative-metric
* path can break Apple-Watch-vs-iPhone ties.
*/
deviceType: string | null;
/** v1.4.25 W8c — feeds the per-MeasurementType device-type override
* inside the picker. The picker keys
* `deviceTypePriority[type]` off this so the user's "phone wins for
* steps but watch wins for HR" config is honoured. */
type: MeasurementType;
}
async function fetchMeasurementSeriesChunked(
@@ -547,6 +559,10 @@ async function fetchMeasurementSeriesChunked(
measuredAt: true,
value: true,
source: true,
// v1.4.25 W8c — read deviceType so the canonical picker can
// honour the per-metric / per-device override. Nullable until
// iOS sends it.
deviceType: true,
...(options.includeSleepStage ? { sleepStage: true } : {}),
},
take: MEASUREMENT_CHUNK_SIZE,
@@ -558,6 +574,8 @@ async function fetchMeasurementSeriesChunked(
measuredAt: row.measuredAt,
value: row.value,
source: row.source,
deviceType: row.deviceType ?? null,
type,
sleepStage:
"sleepStage" in row
? ((row.sleepStage as string | null) ?? null)