75 lines
1.9 KiB
Python
75 lines
1.9 KiB
Python
import pytest
|
|
from httpx import AsyncClient, ASGITransport
|
|
|
|
|
|
@pytest.fixture
|
|
def app():
|
|
from src.backend.main import app
|
|
return app
|
|
|
|
|
|
@pytest.fixture
|
|
async def client(app):
|
|
transport = ASGITransport(app=app)
|
|
async with AsyncClient(
|
|
transport=transport,
|
|
base_url="http://test",
|
|
headers={"X-Api-Key": "test-key-12345"},
|
|
) as ac:
|
|
yield ac
|
|
|
|
|
|
@pytest.fixture
|
|
async def topic_id(client):
|
|
resp = await client.post(
|
|
"/api/topics",
|
|
json={"name": "sessions-test", "system_prompt": "Test prompt."},
|
|
)
|
|
return resp.json()["data"]["id"]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_sessions_empty(client):
|
|
resp = await client.get("/api/sessions")
|
|
assert resp.status_code == 200
|
|
assert resp.json()["data"] == []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_sessions_with_data(client, topic_id):
|
|
resp1 = await client.post(
|
|
"/api/requests",
|
|
json={"prompt": "First", "topic_id": topic_id, "session_id": "new"},
|
|
)
|
|
session_id = resp1.json()["data"]["session_id"]
|
|
|
|
await client.post(
|
|
"/api/requests",
|
|
json={"prompt": "Second", "topic_id": topic_id, "session_id": session_id},
|
|
)
|
|
|
|
resp = await client.get("/api/sessions")
|
|
assert resp.status_code == 200
|
|
sessions = resp.json()["data"]
|
|
assert len(sessions) == 1
|
|
assert sessions[0]["session_id"] == session_id
|
|
assert sessions[0]["request_count"] == 2
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_delete_session(client, topic_id):
|
|
resp1 = await client.post(
|
|
"/api/requests",
|
|
json={"prompt": "Hello", "topic_id": topic_id, "session_id": "new"},
|
|
)
|
|
session_id = resp1.json()["data"]["session_id"]
|
|
|
|
resp = await client.delete(f"/api/sessions/{session_id}")
|
|
assert resp.status_code == 200
|
|
|
|
resp = await client.post(
|
|
"/api/requests",
|
|
json={"prompt": "Again", "topic_id": topic_id, "session_id": session_id},
|
|
)
|
|
assert resp.status_code == 410
|