42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
"""错误响应契约测试。"""
|
|
|
|
from collections.abc import AsyncGenerator
|
|
|
|
import pytest
|
|
from httpx import ASGITransport, AsyncClient
|
|
|
|
from app.main import app
|
|
|
|
|
|
@pytest.fixture
|
|
async def client(auth_headers: dict[str, str]) -> AsyncGenerator[AsyncClient, None]:
|
|
transport = ASGITransport(app=app)
|
|
async with AsyncClient(
|
|
transport=transport,
|
|
base_url="http://test",
|
|
headers=auth_headers,
|
|
) as http_client:
|
|
yield http_client
|
|
|
|
|
|
def assert_error(response, code: str) -> None: # type: ignore[no-untyped-def]
|
|
body = response.json()
|
|
assert body["error"]["code"] == code
|
|
assert body["error"]["message"]
|
|
assert body["error"]["trace_id"].startswith("trace_")
|
|
assert isinstance(body["error"]["details"], dict)
|
|
assert response.headers["X-Trace-Id"]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_error_contracts(client: AsyncClient) -> None:
|
|
missing_run = await client.get("/api/v1/runs/run_missing")
|
|
assert missing_run.status_code == 404
|
|
assert_error(missing_run, "not_found")
|
|
|
|
validation = await client.post("/api/v1/runs", json={"prompt": ""})
|
|
assert validation.status_code == 422
|
|
assert_error(validation, "validation_failed")
|
|
|
|
# 旧 chat 会话错误契约随线性会话模型退役;415/413 契约由镜树/媒体域随需要重建
|