import { describe, expect, test } from "bun:test"; import { mkdtempSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { createApp } from "../src/app"; import { openDb } from "../src/db"; import { biomarkers, labDraws } from "../src/db/schema"; import { loadOrCreateKey } from "../src/lib/crypto"; async function seeded() { const dir = mkdtempSync(join(tmpdir(), "helios-")); const db = openDb(dir); const app = createApp({ db, key: loadOrCreateKey(dir), dataDir: dir }); const j = (b: unknown) => ({ method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(b) }); await app.request("/api/setup", j({ password: "hunter2hunter2" })); const cookie = (await app.request("/api/login", j({ password: "hunter2hunter2" }))).headers.get("set-cookie")!; await db.insert(labDraws).values([ { id: "d1", collectedAt: "2025-06-01", labName: "A", draftId: null, createdAt: 1 }, { id: "d2", collectedAt: "2026-01-15", labName: "B", draftId: null, createdAt: 2 }, ]); await db.insert(biomarkers).values([ { drawId: "d1", panel: "metabolic", name: "Glucose", marker: "glucose", analyteKey: "glucose", value: "90", valueNum: 90, unit: "mg/dL", referenceRange: null, flagged: 0, valueCanonical: 4.996, canonicalUnit: "mmol/L" }, { drawId: "d2", panel: "metabolic", name: "Glucose", marker: "glucose", analyteKey: "glucose", value: "100", valueNum: 100, unit: "mg/dL", referenceRange: "70-99", flagged: 1, valueCanonical: 5.551, canonicalUnit: "mmol/L" }, { drawId: "d2", panel: "lipids", name: "LDL Cholesterol", marker: "ldl", analyteKey: "ldl", value: "3.1", valueNum: 3.1, unit: "mmol/L", referenceRange: "<3.4", flagged: 0, valueCanonical: 3.1, canonicalUnit: "mmol/L" }, ]); return { app, cookie }; } describe("labs queries", () => { test("draw list newest first with counts", async () => { const { app, cookie } = await seeded(); const { draws } = await (await app.request("/api/labs/draws", { headers: { cookie } })).json(); expect(draws.map((d: any) => d.id)).toEqual(["d2", "d1"]); expect(draws[0].markerCount).toBe(2); expect(draws[0].flaggedCount).toBe(1); }); test("draw detail grouped by panel", async () => { const { app, cookie } = await seeded(); const detail = await (await app.request("/api/labs/draws/d2", { headers: { cookie } })).json(); expect(detail.panels.map((p: any) => p.panel)).toEqual(["lipids", "metabolic"]); expect(detail.panels[1].markers[0].flagged).toBe(true); expect((await app.request("/api/labs/draws/nope", { headers: { cookie } })).status).toBe(404); }); test("marker history ascending with registry metadata", async () => { const { app, cookie } = await seeded(); const h = await (await app.request("/api/labs/markers/glucose/history", { headers: { cookie } })).json(); expect(h.display).toBe("Glucose"); expect(h.canonicalUnit).toBe("mmol/L"); expect(h.points.map((p: any) => p.value)).toEqual([4.996, 5.551]); expect((await app.request("/api/labs/markers/unknown_thing/history", { headers: { cookie } })).status).toBe(404); }); });