import pytest from fastapi.testclient import TestClient from app.main import create_app @pytest.fixture() def client() -> TestClient: return TestClient(create_app()) def test_healthz_ok(client: TestClient) -> None: resp = client.get("/healthz") assert resp.status_code == 200 body = resp.json() assert body["status"] == "ok" assert "env" in body def test_validation_error_uses_contract_envelope(client: TestClient) -> None: # 触发路径参数/请求体校验失败,验证统一错误体结构 resp = client.post("/healthz", json={"unexpected": 1}) assert resp.status_code in (405, 422) if resp.status_code == 422: err = resp.json()["error"] assert err["code"] == "validation_failed" assert err["details"]["field_errors"]