"""Unit tests for /api/v1/flow/auditor/* endpoints. Uses a minimal FastAPI test client built from the new router only. No DB required — Choreographer is mocked. """ from __future__ import annotations from unittest.mock import AsyncMock, MagicMock from uuid import uuid4 import pytest from fastapi import FastAPI from fastapi.testclient import TestClient from roboco.api.deps import get_choreographer from roboco.api.routes.v1.flow_auditor import router _HTTP_200 = 200 _AGENT_ID = str(uuid4()) _TASK_ID = str(uuid4()) _HEADERS = {"X-Agent-ID": _AGENT_ID, "X-Agent-Role": "auditor"} def _make_envelope( status: str = "ok", task_id: str | None = None, **extra: object ) -> MagicMock: """Return a mock Envelope whose as_dict() returns a predictable payload.""" env = MagicMock() payload: dict[str, object] = {"status": status, "task_id": task_id, "next": "..."} payload.update(extra) env.as_dict.return_value = payload return env def _build_app(mock_choreographer: MagicMock) -> FastAPI: """Build minimal FastAPI app with the flow_auditor router and a mocked dep.""" app = FastAPI() app.include_router(router) app.dependency_overrides[get_choreographer] = lambda: mock_choreographer return app @pytest.mark.asyncio async def test_triage_returns_envelope() -> None: """POST /api/v1/flow/auditor/triage returns 200 with envelope shape.""" mock_chore = MagicMock() mock_chore.auditor_triage = AsyncMock( return_value=_make_envelope(status="blocked", task_id=_TASK_ID) ) client = TestClient(_build_app(mock_chore)) resp = client.post( "/api/v1/flow/auditor/triage", json={}, headers=_HEADERS, ) assert resp.status_code == _HTTP_200 body = resp.json() assert body["status"] == "blocked" mock_chore.auditor_triage.assert_awaited_once() @pytest.mark.asyncio async def test_i_am_idle_returns_envelope() -> None: """POST /api/v1/flow/auditor/i_am_idle delegates to Choreographer.i_am_idle.""" mock_chore = MagicMock() mock_chore.i_am_idle = AsyncMock(return_value=_make_envelope(status="idle")) client = TestClient(_build_app(mock_chore)) resp = client.post( "/api/v1/flow/auditor/i_am_idle", json={}, headers=_HEADERS, ) assert resp.status_code == _HTTP_200 body = resp.json() assert body["status"] == "idle" mock_chore.i_am_idle.assert_awaited_once()