fix(labs): let an import's first number set a text-only marker's unit

Both the Health Records import and the CSV import checked the unit
before writing and refused a numeric reading against a marker whose
unit was still empty, although the write itself lets the first number
choose it. An empty marker unit now fits any unit in unitFitsMarker;
the write's conversion stays the authority.
This commit is contained in:
Marc-André Bombeck committed 2026-11-17 20:54:54 +01:00
1 parent 8f1af43891
commit 0fedabde13
3 files changed
+203

No files matched your search

@@ -12,6 +12,7 @@ import {
suggestInIndex,
type IndexedMarker,
type MarkerIndex,
unitFitsMarker,
} from "../biomarker-resolve";
let seq = 0;
@@ -314,3 +315,40 @@ describe("a catalog hint from the source (#1400)", () => {
).toMatchObject({ marker: { id: own.id }, matchKind: "name" });
});
});
describe("unitFitsMarker", () => {
it("lets the first number give a text-only marker its unit", () => {
// A marker minted from a text result has no unit yet; the first numeric
// reading sets it, whatever unit it states.
expect(unitFitsMarker({ unit: "", analyteKey: null }, "mg/dL", null)).toBe(
true,
);
expect(
unitFitsMarker(
{ unit: " ", analyteKey: "glucose" },
"mmol/L",
"glucose",
),
).toBe(true);
expect(unitFitsMarker({ unit: "", analyteKey: null }, "1", null)).toBe(
true,
);
});
it("still refuses a unit that does not convert into a chosen one", () => {
expect(unitFitsMarker({ unit: "1", analyteKey: null }, "mg/dL", null)).toBe(
false,
);
expect(
unitFitsMarker({ unit: "mg/dL", analyteKey: null }, "U/L", null),
).toBe(false);
});
it("finds a text-only marker of the catalog analyte for a numeric reading", () => {
const text = marker("Glucose", "");
expect(resolveInIndex(index([text]), "Blood glucose", "mg/dL")).toEqual({
marker: text,
matchKind: "catalog",
});
});
});
+7
View File
@@ -153,6 +153,12 @@ function markerCatalogKeys(
* Whether a reading stated in `unit` can be stored under the marker: the same
* unit, or one the conversion moves into it. No unit (a qualitative reading,
* or none stated) always fits.
*
* A marker with an empty unit takes any unit: it was minted from a text
* result and no number has chosen its unit yet, so the first numeric reading
* gives it one when it is written (`adoptFirstNumericUnit` in
* `./reading-conversion.ts`). The write stays the authority: should the marker
* hold a number after all, the conversion there refuses the reading.
*/
export function unitFitsMarker(
marker: Pick<IndexedMarker, "unit" | "analyteKey">,
@@ -160,6 +166,7 @@ export function unitFitsMarker(
analyteKey: string | null,
): boolean {
if (unit === null || unit === undefined || unit.trim() === "") return true;
if (marker.unit.trim() === "") return true;
if (sameLabUnit(unit, marker.unit)) return true;
return convertLabValue({
analyteKey: marker.analyteKey ?? analyteKey,