"""Integration tests for voice API endpoints. Uses FastAPI TestClient. Registers a test user and tests voice chat flow. """ from fastapi.testclient import TestClient from app.main import app client = TestClient(app) def _register_user() -> tuple[str, str]: import uuid suffix = uuid.uuid4().hex[:8] email = f"voice{suffix}@example.com" resp = client.post( "/api/v1/auth/register", json={"email": email, "password": "TestPass1", "display_name": "VoiceTester"}, ) assert resp.status_code == 201 data = resp.json() return data["access_token"], email class TestVoiceIntegration: def test_list_role_agents(self): resp = client.get("/api/v1/voice/agents") assert resp.status_code == 200 agents = resp.json() assert len(agents) >= 13 names = [a["name"] for a in agents] assert "Бизнес-аналитик" in names assert "Дирижёр" not in names def test_chat_requires_auth(self): resp = client.post( "/api/v1/voice/chat", json={"text": "hello"}, ) assert resp.status_code in (401, 403) def test_chat_with_auth(self): token, _ = _register_user() resp = client.post( "/api/v1/voice/chat", json={"text": "Придумай идею для стартапа"}, headers={"Authorization": f"Bearer {token}"}, ) assert resp.status_code == 200 data = resp.json() assert "response" in data assert "agent_name" in data assert "session_id" in data assert data["session_id"] != "" def test_chat_with_session(self): token, _ = _register_user() chat1 = client.post( "/api/v1/voice/chat", json={"text": "Экология"}, headers={"Authorization": f"Bearer {token}"}, ) session_id = chat1.json()["session_id"] chat2 = client.post( "/api/v1/voice/chat", json={"text": "А какие риски?", "session_id": session_id}, headers={"Authorization": f"Bearer {token}"}, ) assert chat2.status_code == 200 assert chat2.json()["session_id"] == session_id def test_sessions_list(self): token, _ = _register_user() client.post( "/api/v1/voice/chat", json={"text": "Тестовая идея"}, headers={"Authorization": f"Bearer {token}"}, ) resp = client.get( "/api/v1/voice/sessions", headers={"Authorization": f"Bearer {token}"}, ) assert resp.status_code == 200 sessions = resp.json() assert len(sessions) >= 1 def test_rate_interaction(self): token, _ = _register_user() chat = client.post( "/api/v1/voice/chat", json={"text": "Идея для оценки"}, headers={"Authorization": f"Bearer {token}"}, ) interaction_id = chat.json().get("interaction_id") if not interaction_id: return resp = client.post( "/api/v1/voice/rate", json={"interaction_id": interaction_id, "rating": 5}, headers={"Authorization": f"Bearer {token}"}, ) assert resp.status_code == 200