feat: add topic summarize endpoint

This commit is contained in:
2026-05-25 18:53:28 +02:00
parent 1b1a0eb6ff
commit 1e69b1aec2
2 changed files with 92 additions and 0 deletions
+55
View File
@@ -148,3 +148,58 @@ async def test_delete_topic(client):
resp = await client.get(f"/api/topics/{topic_id}")
assert resp.status_code == 404
@pytest.mark.asyncio
async def test_summarize_topic(client):
create_resp = await client.post(
"/api/topics",
json={"name": "summarize-test", "system_prompt": "You help."},
)
topic_id = create_resp.json()["data"]["id"]
from src.backend.db.database import get_db, create_request, complete_request
db = await get_db()
try:
req1 = await create_request(db, "Question 1", topic_id)
await complete_request(db, req1["id"], "Answer 1")
req2 = await create_request(db, "Question 2", topic_id)
await complete_request(db, req2["id"], "Answer 2")
finally:
await db.close()
from unittest.mock import patch, AsyncMock, MagicMock
mock_worker = MagicMock()
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {
"success": True,
"summary": "Comprehensive summary of all exchanges.",
}
mock_client = AsyncMock()
mock_client.post.return_value = mock_response
mock_client.__aenter__ = AsyncMock(return_value=mock_client)
mock_client.__aexit__ = AsyncMock(return_value=False)
with patch("src.backend.services.worker_client.httpx.AsyncClient", return_value=mock_client):
resp = await client.post(f"/api/topics/{topic_id}/summarize")
assert resp.status_code == 200
data = resp.json()
assert data["success"] is True
assert "summary" in data["data"]
@pytest.mark.asyncio
async def test_summarize_topic_no_requests(client):
create_resp = await client.post(
"/api/topics",
json={"name": "empty-topic", "system_prompt": "Empty."},
)
topic_id = create_resp.json()["data"]["id"]
resp = await client.post(f"/api/topics/{topic_id}/summarize")
assert resp.status_code == 400