|
| 1 | +import pytest |
| 2 | +from fastapi.testclient import TestClient |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | +from langgraph.checkpoint.memory import MemorySaver |
| 5 | + |
| 6 | +import sys |
| 7 | +import os |
| 8 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) |
| 9 | + |
| 10 | +# Mock the cache before importing the app to avoid Redis connection |
| 11 | +with patch('src.core.cache.init_llm_cache'): |
| 12 | + from src.app import app |
| 13 | + from src.core.api.chat import init_chat_dependencies, threads_store, threads_list |
| 14 | + |
| 15 | + |
| 16 | +# Mock agent for testing |
| 17 | +class MockAgent: |
| 18 | + async def astream_events(self, *args, **kwargs): |
| 19 | + yield { |
| 20 | + "event": "on_chat_model_stream", |
| 21 | + "data": {"chunk": MagicMock(content="Test response")}, |
| 22 | + "metadata": {"langgraph_node": "model"} |
| 23 | + } |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture(scope="function") |
| 27 | +def client(): |
| 28 | + mock_agent = MockAgent() |
| 29 | + mock_checkpointer = MemorySaver() |
| 30 | + |
| 31 | + init_chat_dependencies(mock_agent, mock_checkpointer) |
| 32 | + |
| 33 | + # Create test client |
| 34 | + with TestClient(app) as c: |
| 35 | + yield c |
| 36 | + |
| 37 | + |
| 38 | +def test_status_endpoint(client): |
| 39 | + """Test the status endpoint""" |
| 40 | + response = client.get("/status") |
| 41 | + assert response.status_code == 200 |
| 42 | + assert response.json() == {"status": "connected"} |
| 43 | + |
| 44 | + |
| 45 | +def test_chat_endpoint_success(client): |
| 46 | + """Test the chat endpoint with valid input""" |
| 47 | + # Clear any existing threads for clean test |
| 48 | + threads_store.clear() |
| 49 | + threads_list.clear() |
| 50 | + |
| 51 | + payload = { |
| 52 | + "message": "Hello, how are you?", |
| 53 | + "thread_id": "test-thread-123" |
| 54 | + } |
| 55 | + |
| 56 | + response = client.post("/chat", json=payload) |
| 57 | + assert response.status_code == 200 |
| 58 | + assert "text/plain" in response.headers["content-type"] |
| 59 | + |
| 60 | + # Check if thread was created |
| 61 | + assert "test-thread-123" in threads_store |
| 62 | + |
| 63 | + |
| 64 | +def test_create_thread(client): |
| 65 | + """Test creating a new thread""" |
| 66 | + payload = { |
| 67 | + "title": "Test Thread" |
| 68 | + } |
| 69 | + |
| 70 | + response = client.post("/threads", json=payload) |
| 71 | + assert response.status_code == 200 |
| 72 | + |
| 73 | + data = response.json() |
| 74 | + assert "id" in data |
| 75 | + assert data["title"] == "Test Thread" |
| 76 | + assert "created_at" in data |
| 77 | + assert "updated_at" in data |
| 78 | + assert data["message_count"] == 0 |
| 79 | + |
| 80 | + # Verify thread exists in store |
| 81 | + assert data["id"] in threads_store |
| 82 | + |
| 83 | + |
| 84 | +def test_get_thread(client): |
| 85 | + """Test getting a specific thread""" |
| 86 | + # First create a thread |
| 87 | + payload = {"title": "Get Thread Test"} |
| 88 | + create_response = client.post("/threads", json=payload) |
| 89 | + thread_data = create_response.json() |
| 90 | + thread_id = thread_data["id"] |
| 91 | + |
| 92 | + assert create_response.status_code == 200 |
| 93 | + |
| 94 | + # Get the thread |
| 95 | + response = client.get(f"/threads/{thread_id}") |
| 96 | + assert response.status_code == 200 |
| 97 | + |
| 98 | + data = response.json() |
| 99 | + assert data["id"] == thread_id |
| 100 | + assert data["title"] == "Get Thread Test" |
| 101 | + |
| 102 | + |
| 103 | +def test_get_nonexistent_thread(client): |
| 104 | + """Test getting a non-existent thread""" |
| 105 | + response = client.get("/threads/nonexistent-thread-id") |
| 106 | + assert response.status_code == 404 |
| 107 | + assert "Thread not found" in response.text |
| 108 | + |
| 109 | + |
| 110 | +def test_list_threads(client): |
| 111 | + """Test listing threads""" |
| 112 | + # Clear existing threads |
| 113 | + threads_store.clear() |
| 114 | + threads_list.clear() |
| 115 | + |
| 116 | + # Create a few threads |
| 117 | + for i in range(3): |
| 118 | + payload = {"title": f"Test Thread {i}"} |
| 119 | + client.post("/threads", json=payload) |
| 120 | + |
| 121 | + response = client.get("/threads") |
| 122 | + assert response.status_code == 200 |
| 123 | + |
| 124 | + data = response.json() |
| 125 | + assert len(data) <= 3 # Should have at most 3 threads |
| 126 | + |
| 127 | + titles = [t["title"] for t in data] |
| 128 | + for i in range(min(3, len(data))): |
| 129 | + assert f"Test Thread {i}" in titles |
| 130 | + |
| 131 | + |
| 132 | +def test_delete_thread(client): |
| 133 | + """Test deleting a thread""" |
| 134 | + # Create a thread |
| 135 | + payload = {"title": "Delete Test Thread"} |
| 136 | + create_response = client.post("/threads", json=payload) |
| 137 | + thread_data = create_response.json() |
| 138 | + thread_id = thread_data["id"] |
| 139 | + |
| 140 | + assert create_response.status_code == 200 |
| 141 | + assert thread_id in threads_store |
| 142 | + |
| 143 | + # Delete the thread |
| 144 | + response = client.delete(f"/threads/{thread_id}") |
| 145 | + assert response.status_code == 200 |
| 146 | + assert response.json() == {"success": True} |
| 147 | + |
| 148 | + # Verify thread is deleted |
| 149 | + assert thread_id not in threads_store |
| 150 | + assert thread_id not in threads_list |
| 151 | + |
| 152 | + |
| 153 | +def test_delete_nonexistent_thread(client): |
| 154 | + """Test deleting a non-existent thread""" |
| 155 | + response = client.delete("/threads/nonexistent-thread-id") |
| 156 | + assert response.status_code == 404 |
| 157 | + assert "Thread not found" in response.text |
| 158 | + |
| 159 | + |
| 160 | +def test_get_thread_messages(client): |
| 161 | + """Test getting messages from a thread""" |
| 162 | + # Create a thread |
| 163 | + payload = {"title": "Messages Test Thread"} |
| 164 | + create_response = client.post("/threads", json=payload) |
| 165 | + thread_data = create_response.json() |
| 166 | + thread_id = thread_data["id"] |
| 167 | + |
| 168 | + assert create_response.status_code == 200 |
| 169 | + |
| 170 | + # Get messages from the thread (should be empty initially) |
| 171 | + response = client.get(f"/threads/{thread_id}/messages") |
| 172 | + assert response.status_code == 200 |
| 173 | + assert response.json() == [] |
| 174 | + |
0 commit comments