From 4cb15232a29ef64881bc07d2ad9dcf29aa7313f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Thu, 3 Sep 2026 05:54:05 +0200 Subject: [PATCH] test: name the shared glucose factor where the CSV import pinned the old one Three assertions repeated 18.016 and the product it gave. They were the reason the two factors could sit in the tree at once looking correct. They take the exported constant now, and the parser test asserts the round trip a reader on mmol/L experiences rather than a magic product. --- src/app/api/import/csv/__tests__/route.test.ts | 3 ++- src/lib/import/__tests__/csv-measurements.test.ts | 10 ++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/app/api/import/csv/__tests__/route.test.ts b/src/app/api/import/csv/__tests__/route.test.ts index 70e47ed3e..2ae1143a7 100644 --- a/src/app/api/import/csv/__tests__/route.test.ts +++ b/src/app/api/import/csv/__tests__/route.test.ts @@ -59,6 +59,7 @@ vi.mock("@/lib/rollups/measurement-rollups", async () => { import { NextRequest } from "next/server"; import { POST } from "../route"; +import { MGDL_PER_MMOL } from "@/lib/glucose"; import { checkRateLimit } from "@/lib/rate-limit"; import { prisma } from "@/lib/db"; import { recomputeBucketsForMeasurement } from "@/lib/rollups/measurement-rollups"; @@ -369,7 +370,7 @@ describe("POST /api/import/csv — contextless blood glucose", () => { externalId: "sensor-1", glucoseContext: null, }); - expect(row.value).toBeCloseTo(95.4848, 3); + expect(row.value).toBeCloseTo(5.3 * MGDL_PER_MMOL, 3); expect((row.measuredAt as Date).toISOString()).toBe( "2024-04-03T02:15:00.000Z", ); diff --git a/src/lib/import/__tests__/csv-measurements.test.ts b/src/lib/import/__tests__/csv-measurements.test.ts index ab57ccc0b..77ec61908 100644 --- a/src/lib/import/__tests__/csv-measurements.test.ts +++ b/src/lib/import/__tests__/csv-measurements.test.ts @@ -1,4 +1,5 @@ import { describe, it, expect } from "vitest"; +import { MGDL_PER_MMOL, mgdlToMmol } from "@/lib/glucose"; import { parseCsvMeasurements, @@ -98,7 +99,12 @@ describe("parseCsvMeasurements — unit conversion", () => { ]); expect(out.rows[0].status).toBe("ok"); expect(out.rows[0].row?.unit).toBe("mg/dL"); - expect(out.rows[0].row?.value).toBeCloseTo(5.3 * 18.016, 3); + // The one factor the app converts glucose by, both directions. The + // literal that used to sit here was 18.016 while every display path + // divided by 18.0182, so a reading imported in mmol/L did not read back + // as the number it went in as. + expect(out.rows[0].row?.value).toBeCloseTo(5.3 * MGDL_PER_MMOL, 3); + expect(mgdlToMmol(out.rows[0].row?.value ?? 0)).toBe(5.3); }); it("converts weight lb to canonical kg", () => { @@ -176,7 +182,7 @@ describe("parseCsvMeasurements — glucose context", () => { for (const result of out.rows) { expect(result.row).not.toHaveProperty("glucoseContext"); expect(result.row?.unit).toBe("mg/dL"); - expect(result.row?.value).toBeCloseTo(95.4848, 3); + expect(result.row?.value).toBeCloseTo(5.3 * MGDL_PER_MMOL, 3); } expect(out.rows[0].row?.measuredAt.toISOString()).toBe( "2024-04-03T02:15:00.000Z",