def test_chat_returns_clarification_questions_for_ambiguous_symptoms(medibot_app, monkeypatch): monkeypatch.setattr( medibot_app.router, "plan", lambda user_input, history, structured_context=None: { "steps": [ { "agent": "clarification_agent", "payload": {"user_symptoms": ["mild_fever"]}, "reason": "Need more detail before diagnosis.", }, { "agent": "symptom_severity", "payload": {"user_symptoms": ["mild_fever"]}, "reason": "Assess urgency.", }, { "agent": "disease_diagnosis", "payload": {"user_symptoms": ["mild_fever"]}, "reason": "Only diagnose if there is enough signal.", }, ] }, ) monkeypatch.setattr( medibot_app.agents["clarification_agent"], "handle", lambda user_symptoms, **kwargs: { "needs_clarification": True, "reason": "too_few_symptoms", "questions": [ "How long have these symptoms been present?", "Are you also experiencing cough?", ], "candidate_preview": [], }, ) history, state, summary, card_html = medibot_app.chat("fever", None) assert "Follow-Up Questions" in history[-1]["content"] assert state["last_clarification"]["needs_clarification"] is True assert "How long have these symptoms been present?" in card_html assert "Structured context" in summary def test_chat_reuses_structured_context_on_follow_up_turn(medibot_app, monkeypatch): captured_contexts = [] def fake_plan(user_input, history, structured_context=None): captured_contexts.append(structured_context.copy()) symptoms = structured_context.get("reported_symptoms") or ["mild_fever"] return { "steps": [ { "agent": "symptom_severity", "payload": {"user_symptoms": symptoms}, "reason": "Assess urgency from current known symptoms.", } ] } monkeypatch.setattr(medibot_app.router, "plan", fake_plan) _, state, _, _ = medibot_app.chat("fever", None) _, state, _, _ = medibot_app.chat("for 3 days and getting worse after travel", state) assert captured_contexts[0]["reported_symptoms"] == ["mild_fever"] assert captured_contexts[1]["reported_symptoms"] == ["mild_fever"] assert captured_contexts[1]["duration"] == "3 days" assert captured_contexts[1]["progression"] == "worsening" assert "travel" in captured_contexts[1]["exposures"] def test_chat_returns_emergency_message_for_red_flag_symptoms(medibot_app, monkeypatch): monkeypatch.setattr( medibot_app.router, "plan", lambda user_input, history, structured_context=None: { "steps": [ { "agent": "symptom_severity", "payload": {"user_symptoms": ["chest_pain", "breathlessness"]}, "reason": "Red-flag triage.", } ] }, ) history, state, _, _ = medibot_app.chat("chest pain and shortness of breath", None) assert "medical emergency" in history[-1]["content"].lower() assert state["last_severity"]["urgency"] == "urgent" def test_chat_can_answer_condition_overview_without_diagnosis(medibot_app, monkeypatch): monkeypatch.setattr( medibot_app.router, "plan", lambda user_input, history, structured_context=None: { "steps": [ { "agent": "disease_description", "payload": {"disease": "pneumonia"}, "reason": "Explain the condition.", }, { "agent": "precaution_advisor", "payload": {"disease": "pneumonia"}, "reason": "Provide precautions.", }, ] }, ) history, _, _, _ = medibot_app.chat("what is pneumonia", None) assert "Condition Overview" in history[-1]["content"] assert "Pneumonia" in history[-1]["content"] assert "Precautions:" in history[-1]["content"]