File size: 2,277 Bytes
8018595
 
 
 
 
 
 
cc034ee
 
 
 
8018595
 
cc034ee
8018595
 
 
 
 
 
cc034ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8018595
 
 
 
 
 
 
 
 
 
 
cc034ee
8018595
 
 
 
 
 
 
cc034ee
 
8018595
 
cc034ee
 
 
 
 
 
8018595
 
 
 
cc034ee
 
 
 
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
# pylint: disable=missing-docstring
from unittest.mock import MagicMock, patch

from sentinel.conversation import ConversationManager
from sentinel.models import (
    ConversationResponse,
    InitialAssessment,
)
from sentinel.user_input import (
    Anthropometrics,
    Demographics,
    Lifestyle,
    PersonalMedicalHistory,
    SmokingHistory,
    UserInput,
)


def sample_user() -> UserInput:
    return UserInput(
        demographics=Demographics(
            age_years=30,
            sex="male",
            anthropometrics=Anthropometrics(height_cm=175, weight_kg=70),
        ),
        lifestyle=Lifestyle(
            smoking=SmokingHistory(
                status="never",
                cigarettes_per_day=0,
                years_smoked=0,
            ),
        ),
        personal_medical_history=PersonalMedicalHistory(
            chronic_conditions=[],
            previous_cancers=[],
        ),
        family_history=[],
    )


@patch("sentinel.llm_service.create_initial_assessment_chain")
@patch("sentinel.llm_service.create_conversational_chain")
def test_conversation_flow(mock_create_conversational_chain, mock_create_initial_chain):
    structured = MagicMock()
    freeform = MagicMock()
    structured.invoke.return_value = {
        "overall_summary": "ok",
        "llm_risk_interpretations": [],
        "dx_recommendations": [],
    }
    freeform.invoke.return_value = "hi"
    mock_create_initial_chain.return_value = structured
    mock_create_conversational_chain.return_value = freeform

    conv = ConversationManager(structured, freeform)
    user = sample_user()
    result = conv.initial_assessment(user)
    assert isinstance(result, InitialAssessment)
    assert result.overall_summary == "ok"
    assert result.calculated_risk_scores == {}

    # Verify history contains initial assessment message
    assert len(conv.history) == 1
    assert conv.history[0][0].startswith("Initial assessment for user profile:")
    assert conv.history[0][1] == result.model_dump_json()

    answer = conv.follow_up("question")
    assert isinstance(answer, ConversationResponse)
    assert answer.response == "hi"

    # Verify follow-up added to history
    assert len(conv.history) == 2
    assert conv.history[1] == ("question", "hi")