mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 23:52:24 +00:00
fix(labs): block a lab-scan save while a selected reading is incomplete
The review dialog silently dropped any selected reading that could not be written (no date, no unit, no value) and saved the rest. When nothing was left it showed "Select at least one reading to save", which described a different situation and said nothing about what was missing. A save is now all-or-nothing over the selected readings. When one is incomplete, nothing is written; the dialog states how many readings need attention, marks each missing field (aria-invalid, inline message, red border, required marker on the label) and moves focus to the first one. Marks appear after a save attempt and follow the edits. The "nothing selected" message is kept for that case only. Validation is a pure function next to the payload mapping, so the rule that blocks a save and the rule that builds the payload cannot drift apart. Adds EN and DE strings; the other locales are left to the maintainers. Refs #1055
This commit is contained in:
1 parent
ae6a5ac693
commit
eaced8226b
7 files changed
+427
-38
No files matched your search
@@ -0,0 +1,142 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import type { OcrReviewRow } from "../ocr-review-types";
|
||||
import {
|
||||
collectRowErrors,
|
||||
planSave,
|
||||
toCommitRow,
|
||||
validateReviewRow,
|
||||
} from "../ocr-review-validation";
|
||||
|
||||
function row(overrides: Partial<OcrReviewRow> = {}): OcrReviewRow {
|
||||
return {
|
||||
key: "LDL-0",
|
||||
analyte: "LDL",
|
||||
value: 2.4,
|
||||
valueText: null,
|
||||
unit: "mmol/L",
|
||||
referenceLow: 0,
|
||||
referenceHigh: 3,
|
||||
referenceText: null,
|
||||
takenAt: "2026-06-10",
|
||||
confidence: { analyte: 1, value: 1, unit: 1, range: 1 },
|
||||
biomarkerMatch: "existing",
|
||||
duplicateOf: null,
|
||||
confirmed: true,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("validateReviewRow", () => {
|
||||
it.each([
|
||||
["a complete numeric row", {}, {}],
|
||||
[
|
||||
"a complete qualitative row",
|
||||
{ value: null, valueText: "negative", unit: null },
|
||||
{},
|
||||
],
|
||||
["a missing date", { takenAt: null }, { date: true }],
|
||||
["an unparseable date", { takenAt: "not-a-day" }, { date: true }],
|
||||
["a blank analyte", { analyte: " " }, { analyte: true }],
|
||||
["a missing value", { value: null }, { value: true }],
|
||||
["a non-finite value", { value: Number.NaN }, { value: true }],
|
||||
["a blank unit", { unit: " " }, { unit: true }],
|
||||
["a missing unit", { unit: null }, { unit: true }],
|
||||
[
|
||||
"a blank qualitative result",
|
||||
{ value: null, valueText: " ", unit: null },
|
||||
{ valueText: true },
|
||||
],
|
||||
[
|
||||
"several problems at once",
|
||||
{ takenAt: null, unit: "", analyte: "" },
|
||||
{ date: true, unit: true, analyte: true },
|
||||
],
|
||||
] as const)("%s", (_name, overrides, expected) => {
|
||||
expect(validateReviewRow(row(overrides))).toEqual(expected);
|
||||
});
|
||||
|
||||
it("does not ask a qualitative row for a unit", () => {
|
||||
expect(
|
||||
validateReviewRow(row({ value: null, valueText: "trace", unit: null })),
|
||||
).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
describe("toCommitRow agrees with validateReviewRow", () => {
|
||||
const cases: Partial<OcrReviewRow>[] = [
|
||||
{},
|
||||
{ takenAt: null },
|
||||
{ takenAt: "nope" },
|
||||
{ analyte: "" },
|
||||
{ value: null },
|
||||
{ unit: "" },
|
||||
{ value: null, valueText: "negative", unit: null },
|
||||
{ value: null, valueText: "", unit: null },
|
||||
];
|
||||
|
||||
it.each(cases)("row %j", (overrides) => {
|
||||
const r = row(overrides);
|
||||
const blocked = Object.keys(validateReviewRow(r)).length > 0;
|
||||
expect(toCommitRow(r) === null).toBe(blocked);
|
||||
});
|
||||
|
||||
it("anchors the date at noon UTC", () => {
|
||||
expect(toCommitRow(row())?.takenAt).toBe("2026-06-10T12:00:00.000Z");
|
||||
});
|
||||
});
|
||||
|
||||
describe("collectRowErrors", () => {
|
||||
it("ignores rows that are not selected", () => {
|
||||
const rows = [row({ key: "a", takenAt: null, confirmed: false })];
|
||||
expect(collectRowErrors(rows).size).toBe(0);
|
||||
});
|
||||
|
||||
it("keys the problems by row", () => {
|
||||
const rows = [row({ key: "ok" }), row({ key: "bad", takenAt: null })];
|
||||
expect([...collectRowErrors(rows).entries()]).toEqual([
|
||||
["bad", { date: true }],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("planSave", () => {
|
||||
it("reports nothing selected only when no row is selected", () => {
|
||||
expect(planSave([])).toEqual({ kind: "nothing-selected" });
|
||||
expect(planSave([row({ confirmed: false })])).toEqual({
|
||||
kind: "nothing-selected",
|
||||
});
|
||||
});
|
||||
|
||||
it("blocks the whole save when a selected row has no date, even if the others are fine", () => {
|
||||
const plan = planSave([
|
||||
row({ key: "ok" }),
|
||||
row({ key: "bad", takenAt: null }),
|
||||
]);
|
||||
expect(plan).toEqual({ kind: "blocked" });
|
||||
});
|
||||
|
||||
it("is not reported as 'nothing selected' when the only selected row lacks a date", () => {
|
||||
expect(planSave([row({ takenAt: null })]).kind).toBe("blocked");
|
||||
});
|
||||
|
||||
it("ignores an unselected invalid row and saves the selected ones", () => {
|
||||
const plan = planSave([
|
||||
row({ key: "ok" }),
|
||||
row({ key: "skipped", takenAt: null, confirmed: false }),
|
||||
]);
|
||||
expect(plan.kind).toBe("ready");
|
||||
if (plan.kind === "ready") expect(plan.payload).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("returns every selected row once all of them are complete", () => {
|
||||
const plan = planSave([
|
||||
row({ key: "a" }),
|
||||
row({ key: "b", analyte: "HDL" }),
|
||||
]);
|
||||
expect(plan.kind).toBe("ready");
|
||||
if (plan.kind === "ready") {
|
||||
expect(plan.payload.map((r) => r.analyte)).toEqual(["LDL", "HDL"]);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,84 @@
|
||||
import { renderToStaticMarkup } from "react-dom/server";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { I18nProvider } from "@/lib/i18n/context";
|
||||
|
||||
import type { OcrReviewRow } from "../ocr-review-types";
|
||||
import { OcrRowEditor } from "../ocr-row-editor";
|
||||
|
||||
const baseRow: OcrReviewRow = {
|
||||
key: "LDL-0",
|
||||
analyte: "LDL",
|
||||
value: 2.4,
|
||||
valueText: null,
|
||||
unit: "mmol/L",
|
||||
referenceLow: 0,
|
||||
referenceHigh: 3,
|
||||
referenceText: null,
|
||||
takenAt: null,
|
||||
confidence: { analyte: 1, value: 1, unit: 1, range: 1 },
|
||||
biomarkerMatch: "existing",
|
||||
duplicateOf: null,
|
||||
confirmed: true,
|
||||
};
|
||||
|
||||
function render(
|
||||
errors: Parameters<typeof OcrRowEditor>[0]["errors"],
|
||||
locale: "en" | "de" = "en",
|
||||
row: OcrReviewRow = baseRow,
|
||||
): string {
|
||||
return renderToStaticMarkup(
|
||||
<I18nProvider initialLocale={locale}>
|
||||
<OcrRowEditor row={row} onChange={() => {}} errors={errors} />
|
||||
</I18nProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
describe("<OcrRowEditor> required-field marking", () => {
|
||||
it("shows no error state before a save was attempted", () => {
|
||||
const html = render(undefined);
|
||||
expect(html).not.toContain('aria-invalid="true"');
|
||||
expect(html).not.toContain('role="alert"');
|
||||
});
|
||||
|
||||
it("marks the missing date, states why, and links the message to the field", () => {
|
||||
const html = render({ date: true });
|
||||
const invalid = html.match(/<input[^>]*aria-invalid="true"[^>]*>/g) ?? [];
|
||||
expect(invalid).toHaveLength(1);
|
||||
const describedBy = invalid[0].match(/aria-describedby="([^"]+)"/)?.[1];
|
||||
expect(describedBy).toBeTruthy();
|
||||
expect(html).toContain(`id="${describedBy}"`);
|
||||
expect(html).toContain("Add the sample date.");
|
||||
expect(html).toContain('role="alert"');
|
||||
// The bordered wrapper is what the eye sees.
|
||||
expect(html).toContain("border-destructive");
|
||||
});
|
||||
|
||||
it("marks only the fields that are wrong", () => {
|
||||
const html = render({ unit: true });
|
||||
expect(html.match(/aria-invalid="true"/g)).toHaveLength(1);
|
||||
expect(html).toContain("Add a unit.");
|
||||
expect(html).not.toContain("Add the sample date.");
|
||||
});
|
||||
|
||||
it("marks a blank qualitative result", () => {
|
||||
const html = render({ valueText: true }, "en", {
|
||||
...baseRow,
|
||||
value: null,
|
||||
valueText: "",
|
||||
unit: null,
|
||||
});
|
||||
expect(html).toContain("Add a result.");
|
||||
expect(html.match(/aria-invalid="true"/g)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("carries a visible required marker on the fields a row needs", () => {
|
||||
const html = render(undefined);
|
||||
// Value, unit and date on a numeric row.
|
||||
expect(html.match(/text-destructive"[^>]*> \*</g)).toHaveLength(3);
|
||||
});
|
||||
|
||||
it("speaks German too", () => {
|
||||
expect(render({ date: true }, "de")).toContain("Bitte ein Datum angeben.");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user