|
| 1 | +"""Tests for the simple chat API endpoint.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 6 | + |
| 7 | +import pytest |
| 8 | +from fastapi.testclient import TestClient |
| 9 | + |
| 10 | +# Mock Azure dependencies to avoid import errors during tests |
| 11 | +sys.modules["azure.monitor"] = MagicMock() |
| 12 | +sys.modules["azure.monitor.events.extension"] = MagicMock() |
| 13 | +sys.modules["azure.monitor.opentelemetry"] = MagicMock() |
| 14 | + |
| 15 | +# Set required environment variables before importing the app |
| 16 | +os.environ["COSMOSDB_ENDPOINT"] = "https://mock-endpoint" |
| 17 | +os.environ["COSMOSDB_KEY"] = "mock-key" |
| 18 | +os.environ["COSMOSDB_DATABASE"] = "mock-database" |
| 19 | +os.environ["COSMOSDB_CONTAINER"] = "mock-container" |
| 20 | +os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] = ( |
| 21 | + "InstrumentationKey=mock;IngestionEndpoint=https://mock" |
| 22 | +) |
| 23 | +os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"] = "mock-deployment-name" |
| 24 | +os.environ["AZURE_OPENAI_API_VERSION"] = "2023-01-01" |
| 25 | +os.environ["AZURE_OPENAI_ENDPOINT"] = "https://mock-openai-endpoint" |
| 26 | +os.environ["FRONTEND_SITE_NAME"] = "http://localhost" |
| 27 | + |
| 28 | +with patch("azure.monitor.opentelemetry.configure_azure_monitor", MagicMock()): |
| 29 | + from src.backend.app_kernel import app |
| 30 | + |
| 31 | + |
| 32 | +client = TestClient(app) |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture(autouse=True) |
| 36 | +def mock_dependencies(monkeypatch): |
| 37 | + """Mock dependencies used by the chat endpoint.""" |
| 38 | + |
| 39 | + # Mock authentication helper |
| 40 | + monkeypatch.setattr( |
| 41 | + "src.backend.app_kernel.get_authenticated_user_details", |
| 42 | + lambda request_headers: {"user_principal_id": "mock-user"}, |
| 43 | + ) |
| 44 | + |
| 45 | + # Mock runtime initialization |
| 46 | + async def mock_initialize_runtime_and_context(session_id, user_id): |
| 47 | + return None, MagicMock() |
| 48 | + |
| 49 | + monkeypatch.setattr( |
| 50 | + "src.backend.app_kernel.initialize_runtime_and_context", |
| 51 | + mock_initialize_runtime_and_context, |
| 52 | + ) |
| 53 | + |
| 54 | + # Mock config client creation |
| 55 | + monkeypatch.setattr( |
| 56 | + "src.backend.app_kernel.config.get_ai_project_client", |
| 57 | + lambda: MagicMock(close=MagicMock()), |
| 58 | + ) |
| 59 | + |
| 60 | + # Mock AgentFactory.create_agent to return an agent with handle_user_message |
| 61 | + async def mock_create_agent(*args, **kwargs): |
| 62 | + agent = MagicMock() |
| 63 | + agent.handle_user_message = AsyncMock(return_value="mock response") |
| 64 | + return agent |
| 65 | + |
| 66 | + monkeypatch.setattr( |
| 67 | + "src.backend.app_kernel.AgentFactory.create_agent", |
| 68 | + mock_create_agent, |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +def test_chat_endpoint_returns_reply(): |
| 73 | + """Ensure the chat endpoint returns the agent's reply.""" |
| 74 | + |
| 75 | + payload = {"session_id": "123", "user_message": "Hello"} |
| 76 | + |
| 77 | + response = client.post("/api/chat", json=payload) |
| 78 | + |
| 79 | + assert response.status_code == 200 |
| 80 | + assert response.json() == {"reply": "mock response"} |
| 81 | + |
0 commit comments