Merge remote-tracking branch 'origin/fix/apple-health-pulse-cluster' into release/v1.32.1

This commit is contained in:
Marc-André Bombeck committed 2026-07-23 10:27:20 +02:00
commit 0375fadd85
12 files changed
+1054 -101

No files matched your search

@@ -0,0 +1,269 @@
/**
* v1.32.1 (issue #585) — wide-event observability for the go-forward
* aggregated 10-min heart-rate bucket wire contract.
*
* The intraday pulse chart reads exactly the `stats:HKQuantityTypeIdentifier
* HeartRate:<10-min-Z>` rows this batch route accepts (or rejects). A report
* of "the chart looks sparser after the ZIP-import cutoff" was previously
* undiagnosable from server logs: the baseline `measurement.batch.ingest`
* annotation carried only a total `skipped` count with no reason breakdown
* and no visibility into how many of a batch's entries specifically targeted
* an HR bucket. These tests pin the new `measurement.batch.hr-bucket`
* annotation (fires only when the batch actually carried an HR-bucket entry)
* and the `skipped_by_reason` breakdown on the baseline annotation.
*/
import { describe, expect, it, vi, beforeEach } from "vitest";
import { NextRequest } from "next/server";
const mocks = vi.hoisted(() => ({ annotate: vi.fn() }));
vi.mock("@/lib/db", () => ({
prisma: {
user: {
update: vi.fn(),
},
measurement: {
findMany: vi.fn(),
createManyAndReturn: vi.fn(),
updateMany: vi.fn(),
},
$transaction: vi.fn(async (fn: unknown) => {
if (typeof fn === "function") {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (fn as any)(prisma as unknown as { measurement: unknown });
}
}),
},
}));
vi.mock("@/lib/logging/context", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/lib/logging/context")>();
return { ...actual, annotate: mocks.annotate };
});
vi.mock("@/lib/measurements/reconcile-external-measurement", () => ({
reconcileExternalMeasurement: async (
tx: {
measurement: {
findMany: (args: unknown) => Promise<
Array<{
id?: string;
type?: string;
source?: string;
externalId?: string | null;
}>
>;
createManyAndReturn: (args: {
data: Record<string, unknown>;
}) => Promise<Array<Record<string, unknown>>>;
updateMany: (args: {
where: Record<string, unknown>;
data: Record<string, unknown>;
}) => Promise<unknown>;
};
},
input: Record<string, unknown> & {
userId: string;
type: string;
source: string;
externalId: string;
},
options: { exactExternalMatch?: "update" | "duplicate" } = {},
) => {
const existing = await tx.measurement.findMany({});
const exact = existing.find(
(row: { type?: string; source?: string; externalId?: string | null }) =>
row.type === input.type &&
row.source === input.source &&
row.externalId === input.externalId,
);
if (exact && options.exactExternalMatch !== "update") {
return { status: "duplicate", row: exact };
}
if (exact) {
await tx.measurement.updateMany({
where: { id: exact.id },
data: { ...input, deletedAt: null },
});
return { status: "updated", row: { ...exact, ...input } };
}
const [inserted] = await tx.measurement.createManyAndReturn({
data: input,
});
if (inserted) return { status: "inserted", row: inserted };
if (options.exactExternalMatch === "update") {
await tx.measurement.updateMany({ where: {}, data: input });
return { status: "updated", row: input };
}
return { status: "duplicate" };
},
}));
vi.mock("@/lib/auth/session", () => ({ getSession: vi.fn() }));
vi.mock("@/lib/auth/audit", () => ({
auditLog: vi.fn().mockResolvedValue(undefined),
}));
vi.mock("@/lib/logging/transports", () => ({ emitIfSampled: vi.fn() }));
vi.mock("@/lib/db-compat", () => ({
ensureDbCompatibility: vi.fn().mockResolvedValue(undefined),
}));
vi.mock("@/lib/rate-limit", () => ({ checkRateLimit: vi.fn() }));
vi.mock("@/lib/jobs/pr-detection", () => ({
enqueuePrDetection: vi.fn().mockResolvedValue(undefined),
}));
vi.mock("@/lib/jobs/reminder-satisfy", () => ({
enqueueReminderSatisfy: vi.fn().mockResolvedValue(undefined),
}));
vi.mock("@/lib/cache/invalidate", () => ({
invalidateUserMeasurements: vi.fn(),
}));
vi.mock("@/lib/insights/comprehensive-generate", () => ({
invalidateStatusInsightsForTypes: vi.fn().mockResolvedValue(undefined),
}));
vi.mock("@/lib/rollups/measurement-rollups", () => ({
recomputeBucketsForMeasurement: vi.fn().mockResolvedValue(undefined),
collapseToTypeDayKeys: vi.fn(() => []),
}));
vi.mock("next/headers", () => ({
headers: vi.fn(async () => ({ get: () => null })),
cookies: vi.fn(async () => ({
get: () => undefined,
set: () => {},
delete: () => {},
})),
}));
import { POST } from "../route";
import { prisma } from "@/lib/db";
import { getSession } from "@/lib/auth/session";
import { checkRateLimit } from "@/lib/rate-limit";
const SESSION_OK = {
session: { id: "sess-1", expiresAt: new Date(Date.now() + 3_600_000) },
user: { id: "user-1", username: "testuser", role: "USER" as const },
};
const FRESH_BUCKET_ID =
"stats:HKQuantityTypeIdentifierHeartRate:2026-06-21T14:00:00.000Z";
const REPOST_BUCKET_ID =
"stats:HKQuantityTypeIdentifierHeartRate:2026-06-21T15:00:00.000Z";
const MALFORMED_BUCKET_ID =
"stats:HKQuantityTypeIdentifierHeartRate:2026-06-21T14:35:00.000Z";
const SAMPLE_ID = "C0FFEE00-0000-4000-8000-000000000000";
function makeRequest(body: unknown): NextRequest {
return new NextRequest("http://localhost/api/measurements/batch", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(body),
});
}
function hrEntry(externalId: string, value: number) {
return {
hkIdentifier: "HKQuantityTypeIdentifierHeartRate",
value,
unit: "count/min",
startDate: "2026-06-21T14:00:00.000Z",
endDate: "2026-06-21T14:59:59.000Z",
externalId,
};
}
function annotateCallsFor(actionName: string) {
return mocks.annotate.mock.calls.filter(
([arg]) =>
(arg as { action?: { name?: string } })?.action?.name === actionName,
);
}
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(getSession).mockResolvedValue(SESSION_OK as never);
vi.mocked(checkRateLimit).mockResolvedValue({
allowed: true,
remaining: 60,
resetAt: Date.now() + 60_000,
});
vi.mocked(prisma.measurement.createManyAndReturn).mockImplementation((async (
args: unknown,
) => {
const { data } = args as {
data:
| { type: unknown; source: string; externalId: string | null }
| Array<{ type: unknown; source: string; externalId: string | null }>;
};
const rows = Array.isArray(data) ? data : [data];
return rows.map(({ type, source, externalId }) => ({
type,
source,
externalId,
}));
}) as never);
vi.mocked(prisma.measurement.updateMany).mockResolvedValue({ count: 1 });
});
describe("POST /api/measurements/batch — HR-bucket observability (issue #585)", () => {
it("reports a per-outcome breakdown covering only HR-bucket entries", async () => {
// REPOST_BUCKET_ID already exists — the reconcile mock overwrites it.
vi.mocked(prisma.measurement.findMany).mockResolvedValue([
{
type: "PULSE",
source: "APPLE_HEALTH",
externalId: REPOST_BUCKET_ID,
},
] as never);
const res = await POST(
makeRequest({
entries: [
hrEntry(FRESH_BUCKET_ID, 72), // inserted
hrEntry(REPOST_BUCKET_ID, 78), // updated (overwrite)
hrEntry(MALFORMED_BUCKET_ID, 70), // skipped (malformed)
hrEntry(SAMPLE_ID, 64), // NOT an HR bucket — must be excluded
],
}),
);
expect(res.status).toBe(200);
const calls = annotateCallsFor("measurement.batch.hr-bucket");
expect(calls).toHaveLength(1);
const meta = (calls[0][0] as { meta: Record<string, number> }).meta;
expect(meta).toEqual({
attempted: 3,
inserted: 1,
updated: 1,
duplicate: 0,
skipped: 1,
failed: 0,
processed: 4,
});
});
it("never fires the HR-bucket annotation for a batch with no bucket entries", async () => {
vi.mocked(prisma.measurement.findMany).mockResolvedValue([]);
const res = await POST(makeRequest({ entries: [hrEntry(SAMPLE_ID, 64)] }));
expect(res.status).toBe(200);
expect(annotateCallsFor("measurement.batch.hr-bucket")).toHaveLength(0);
});
it("breaks the baseline ingest annotation's skip count down by reason", async () => {
vi.mocked(prisma.measurement.findMany).mockResolvedValue([]);
const res = await POST(
makeRequest({
entries: [
hrEntry(MALFORMED_BUCKET_ID, 70), // skipped: malformed_hr_bucket_id
],
}),
);
expect(res.status).toBe(200);
const calls = annotateCallsFor("measurement.batch.ingest");
expect(calls).toHaveLength(1);
const meta = calls[0][0] as { meta: { skipped_by_reason: unknown } };
expect(meta.meta.skipped_by_reason).toEqual({
malformed_hr_bucket_id: 1,
});
});
});
+73
View File
@@ -603,6 +603,18 @@ async function postBatch(request: NextRequest): Promise<Response> {
.filter((r) => r.status === "skipped")
.map((r) => ({ index: r.index, reason: r.reason ?? "unknown" }));
// v1.32.1 (issue #585) — per-reason skip breakdown. The baseline
// annotation below only ever carried the total `skipped` count, so an
// operator investigating "why does this account's uploaded data look
// sparser than expected" had no way to see WHICH validation gate an
// entry tripped (malformed bucket id, out-of-range value, unmappable
// identifier, …) without pulling raw rows. Grouping by reason turns
// that into a one-glance wide-event field.
const skippedByReason: Record<string, number> = {};
for (const s of skipped) {
skippedByReason[s.reason] = (skippedByReason[s.reason] ?? 0) + 1;
}
await auditLog("measurement.batch.ingest", {
userId: user.id,
ipAddress: getClientIp(request),
@@ -648,6 +660,64 @@ async function postBatch(request: NextRequest): Promise<Response> {
});
}
// v1.32.1 (issue #585) — dedicated outcome breakdown for entries
// targeting the go-forward aggregated 10-min heart-rate bucket prefix
// (`stats:HKQuantityTypeIdentifierHeartRate:<10-min-Z>`). The intraday
// pulse chart reads exactly these rows for a live-synced day; a report
// of "the chart looks sparser after the ZIP import cutoff" is
// unanswerable from the baseline ingest trace alone, since it never
// distinguished an HR-bucket write from any other entry in the batch.
// Grepping `measurement.batch.hr-bucket` now shows, per sync, how many
// bucket entries the client sent and what happened to each — confirming
// (or ruling out) server-side rejection/dedup as the density-gap cause.
// Only fires when the batch actually carried at least one HR-bucket
// entry so the baseline trace is unchanged for every other sync.
let hrBucketInserted = 0;
let hrBucketUpdated = 0;
let hrBucketDuplicate = 0;
let hrBucketSkipped = 0;
let hrBucketFailed = 0;
for (let index = 0; index < entries.length; index++) {
if (!targetsAggregatedBucket(entries[index].externalId)) continue;
switch (results[index]?.status) {
case "inserted":
hrBucketInserted++;
break;
case "updated":
hrBucketUpdated++;
break;
case "duplicate":
hrBucketDuplicate++;
break;
case "skipped":
hrBucketSkipped++;
break;
case "failed":
hrBucketFailed++;
break;
}
}
const hrBucketAttempted =
hrBucketInserted +
hrBucketUpdated +
hrBucketDuplicate +
hrBucketSkipped +
hrBucketFailed;
if (hrBucketAttempted > 0) {
annotate({
action: { name: "measurement.batch.hr-bucket" },
meta: {
attempted: hrBucketAttempted,
inserted: hrBucketInserted,
updated: hrBucketUpdated,
duplicate: hrBucketDuplicate,
skipped: hrBucketSkipped,
failed: hrBucketFailed,
processed: entries.length,
},
});
}
// v1.4.25 W16c — kick off PR detection for this user. We always
// enqueue when at least one row was written (or the batch had any
// measurements to consider) so a single off-day reading still gets
@@ -693,6 +763,9 @@ async function postBatch(request: NextRequest): Promise<Response> {
updated: updatedCount,
duplicates: duplicateCount,
skipped: skipped.length,
// v1.32.1 (issue #585) — per-reason skip breakdown; see the
// `skippedByReason` build above.
skipped_by_reason: skippedByReason,
failed: failedCount,
},
});
@@ -0,0 +1,144 @@
import { describe, expect, it, vi, beforeEach } from "vitest";
import { renderToStaticMarkup } from "react-dom/server";
/**
* v1.32.1 (issue #584) — `/insights/pulse` must chart the metric its own
* title, capture action, and stat strip claim. `hasRestingHr` used to swap
* the ENTIRE primary chart + Coach read strip to RESTING_HEART_RATE the
* moment any resting-HR row existed, so the page could show a resting-only
* series while every other surface (title, stat strip, capture action,
* "show all values") stayed labelled "Pulse" — a silent full swap.
* RESTING_HEART_RATE may now appear only as a second, clearly-labelled
* series alongside PULSE (the same multi-series pattern the blood-pressure
* page uses for systolic/diastolic) — never a swap.
*
* `SubPageShell` is stubbed to a probe recording its props (including the
* `children` array holding the chart element) so these assert the resolved
* chart/coach-strip props rather than rendered text — responsive classes
* have broken text-based queries in this repo.
*/
const shellProps = vi.fn();
vi.mock("@/components/insights/sub-page-shell", () => ({
SubPageShell: (props: Record<string, unknown>) => {
shellProps(props);
return null;
},
}));
vi.mock("@/lib/i18n/context", () => ({
useTranslations: () => ({
t: (key: string) => key,
}),
}));
vi.mock("@/hooks/use-auth", () => ({
useAuth: () => ({
isAuthenticated: true,
user: { dateOfBirth: null, gender: null, timezone: "UTC" },
}),
}));
const analytics = {
current: { summaries: {} as Record<string, { count: number }> },
};
vi.mock("@/hooks/use-insights-analytics", () => ({
useInsightsAnalytics: () => ({
data: analytics.current,
isLoading: false,
isEmpty: false,
error: null,
refetch: () => {},
}),
}));
// `useInsightsLayoutPrefs` is the one real hook left that reaches
// `@tanstack/react-query`; stub `useQuery` so the page renders without a
// `QueryClientProvider` ancestor (mirrors the coach-page-launch-params test).
vi.mock("@tanstack/react-query", () => ({
useQuery: () => ({ data: undefined, isLoading: false, error: null }),
}));
import InsightsPulsPage from "../page";
interface CapturedShellProps {
captureType?: string;
showAllValuesType?: string;
coachReadStrip?: { props?: Record<string, unknown> };
children?: unknown;
}
function renderPage(
summaries: Record<string, { count: number }>,
): CapturedShellProps {
analytics.current = { summaries };
renderToStaticMarkup(<InsightsPulsPage />);
const calls = shellProps.mock.calls;
return calls[calls.length - 1][0] as CapturedShellProps;
}
/** Locate the primary `<HealthChartDynamic>` element among the page's
* children by its distinctive `types` array prop. Never rendered — the
* `SubPageShell` stub does not paint `children` — so this reads the plain
* React element object the page constructed. */
function findChartElement(
children: unknown,
): { props: { types?: string[] } } | undefined {
const arr = Array.isArray(children) ? children : [children];
return arr.find(
(el): el is { props: { types?: string[] } } =>
Boolean(el) &&
typeof el === "object" &&
el !== null &&
"props" in el &&
Array.isArray((el as { props?: { types?: unknown } }).props?.types),
);
}
describe("/insights/pulse page — chart identity (issue #584)", () => {
beforeEach(() => {
shellProps.mockClear();
});
it("charts PULSE as the primary (first) series even when resting-HR data exists", () => {
const props = renderPage({
PULSE: { count: 50 },
RESTING_HEART_RATE: { count: 30 },
});
const chart = findChartElement(props.children);
expect(chart).toBeDefined();
expect(chart!.props.types).toEqual(["PULSE", "RESTING_HEART_RATE"]);
});
it("never swaps the chart to a RESTING_HEART_RATE-only series", () => {
const props = renderPage({
PULSE: { count: 50 },
RESTING_HEART_RATE: { count: 30 },
});
const chart = findChartElement(props.children);
expect(chart!.props.types).not.toEqual(["RESTING_HEART_RATE"]);
});
it("charts PULSE alone when there is no resting-HR data", () => {
const props = renderPage({ PULSE: { count: 50 } });
const chart = findChartElement(props.children);
expect(chart!.props.types).toEqual(["PULSE"]);
});
it("keeps the Coach read strip pinned to PULSE regardless of resting-HR data", () => {
const props = renderPage({
PULSE: { count: 50 },
RESTING_HEART_RATE: { count: 30 },
});
expect(props.coachReadStrip?.props?.metricType).toBe("PULSE");
});
it("keeps captureType and showAllValuesType pinned to PULSE", () => {
const props = renderPage({
PULSE: { count: 50 },
RESTING_HEART_RATE: { count: 30 },
});
expect(props.captureType).toBe("PULSE");
expect(props.showAllValuesType).toBe("PULSE");
});
});
+30 -61
View File
@@ -35,20 +35,23 @@ const IntradayPulseChart = dynamic(
})),
{ ssr: false, loading: () => <ChartSkeleton /> },
);
import {
getAgeFromDateOfBirth,
getPersonalizedPulseTarget,
} from "@/lib/analytics/pulse-targets";
/**
* v1.4.25 W4 — `/insights/pulse`.
*
* Routed Pulse sub-page. Renders the pulse chart with the personalized
* Karvonen-derived target band plus the per-section AI assessment.
* Note: `chartKey="pulse"` so the chart-cog can override the
* comparison-overlay independently from the dashboard pulse card; the
* MeasurementType filter is `PULSE` (the same field used elsewhere
* in the codebase).
* Routed Pulse sub-page. Renders the raw/workout-inclusive pulse chart
* plus the per-section AI assessment. Note: `chartKey="pulse"` so the
* chart-cog can override the comparison-overlay independently from the
* dashboard pulse card; the MeasurementType filter is `PULSE` (the same
* field used elsewhere in the codebase).
*
* v1.32.1 (issue #584) — this page no longer paints the personalized
* resting-pulse target band (`getPersonalizedPulseTarget()`, CDC/NCHS
* resting-pulse percentiles) behind the chart: that band is calibrated
* to steady-state resting readings and would misreport an expected
* workout spike in the raw PULSE series as "out of target". The
* personal target + its band belong on the dedicated
* `/insights/resting-pulse` page, against the clean resting series.
*
* Analytics fetch + empty-state branch consume `useInsightsAnalytics()`
* + `<MetricEmptyState>`. VO₂ max is a cardio-fitness metric, so the page
@@ -66,11 +69,16 @@ export default function InsightsPulsPage() {
// dedicated cardio-fitness page.
const hasVo2 = (analytics?.summaries?.VO2_MAX?.count ?? 0) > 0;
const pulseSummary = analytics?.summaries?.PULSE ?? null;
// v1.15.12 A2 — the resting-pulse band is judged against
// RESTING_HEART_RATE (Apple's clean daily resting figure). When the
// user has resting rows the chart shows that series against the band;
// otherwise it charts raw heart rate WITHOUT the resting-band overlay
// (which would flag expected-high workout HR as "outside target").
// v1.32.1 (issue #584) — this page is titled, captured, and stat-stripped
// as PULSE throughout; the chart must chart PULSE too. `hasRestingHr` used
// to swap the ENTIRE primary chart + Coach read to RESTING_HEART_RATE
// whenever any resting row existed, which silently showed a different
// metric than the page's own title/stat-strip/capture action claimed.
// RESTING_HEART_RATE now only ever appears as a second, clearly-labelled
// series alongside PULSE (same multi-series pattern the blood-pressure
// page uses for systolic/diastolic) — never a full swap. The dedicated
// `/insights/resting-pulse` page owns the resting-only view + its target
// band.
const hasRestingHr =
(analytics?.summaries?.RESTING_HEART_RATE?.count ?? 0) > 0;
@@ -108,44 +116,6 @@ export default function InsightsPulsPage() {
);
}
const pulseAge = getAgeFromDateOfBirth(user?.dateOfBirth ?? null);
const pulseTarget = getPersonalizedPulseTarget(
pulseAge,
(user?.gender as "MALE" | "FEMALE" | null | undefined) ?? null,
);
const pulseBands = [
{
min: 30,
max: pulseTarget.orangeMin,
color: "var(--destructive)",
opacity: 0.16,
},
{
min: pulseTarget.orangeMin,
max: pulseTarget.greenMin,
color: "var(--warning)",
opacity: 0.18,
},
{
min: pulseTarget.greenMin,
max: pulseTarget.greenMax,
color: "var(--success)",
opacity: 0.2,
},
{
min: pulseTarget.greenMax,
max: pulseTarget.orangeMax,
color: "var(--warning)",
opacity: 0.18,
},
{
min: pulseTarget.orangeMax,
max: 220,
color: "var(--destructive)",
opacity: 0.16,
},
].filter((band) => band.max > band.min);
return (
<SubPageShell
title={t("insights.pulseSectionTitle")}
@@ -161,11 +131,7 @@ export default function InsightsPulsPage() {
/>
}
coachReadStrip={
<CoachReadStrip
metricType={hasRestingHr ? "RESTING_HEART_RATE" : "PULSE"}
unit="bpm"
fractionDigits={0}
/>
<CoachReadStrip metricType="PULSE" unit="bpm" fractionDigits={0} />
}
diversityNudge={
<MeasurementDiversityNudge
@@ -180,12 +146,15 @@ export default function InsightsPulsPage() {
>
<HealthChartDynamic
chartKey="pulse"
types={hasRestingHr ? ["RESTING_HEART_RATE"] : ["PULSE"]}
types={hasRestingHr ? ["PULSE", "RESTING_HEART_RATE"] : ["PULSE"]}
title={t("charts.pulse")}
titleIcon={Heart}
colors={["var(--success)"]}
colors={
hasRestingHr
? ["var(--success)", "var(--destructive)"]
: ["var(--success)"]
}
unit="bpm"
valueBands={hasRestingHr ? pulseBands : undefined}
compareBaseline={compareBaseline}
userTimezone={user?.timezone}
onVisibleStats={onVisibleStats}