Files
life-echo/api/tests/evaluation/test_memoir_pipeline_run_router.py
Sully 53e0065e3e refactor(api): TOML 配置 SSOT、统一错误契约、Auth/事务加固与可观测性 (#33)
配置 SSOT(TOML + .env)
统一错误契约
Auth 与事务边界
Redis / Celery 可靠性:业务 Redis(DB/0)与 Celery broker/backend(DB/1)显式拆分;连接池、sync client
可观测性(OpenTelemetry + LGTM)
2026-05-22 13:44:50 +08:00

103 lines
3.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""GET /users/{user_id}/memoir-pipeline-run快照读取"""
import pytest
from tests.conftest import install_test_error_handlers
from httpx import ASGITransport, AsyncClient
from app.features.evaluation.internal_auth import get_internal_eval_principal
@pytest.mark.asyncio
async def test_memoir_pipeline_run_ok_by_phase1_task(
monkeypatch: pytest.MonkeyPatch,
) -> None:
from fastapi import FastAPI
monkeypatch.setattr(
"app.core.config.settings.internal_eval_api_key",
"secret",
raising=False,
)
from app.features.evaluation.router import router
def _fake_eval(user_id: str, **kwargs: object):
assert user_id == "u1"
assert kwargs.get("phase1_task_id") == "tid-z"
return {
"memoir_correlation_id": "cid-z",
"user_id": "u1",
"started_at_utc": "2026-04-09T00:00:00Z",
"phase1": {"task_id": "tid-z", "status": "running", "step": "started"},
"phase2": [],
"fanout": {
"story_images": [],
"recompose_chapters": [],
"memory_enrichment": [],
"quality_pass": None,
"compaction": None,
},
}
monkeypatch.setattr(
"app.features.evaluation.router.get_pipeline_run_for_eval",
_fake_eval,
)
app = install_test_error_handlers(FastAPI())
app.include_router(router, prefix="/internal/api/evaluation")
async def _override_auth():
from app.features.evaluation.internal_auth import InternalEvalPrincipal
return InternalEvalPrincipal()
app.dependency_overrides[get_internal_eval_principal] = _override_auth
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://t") as client:
r = await client.get(
"/internal/api/evaluation/users/u1/memoir-pipeline-run",
headers={"X-Internal-Eval-Key": "secret"},
params={"phase1_task_id": "tid-z"},
)
assert r.status_code == 200
body = r.json()
assert body["memoir_correlation_id"] == "cid-z"
assert body["phase1"]["task_id"] == "tid-z"
@pytest.mark.asyncio
async def test_memoir_pipeline_run_400_both_ids(
monkeypatch: pytest.MonkeyPatch,
) -> None:
from fastapi import FastAPI
monkeypatch.setattr(
"app.core.config.settings.internal_eval_api_key",
"secret",
raising=False,
)
from app.features.evaluation.router import router
app = install_test_error_handlers(FastAPI())
app.include_router(router, prefix="/internal/api/evaluation")
async def _override_auth():
from app.features.evaluation.internal_auth import InternalEvalPrincipal
return InternalEvalPrincipal()
app.dependency_overrides[get_internal_eval_principal] = _override_auth
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://t") as client:
r = await client.get(
"/internal/api/evaluation/users/u1/memoir-pipeline-run",
headers={"X-Internal-Eval-Key": "secret"},
params={
"phase1_task_id": "a",
"memoir_correlation_id": "b",
},
)
assert r.status_code == 400