feat(labs): PDF upload, text extraction, LLM draft pipeline
This commit is contained in:
46
server/test/extract.test.ts
Normal file
46
server/test/extract.test.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { mkdtempSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { openDb } from "../src/db";
|
||||
import { loadOrCreateKey } from "../src/lib/crypto";
|
||||
import { extractFromText } from "../src/lib/extract";
|
||||
|
||||
const FIXTURE_TEXT = `
|
||||
Sample Diagnostics — Final Report
|
||||
Collected: 15 Jan 2026
|
||||
CHEMISTRY
|
||||
Glucose (Fasting) 100 mg/dL (70-99) H
|
||||
LIPIDS
|
||||
LDL Cholesterol 3.1 mmol/L (<3.4)
|
||||
`;
|
||||
|
||||
describe("extractFromText", () => {
|
||||
test("passes text to LLM and validates the draft shape", async () => {
|
||||
let userPrompt = "";
|
||||
const mock = (async (_: any, init: any) => {
|
||||
userPrompt = JSON.parse(String(init.body)).messages[1].content;
|
||||
return new Response(JSON.stringify({ choices: [{ message: { content: JSON.stringify({
|
||||
collectedDate: "2026-01-15",
|
||||
labName: "Sample Diagnostics",
|
||||
markers: [
|
||||
{ panel: "chemistry", name: "Glucose (Fasting)", value: "100", unit: "mg/dL", referenceRange: "70-99", flagged: true },
|
||||
{ panel: "lipids", name: "LDL Cholesterol", value: "3.1", unit: "mmol/L", referenceRange: "<3.4", flagged: false },
|
||||
],
|
||||
}) } }] }), { status: 200 });
|
||||
}) as typeof fetch;
|
||||
|
||||
const dir = mkdtempSync(join(tmpdir(), "helios-"));
|
||||
const draft = await extractFromText({ db: openDb(dir), key: loadOrCreateKey(dir), fetchImpl: mock }, FIXTURE_TEXT);
|
||||
expect(userPrompt).toContain("Glucose (Fasting)");
|
||||
expect(draft.markers).toHaveLength(2);
|
||||
expect(draft.collectedDate).toBe("2026-01-15");
|
||||
expect(draft.markers[0].flagged).toBe(true);
|
||||
});
|
||||
|
||||
test("malformed LLM output → llm_error, not a crash", async () => {
|
||||
const mock = (async () => new Response(JSON.stringify({ choices: [{ message: { content: '{"markers": "not an array"}' } }] }), { status: 200 })) as unknown as typeof fetch;
|
||||
const dir = mkdtempSync(join(tmpdir(), "helios-"));
|
||||
expect(extractFromText({ db: openDb(dir), key: loadOrCreateKey(dir), fetchImpl: mock }, "x")).rejects.toThrow(/llm_error/);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user