Files
life-echo/api/tests/evaluation/test_memoir_pipeline_run_router.py

103 lines
3.2 KiB
Python
Raw Normal View History

"""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