feat(api): Enhance persistence logic and refactor codebase structure

This commit is contained in:
Yunxiao Xu
2026-02-11 15:33:56 -08:00
parent 371582dcd1
commit 85329cffda
7 changed files with 244 additions and 95 deletions

View File

@@ -25,7 +25,7 @@ def test_stream_agent_unauthorized():
assert response.status_code == 401
@pytest.mark.asyncio
async def test_stream_agent_success(auth_header):
async def test_stream_agent_success(auth_header, mock_user):
"""Test successful agent streaming with SSE."""
# We need to mock the LangGraph app.astream_events
mock_events = [
@@ -39,10 +39,18 @@ async def test_stream_agent_success(auth_header):
yield event
with patch("ea_chatbot.api.routers.agent.app.astream_events", side_effect=mock_astream_events), \
patch("ea_chatbot.api.routers.agent.get_checkpointer") as mock_cp:
patch("ea_chatbot.api.routers.agent.get_checkpointer") as mock_cp, \
patch("ea_chatbot.api.routers.agent.history_manager") as mock_hm:
mock_cp.return_value.__aenter__.return_value = AsyncMock()
# Mock session and DB objects
mock_session = MagicMock()
mock_hm.get_session.return_value.__enter__.return_value = mock_session
from ea_chatbot.history.models import Conversation
mock_conv = Conversation(id="t1", user_id=mock_user.id)
mock_session.get.return_value = mock_conv
# Using TestClient with a stream context
with client.stream("POST", "/chat/stream",
json={"message": "hello", "thread_id": "t1"},