mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 15:42:24 +00:00
Every interactive path, every import, MCP and Telegram checked an incoming measurement against VALUE_RANGES before storing it. The provider sync writers did not, so a decode slip or a glitched sensor landed a row the application itself calls impossible, and every mean, median and personal band derived from it inherited the impossibility. Filtering the read side hid the row. It kept landing. The gate is one module reading the domain the app already declares, applied at two kinds of seam. reconcileExternalMeasurement is the shared identity writer for Polar, Oura, WHOOP, the Withings sleep leg, the Apple export importer and the batch endpoint, so gating there means a provider added later inherits the refusal rather than having to remember a check it never knew about; the new rejected_range verdict carries no row, which is what makes the compiler force every caller to handle it. The five writers that hold Prisma themselves gate at their entry, before the existence probe decides insert versus overwrite, so an impossible reading can no longer replace a good stored value. An out-of-band value is dropped. Not clamped, because a clamp invents a reading nobody took. Not stored behind a flag, because a quarantined row is still a row and every consumer would have to learn the flag. Not fatal to the batch, because one bad sample must not strand the run's other readings behind a held watermark. Absence reads as absence. A silent drop, though, is indistinguishable from a provider that sent nothing, so every drop is tallied on the wide event under measurement.range_rejected with the metric, the provider and the edge it broke. Counts only: the reading itself is health data and never enters the log line, and the direction plus the metric's own declared bound is what a diagnosis actually needs. The tally accumulates rather than replacing, because a provider gates once per resource leg and the reconciler once per row. One warning per provider lifts the event to warn so a drop does not hide inside a green line. A structural guard enumerates all twenty measurement writers with the reason each is allowed to write, so a new one has to answer the question before it ships. scripts/diagnose-implausible-measurements.ts reports the rows already stored, aggregated per metric and source with no value beyond the bucket's own extremes. It repairs nothing: whether a stored impossible row is a decode slip or a band that is too narrow is not a call a script gets to make.
262 lines
7.9 KiB
TypeScript
262 lines
7.9 KiB
TypeScript
/**
|
||
* Withings writes the measurement table directly — the measures leg row by
|
||
* row, the activity leg as a planned batch — so neither inherits the shared
|
||
* reconciler's gate. These cases pin that both legs now clear the app's own
|
||
* declared plausibility band before anything reaches Prisma, and that a
|
||
* refusal lands on the wide event rather than vanishing.
|
||
*
|
||
* The measures leg matters twice over: it does not only insert, it OVERWRITES
|
||
* the stored value of an existing slot, so an ungated impossible reading could
|
||
* replace a real one.
|
||
*/
|
||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||
|
||
vi.mock("@/lib/db", () => ({
|
||
prisma: {
|
||
measurement: {
|
||
findFirst: vi.fn(),
|
||
findMany: vi.fn(),
|
||
create: vi.fn(),
|
||
createMany: vi.fn(),
|
||
update: vi.fn(),
|
||
},
|
||
withingsConnection: {
|
||
findUnique: vi.fn(),
|
||
update: vi.fn(),
|
||
},
|
||
$queryRaw: vi.fn(),
|
||
$transaction: vi.fn((ops: unknown) =>
|
||
Array.isArray(ops) ? Promise.all(ops) : Promise.resolve(),
|
||
),
|
||
},
|
||
}));
|
||
|
||
vi.mock("@/lib/crypto", () => ({
|
||
decrypt: vi.fn((v: string) => v),
|
||
encrypt: vi.fn((v: string) => v),
|
||
}));
|
||
|
||
vi.mock("../client", async () => {
|
||
const actual = await vi.importActual<typeof import("../client")>("../client");
|
||
return {
|
||
...actual,
|
||
fetchMeasurements: vi.fn(),
|
||
refreshAccessToken: vi.fn(),
|
||
subscribeWebhook: vi.fn(),
|
||
};
|
||
});
|
||
|
||
vi.mock("../credentials", () => ({
|
||
getUserWithingsCredentials: vi.fn(async () => ({
|
||
clientId: "cid",
|
||
clientSecret: "secret",
|
||
})),
|
||
}));
|
||
|
||
vi.mock("@/lib/integrations/status", () => ({
|
||
isReauthRequired: vi.fn().mockResolvedValue(false),
|
||
parkIntegrationAtReauth: vi.fn(),
|
||
recordSyncFailure: vi.fn().mockResolvedValue(undefined),
|
||
recordSyncSuccess: vi.fn().mockResolvedValue(undefined),
|
||
}));
|
||
|
||
vi.mock("@/lib/rollups/measurement-rollups", async () => {
|
||
const actual = await vi.importActual<
|
||
typeof import("@/lib/rollups/measurement-rollups")
|
||
>("@/lib/rollups/measurement-rollups");
|
||
return {
|
||
...actual,
|
||
recomputeBucketsForMeasurement: vi.fn().mockResolvedValue(undefined),
|
||
};
|
||
});
|
||
|
||
vi.mock("@/lib/insights/comprehensive-generate", () => ({
|
||
invalidateStatusInsightsForTypes: vi.fn().mockResolvedValue(undefined),
|
||
}));
|
||
|
||
vi.mock("@/lib/cache/invalidate", () => ({
|
||
invalidateUserDashboardSnapshot: vi.fn(),
|
||
}));
|
||
|
||
import { prisma } from "@/lib/db";
|
||
import { eventStorage } from "@/lib/logging/context";
|
||
import { WideEventBuilder } from "@/lib/logging/event-builder";
|
||
import { RANGE_REJECTED_META_KEY } from "@/lib/measurements/plausibility-gate";
|
||
import type { RangeRejectionTally } from "@/lib/measurements/plausibility-gate";
|
||
|
||
import { fetchMeasurements } from "../client";
|
||
import { syncUserActivity } from "../sync-activity";
|
||
import { syncUserMeasurements } from "../sync";
|
||
|
||
let event: WideEventBuilder;
|
||
|
||
function tally(): RangeRejectionTally | undefined {
|
||
return event.toJSON().meta?.[RANGE_REJECTED_META_KEY] as
|
||
RangeRejectionTally | undefined;
|
||
}
|
||
|
||
function installActivityFetch(
|
||
entries: Array<{ date: string; steps?: number; distance?: number }>,
|
||
) {
|
||
vi.stubGlobal(
|
||
"fetch",
|
||
vi.fn(async () => ({
|
||
status: 200,
|
||
headers: new Headers(),
|
||
json: async () => ({
|
||
status: 0,
|
||
body: { activities: entries, more: false, offset: 0 },
|
||
}),
|
||
})),
|
||
);
|
||
}
|
||
|
||
beforeEach(() => {
|
||
vi.clearAllMocks();
|
||
event = new WideEventBuilder("background");
|
||
vi.mocked(prisma.withingsConnection.findUnique).mockResolvedValue({
|
||
id: "conn-1",
|
||
withingsUserId: "wu-1",
|
||
accessToken: "enc-access",
|
||
refreshToken: "enc-refresh",
|
||
scope: "user.metrics,user.activity",
|
||
tokenExpiresAt: new Date(Date.now() + 60 * 60 * 1000),
|
||
lastSyncedAt: new Date(Date.now() - 60 * 60 * 1000),
|
||
} as never);
|
||
vi.mocked(prisma.withingsConnection.update).mockResolvedValue({} as never);
|
||
vi.mocked(prisma.measurement.findFirst).mockResolvedValue(null);
|
||
vi.mocked(prisma.measurement.findMany).mockResolvedValue([] as never);
|
||
vi.mocked(prisma.measurement.create).mockResolvedValue({
|
||
id: "m-1",
|
||
type: "PULSE",
|
||
measuredAt: new Date(),
|
||
} as never);
|
||
vi.mocked(prisma.measurement.createMany).mockResolvedValue({
|
||
count: 1,
|
||
} as never);
|
||
vi.mocked(prisma.measurement.update).mockResolvedValue({} as never);
|
||
});
|
||
|
||
afterEach(() => {
|
||
vi.unstubAllGlobals();
|
||
});
|
||
|
||
describe("syncUserMeasurements — plausibility gate", () => {
|
||
it("writes a reading inside the metric's declared band", async () => {
|
||
vi.mocked(fetchMeasurements).mockResolvedValue([
|
||
{
|
||
type: "PULSE",
|
||
value: 112,
|
||
measuredAt: new Date("2026-05-16T08:00:00Z"),
|
||
},
|
||
] as never);
|
||
|
||
const result = await eventStorage.run(event, () =>
|
||
syncUserMeasurements("user-1"),
|
||
);
|
||
|
||
expect(result).toEqual({ imported: 1, failed: false });
|
||
expect(prisma.measurement.create).toHaveBeenCalledTimes(1);
|
||
expect(tally()).toBeUndefined();
|
||
});
|
||
|
||
it("refuses an out-of-band reading and annotates the drop", async () => {
|
||
vi.mocked(fetchMeasurements).mockResolvedValue([
|
||
{
|
||
type: "PULSE",
|
||
value: 112,
|
||
measuredAt: new Date("2026-05-16T08:00:00Z"),
|
||
},
|
||
{
|
||
type: "PULSE",
|
||
value: 55643821.505,
|
||
measuredAt: new Date("2026-05-16T08:05:00Z"),
|
||
},
|
||
] as never);
|
||
|
||
const result = await eventStorage.run(event, () =>
|
||
syncUserMeasurements("user-1"),
|
||
);
|
||
|
||
// Only the real reading landed, and the run is still a clean success: one
|
||
// impossible sample must not strand the rest behind a held watermark.
|
||
expect(result).toEqual({ imported: 1, failed: false });
|
||
expect(prisma.measurement.create).toHaveBeenCalledTimes(1);
|
||
expect(tally()).toEqual({
|
||
total: 1,
|
||
buckets: [
|
||
{ source: "withings", type: "PULSE", direction: "above_max", count: 1 },
|
||
],
|
||
});
|
||
expect(event.getLevel()).toBe("warn");
|
||
});
|
||
|
||
it("does not overwrite a stored reading with an out-of-band one", async () => {
|
||
// The slot already holds a real pulse; the provider re-sends the same
|
||
// instant carrying a decode slip. Ungated, the update branch rewrote the
|
||
// good value with the bad one.
|
||
vi.mocked(prisma.measurement.findFirst).mockResolvedValue({
|
||
id: "existing-row",
|
||
} as never);
|
||
vi.mocked(fetchMeasurements).mockResolvedValue([
|
||
{
|
||
type: "PULSE",
|
||
value: 55643821.505,
|
||
measuredAt: new Date("2026-05-16T08:00:00Z"),
|
||
},
|
||
] as never);
|
||
|
||
const result = await eventStorage.run(event, () =>
|
||
syncUserMeasurements("user-1"),
|
||
);
|
||
|
||
expect(result).toEqual({ imported: 0, failed: false });
|
||
expect(prisma.measurement.update).not.toHaveBeenCalled();
|
||
expect(tally()?.total).toBe(1);
|
||
});
|
||
});
|
||
|
||
describe("syncUserActivity — plausibility gate", () => {
|
||
it("plans a daily total inside the metric's declared band", async () => {
|
||
installActivityFetch([{ date: "2026-05-16", steps: 8_400 }]);
|
||
|
||
const imported = await eventStorage.run(event, () =>
|
||
syncUserActivity("user-1"),
|
||
);
|
||
|
||
expect(imported).toBe(1);
|
||
expect(prisma.measurement.createMany).toHaveBeenCalledTimes(1);
|
||
expect(tally()).toBeUndefined();
|
||
});
|
||
|
||
it("refuses an out-of-band daily total and annotates the drop", async () => {
|
||
// ACTIVITY_STEPS is declared 0–200 000.
|
||
installActivityFetch([
|
||
{ date: "2026-05-16", steps: 8_400 },
|
||
{ date: "2026-05-17", steps: 9_900_000 },
|
||
]);
|
||
|
||
const imported = await eventStorage.run(event, () =>
|
||
syncUserActivity("user-1"),
|
||
);
|
||
|
||
expect(imported).toBe(1);
|
||
const created = vi.mocked(prisma.measurement.createMany).mock.calls[0]?.[0]
|
||
?.data as Array<{ value: number }>;
|
||
expect(created).toHaveLength(1);
|
||
expect(created[0]!.value).toBe(8_400);
|
||
expect(tally()).toEqual({
|
||
total: 1,
|
||
buckets: [
|
||
{
|
||
source: "withings",
|
||
type: "ACTIVITY_STEPS",
|
||
direction: "above_max",
|
||
count: 1,
|
||
},
|
||
],
|
||
});
|
||
expect(event.getLevel()).toBe("warn");
|
||
});
|
||
});
|