Files
HealthLog/src/lib/labs/__tests__/unit-normalise.test.ts
T
BernhardBuckel d3916036c2 fix(labs): refuse a reading whose unit differs from its marker's
A numeric lab reading was stored with its value as received and the marker's
unit stamped on it, so 5.6 mmol/L against a marker kept in mg/dL became
5.6 mg/dL and read as far below range. Both the manual/API write and the
scan commit did this; nothing compared the two units.

- POST /api/labs returns 422 labs.unit.mismatch, with the marker's unit and
  the reading's unit in meta, and writes nothing. The unit is compared only
  when one was sent: on the biomarkerId path it may be omitted, and the
  marker's unit is then the stated one. A qualitative reading has no unit.
- The scan commit skips such a row with reason unit_mismatch, so the outcome
  reads partial or failed instead of a clean save, and the toast names the
  analytes concerned rather than saying "duplicates".
- The extract response carries the matched marker's unit. The review screen
  shows a calm badge on a row in another unit, starts it unticked like a
  duplicate, and treats it as a blocking error on the unit field if it is
  ticked anyway.
- A small lab-specific normaliser forgives spelling only: spacing, the micro
  prefix, the case of the litre, and a unit printed in capitals for a short
  list where capitals cannot mean another unit. Case is otherwise significant,
  so mIU/L and MIU/L, or g/L and G/L, are never merged.

The document-commit path already refuses a mismatch and is left as it is.
Readings stored wrongly before this cannot be identified, because the
original unit was never kept; they are not touched.

Adds EN and DE strings; the other locales are left to the maintainers. The
OpenAPI spec and the error-code catalogue carry the new code.

Fixes #1094
2026-10-03 15:44:51 +02:00

91 lines
2.5 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { normaliseLabUnit, sameLabUnit } from "../unit-normalise";
describe("sameLabUnit — spellings of one unit", () => {
it.each([
["mmol/L", "mmol/l"],
["mg/dL", "mg/dl"],
["mg/dL", "mg/DL"],
["mg/dL", " mg / dL "],
["mg/dL", "mg/ dL"],
["µg/L", "ug/L"],
["µg/L", "μg/L"],
["µg/L", "mcg/L"],
["µmol/L", "umol/l"],
["µIU/mL", "uIU/mL"],
["x10^3/µL", "x10^3/uL"],
["x10^3/µL", "x10^3/µl"],
["mg/dL", "MG/DL"],
["mmol/L", "MMOL/L"],
["µmol/L", "UMOL/L"],
["ng/mL", "NG/ML"],
["IU/L", "IU/l"],
["IU/L", "IU/L"],
])("%s = %s", (a, b) => {
expect(sameLabUnit(a, b)).toBe(true);
expect(sameLabUnit(b, a)).toBe(true);
});
});
describe("sameLabUnit — never merges units of different magnitude or kind", () => {
it.each([
// The point of the module: nothing that changes the number.
["mg/dL", "mmol/L"],
["mg/dL", "mg/L"],
["mg/L", "µg/L"],
["g/dL", "g/L"],
["ng/mL", "ng/dL"],
["µg/L", "mg/L"],
["%", "mmol/mol"],
["mmol/L", "µmol/L"],
["mmol/L", "mol/L"],
// Case that carries meaning stays significant.
["mIU/L", "MIU/L"],
["g/L", "G/L"],
["u/L", "U/L"],
["mmol/L", "Mmol/L"],
["pmol/L", "Pmol/L"],
// A unit the module does not know compares exactly, so it cannot be guessed.
["mg/dL", "mg/dl."],
["mg/dL", "mg per dL"],
["mg/dL", ""],
])("%s ≠ %s", (a, b) => {
expect(sameLabUnit(a, b)).toBe(false);
expect(sameLabUnit(b, a)).toBe(false);
});
it("does not turn a unit printed in capitals into one that is not on the list", () => {
// `G/L` shouted is the blood-count unit, not grams per litre.
expect(normaliseLabUnit("G/L")).toBe("G/L");
expect(normaliseLabUnit("MIU/L")).toBe("MIU/L");
});
it("keeps enzyme units apart from the micro prefix", () => {
expect(normaliseLabUnit("u/L")).toBe("u/L");
expect(normaliseLabUnit("U/L")).toBe("U/L");
});
});
describe("normaliseLabUnit", () => {
it("is idempotent", () => {
for (const unit of [
"ug/l",
"MG/DL",
"mmol/l",
" x10^3 / uL ",
"mIU/L",
"G/L",
]) {
const once = normaliseLabUnit(unit);
expect(normaliseLabUnit(once)).toBe(once);
}
});
it("names the canonical spelling", () => {
expect(normaliseLabUnit("mmol/l")).toBe("mmol/L");
expect(normaliseLabUnit("ug/l")).toBe("µg/L");
expect(normaliseLabUnit("MG/DL")).toBe("mg/dL");
});
});