File size: 6,775 Bytes
12041df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""
Tests for chat service and intent detection
"""
import pytest
from src.services.chat_service import IntentDetector, IntentType


class TestIntentDetection:
    """Test intent detection for conversational and task-specific intents"""
    
    @pytest.fixture
    def detector(self):
        return IntentDetector()
    
    @pytest.mark.asyncio
    async def test_greeting_intents(self, detector):
        """Test greeting intent detection"""
        greetings = [
            "Olá, bom dia!",
            "Oi, tudo bem?",
            "Boa tarde",
            "E aí, como vai?",
            "Bom dia, preciso de ajuda",
        ]
        
        for greeting in greetings:
            intent = await detector.detect(greeting)
            assert intent.type == IntentType.GREETING, f"Failed for: {greeting}"
            assert intent.suggested_agent == "drummond"
    
    @pytest.mark.asyncio
    async def test_conversation_intents(self, detector):
        """Test general conversation intent detection"""
        conversations = [
            "Vamos conversar sobre transparência",
            "Quero falar sobre corrupção no governo",
            "Me conte mais sobre isso",
            "Pode me falar sobre os gastos públicos?",
        ]
        
        for message in conversations:
            intent = await detector.detect(message)
            assert intent.type == IntentType.CONVERSATION, f"Failed for: {message}"
            assert intent.suggested_agent == "drummond"
    
    @pytest.mark.asyncio
    async def test_help_request_intents(self, detector):
        """Test help request intent detection"""
        help_requests = [
            "Preciso de ajuda para entender",
            "Me ajuda com isso?",
            "Não sei como fazer",
            "Pode ajudar?",
            "Não entendi direito",
        ]
        
        for message in help_requests:
            intent = await detector.detect(message)
            assert intent.type == IntentType.HELP_REQUEST, f"Failed for: {message}"
            assert intent.suggested_agent == "drummond"
    
    @pytest.mark.asyncio
    async def test_about_system_intents(self, detector):
        """Test system information intent detection"""
        about_messages = [
            "O que é o Cidadão.AI?",
            "Como você funciona?",
            "Quem é você?",
            "Para que serve este sistema?",
            "O que você faz?",
            "Qual sua função aqui?",
        ]
        
        for message in about_messages:
            intent = await detector.detect(message)
            assert intent.type == IntentType.ABOUT_SYSTEM, f"Failed for: {message}"
            assert intent.suggested_agent == "drummond"
    
    @pytest.mark.asyncio
    async def test_smalltalk_intents(self, detector):
        """Test smalltalk intent detection"""
        smalltalk_messages = [
            "Como está o tempo hoje?",
            "Você gosta de poesia?",
            "Qual sua opinião sobre política?",
            "O que acha do Brasil?",
            "Conte uma história",
            "Você é brasileiro?",
        ]
        
        for message in smalltalk_messages:
            intent = await detector.detect(message)
            assert intent.type == IntentType.SMALLTALK, f"Failed for: {message}"
            assert intent.suggested_agent == "drummond"
    
    @pytest.mark.asyncio
    async def test_thanks_intents(self, detector):
        """Test gratitude intent detection"""
        thanks_messages = [
            "Obrigado!",
            "Muito obrigada pela ajuda",
            "Valeu mesmo",
            "Gratidão",
            "Agradeço a atenção",
            "Foi útil, obrigado",
        ]
        
        for message in thanks_messages:
            intent = await detector.detect(message)
            assert intent.type == IntentType.THANKS, f"Failed for: {message}"
            assert intent.suggested_agent == "drummond"
    
    @pytest.mark.asyncio
    async def test_goodbye_intents(self, detector):
        """Test farewell intent detection"""
        goodbye_messages = [
            "Tchau!",
            "Até logo",
            "Até mais ver",
            "Adeus",
            "Falou, valeu!",
            "Tenho que ir agora",
            "Até breve",
        ]
        
        for message in goodbye_messages:
            intent = await detector.detect(message)
            assert intent.type == IntentType.GOODBYE, f"Failed for: {message}"
            assert intent.suggested_agent == "drummond"
    
    @pytest.mark.asyncio
    async def test_task_specific_intents_remain_unchanged(self, detector):
        """Test that task-specific intents still work correctly"""
        task_messages = [
            ("Investigar contratos da saúde", IntentType.INVESTIGATE, "abaporu"),
            ("Analisar gastos excessivos", IntentType.ANALYZE, "anita"),
            ("Gerar relatório completo", IntentType.REPORT, "tiradentes"),
            ("Qual o status da investigação?", IntentType.STATUS, "abaporu"),
        ]
        
        for message, expected_type, expected_agent in task_messages:
            intent = await detector.detect(message)
            assert intent.type == expected_type, f"Failed for: {message}"
            assert intent.suggested_agent == expected_agent
    
    @pytest.mark.asyncio
    async def test_mixed_intents_prioritize_correctly(self, detector):
        """Test that mixed messages are correctly prioritized"""
        mixed_messages = [
            # Greeting + task should prioritize task
            ("Bom dia, quero investigar contratos", IntentType.INVESTIGATE, "abaporu"),
            # Help + specific task should prioritize task
            ("Me ajuda a analisar anomalias", IntentType.ANALYZE, "anita"),
            # Pure conversational should go to Drummond
            ("Olá, como funciona isso aqui?", IntentType.GREETING, "drummond"),
        ]
        
        for message, expected_type, expected_agent in mixed_messages:
            intent = await detector.detect(message)
            assert intent.type == expected_type, f"Failed for: {message}"
            assert intent.suggested_agent == expected_agent
    
    @pytest.mark.asyncio
    async def test_unknown_defaults_to_drummond(self, detector):
        """Test that unknown intents go to Drummond for handling"""
        unknown_messages = [
            "xyzabc123",
            "????????",
            "asdfghjkl",
        ]
        
        for message in unknown_messages:
            intent = await detector.detect(message)
            # Should be QUESTION (default) or UNKNOWN
            assert intent.type in [IntentType.QUESTION, IntentType.UNKNOWN]
            assert intent.suggested_agent == "drummond"