from __future__ import annotations import pytest from backend.app.api.routes import mohp from backend.app.api.routes.mohp import MOHPEvaluateRequest, evaluate_mohp @pytest.fixture(autouse=True) def _disable_session_writes(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(mohp, "add_objection", lambda **kwargs: None) def test_mohp_rule_fallback_without_ollama_key(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(mohp.settings, "ollama_api_key", "") response = evaluate_mohp( MOHPEvaluateRequest( session_id="mohp-rule", input_text="The survival and efficacy evidence is strong.", cluster_id=0, persona_id="HCP-00-001", ) ) assert response["mohp_mode"] == "rule_fallback" assert response["llm_enhancement_used"] is False assert response["clinical_evidence_retrieval_called"] is False assert response["count"] >= 1 def test_mohp_llm_rag_path_uses_clinical_evidence(monkeypatch: pytest.MonkeyPatch) -> None: calls: list[str] = [] monkeypatch.setattr(mohp.settings, "ollama_api_key", "present-for-test") def fake_retrieve(text: str, limit: int = 3) -> list[dict[str, str]]: calls.append(text) return [ { "id": "clinical-evidence-1", "text": "Phase III oncology evidence supports qualified efficacy messaging.", "source": "retrieved clinical evidence", } ] monkeypatch.setattr(mohp, "_retrieve_clinical_evidence", fake_retrieve) monkeypatch.setattr( mohp, "_call_llm_for_objections", lambda prompt: ( '[{"objection":"Clarify evidence strength before making survival claims.",' '"guideline":"retrieved clinical evidence",' '"severity":"high",' '"safer_phrasing":"Use qualified evidence language.",' '"evidence_gap":"Head-to-head survival evidence was not retrieved."}]' ), ) response = evaluate_mohp( MOHPEvaluateRequest( session_id="mohp-llm", input_text="We can claim superior survival and broad biomarker benefit.", cluster_id=0, persona_id="HCP-00-001", ) ) assert calls assert response["mohp_mode"] == "llm_rag_enhanced" assert response["llm_enhancement_used"] is True assert response["clinical_evidence_retrieval_called"] is True assert response["clinical_evidence_count"] == 1 assert response["retrieval_empty"] is False assert response["objections"][0]["safer_phrasing"] def test_mohp_empty_retrieval_is_reported_without_guideline_hallucination(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(mohp.settings, "ollama_api_key", "present-for-test") monkeypatch.setattr(mohp, "_retrieve_clinical_evidence", lambda text, limit=3: []) monkeypatch.setattr( mohp, "_call_llm_for_objections", lambda prompt: ( '[{"objection":"Evidence support is unclear.",' '"guideline":"Invented Oncology Guideline 2030",' '"severity":"medium"}]' ), ) response = evaluate_mohp( MOHPEvaluateRequest( session_id="mohp-empty", input_text="We can claim a new standard across all patients.", cluster_id=2, persona_id="HCP-02-001", ) ) assert response["mohp_mode"] == "llm_rag_enhanced" assert response["retrieval_empty"] is True assert response["clinical_evidence_count"] == 0 assert response["objections"][0]["guideline"] == "No retrieved clinical evidence" def test_mohp_llm_failure_falls_back_to_rules(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(mohp.settings, "ollama_api_key", "present-for-test") monkeypatch.setattr(mohp, "_retrieve_clinical_evidence", lambda text, limit=3: [{"text": "evidence"}]) def fail_llm(prompt: str) -> str: raise RuntimeError("provider unavailable") monkeypatch.setattr(mohp, "_call_llm_for_objections", fail_llm) response = evaluate_mohp( MOHPEvaluateRequest( session_id="mohp-fail", input_text="The efficacy and survival evidence is strong.", cluster_id=0, persona_id="HCP-00-001", ) ) assert response["mohp_mode"] == "rule_fallback" assert response["llm_enhancement_used"] is False assert response["llm_error"] is True assert response["clinical_evidence_retrieval_called"] is True assert response["count"] >= 1