File size: 1,939 Bytes
151ec26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pytest
from httpx import AsyncClient


@pytest.mark.asyncio
async def test_ai_chat_consultation(client: AsyncClient, auth_headers: dict):
    payload = {
        "message": "What are typical lifestyle considerations for managing Type 2 Diabetes?"
    }
    response = await client.post("/api/v1/chat/message", headers=auth_headers, json=payload)
    assert response.status_code == 200
    data = response.json()
    assert data["success"] is True

    completion = data["data"]
    assert "conversation_id" in completion
    msg = completion["message"]
    assert msg["role"] == "assistant"
    assert msg["structured_data"] is not None
    assert len(msg["structured_data"]["possible_considerations"]) > 0
    assert len(msg["structured_data"]["questions_for_doctor"]) > 0
    assert msg["structured_data"]["is_emergency"] is False


@pytest.mark.asyncio
async def test_ai_chat_emergency_detection(client: AsyncClient, auth_headers: dict):
    payload = {
        "message": "I am having sudden severe chest pain and difficulty breathing!"
    }
    response = await client.post("/api/v1/chat/message", headers=auth_headers, json=payload)
    assert response.status_code == 200
    data = response.json()
    structured = data["data"]["message"]["structured_data"]

    assert structured["is_emergency"] is True
    assert "emergency" in structured["summary"].lower() or "emergency" in structured["safety_warning"].lower()


@pytest.mark.asyncio
async def test_chat_conversations_history(client: AsyncClient, auth_headers: dict):
    # Send a message to ensure conversation exists
    await client.post(
        "/api/v1/chat/message",
        headers=auth_headers,
        json={"message": "Can metformin cause vitamin B12 deficiency?"}
    )

    response = await client.get("/api/v1/chat/conversations", headers=auth_headers)
    assert response.status_code == 200
    convs = response.json()["data"]
    assert len(convs) >= 1