File size: 4,425 Bytes
18bfaa1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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"]