import asyncio import json from pathlib import Path import httpx from fastapi.testclient import TestClient from sqlalchemy import select from ai_platform.infrastructure.persistence.models import ScoringRun from ai_platform.presentation.http.schemas import RunRequest FIXTURE = Path(__file__).parent / "fixtures" / "mock_omni_run.json" def test_api_seed_persists_and_reads_back_conversation_snapshot( aihub_test_environment, wait_for_terminal ): payload = json.loads(FIXTURE.read_text()) expected = RunRequest.model_validate(payload).model_dump(mode="json") app = aihub_test_environment["app_for"](lambda _request: httpx.Response(500)) headers = { "Authorization": f"Bearer {aihub_test_environment['app_key']}", "Idempotency-Key": payload["request_id"], } with TestClient(app) as client: admitted = client.post("/v1/lead-scoring/runs", headers=headers, json=payload) assert admitted.status_code == 202 run_id = admitted.json()["run_id"] completed = wait_for_terminal(client, run_id, headers) assert completed["status"] == "completed" assert completed["ai_score"] is None and completed["degraded"] is False assert completed["rule_score"] == completed["final_score"] == 80 assert completed["snapshot"] == expected["snapshot"] assert client.get(f"/v1/lead-scoring/runs/{run_id}", headers=headers).json() == completed async def verify(): factory = aihub_test_environment["factory"] async with factory() as db: row = await db.scalar( select(ScoringRun).where( ScoringRun.id == run_id, ScoringRun.app_id == aihub_test_environment["app_id"] ) ) assert row is not None and row.account_id == payload["account_id"] assert row.request["snapshot"] == expected["snapshot"] await factory.kw["bind"].dispose() asyncio.run(verify())