import pytest from httpx import AsyncClient pytestmark = pytest.mark.asyncio @pytest.fixture async def test_session(client: AsyncClient, auth_headers: dict, test_tree: dict): """Create a test session from the test tree.""" response = await client.post( "/api/v1/sessions", json={"tree_id": test_tree["id"]}, headers=auth_headers, ) assert response.status_code == 201, f"Failed to create session: {response.text}" return response.json() @pytest.fixture async def completed_session(client: AsyncClient, auth_headers: dict, test_session: dict): """Complete a session so it can be rated.""" response = await client.post( f"/api/v1/sessions/{test_session['id']}/complete", json={"outcome": "resolved", "outcome_notes": "Test resolved"}, headers=auth_headers, ) assert response.status_code == 200, f"Failed to complete session: {response.text}" return response.json() async def test_rate_session_success(client: AsyncClient, auth_headers: dict, completed_session: dict): """Rate a completed session with CSAT score.""" response = await client.post( f"/api/v1/sessions/{completed_session['id']}/rate", json={"rating": 4, "comment": "Very helpful flow"}, headers=auth_headers, ) assert response.status_code == 201 data = response.json() assert data["rating"] == 4 assert data["comment"] == "Very helpful flow" async def test_rate_session_no_comment(client: AsyncClient, auth_headers: dict, completed_session: dict): """Rate without a comment.""" response = await client.post( f"/api/v1/sessions/{completed_session['id']}/rate", json={"rating": 5}, headers=auth_headers, ) assert response.status_code == 201 data = response.json() assert data["rating"] == 5 assert data["comment"] is None async def test_rate_session_duplicate(client: AsyncClient, auth_headers: dict, completed_session: dict): """Cannot rate same session twice.""" await client.post( f"/api/v1/sessions/{completed_session['id']}/rate", json={"rating": 4}, headers=auth_headers, ) response = await client.post( f"/api/v1/sessions/{completed_session['id']}/rate", json={"rating": 5}, headers=auth_headers, ) assert response.status_code == 409 async def test_rate_session_invalid_rating(client: AsyncClient, auth_headers: dict, completed_session: dict): """Rating must be 1-5.""" response = await client.post( f"/api/v1/sessions/{completed_session['id']}/rate", json={"rating": 6}, headers=auth_headers, ) assert response.status_code == 422 async def test_rate_incomplete_session(client: AsyncClient, auth_headers: dict, test_session: dict): """Cannot rate a session that hasn't been completed.""" response = await client.post( f"/api/v1/sessions/{test_session['id']}/rate", json={"rating": 4}, headers=auth_headers, ) assert response.status_code == 400