mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 08:02:12 +00:00
fix(score): read biomarker bounds by their real column names
The score reader's labs query selected referenceLow/referenceHigh on the joined Biomarker, but the catalog's range columns are lowerBound and upperBound. Prisma rejects a select against names a model does not have at query-validation time, before any row is read, so every labs read threw and both the Glycaemia and Lipids pillars reported a failed read permanently, Retry included, even for accounts with valid lab results. Select the real columns, map the catalog bounds onto the reader's range fallback, and pin two contracts: a real-Postgres round trip that scores both pillars through the biomarker join (including a lipid row whose range lives only on the catalog), and the honest empty state, where a labs account with no rows reads as not tracked rather than read_failed, which stays reserved for a read that genuinely failed. Refs #780
This commit is contained in:
1 parent
d21fe48e3c
commit
19c54082bf
3 files changed
+250
-8
No files matched your search
@@ -1,7 +1,11 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import type { PrismaClient } from "@/generated/prisma/client";
|
||||
|
||||
import { readActivityDays, readFastingGlucoseMeasurements } from "../reader";
|
||||
import {
|
||||
computeUserHealthScore,
|
||||
readActivityDays,
|
||||
readFastingGlucoseMeasurements,
|
||||
} from "../reader";
|
||||
|
||||
function dbWithRows(rows: unknown[]) {
|
||||
return {
|
||||
@@ -107,6 +111,51 @@ describe("score reader bounds and source parity", () => {
|
||||
expect(result.days).toEqual([{ day: "2026-07-02", value: 1_000 }]);
|
||||
});
|
||||
|
||||
it("reads an empty labs set as not_tracked, never read_failed", async () => {
|
||||
// A labs-enabled account with no lab rows has honestly recorded
|
||||
// nothing: the read SUCCEEDED and found an empty set. `read_failed`
|
||||
// is reserved for a read that threw — showing it here would offer a
|
||||
// Retry that can never help.
|
||||
const db = {
|
||||
measurement: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
mentalHealthAssessment: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
labResult: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
dismissedPriorityItem: { findUnique: vi.fn().mockResolvedValue(null) },
|
||||
} as unknown as PrismaClient;
|
||||
|
||||
const report = await computeUserHealthScore({
|
||||
prisma: db,
|
||||
userId: "user-1",
|
||||
now: new Date("2026-07-28T12:00:00.000Z"),
|
||||
profile: {
|
||||
dateOfBirth: new Date("1985-07-09"),
|
||||
heightCm: 178,
|
||||
timezone: "Europe/Berlin",
|
||||
sourcePriorityJson: null,
|
||||
thresholdsJson: null,
|
||||
},
|
||||
modules: {
|
||||
glucose: true,
|
||||
labs: true,
|
||||
sleep: true,
|
||||
mentalHealth: true,
|
||||
},
|
||||
healthScoreConfigJson: null,
|
||||
bpTargets: null,
|
||||
bpEnvelope: null,
|
||||
bpEnvelopePriorWeek: null,
|
||||
bpEnvelopePriorTwoWeeks: null,
|
||||
});
|
||||
|
||||
for (const id of ["GLYCAEMIA", "LIPIDS"] as const) {
|
||||
const pillar = report.pillars.find((p) => p.id === id)!;
|
||||
expect(pillar.result.status).toBe("insufficient");
|
||||
expect(
|
||||
pillar.result.status === "insufficient" && pillar.result.reason,
|
||||
).toBe("not_tracked");
|
||||
}
|
||||
});
|
||||
|
||||
it("excludes source writers outside the current 28-day score window", async () => {
|
||||
const db = dbWithRows([
|
||||
stepRow("2026-06-15T09:00:00.000Z", 8_000, "watch", "APPLE_HEALTH"),
|
||||
|
||||
@@ -443,8 +443,8 @@ function scoreInputsFor(args: {
|
||||
biomarker: {
|
||||
name: string;
|
||||
unit: string;
|
||||
referenceLow: number | null;
|
||||
referenceHigh: number | null;
|
||||
lowerBound: number | null;
|
||||
upperBound: number | null;
|
||||
} | null;
|
||||
}>
|
||||
>;
|
||||
@@ -597,9 +597,8 @@ function scoreInputsFor(args: {
|
||||
marker: row.biomarker?.name ?? row.analyte,
|
||||
value: row.value!,
|
||||
unit: row.unit || row.biomarker?.unit || "",
|
||||
referenceLow: row.referenceLow ?? row.biomarker?.referenceLow ?? null,
|
||||
referenceHigh:
|
||||
row.referenceHigh ?? row.biomarker?.referenceHigh ?? null,
|
||||
referenceLow: row.referenceLow ?? row.biomarker?.lowerBound ?? null,
|
||||
referenceHigh: row.referenceHigh ?? row.biomarker?.upperBound ?? null,
|
||||
panel: row.panel,
|
||||
at: row.takenAt,
|
||||
source: row.source,
|
||||
@@ -708,12 +707,18 @@ export async function computeUserHealthScore(
|
||||
panel: true,
|
||||
takenAt: true,
|
||||
source: true,
|
||||
// The catalog's range columns are `lowerBound` / `upperBound`
|
||||
// (`lower_bound` / `upper_bound`), not the LabResult's own
|
||||
// `reference*` names. Prisma rejects a select against names a
|
||||
// model does not have at query-validation time — before any
|
||||
// row is read — so getting this wrong fails EVERY labs read,
|
||||
// not just rows that use the catalog fallback.
|
||||
biomarker: {
|
||||
select: {
|
||||
name: true,
|
||||
unit: true,
|
||||
referenceLow: true,
|
||||
referenceHigh: true,
|
||||
lowerBound: true,
|
||||
upperBound: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user