diff --git a/tests/integration/apple-health-import-clinical-labs.test.ts b/tests/integration/apple-health-import-clinical-labs.test.ts new file mode 100644 index 000000000..efb07fc96 --- /dev/null +++ b/tests/integration/apple-health-import-clinical-labs.test.ts @@ -0,0 +1,288 @@ +/** + * Lab results from Health Records inside an Apple Health export (#1400), + * through the real import worker against a real Postgres: + * + * - a lab result reaches the account's marker by its LOINC code, and a + * reading in another unit is converted with what the report printed kept; + * - panel members carry the panel's name, a panel header and a preliminary + * result are skipped, a unit the marker cannot take fails without failing + * the import, a non-lab clinical record stays in the clinical skips; + * - a reading already typed by hand is a duplicate; + * - uploading the same archive again writes nothing, and a deleted result + * stays deleted. + * + * Every fixture is synthetic. + */ +import { mkdtempSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { createHash } from "node:crypto"; +import type { Job } from "pg-boss"; +import { strToU8, zipSync } from "fflate"; +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; + +import { + APPLE_HEALTH_IMPORT_PARSER_REVISION, + _setWorkerPrismaForTests, + handleAppleHealthImport, + type AppleHealthImportPayload, +} from "@/lib/jobs/apple-health-import-worker"; +import { getPrismaClient, truncateAllTables } from "./setup"; + +vi.mock("@/lib/jobs/boss-instance", () => ({ + getGlobalBoss: vi.fn(() => null), +})); +vi.mock("@/lib/rollups/measurement-rollups", async (importOriginal) => ({ + ...(await importOriginal< + typeof import("@/lib/rollups/measurement-rollups") + >()), + recomputeUserRollups: vi.fn(async () => {}), +})); + +const prisma = getPrismaClient(); +const USER_ID = "user-apple-clinical-labs"; +const SOURCE_URL = "https://fhir.example-clinic.test/api/FHIR/R4"; +let jobSequence = 0; + +function lab( + id: string, + extra: Record, +): Record { + return { + resourceType: "Observation", + id, + status: "final", + category: [{ coding: [{ code: "laboratory" }] }], + effectiveDateTime: "2025-03-04T08:15:00+01:00", + subject: { reference: "Patient/synthetic-patient" }, + ...extra, + }; +} + +const RESOURCES: Record> = { + ldl: lab("ldl", { + code: { + text: "LDL Cholesterol (calc)", + coding: [{ system: "http://loinc.org", code: "13457-7" }], + }, + valueQuantity: { value: 3.1, unit: "mmol/L", code: "mmol/L" }, + referenceRange: [{ high: { value: 3, unit: "mmol/L" } }], + }), + cbc: lab("cbc", { + code: { text: "Complete blood count" }, + hasMember: [{ reference: "Observation/hgb" }], + }), + hgb: lab("hgb", { + code: { text: "Hemoglobin" }, + valueQuantity: { value: 14.2, unit: "g/dL" }, + }), + prelim: lab("prelim", { + status: "preliminary", + code: { text: "Potassium" }, + valueQuantity: { value: 4.1, unit: "mmol/L" }, + }), + odd: lab("odd", { + code: { text: "Odd marker" }, + valueQuantity: { value: 2, unit: "mmol/L" }, + }), + glucose: lab("glucose", { + code: { text: "Glucose" }, + valueQuantity: { value: 92, unit: "mg/dL" }, + }), +}; + +function exportXml(): string { + const records = Object.keys(RESOURCES) + .map( + (id) => ` `, + ) + .join("\n"); + return ` + + +${records} + +`; +} + +function createArchive(): { path: string; bytes: Buffer } { + const dir = mkdtempSync(join(tmpdir(), "healthlog-clinical-integration-")); + const path = join(dir, "export.zip"); + const files: Record = { + "apple_health_export/export.xml": strToU8(exportXml()), + "apple_health_export/clinical-records/Condition-cond-1.json": strToU8( + JSON.stringify({ resourceType: "Condition", id: "cond-1" }), + ), + }; + for (const [id, resource] of Object.entries(RESOURCES)) { + files[`apple_health_export/clinical-records/Observation-${id}.json`] = + strToU8(JSON.stringify(resource)); + } + const bytes = Buffer.from(zipSync(files, { level: 6 })); + writeFileSync(path, bytes); + return { path, bytes }; +} + +async function runArchive(): Promise> { + const archive = createArchive(); + jobSequence += 1; + const jobId = `apple-clinical-job-${jobSequence}`; + const mirror = await prisma.importJob.create({ + data: { + userId: USER_ID, + pgBossJobId: jobId, + status: "queued", + uploadBytes: archive.bytes.length, + uploadSha256: createHash("sha256").update(archive.bytes).digest("hex"), + parserRevision: APPLE_HEALTH_IMPORT_PARSER_REVISION, + }, + }); + const data: AppleHealthImportPayload = { + userId: USER_ID, + uploadPath: archive.path, + uploadBytes: archive.bytes.length, + enqueuedAt: "2025-03-06T09:00:00.000Z", + }; + const outcome = await handleAppleHealthImport({ + id: jobId, + data, + } as unknown as Job); + expect(outcome.ok).toBe(true); + const row = await prisma.importJob.findUnique({ where: { id: mirror.id } }); + expect(row?.status).toBe("done"); + return row?.result as Record; +} + +let ldlMarkerId = ""; +let glucoseMarkerId = ""; + +beforeAll(async () => { + await truncateAllTables(prisma); + _setWorkerPrismaForTests(prisma); + await prisma.user.create({ + data: { + id: USER_ID, + username: "apple-clinical-labs", + email: "apple-clinical-labs@example.test", + timezone: "Europe/Berlin", + }, + }); + ldlMarkerId = ( + await prisma.biomarker.create({ + data: { userId: USER_ID, name: "LDL", unit: "mg/dL" }, + }) + ).id; + await prisma.biomarker.create({ + data: { userId: USER_ID, name: "Odd marker", unit: "U/L" }, + }); + glucoseMarkerId = ( + await prisma.biomarker.create({ + data: { userId: USER_ID, name: "Glucose", unit: "mg/dL" }, + }) + ).id; + // Typed by hand on the same day, before the export arrived. + await prisma.labResult.create({ + data: { + userId: USER_ID, + biomarkerId: glucoseMarkerId, + analyte: "Glucose", + value: 92, + unit: "mg/dL", + takenAt: new Date("2025-03-04T09:00:00.000Z"), + source: "MANUAL", + }, + }); +}); + +afterAll(() => { + _setWorkerPrismaForTests(null); +}); + +describe("Apple Health export: lab results from Health Records", () => { + it("imports the lab results and reports bounded counts", async () => { + const result = await runArchive(); + expect(result.labs).toEqual({ + discovered: 6, + imported: 2, + duplicate: 1, + skipped: 2, + failed: 1, + }); + expect(result.clinical).toEqual({ skipped: 1 }); + + const rows = await prisma.labResult.findMany({ + where: { userId: USER_ID, source: "APPLE_HEALTH" }, + orderBy: { analyte: "asc" }, + }); + expect(rows).toHaveLength(2); + const [hemoglobin, ldl] = rows; + + // Matched to the existing "LDL" marker through its LOINC code, converted + // into the marker's unit, the printed value kept. + expect(ldl.biomarkerId).toBe(ldlMarkerId); + expect(ldl.analyte).toBe("LDL"); + expect(ldl.unit).toBe("mg/dL"); + expect(ldl.value).toBeCloseTo(119.9, 0); + expect(ldl.sourceValue).toBe(3.1); + expect(ldl.sourceUnit).toBe("mmol/L"); + expect(ldl.sourceReferenceHigh).toBeCloseTo(116, 0); + expect(ldl.takenAt.toISOString()).toBe("2025-03-04T07:15:00.000Z"); + expect(ldl.externalId).toMatch(/^fhir:[0-9a-f]{40}$/); + + // A new analyte gets a marker; the member carries its panel's name. + expect(hemoglobin.analyte).toBe("Hemoglobin"); + expect(hemoglobin.panel).toBe("Complete blood count"); + expect(hemoglobin.value).toBe(14.2); + const marker = await prisma.biomarker.findFirst({ + where: { userId: USER_ID, name: "Hemoglobin" }, + }); + expect(marker?.unit).toBe("g/dL"); + + // Nothing from the resource outside the reading. + expect(JSON.stringify(result)).not.toContain("synthetic-patient"); + expect(JSON.stringify(result)).not.toContain("example-clinic"); + }); + + it("writes nothing when the same archive is uploaded again", async () => { + const result = await runArchive(); + expect(result.labs).toEqual({ + discovered: 6, + imported: 0, + duplicate: 3, + skipped: 2, + failed: 1, + }); + expect( + await prisma.labResult.count({ + where: { userId: USER_ID, source: "APPLE_HEALTH" }, + }), + ).toBe(2); + }); + + it("keeps a deleted result deleted", async () => { + const hemoglobin = await prisma.labResult.findFirstOrThrow({ + where: { userId: USER_ID, analyte: "Hemoglobin" }, + }); + await prisma.labResult.update({ + where: { id: hemoglobin.id }, + data: { deletedAt: new Date() }, + }); + const result = await runArchive(); + expect(result.labs).toMatchObject({ imported: 0, duplicate: 3 }); + expect( + await prisma.labResult.count({ + where: { userId: USER_ID, analyte: "Hemoglobin" }, + }), + ).toBe(1); + expect( + await prisma.labResult.count({ + where: { userId: USER_ID, analyte: "Hemoglobin", deletedAt: null }, + }), + ).toBe(0); + }); +}); diff --git a/tests/integration/labs-fhir-batch.test.ts b/tests/integration/labs-fhir-batch.test.ts new file mode 100644 index 000000000..0901ba7e3 --- /dev/null +++ b/tests/integration/labs-fhir-batch.test.ts @@ -0,0 +1,299 @@ +/** + * `POST /api/labs/fhir-batch` against a real Postgres (#1400): + * + * - a batch is written, a panel's members carry its name, and sending the + * same batch again writes nothing; + * - a result already imported from an Apple Health export is recognised + * here by its identity; + * - a narrow Bearer token is refused, the login token admitted; + * - more than 200 records, or a `userId` field, is refused with 422; + * - an Idempotency-Key replays the first answer. + * + * Every fixture is synthetic. + */ +import { mkdtempSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { NextRequest } from "next/server"; +import { strToU8, zipSync } from "fflate"; +import { beforeEach, describe, expect, it, vi } from "vitest"; + +import { importAppleHealthClinicalLabs } from "@/lib/apple-health/clinical-labs-import"; +import { cookieJar, headerJar } from "./mock-next-headers"; +import { getPrismaClient, truncateAllTables } from "./setup"; + +process.env.API_TOKEN_HMAC_KEY ??= + "test-hmac-key-labs-fhir-batch-integration-32-bytes-min-123456"; + +const { hashToken } = await import("@/lib/auth/hmac"); + +vi.mock("@/lib/jobs/boss-instance", () => ({ + getGlobalBoss: vi.fn(() => null), +})); +vi.mock("next/headers", async () => { + const { cookieJar, headerJar } = await import("./mock-next-headers"); + return { + headers: vi.fn(async () => ({ + get: (name: string) => headerJar.get(name.toLowerCase()) ?? null, + })), + cookies: vi.fn(async () => ({ + get: (name: string) => { + const value = cookieJar.get(name); + return value ? { name, value } : undefined; + }, + set: (name: string, value: string) => { + cookieJar.set(name, value); + }, + delete: (name: string) => { + cookieJar.delete(name); + }, + })), + }; +}); + +const prisma = getPrismaClient(); +const USER_ID = "user-labs-fhir-batch"; +const SOURCE_URL = "https://fhir.example-clinic.test/api/FHIR/R4"; + +function observation( + id: string, + extra: Record = {}, +): Record { + return { + resourceType: "Observation", + id, + status: "final", + category: [{ coding: [{ code: "laboratory" }] }], + code: { text: `Analyte ${id}` }, + effectiveDateTime: "2025-05-01T07:00:00Z", + valueQuantity: { value: 5, unit: "mmol/L" }, + ...extra, + }; +} + +function record(resource: Record) { + return { + resource, + fhirVersion: "4.0.1", + identifier: String(resource.id), + sourceURL: SOURCE_URL, + sourceName: "Example Clinic", + }; +} + +function post( + body: unknown, + opts: { idempotencyKey?: string; bearer?: string } = {}, +): NextRequest { + const headers: Record = { + "content-type": "application/json", + }; + if (opts.idempotencyKey) headers["idempotency-key"] = opts.idempotencyKey; + if (opts.bearer) headers.authorization = `Bearer ${opts.bearer}`; + return new NextRequest("http://localhost/api/labs/fhir-batch", { + method: "POST", + headers, + body: JSON.stringify(body), + }); +} + +interface BatchBody { + data: { + results: Array<{ index: number; status: string; reason?: string }>; + inserted: number; + duplicate: number; + skipped: number; + failed: number; + outcome: string; + }; +} + +beforeEach(async () => { + await truncateAllTables(prisma); + cookieJar.clear(); + headerJar.clear(); + await prisma.user.create({ + data: { + id: USER_ID, + username: "labs-fhir-batch", + email: "labs-fhir-batch@example.test", + timezone: "Europe/Berlin", + }, + }); + const session = await prisma.session.create({ + data: { userId: USER_ID, expiresAt: new Date(Date.now() + 3_600_000) }, + }); + cookieJar.set("healthlog_session", session.id); +}); + +describe("POST /api/labs/fhir-batch", () => { + it("writes a batch with its panel, and a resend writes nothing", async () => { + const { POST } = await import("@/app/api/labs/fhir-batch/route"); + const body = { + records: [ + record( + observation("panel", { + code: { text: "Electrolytes" }, + valueQuantity: undefined, + hasMember: [{ reference: "Observation/na" }], + }), + ), + record(observation("na", { code: { text: "Sodium" } })), + record(observation("vital", { category: [{ text: "vital-signs" }] })), + record(observation("nounit", { valueQuantity: { value: 1 } })), + ], + }; + const first = await POST(post(body)); + expect(first.status).toBe(200); + const firstJson = (await first.json()) as BatchBody; + expect(firstJson.data).toMatchObject({ + inserted: 1, + duplicate: 0, + skipped: 2, + failed: 1, + outcome: "partial", + }); + expect(firstJson.data.results).toEqual([ + { index: 0, status: "skipped", reason: "panel_header" }, + { index: 1, status: "inserted" }, + { index: 2, status: "skipped", reason: "not_lab" }, + { index: 3, status: "failed", reason: "missing_unit" }, + ]); + const sodium = await prisma.labResult.findFirstOrThrow({ + where: { userId: USER_ID, analyte: "Sodium" }, + }); + expect(sodium).toMatchObject({ + source: "APPLE_HEALTH", + panel: "Electrolytes", + value: 5, + unit: "mmol/L", + }); + + const second = await POST(post(body)); + const secondJson = (await second.json()) as BatchBody; + expect(secondJson.data).toMatchObject({ inserted: 0, duplicate: 1 }); + expect(await prisma.labResult.count({ where: { userId: USER_ID } })).toBe( + 1, + ); + }); + + it("recognises a result an export already imported", async () => { + const dir = mkdtempSync(join(tmpdir(), "healthlog-fhir-batch-")); + const path = join(dir, "export.zip"); + const resource = observation("shared", { + code: { text: "Potassium" }, + valueQuantity: { value: 4.2, unit: "mmol/L" }, + }); + writeFileSync( + path, + zipSync({ + "apple_health_export/clinical-records/Observation-shared.json": strToU8( + JSON.stringify(resource), + ), + }), + ); + const fromExport = await importAppleHealthClinicalLabs({ + archivePath: path, + userId: USER_ID, + tz: "Europe/Berlin", + references: [ + { + path: "/clinical-records/Observation-shared.json", + identifier: "shared", + sourceURL: `${SOURCE_URL}/`, + sourceName: "Example Clinic", + fhirVersion: "4.0.1", + }, + ], + }); + expect(fromExport.imported).toBe(1); + + // The phone sends the same result, its value since corrected: the + // identity still recognises it (first write wins). + const { POST } = await import("@/app/api/labs/fhir-batch/route"); + const res = await POST( + post({ + records: [ + record({ + ...resource, + status: "corrected", + valueQuantity: { value: 4.4, unit: "mmol/L" }, + }), + ], + }), + ); + const json = (await res.json()) as BatchBody; + expect(json.data.results).toEqual([{ index: 0, status: "duplicate" }]); + const rows = await prisma.labResult.findMany({ + where: { userId: USER_ID }, + }); + expect(rows).toHaveLength(1); + expect(rows[0].value).toBe(4.2); + }); + + it("refuses a narrow token and admits the login token", async () => { + const { POST } = await import("@/app/api/labs/fhir-batch/route"); + cookieJar.delete("healthlog_session"); + const narrow = "hlk_narrow_scope_labs_fhir_batch_token_value_0001"; + await prisma.apiToken.create({ + data: { + userId: USER_ID, + name: "narrow", + tokenHash: hashToken(narrow), + permissions: ["fhir:read"], + }, + }); + headerJar.set("authorization", `Bearer ${narrow}`); + const denied = await POST( + post({ records: [record(observation("a"))] }, { bearer: narrow }), + ); + expect(denied.status).toBe(403); + expect(await prisma.labResult.count()).toBe(0); + + const full = "hlk_full_scope_labs_fhir_batch_token_value_00000002"; + await prisma.apiToken.create({ + data: { + userId: USER_ID, + name: "login", + tokenHash: hashToken(full), + permissions: ["*"], + }, + }); + headerJar.set("authorization", `Bearer ${full}`); + const admitted = await POST( + post({ records: [record(observation("a"))] }, { bearer: full }), + ); + expect(admitted.status).toBe(200); + expect(await prisma.labResult.count()).toBe(1); + }); + + it("refuses more than 200 records and an unknown userId field", async () => { + const { POST } = await import("@/app/api/labs/fhir-batch/route"); + const tooMany = await POST( + post({ + records: Array.from({ length: 201 }, (_, i) => + record(observation(`r${i}`)), + ), + }), + ); + expect(tooMany.status).toBe(422); + + const withUser = await POST( + post({ userId: "someone-else", records: [record(observation("a"))] }), + ); + expect(withUser.status).toBe(422); + expect(await prisma.labResult.count()).toBe(0); + }); + + it("replays the first answer for a reused Idempotency-Key", async () => { + const { POST } = await import("@/app/api/labs/fhir-batch/route"); + const body = { records: [record(observation("idem"))] }; + const key = "ios-labs-fhir-batch-0001"; + const first = await POST(post(body, { idempotencyKey: key })); + expect(((await first.json()) as BatchBody).data.inserted).toBe(1); + const second = await POST(post(body, { idempotencyKey: key })); + expect(second.headers.get("X-Idempotent-Replay")).toBe("true"); + expect(((await second.json()) as BatchBody).data.inserted).toBe(1); + expect(await prisma.labResult.count()).toBe(1); + }); +});