import { describe, expect, it } from "vitest";
import { checkHeadline, latestMarks, notCheckedLines } from "../src/logic/checks";
import { contentTypeOf, fileProblem, guessType, roomFor, titleFromFile } from "../src/logic/records";
const MB = 1024 * 1024;
describe("adding records (REC-1 to REC-4)", () => {
it("names the file and how far over 15 MB it is", () => {
expect(fileProblem({ name: "scan.pdf", type: "application/pdf", size: 18.2 * MB })).toBe("scan.pdf is 18.2 MB, 3.2 MB over the 15 MB limit per file.");
expect(fileProblem({ name: "notes.docx", type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", size: 1000 })).toContain("Use TXT, PDF, JPG, PNG or HEIC");
expect(fileProblem({ name: "empty.txt", type: "text/plain", size: 0 })).toBe("empty.txt is empty.");
expect(fileProblem({ name: "photo.heic", type: "", size: 2 * MB })).toBeNull();
expect(contentTypeOf({ name: "IMG_0042.HEIC", type: "" })).toBe("image/heic");
});
it("keeps to 20 records per check and says how many were left out", () => {
expect(roomFor(18, 2)).toEqual({ fits: 2, message: null });
expect(roomFor(19, 3)).toEqual({ fits: 1, message: "A check can use up to 20 records. This patient has 19, so 2 of these are left out." });
});
it("titles a record from its file name and guesses its type", () => {
expect(titleFromFile("01-clinic_prescription.pdf")).toBe("Clinic prescription");
expect(titleFromFile(".pdf")).toBe("Untitled record");
expect(guessType("yaba-lab-egfr.jpg")).toBe("lab_result");
expect(guessType("ikeja-discharge.pdf")).toBe("discharge_summary");
expect(guessType("rx.txt")).toBe("prescription");
expect(guessType("IMG_0042.jpg")).toBe("other");
});
});
describe("the result (CHK-3, FND-3)", () => {
const result = (before: number, after: number, added: string[]) => ({
baseline: { findings: Array.from({ length: before }) },
contextual: { findings: Array.from({ length: after }) },
addedRuleIds: added,
}) as unknown as Parameters<typeof checkHeadline>[0];
it("says what the patient's details added", () => {
expect(checkHeadline(result(0, 2, ["a", "b"]), "Mariam Adeyemi").headline).toBe("Personalized to Mariam: 2 findings the medication list alone did not show.");
expect(checkHeadline(result(1, 1, []), "Mariam Adeyemi").headline).toBe("1 finding for clinician review. The medication list alone showed the same.");
});
it("never calls zero findings safe", () => {
expect(checkHeadline(result(0, 0, []), "Mariam Adeyemi").headline).toBe("No findings. This does not mean the prescription is safe.");
});
it("ends with what was not checked", () => {
const lines = notCheckedLines({ productsWithoutRules: ["agbo"], unconfirmedFacts: 2, rulesAwaitingReview: 8, checkInAnswers: true, other: [] }, false);
expect(lines).toEqual([
"agbo: recorded, not checked. No rules for this product yet.",
"2 facts or lines not confirmed for this check.",
"Rules waiting for clinical review: 8 changes. Their findings are shown, labelled as not yet reviewed.",
"Answers from check-ins: no rule reads them.",
]);
});
it("keeps the newest mark per finding", () => {
const marks = latestMarks({
marks: [
{ findingId: "f1", kind: "reviewed", note: null, reason: null, by: "Dr Okafor", at: "1" },
{ findingId: "f1", kind: "not_relevant", note: null, reason: "Stopped last week", by: "Dr Okafor", at: "2" },
],
});
expect(marks.get("f1")?.kind).toBe("not_relevant");
});
});