|
| 1 | +"""Tests for chat API endpoints.""" |
| 2 | +import pytest |
| 3 | +from datetime import datetime, timezone |
| 4 | +from unittest.mock import AsyncMock, patch, MagicMock |
| 5 | +import uuid |
| 6 | + |
| 7 | + |
| 8 | +class TestChatAPI: |
| 9 | + """Tests for /chat endpoints.""" |
| 10 | + |
| 11 | + @pytest.mark.asyncio |
| 12 | + async def test_chat_basic_message(self, auth_client, mock_user): |
| 13 | + """Test basic chat message processing.""" |
| 14 | + mock_memory = MagicMock() |
| 15 | + mock_memory.id = uuid.uuid4() |
| 16 | + mock_memory.content = "Meeting with John last week about project" |
| 17 | + mock_memory.memory_type = "text" |
| 18 | + mock_memory.memory_date = datetime.now(timezone.utc) |
| 19 | + mock_memory.photo_url = None |
| 20 | + mock_memory.audio_url = None |
| 21 | + |
| 22 | + with patch('app.api.chat.ChatService') as MockChat, \ |
| 23 | + patch('app.api.chat.SearchService') as MockSearch: |
| 24 | + mock_chat_instance = AsyncMock() |
| 25 | + mock_chat_instance.chat.return_value = ( |
| 26 | + "Based on your memories, you met with John last week.", |
| 27 | + [mock_memory], |
| 28 | + "conv_123", |
| 29 | + [], # actions_taken |
| 30 | + [], # pending_actions |
| 31 | + ) |
| 32 | + MockChat.return_value = mock_chat_instance |
| 33 | + |
| 34 | + response = await auth_client.post( |
| 35 | + "/chat", |
| 36 | + json={ |
| 37 | + "message": "What did I discuss with John?" |
| 38 | + } |
| 39 | + ) |
| 40 | + |
| 41 | + assert response.status_code == 200 |
| 42 | + data = response.json() |
| 43 | + assert "response" in data |
| 44 | + assert len(data["memories_used"]) == 1 |
| 45 | + assert data["conversation_id"] == "conv_123" |
| 46 | + |
| 47 | + @pytest.mark.asyncio |
| 48 | + async def test_chat_with_conversation_id(self, auth_client, mock_user): |
| 49 | + """Test chat with existing conversation context.""" |
| 50 | + with patch('app.api.chat.ChatService') as MockChat, \ |
| 51 | + patch('app.api.chat.SearchService') as MockSearch: |
| 52 | + mock_chat_instance = AsyncMock() |
| 53 | + mock_chat_instance.chat.return_value = ( |
| 54 | + "Continuing from our previous discussion...", |
| 55 | + [], |
| 56 | + "conv_456", |
| 57 | + [], |
| 58 | + [], |
| 59 | + ) |
| 60 | + MockChat.return_value = mock_chat_instance |
| 61 | + |
| 62 | + response = await auth_client.post( |
| 63 | + "/chat", |
| 64 | + json={ |
| 65 | + "message": "Can you elaborate?", |
| 66 | + "conversation_id": "conv_456" |
| 67 | + } |
| 68 | + ) |
| 69 | + |
| 70 | + assert response.status_code == 200 |
| 71 | + data = response.json() |
| 72 | + assert data["conversation_id"] == "conv_456" |
| 73 | + |
| 74 | + @pytest.mark.asyncio |
| 75 | + async def test_chat_returns_pending_actions(self, auth_client, mock_user): |
| 76 | + """Test chat returns pending actions for user approval.""" |
| 77 | + with patch('app.api.chat.ChatService') as MockChat, \ |
| 78 | + patch('app.api.chat.SearchService') as MockSearch: |
| 79 | + mock_chat_instance = AsyncMock() |
| 80 | + mock_chat_instance.chat.return_value = ( |
| 81 | + "I'll create a meeting for you. Please confirm.", |
| 82 | + [], |
| 83 | + "conv_789", |
| 84 | + [], |
| 85 | + [{"action_id": "act_1", "tool": "create_event", "arguments": {"title": "Team Meeting"}}], |
| 86 | + ) |
| 87 | + MockChat.return_value = mock_chat_instance |
| 88 | + |
| 89 | + response = await auth_client.post( |
| 90 | + "/chat", |
| 91 | + json={"message": "Schedule a team meeting tomorrow"} |
| 92 | + ) |
| 93 | + |
| 94 | + assert response.status_code == 200 |
| 95 | + data = response.json() |
| 96 | + assert len(data["pending_actions"]) == 1 |
| 97 | + assert data["pending_actions"][0]["tool"] == "create_event" |
| 98 | + |
| 99 | + @pytest.mark.asyncio |
| 100 | + async def test_chat_empty_message_fails(self, auth_client, mock_user): |
| 101 | + """Test chat with empty message fails validation.""" |
| 102 | + response = await auth_client.post( |
| 103 | + "/chat", |
| 104 | + json={"message": ""} |
| 105 | + ) |
| 106 | + assert response.status_code == 422 |
| 107 | + |
| 108 | + @pytest.mark.asyncio |
| 109 | + async def test_chat_with_context(self, auth_client, mock_user): |
| 110 | + """Test chat with context data for reinstatement.""" |
| 111 | + with patch('app.api.chat.ChatService') as MockChat, \ |
| 112 | + patch('app.api.chat.SearchService') as MockSearch: |
| 113 | + mock_chat_instance = AsyncMock() |
| 114 | + mock_chat_instance.chat.return_value = ( |
| 115 | + "Here are your work-related memories...", |
| 116 | + [], |
| 117 | + "conv_101", |
| 118 | + [], |
| 119 | + [], |
| 120 | + ) |
| 121 | + MockChat.return_value = mock_chat_instance |
| 122 | + |
| 123 | + response = await auth_client.post( |
| 124 | + "/chat", |
| 125 | + json={ |
| 126 | + "message": "What meetings do I have?", |
| 127 | + "context": { |
| 128 | + "time_of_day": "morning", |
| 129 | + "day_of_week": "Monday", |
| 130 | + "activity": "working", |
| 131 | + } |
| 132 | + } |
| 133 | + ) |
| 134 | + |
| 135 | + assert response.status_code == 200 |
| 136 | + |
| 137 | + |
| 138 | +class TestChatStreamAPI: |
| 139 | + """Tests for /chat/stream endpoint.""" |
| 140 | + |
| 141 | + @pytest.mark.asyncio |
| 142 | + @pytest.mark.skip(reason="Requires full database setup with connected_accounts table") |
| 143 | + async def test_chat_stream_endpoint_exists(self, auth_client, mock_user): |
| 144 | + """Test chat stream endpoint is accessible.""" |
| 145 | + with patch('app.api.chat.ChatService') as MockChat, \ |
| 146 | + patch('app.api.chat.SearchService') as MockSearch: |
| 147 | + # Mock the streaming response |
| 148 | + async def mock_stream(): |
| 149 | + yield "data: {\"type\": \"text\", \"content\": \"Hello\"}\n\n" |
| 150 | + yield "data: {\"type\": \"done\"}\n\n" |
| 151 | + |
| 152 | + mock_chat_instance = AsyncMock() |
| 153 | + mock_chat_instance.chat_stream = mock_stream |
| 154 | + MockChat.return_value = mock_chat_instance |
| 155 | + |
| 156 | + response = await auth_client.post( |
| 157 | + "/chat/stream", |
| 158 | + json={"message": "Hello"} |
| 159 | + ) |
| 160 | + |
| 161 | + # Stream endpoints return 200 with text/event-stream |
| 162 | + assert response.status_code == 200 |
| 163 | + |
| 164 | + |
| 165 | +class TestGreetingAPI: |
| 166 | + """Tests for /chat/greeting endpoint.""" |
| 167 | + |
| 168 | + @pytest.mark.asyncio |
| 169 | + @pytest.mark.skip(reason="Requires full database setup with connected_accounts table") |
| 170 | + async def test_get_greeting_morning(self, auth_client, mock_user): |
| 171 | + """Test morning greeting generation.""" |
| 172 | + with patch('app.api.chat.ChatService') as MockChat, \ |
| 173 | + patch('app.api.chat.SearchService') as MockSearch: |
| 174 | + mock_chat_instance = AsyncMock() |
| 175 | + mock_chat_instance.generate_greeting.return_value = ( |
| 176 | + "Good morning! Ready to start the day?", |
| 177 | + {"weather": "sunny", "temperature": "72°F"} |
| 178 | + ) |
| 179 | + MockChat.return_value = mock_chat_instance |
| 180 | + |
| 181 | + response = await auth_client.get( |
| 182 | + "/chat/greeting", |
| 183 | + params={"hour": 9} |
| 184 | + ) |
| 185 | + |
| 186 | + assert response.status_code == 200 |
| 187 | + data = response.json() |
| 188 | + assert "greeting" in data |
| 189 | + assert "morning" in data["greeting"].lower() |
| 190 | + |
| 191 | + @pytest.mark.asyncio |
| 192 | + @pytest.mark.skip(reason="Requires full database setup with connected_accounts table") |
| 193 | + async def test_get_greeting_default_hour(self, auth_client, mock_user): |
| 194 | + """Test greeting with default hour parameter.""" |
| 195 | + with patch('app.api.chat.ChatService') as MockChat, \ |
| 196 | + patch('app.api.chat.SearchService') as MockSearch: |
| 197 | + mock_chat_instance = AsyncMock() |
| 198 | + mock_chat_instance.generate_greeting.return_value = ( |
| 199 | + "Hello there!", |
| 200 | + {} |
| 201 | + ) |
| 202 | + MockChat.return_value = mock_chat_instance |
| 203 | + |
| 204 | + response = await auth_client.get("/chat/greeting") |
| 205 | + |
| 206 | + assert response.status_code == 200 |
| 207 | + |
| 208 | + |
| 209 | +class TestSuggestionsAPI: |
| 210 | + """Tests for /chat/suggestions endpoint.""" |
| 211 | + |
| 212 | + @pytest.mark.asyncio |
| 213 | + @pytest.mark.skip(reason="Requires full database setup with connected_accounts table") |
| 214 | + async def test_get_smart_suggestions(self, auth_client, mock_user): |
| 215 | + """Test getting smart suggestions.""" |
| 216 | + with patch('app.api.chat.SuggestionService') as MockSuggestion: |
| 217 | + mock_instance = AsyncMock() |
| 218 | + mock_instance.get_smart_suggestions.return_value = [ |
| 219 | + { |
| 220 | + "text": "What meetings do I have today?", |
| 221 | + "icon": "calendar", |
| 222 | + "category": "calendar", |
| 223 | + } |
| 224 | + ] |
| 225 | + MockSuggestion.return_value = mock_instance |
| 226 | + |
| 227 | + response = await auth_client.get("/chat/suggestions") |
| 228 | + |
| 229 | + assert response.status_code == 200 |
| 230 | + data = response.json() |
| 231 | + assert "suggestions" in data |
| 232 | + assert len(data["suggestions"]) >= 0 |
| 233 | + |
| 234 | + |
| 235 | +class TestExecuteActionAPI: |
| 236 | + """Tests for /chat/execute-action endpoint.""" |
| 237 | + |
| 238 | + @pytest.mark.asyncio |
| 239 | + @pytest.mark.skip(reason="Requires full database setup - execute-action endpoint needs conversation state") |
| 240 | + async def test_execute_action_success(self, auth_client, mock_user): |
| 241 | + """Test executing a pending action.""" |
| 242 | + with patch('app.api.chat.ChatService') as MockChat, \ |
| 243 | + patch('app.api.chat.SearchService') as MockSearch: |
| 244 | + mock_chat_instance = AsyncMock() |
| 245 | + mock_chat_instance.execute_pending_action.return_value = { |
| 246 | + "success": True, |
| 247 | + "message": "Event created successfully", |
| 248 | + "event_id": "evt_123", |
| 249 | + } |
| 250 | + MockChat.return_value = mock_chat_instance |
| 251 | + |
| 252 | + response = await auth_client.post( |
| 253 | + "/chat/execute-action", |
| 254 | + json={ |
| 255 | + "action_id": "act_1", |
| 256 | + "conversation_id": "conv_123", |
| 257 | + "approved": True, |
| 258 | + } |
| 259 | + ) |
| 260 | + |
| 261 | + assert response.status_code == 200 |
| 262 | + data = response.json() |
| 263 | + assert data["success"] is True |
| 264 | + |
| 265 | + @pytest.mark.asyncio |
| 266 | + @pytest.mark.skip(reason="Requires full database setup - execute-action endpoint needs conversation state") |
| 267 | + async def test_execute_action_rejected(self, auth_client, mock_user): |
| 268 | + """Test rejecting a pending action.""" |
| 269 | + with patch('app.api.chat.ChatService') as MockChat, \ |
| 270 | + patch('app.api.chat.SearchService') as MockSearch: |
| 271 | + mock_chat_instance = AsyncMock() |
| 272 | + mock_chat_instance.execute_pending_action.return_value = { |
| 273 | + "success": True, |
| 274 | + "message": "Action cancelled", |
| 275 | + } |
| 276 | + MockChat.return_value = mock_chat_instance |
| 277 | + |
| 278 | + response = await auth_client.post( |
| 279 | + "/chat/execute-action", |
| 280 | + json={ |
| 281 | + "action_id": "act_1", |
| 282 | + "conversation_id": "conv_123", |
| 283 | + "approved": False, |
| 284 | + } |
| 285 | + ) |
| 286 | + |
| 287 | + assert response.status_code == 200 |
| 288 | + |
| 289 | + |
| 290 | +class TestChatAPIAuthentication: |
| 291 | + """Tests for authentication on chat endpoints.""" |
| 292 | + |
| 293 | + @pytest.mark.asyncio |
| 294 | + async def test_chat_unauthorized(self, client): |
| 295 | + """Test chat without auth returns 401.""" |
| 296 | + response = await client.post( |
| 297 | + "/chat", |
| 298 | + json={"message": "Hello"} |
| 299 | + ) |
| 300 | + assert response.status_code == 401 |
| 301 | + |
| 302 | + @pytest.mark.asyncio |
| 303 | + async def test_chat_stream_unauthorized(self, client): |
| 304 | + """Test chat stream without auth returns 401.""" |
| 305 | + response = await client.post( |
| 306 | + "/chat/stream", |
| 307 | + json={"message": "Hello"} |
| 308 | + ) |
| 309 | + assert response.status_code == 401 |
| 310 | + |
| 311 | + @pytest.mark.asyncio |
| 312 | + async def test_greeting_unauthorized(self, client): |
| 313 | + """Test greeting without auth returns 401.""" |
| 314 | + response = await client.get("/chat/greeting") |
| 315 | + assert response.status_code == 401 |
| 316 | + |
| 317 | + @pytest.mark.asyncio |
| 318 | + async def test_suggestions_unauthorized(self, client): |
| 319 | + """Test suggestions without auth returns 401.""" |
| 320 | + response = await client.get("/chat/suggestions") |
| 321 | + assert response.status_code == 401 |
0 commit comments