mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-09 13:01:00 +00:00
A lab runs its own method on its own device, so the window a physician evaluates a value against is the one printed next to it on that report, not a catalog band averaged over every lab. Reading a report pulled the value and dropped that range: the extraction asked for it, the commit handed it to the biomarker resolver, and the resolver only uses it when it mints a NEW marker. For every analyte the user already had — the normal case after a first panel — the printed range was read and then thrown away. Add `sourceReferenceLow` / `sourceReferenceHigh` / `sourceReferenceText` to `LabResult` and carry the range from the report to the reading. The text is stored whether or not numbers can be read out of it, because "bis 5,0" and "negativ" state a window too; a range that could not be parsed is kept verbatim, never replaced with invented bounds. `resolveEffectiveReferenceRange` is the one place the precedence lives: the source window wins for that single reading, and the catalog stays the net for readings without one. A printed string with no derivable bound does not displace the catalog. Every surface that judges a reading routes through it — the API verdict, the doctor report, the insights blocks and per-marker assessment, the coach snapshot, the MCP read and the FHIR `referenceRange` — so a value cannot read in range on one surface and out on another. The API keeps `referenceLow` / `referenceHigh` as the window in force, so a client that reads only those two renders the right range and the right verdict with no change of its own; the catalog band, the source window, its origin and a divergence flag ride alongside for clients that want to show both. Read side: the chart draws the source window as a second, visually distinct band with the reading it came from named beneath it, the reading rows name the printed range and the catalog band where the two disagree, and the doctor-report PDF prints the report's own string with a footnote naming the saved range. Entry side: one optional field on the lab form, folded away until asked for, and the printed range is editable on the scan review screen. The range parser covers the notations German and English reports actually print — two-sided, one-sided, comma or dot decimals, thousands separators, trailing units — reads units without ever converting them, and refuses to guess on anything ambiguous.
129 lines
4.1 KiB
TypeScript
129 lines
4.1 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
||
|
||
import {
|
||
classifyAgainstEffectiveRange,
|
||
resolveEffectiveReferenceRange,
|
||
} from "@/lib/labs/reference-range";
|
||
|
||
const NO_SOURCE = {
|
||
sourceReferenceLow: null,
|
||
sourceReferenceHigh: null,
|
||
sourceReferenceText: null,
|
||
};
|
||
|
||
describe("resolveEffectiveReferenceRange", () => {
|
||
it("falls back to the catalog band when the reading carries no source window", () => {
|
||
const eff = resolveEffectiveReferenceRange(3.5, 5, NO_SOURCE);
|
||
expect(eff).toMatchObject({
|
||
low: 3.5,
|
||
high: 5,
|
||
origin: "catalog",
|
||
catalogLow: 3.5,
|
||
catalogHigh: 5,
|
||
sourceText: null,
|
||
divergesFromCatalog: false,
|
||
});
|
||
});
|
||
|
||
it("reports no window when neither side states one", () => {
|
||
expect(resolveEffectiveReferenceRange(null, null, NO_SOURCE)).toMatchObject(
|
||
{ low: null, high: null, origin: "none", divergesFromCatalog: false },
|
||
);
|
||
});
|
||
|
||
it("lets the source window win over the catalog band for that reading", () => {
|
||
const eff = resolveEffectiveReferenceRange(3.5, 5, {
|
||
sourceReferenceLow: 3.9,
|
||
sourceReferenceHigh: 5.4,
|
||
sourceReferenceText: "3,9 - 5,4",
|
||
});
|
||
expect(eff).toMatchObject({
|
||
low: 3.9,
|
||
high: 5.4,
|
||
origin: "source",
|
||
catalogLow: 3.5,
|
||
catalogHigh: 5,
|
||
sourceText: "3,9 - 5,4",
|
||
divergesFromCatalog: true,
|
||
});
|
||
});
|
||
|
||
it("does not call an identical window a divergence", () => {
|
||
const eff = resolveEffectiveReferenceRange(3.5, 5, {
|
||
sourceReferenceLow: 3.5,
|
||
sourceReferenceHigh: 5,
|
||
sourceReferenceText: "3,5 - 5,0",
|
||
});
|
||
expect(eff.origin).toBe("source");
|
||
expect(eff.divergesFromCatalog).toBe(false);
|
||
});
|
||
|
||
it("takes a one-sided source window whole, not merged with the catalog", () => {
|
||
// A report that prints "< 5" states a ceiling and NO floor. Borrowing the
|
||
// catalog's floor would invent a window the report never stated.
|
||
const eff = resolveEffectiveReferenceRange(3.5, 5, {
|
||
sourceReferenceLow: null,
|
||
sourceReferenceHigh: 4.2,
|
||
sourceReferenceText: "< 4,2",
|
||
});
|
||
expect(eff).toMatchObject({
|
||
low: null,
|
||
high: 4.2,
|
||
origin: "source",
|
||
divergesFromCatalog: true,
|
||
});
|
||
});
|
||
|
||
it("does not displace the catalog for a printed window with no derivable bound", () => {
|
||
// "negativ" is carried through verbatim, but it is not an argument for
|
||
// discarding the catalog band.
|
||
const eff = resolveEffectiveReferenceRange(3.5, 5, {
|
||
sourceReferenceLow: null,
|
||
sourceReferenceHigh: null,
|
||
sourceReferenceText: "negativ",
|
||
});
|
||
expect(eff).toMatchObject({
|
||
low: 3.5,
|
||
high: 5,
|
||
origin: "catalog",
|
||
sourceText: "negativ",
|
||
divergesFromCatalog: false,
|
||
});
|
||
});
|
||
|
||
it("reports a source window as no divergence when the catalog has none", () => {
|
||
const eff = resolveEffectiveReferenceRange(null, null, {
|
||
sourceReferenceLow: 3.9,
|
||
sourceReferenceHigh: 5.4,
|
||
sourceReferenceText: "3,9 - 5,4",
|
||
});
|
||
expect(eff.origin).toBe("source");
|
||
expect(eff.divergesFromCatalog).toBe(false);
|
||
});
|
||
});
|
||
|
||
describe("classifyAgainstEffectiveRange", () => {
|
||
it("judges a reading by the source window, not the catalog band", () => {
|
||
// 5.2 sits inside the catalog band (3.5–5.0 would call it high) but the
|
||
// report that produced this value states 3.9–5.4, so it is in range.
|
||
const eff = resolveEffectiveReferenceRange(3.5, 5, {
|
||
sourceReferenceLow: 3.9,
|
||
sourceReferenceHigh: 5.4,
|
||
sourceReferenceText: "3,9 - 5,4",
|
||
});
|
||
expect(classifyAgainstEffectiveRange(5.2, eff)).toBe("in-range");
|
||
expect(classifyAgainstEffectiveRange(3.7, eff)).toBe("below");
|
||
expect(classifyAgainstEffectiveRange(5.5, eff)).toBe("above");
|
||
});
|
||
|
||
it("keeps the catalog verdict for a reading with no source window", () => {
|
||
const eff = resolveEffectiveReferenceRange(3.5, 5, NO_SOURCE);
|
||
expect(classifyAgainstEffectiveRange(5.2, eff)).toBe("above");
|
||
});
|
||
|
||
it("never classifies a qualitative reading", () => {
|
||
const eff = resolveEffectiveReferenceRange(3.5, 5, NO_SOURCE);
|
||
expect(classifyAgainstEffectiveRange(null, eff)).toBe("unknown");
|
||
});
|
||
});
|