"""Tests for the safety-critical, deterministic parts of LabLens.""" import pytest from lablens.catalog import load_catalog, match_test_name from lablens.explain import explain, render_markdown, violates_guardrails from lablens.extract import extract, parse_line, parse_range from lablens.flagger import flag_all, flag_result from lablens.models import Flag, Result def R(key, value, unit, lo=None, hi=None): return Result(test_key=key, raw_name=key, value=value, unit=unit, ref_low=lo, ref_high=hi) # ---------- flagging ---------- def test_report_range_decides_low_high(): assert flag_result(R("hemoglobin", 11.0, "g/dL", 12, 16)).flag == Flag.LOW assert flag_result(R("hemoglobin", 16.5, "g/dL", 12, 16)).flag == Flag.HIGH assert flag_result(R("hemoglobin", 14.0, "g/dL", 12, 16)).flag == Flag.NORMAL def test_critical_beats_range_and_converts_units(): # 6.8 mmol/L potassium is critical regardless of the printed range assert flag_result(R("potassium", 6.8, "mmol/L", 3.5, 5.1)).flag == Flag.CRITICAL_HIGH # French g/L glucose: 0.45 g/L = 45 mg/dL, below the 50 mg/dL critical threshold assert flag_result(R("glucose", 0.45, "g/L", 0.70, 1.10)).flag == Flag.CRITICAL_LOW # 4.5 g/L glucose = 450 mg/dL -> critical high assert flag_result(R("glucose", 4.5, "g/L", 0.70, 1.10)).flag == Flag.CRITICAL_HIGH def test_cell_count_units_giga_per_litre(): # 40 G/L platelets = 40 x10^3/µL -> critical low assert flag_result(R("platelets", 40, "G/L", 150, 400)).flag == Flag.CRITICAL_LOW def test_implausible_value_is_never_normal_or_urgent(): # 436% HbA1c is a dropped decimal point, not a medical emergency or a normal value r = flag_result(R("hba1c", 436, "%", 4.0, 5.6)) assert r.flag == Flag.CHECK a = flag_all([R("hemoglobin", 151, "g/dL", 12, 17)]) assert a.results[0].flag == Flag.CHECK and not a.urgent def test_catalog_range_used_when_report_has_none(): r = flag_result(R("sodium", 150, "mmol/L")) assert r.flag == Flag.HIGH and r.ref_source == "catalog" def test_unknown_unit_skips_critical_but_uses_printed_range(): r = flag_result(R("potassium", 7.0, "zz/qq", 3.5, 5.1)) assert r.flag == Flag.HIGH # not escalated: we can't be sure of the unit def test_unknown_unit_and_no_range_is_unknown(): assert flag_result(R("potassium", 4.0, "zz/qq")).flag == Flag.UNKNOWN def test_urgent_flag_set(): a = flag_all([R("hemoglobin", 14, "g/dL", 12, 16), R("sodium", 118, "mmol/L", 135, 145)]) assert a.urgent def test_every_critical_threshold_is_plausible(): for td in load_catalog().values(): for v in td.critical.values(): assert td.plausible[0] < v < td.plausible[1], td.key # ---------- name matching ---------- @pytest.mark.parametrize("name,key", [ ("Glycémie à jeun", "glucose"), ("GLYCEMIE A JEUN", "glucose"), ("Kaliémie", "potassium"), ("Hemiogiouine", "hemoglobin"), ("HBAIC", "hba1c"), ("Ldi-choiestéroi", "ldl"), ("Hdl-chatesteroi", "hdl"), ("K", "potassium"), ("NA", "sodium"), ("الكرياتينين", "creatinine"), ]) def test_name_matching(name, key): assert match_test_name(name)[0] == key @pytest.mark.parametrize("name", ["Patient", "Collection date", "No", "Dossier", "Page"]) def test_non_tests_do_not_match(name): assert match_test_name(name)[0] is None # ---------- line and range parsing ---------- @pytest.mark.parametrize("text,expected", [ ("12.0 - 16.0", (12.0, 16.0)), ("(0,70-1,10)", (0.70, 1.10)), ("[4,00 – 5,60]", (4.0, 5.6)), ("< 2,00", (None, 2.0)), ("> 0,40", (0.40, None)), ("[4.20 - 5.901", (4.20, 5.90)), ("", (None, None)), ]) def test_parse_range(text, expected): assert parse_range(text) == expected def test_parse_line_french_with_marker(): r = parse_line("Kaliémie 5,36H mmol/L 3,50 - 5,10") assert (r.test_key, r.value, r.unit, r.ref_low, r.ref_high) == ("potassium", 5.36, "mmol/L", 3.5, 5.1) def test_parse_line_skips_ocr_noise_and_stray_digit(): assert parse_line("TSH ae 3.11 mIU/L [0.40 - 4.00]").value == 3.11 assert parse_line("K 7 3.79 mmol/L [3.50 - 5.10]").value == 3.79 def test_parse_line_ignores_headers(): assert parse_line("Date de prélèvement: 2026-04-05") is None assert parse_line("Patient: Karim Mansour") is None # ---------- end to end on samples ---------- def test_sample_pdf_end_to_end(): results, _, lang, method = extract("samples/sample_en_report.pdf") assert method == "pdf-text" and lang == "en" and len(results) == 13 def test_sample_urgent_photo_end_to_end(): results, _, lang, method = extract("samples/sample_fr_urgent.png") a = flag_all(results) assert method == "ocr" and lang == "fr" and a.urgent # ---------- explanation guardrails ---------- @pytest.mark.parametrize("text", [ "You have diabetes.", "Vous avez une anémie.", "Take 1000 mg of iron daily.", "Stop taking your pills.", ]) def test_guardrails_block_diagnosis_and_dosing(text): assert violates_guardrails(text) def test_guardrails_allow_hedged_language(): assert not violates_guardrails("A high value can be linked to diabetes; your doctor can confirm.") def test_template_explanation_without_api_key(monkeypatch): monkeypatch.delenv("HF_TOKEN", raising=False) monkeypatch.delenv("LLM_API_KEY", raising=False) a = flag_all([R("ferritin", 8, "ng/mL", 15, 200), R("tsh", 2.0, "mIU/L", 0.4, 4.0)]) exp, source = explain(a, "fr") assert source.startswith("template") md = render_markdown(a, exp, "fr") assert "Ferritine" in md and "Bas" in md and len(exp["questions"]) == 1 # ---------- LLM path (mocked: no network in tests) ---------- class _FakeClient: def __init__(self, content): msg = type("M", (), {"content": content}) choice = type("C", (), {"message": msg}) self.chat = type("Ch", (), {"completions": type("Co", (), { "create": staticmethod(lambda **kw: type("R", (), {"choices": [choice]}))})}) def _patch_llm(monkeypatch, content): import openai monkeypatch.setenv("HF_TOKEN", "test") monkeypatch.setattr(openai, "OpenAI", lambda **kw: _FakeClient(content)) def test_llm_output_used_when_safe(monkeypatch): _patch_llm(monkeypatch, '{"summary": "Mostly fine.", "results": [{"key": "ferritin", "explanation": ' '"Low iron stores."}, {"key": "made_up", "explanation": "x"}], "questions": ["Why?"]}') a = flag_all([R("ferritin", 8, "ng/mL", 15, 200)]) exp, source = explain(a, "en") assert source == "llm" assert [x["key"] for x in exp["results"]] == ["ferritin"] # hallucinated test dropped def test_llm_diagnosis_falls_back_to_template(monkeypatch): _patch_llm(monkeypatch, '{"summary": "You have anemia.", "results": [], "questions": []}') a = flag_all([R("hemoglobin", 9, "g/dL", 12, 16)]) exp, source = explain(a, "en") assert source.startswith("template") and "guardrail" in source def test_llm_garbage_falls_back_to_template(monkeypatch): _patch_llm(monkeypatch, "Sorry, I can't help with that.") exp, source = explain(flag_all([R("tsh", 9, "mIU/L", 0.4, 4.0)]), "ar") assert source.startswith("template") def test_lab_marker_contradiction_becomes_check(): r = R("ast", 6.5, "UI/L", 3.0, 48.0) # range misread (8,0 -> 3,0) but lab printed "L" r.report_marker = "L" assert flag_result(r).flag == Flag.CHECK ok = R("ast", 6.5, "UI/L", 8.0, 48.0) ok.report_marker = "L" assert flag_result(ok).flag == Flag.LOW def test_glued_marker_with_trailing_punctuation(): r = parse_line("Vitamine d 22L. ng/mL 30 - 100") assert (r.value, r.report_marker, r.ref_low) == (22.0, "L", 30.0)