Files
opus-agent/tests/test_api_sessions.py
Adriano Dal Pastro e54f828722 test: update tests for new session_id behavior
session_id "new" now returns null at creation (set from SDK after completion).
Session tests use custom session_ids directly.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-25 21:02:11 +00:00

75 lines
2.0 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):
# Use a custom session_id (not "new") so it's set immediately
session_id = "test-session-abc"
await client.post(
"/api/requests",
json={"prompt": "First", "topic_id": topic_id, "session_id": 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):
session_id = "test-session-delete"
await client.post(
"/api/requests",
json={"prompt": "Hello", "topic_id": topic_id, "session_id": 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