Spaces:
Sleeping
Sleeping
File size: 21,478 Bytes
6a3de9e |
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 |
"""
Integration tests for the AI agent functionality.
These tests verify the end-to-end functionality of the AI agent with
realistic scenarios and actual tool execution.
"""
import pytest
from fastapi.testclient import TestClient
from unittest.mock import AsyncMock, MagicMock, patch
from main import app
from models.conversation import Conversation
from models.message import Message
from uuid import UUID, uuid4
@pytest.fixture
def client():
"""Create a test client for the API."""
return TestClient(app)
@pytest.mark.asyncio
async def test_agent_full_add_task_workflow(client):
"""Test the complete workflow for adding a task via AI agent."""
with patch('ai.endpoints.chat.ConversationManager') as mock_conv_manager, \
patch('ai.endpoints.chat.TodoAgent') as mock_agent_class:
# Mock conversation manager
mock_conv_instance = MagicMock()
mock_conv_instance.create_conversation = AsyncMock(return_value=MagicMock(id=uuid4()))
mock_conv_instance.add_message = AsyncMock()
mock_conv_instance.update_conversation_timestamp = AsyncMock()
mock_conv_manager.return_value = mock_conv_instance
# Mock agent instance to simulate add_task workflow
mock_agent_instance = MagicMock()
mock_agent_instance.process_message = AsyncMock(return_value={
"response": "I've added the task 'Buy groceries' to your list successfully!",
"conversation_id": str(uuid4()),
"tool_calls": [
{
"id": "call_123",
"function": {
"name": "add_task",
"arguments": "{\"user_id\":\"test-user-123\",\"title\":\"Buy groceries\",\"description\":\"Need to buy milk, bread, and eggs\",\"priority\":\"medium\"}"
}
}
],
"requires_action": True
})
mock_agent_class.return_value = mock_agent_instance
# Send a request to add a task
response = client.post(
"/api/test-user-123/chat",
json={
"message": "Add a task: Buy groceries - need milk, bread, and eggs"
},
headers={"Content-Type": "application/json"}
)
# Verify the response
assert response.status_code == 200
data = response.json()
assert "response" in data
assert "Buy groceries" in data["response"]
assert "conversation_id" in data
assert "tool_calls" in data
assert len(data["tool_calls"]) > 0
assert data["tool_calls"][0]["function"]["name"] == "add_task"
@pytest.mark.asyncio
async def test_agent_full_list_tasks_workflow(client):
"""Test the complete workflow for listing tasks via AI agent."""
with patch('ai.endpoints.chat.ConversationManager') as mock_conv_manager, \
patch('ai.endpoints.chat.TodoAgent') as mock_agent_class:
# Mock conversation manager
mock_conv_instance = MagicMock()
mock_conv_instance.create_conversation = AsyncMock(return_value=MagicMock(id=uuid4()))
mock_conv_instance.add_message = AsyncMock()
mock_conv_instance.update_conversation_timestamp = AsyncMock()
mock_conv_manager.return_value = mock_conv_instance
# Mock agent instance to simulate list_tasks workflow
mock_agent_instance = MagicMock()
mock_agent_instance.process_message = AsyncMock(return_value={
"response": "Here are your tasks: 1. Buy groceries (pending), 2. Clean house (pending), 3. Pay bills (completed)",
"conversation_id": str(uuid4()),
"tool_calls": [
{
"id": "call_456",
"function": {
"name": "list_tasks",
"arguments": "{\"user_id\":\"test-user-123\",\"status\":\"all\"}"
}
}
],
"requires_action": True
})
mock_agent_class.return_value = mock_agent_instance
# Send a request to list tasks
response = client.post(
"/api/test-user-123/chat",
json={
"message": "Show me all my tasks"
},
headers={"Content-Type": "application/json"}
)
# Verify the response
assert response.status_code == 200
data = response.json()
assert "response" in data
assert "tasks" in data["response"].lower()
assert "conversation_id" in data
assert "tool_calls" in data
assert len(data["tool_calls"]) > 0
assert data["tool_calls"][0]["function"]["name"] == "list_tasks"
@pytest.mark.asyncio
async def test_agent_full_complete_task_workflow(client):
"""Test the complete workflow for completing a task via AI agent."""
with patch('ai.endpoints.chat.ConversationManager') as mock_conv_manager, \
patch('ai.endpoints.chat.TodoAgent') as mock_agent_class:
# Mock conversation manager
mock_conv_instance = MagicMock()
mock_conv_instance.create_conversation = AsyncMock(return_value=MagicMock(id=uuid4()))
mock_conv_instance.add_message = AsyncMock()
mock_conv_instance.update_conversation_timestamp = AsyncMock()
mock_conv_manager.return_value = mock_conv_instance
# Mock agent instance to simulate complete_task workflow
mock_agent_instance = MagicMock()
mock_agent_instance.process_message = AsyncMock(return_value={
"response": "I've marked task 'Buy groceries' as completed successfully!",
"conversation_id": str(uuid4()),
"tool_calls": [
{
"id": "call_789",
"function": {
"name": "complete_task",
"arguments": "{\"user_id\":\"test-user-123\",\"task_id\":\"1\"}"
}
}
],
"requires_action": True
})
mock_agent_class.return_value = mock_agent_instance
# Send a request to complete a task
response = client.post(
"/api/test-user-123/chat",
json={
"message": "Complete task 1"
},
headers={"Content-Type": "application/json"}
)
# Verify the response
assert response.status_code == 200
data = response.json()
assert "response" in data
assert "completed" in data["response"].lower()
assert "conversation_id" in data
assert "tool_calls" in data
assert len(data["tool_calls"]) > 0
assert data["tool_calls"][0]["function"]["name"] == "complete_task"
@pytest.mark.asyncio
async def test_agent_full_delete_task_workflow(client):
"""Test the complete workflow for deleting a task via AI agent."""
with patch('ai.endpoints.chat.ConversationManager') as mock_conv_manager, \
patch('ai.endpoints.chat.TodoAgent') as mock_agent_class:
# Mock conversation manager
mock_conv_instance = MagicMock()
mock_conv_instance.create_conversation = AsyncMock(return_value=MagicMock(id=uuid4()))
mock_conv_instance.add_message = AsyncMock()
mock_conv_instance.update_conversation_timestamp = AsyncMock()
mock_conv_manager.return_value = mock_conv_instance
# Mock agent instance to simulate delete_task workflow
mock_agent_instance = MagicMock()
mock_agent_instance.process_message = AsyncMock(return_value={
"response": "I've deleted task 'Clean house' from your list successfully!",
"conversation_id": str(uuid4()),
"tool_calls": [
{
"id": "call_abc",
"function": {
"name": "delete_task",
"arguments": "{\"user_id\":\"test-user-123\",\"task_id\":\"2\"}"
}
}
],
"requires_action": True
})
mock_agent_class.return_value = mock_agent_instance
# Send a request to delete a task
response = client.post(
"/api/test-user-123/chat",
json={
"message": "Delete task 2"
},
headers={"Content-Type": "application/json"}
)
# Verify the response
assert response.status_code == 200
data = response.json()
assert "response" in data
assert "deleted" in data["response"].lower()
assert "conversation_id" in data
assert "tool_calls" in data
assert len(data["tool_calls"]) > 0
assert data["tool_calls"][0]["function"]["name"] == "delete_task"
@pytest.mark.asyncio
async def test_agent_full_update_task_workflow(client):
"""Test the complete workflow for updating a task via AI agent."""
with patch('ai.endpoints.chat.ConversationManager') as mock_conv_manager, \
patch('ai.endpoints.chat.TodoAgent') as mock_agent_class:
# Mock conversation manager
mock_conv_instance = MagicMock()
mock_conv_instance.create_conversation = AsyncMock(return_value=MagicMock(id=uuid4()))
mock_conv_instance.add_message = AsyncMock()
mock_conv_instance.update_conversation_timestamp = AsyncMock()
mock_conv_manager.return_value = mock_conv_instance
# Mock agent instance to simulate update_task workflow
mock_agent_instance = MagicMock()
mock_agent_instance.process_message = AsyncMock(return_value={
"response": "I've updated task 'Buy groceries' with the new due date successfully!",
"conversation_id": str(uuid4()),
"tool_calls": [
{
"id": "call_def",
"function": {
"name": "update_task",
"arguments": "{\"user_id\":\"test-user-123\",\"task_id\":\"1\",\"due_date\":\"2024-12-31\"}"
}
}
],
"requires_action": True
})
mock_agent_class.return_value = mock_agent_instance
# Send a request to update a task
response = client.post(
"/api/test-user-123/chat",
json={
"message": "Update task 1: Change due date to December 31st"
},
headers={"Content-Type": "application/json"}
)
# Verify the response
assert response.status_code == 200
data = response.json()
assert "response" in data
assert "updated" in data["response"].lower()
assert "conversation_id" in data
assert "tool_calls" in data
assert len(data["tool_calls"]) > 0
assert data["tool_calls"][0]["function"]["name"] == "update_task"
@pytest.mark.asyncio
async def test_agent_error_handling_workflow(client):
"""Test the agent's error handling workflow."""
with patch('ai.endpoints.chat.ConversationManager') as mock_conv_manager, \
patch('ai.endpoints.chat.TodoAgent') as mock_agent_class:
# Mock conversation manager
mock_conv_instance = MagicMock()
mock_conv_instance.create_conversation = AsyncMock(return_value=MagicMock(id=uuid4()))
mock_conv_instance.add_message = AsyncMock()
mock_conv_manager.return_value = mock_conv_instance
# Mock agent to raise an exception
mock_agent_instance = MagicMock()
mock_agent_instance.process_message = AsyncMock(side_effect=Exception("API Error"))
mock_agent_class.return_value = mock_agent_instance
# Send a request that will cause an error
response = client.post(
"/api/test-user-123/chat",
json={
"message": "Add a task: Test error handling"
},
headers={"Content-Type": "application/json"}
)
# Should return a server error
assert response.status_code == 500
@pytest.mark.asyncio
async def test_agent_unrecognized_command_response(client):
"""Test the agent's response to unrecognized commands."""
with patch('ai.endpoints.chat.ConversationManager') as mock_conv_manager, \
patch('ai.endpoints.chat.TodoAgent') as mock_agent_class:
# Mock conversation manager
mock_conv_instance = MagicMock()
mock_conv_instance.create_conversation = AsyncMock(return_value=MagicMock(id=uuid4()))
mock_conv_instance.add_message = AsyncMock()
mock_conv_instance.update_conversation_timestamp = AsyncMock()
mock_conv_manager.return_value = mock_conv_instance
# Mock agent instance to return a clarification response
mock_agent_instance = MagicMock()
mock_agent_instance.process_message = AsyncMock(return_value={
"response": "I'm sorry, I didn't understand that command. Could you please rephrase or be more specific?",
"conversation_id": str(uuid4()),
"tool_calls": [],
"requires_action": False
})
mock_agent_class.return_value = mock_agent_instance
# Send an unrecognized command
response = client.post(
"/api/test-user-123/chat",
json={
"message": "Random command that doesn't make sense for task management"
},
headers={"Content-Type": "application/json"}
)
# Verify the response
assert response.status_code == 200
data = response.json()
assert "response" in data
assert "sorry" in data["response"].lower() or "understand" in data["response"].lower()
assert "conversation_id" in data
assert "tool_calls" in data
assert len(data["tool_calls"]) == 0
assert data["requires_action"] is False
@pytest.mark.asyncio
async def test_agent_multiple_tool_calls_in_single_request(client):
"""Test the agent's ability to handle multiple tool calls in a single request."""
with patch('ai.endpoints.chat.ConversationManager') as mock_conv_manager, \
patch('ai.endpoints.chat.TodoAgent') as mock_agent_class:
# Mock conversation manager
mock_conv_instance = MagicMock()
mock_conv_instance.create_conversation = AsyncMock(return_value=MagicMock(id=uuid4()))
mock_conv_instance.add_message = AsyncMock()
mock_conv_instance.update_conversation_timestamp = AsyncMock()
mock_conv_manager.return_value = mock_conv_instance
# Mock agent instance to return multiple tool calls
mock_agent_instance = MagicMock()
mock_agent_instance.process_message = AsyncMock(return_value={
"response": "I've processed your request by adding a task and listing your tasks.",
"conversation_id": str(uuid4()),
"tool_calls": [
{
"id": "call_multi_1",
"function": {
"name": "add_task",
"arguments": "{\"user_id\":\"test-user-123\",\"title\":\"New task\",\"priority\":\"high\"}"
}
},
{
"id": "call_multi_2",
"function": {
"name": "list_tasks",
"arguments": "{\"user_id\":\"test-user-123\",\"status\":\"all\"}"
}
}
],
"requires_action": True
})
mock_agent_class.return_value = mock_agent_instance
# Send a complex request that might trigger multiple tools
response = client.post(
"/api/test-user-123/chat",
json={
"message": "Add a high priority task 'New task' and then show me all my tasks"
},
headers={"Content-Type": "application/json"}
)
# Verify the response
assert response.status_code == 200
data = response.json()
assert "response" in data
assert "conversation_id" in data
assert "tool_calls" in data
assert len(data["tool_calls"]) == 2
assert data["tool_calls"][0]["function"]["name"] in ["add_task", "list_tasks"]
assert data["tool_calls"][1]["function"]["name"] in ["add_task", "list_tasks"]
@pytest.mark.asyncio
async def test_agent_conversation_management(client):
"""Test that the agent properly manages conversation state."""
test_conversation_id = uuid4()
with patch('ai.endpoints.chat.ConversationManager') as mock_conv_manager, \
patch('ai.endpoints.chat.TodoAgent') as mock_agent_class:
# Mock conversation manager methods
mock_conv_instance = MagicMock()
mock_conv_instance.get_conversation = AsyncMock(return_value=MagicMock(id=test_conversation_id, user_id="test-user-123"))
mock_conv_instance.add_message = AsyncMock()
mock_conv_instance.update_conversation_timestamp = AsyncMock()
mock_conv_manager.return_value = mock_conv_instance
# Mock agent instance
mock_agent_instance = MagicMock()
mock_agent_instance.process_message = AsyncMock(return_value={
"response": "Processed your message in conversation",
"conversation_id": str(test_conversation_id),
"tool_calls": [],
"requires_action": False
})
mock_agent_class.return_value = mock_agent_instance
# Send a request with an existing conversation ID
response = client.post(
f"/api/test-user-123/chat",
params={"conversation_id": str(test_conversation_id)},
json={
"message": "Continue working on my tasks"
},
headers={"Content-Type": "application/json"}
)
# Verify the response
assert response.status_code == 200
data = response.json()
assert data["conversation_id"] == str(test_conversation_id)
assert "response" in data
@pytest.mark.asyncio
async def test_agent_response_timing_and_performance(client):
"""Test that the agent responds within reasonable time limits."""
import time
with patch('ai.endpoints.chat.ConversationManager') as mock_conv_manager, \
patch('ai.endpoints.chat.TodoAgent') as mock_agent_class:
# Mock conversation manager
mock_conv_instance = MagicMock()
mock_conv_instance.create_conversation = AsyncMock(return_value=MagicMock(id=uuid4()))
mock_conv_instance.add_message = AsyncMock()
mock_conv_instance.update_conversation_timestamp = AsyncMock()
mock_conv_manager.return_value = mock_conv_instance
# Mock agent with a simulated processing time
mock_agent_instance = MagicMock()
mock_agent_instance.process_message = AsyncMock(return_value={
"response": "Task processed successfully",
"conversation_id": str(uuid4()),
"tool_calls": [],
"requires_action": False
})
mock_agent_class.return_value = mock_agent_instance
# Measure response time
start_time = time.time()
response = client.post(
"/api/test-user-123/chat",
json={
"message": "Add a simple task: Test timing"
},
headers={"Content-Type": "application/json"}
)
end_time = time.time()
response_time = end_time - start_time
# Verify response is successful and timely
assert response.status_code == 200
assert response_time < 5.0 # Should respond in under 5 seconds
@pytest.mark.asyncio
async def test_agent_user_isolation(client):
"""Test that different users' tasks are properly isolated."""
test_users = ["user-1", "user-2", "user-3"]
for user_id in test_users:
with patch('ai.endpoints.chat.ConversationManager') as mock_conv_manager, \
patch('ai.endpoints.chat.TodoAgent') as mock_agent_class:
# Mock conversation manager
mock_conv_instance = MagicMock()
mock_conv_instance.create_conversation = AsyncMock(return_value=MagicMock(id=uuid4()))
mock_conv_instance.add_message = AsyncMock()
mock_conv_instance.update_conversation_timestamp = AsyncMock()
mock_conv_manager.return_value = mock_conv_instance
# Mock agent instance
mock_agent_instance = MagicMock()
mock_agent_instance.process_message = AsyncMock(return_value={
"response": f"Processed for {user_id}",
"conversation_id": str(uuid4()),
"tool_calls": [],
"requires_action": False
})
mock_agent_class.return_value = mock_agent_instance
response = client.post(
f"/api/{user_id}/chat",
json={
"message": "Show me my tasks"
},
headers={"Content-Type": "application/json"}
)
# Verify each user gets a proper response
assert response.status_code == 200
data = response.json()
assert "response" in data
# Verify the response mentions the correct user
assert user_id in data["response"] or user_id.replace("-", "") in data["response"]
if __name__ == "__main__":
pytest.main([__file__]) |