"""Unit tests for /api/v2/flow/cell_pm/* 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.v2.flow_cell_pm import router _HTTP_200 = 200 _HTTP_422 = 422 _AGENT_ID = str(uuid4()) _TASK_ID = str(uuid4()) _HEADERS = {"X-Agent-ID": _AGENT_ID} 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_cell_pm 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_give_me_work_returns_envelope() -> None: """POST /api/v2/flow/cell_pm/give_me_work returns 200 with envelope shape.""" mock_chore = MagicMock() mock_chore.give_me_work = AsyncMock(return_value=_make_envelope(status="idle")) client = TestClient(_build_app(mock_chore)) resp = client.post( "/api/v2/flow/cell_pm/give_me_work", json={}, headers=_HEADERS, ) assert resp.status_code == _HTTP_200 body = resp.json() assert body["status"] == "idle" mock_chore.give_me_work.assert_awaited_once() @pytest.mark.asyncio async def test_triage_returns_envelope() -> None: """POST /api/v2/flow/cell_pm/triage returns 200 with task or idle status.""" mock_chore = MagicMock() mock_chore.triage = AsyncMock( return_value=_make_envelope(status="awaiting_pm_review", task_id=_TASK_ID) ) client = TestClient(_build_app(mock_chore)) resp = client.post( "/api/v2/flow/cell_pm/triage", json={}, headers=_HEADERS, ) assert resp.status_code == _HTTP_200 body = resp.json() assert body["status"] == "awaiting_pm_review" mock_chore.triage.assert_awaited_once() @pytest.mark.asyncio async def test_unblock_dispatches_task_id_with_restore_true() -> None: """POST /api/v2/flow/cell_pm/unblock forwards task_id and restore=True.""" mock_chore = MagicMock() mock_chore.unblock = AsyncMock( return_value=_make_envelope(status="in_progress", task_id=_TASK_ID) ) client = TestClient(_build_app(mock_chore)) resp = client.post( "/api/v2/flow/cell_pm/unblock", json={"task_id": _TASK_ID}, headers=_HEADERS, ) assert resp.status_code == _HTTP_200 body = resp.json() assert body["status"] == "in_progress" mock_chore.unblock.assert_awaited_once() call_kwargs = mock_chore.unblock.call_args.kwargs assert call_kwargs["restore"] is True @pytest.mark.asyncio async def test_unblock_with_restore_false() -> None: """POST /api/v2/flow/cell_pm/unblock forwards restore=False when specified.""" mock_chore = MagicMock() mock_chore.unblock = AsyncMock( return_value=_make_envelope(status="in_progress", task_id=_TASK_ID) ) client = TestClient(_build_app(mock_chore)) resp = client.post( "/api/v2/flow/cell_pm/unblock", json={"task_id": _TASK_ID, "restore": False}, headers=_HEADERS, ) assert resp.status_code == _HTTP_200 mock_chore.unblock.assert_awaited_once() call_kwargs = mock_chore.unblock.call_args.kwargs assert call_kwargs["restore"] is False @pytest.mark.asyncio async def test_complete_dispatches_task_and_notes() -> None: """POST /api/v2/flow/cell_pm/complete forwards task_id and notes.""" mock_chore = MagicMock() mock_chore.complete = AsyncMock( return_value=_make_envelope(status="completed", task_id=_TASK_ID) ) client = TestClient(_build_app(mock_chore)) resp = client.post( "/api/v2/flow/cell_pm/complete", json={"task_id": _TASK_ID, "notes": "All subtasks done, PR merged."}, headers=_HEADERS, ) assert resp.status_code == _HTTP_200 body = resp.json() assert body["status"] == "completed" mock_chore.complete.assert_awaited_once() call_args = mock_chore.complete.call_args assert str(call_args.args[1]) == _TASK_ID assert call_args.args[2] == "All subtasks done, PR merged." @pytest.mark.asyncio async def test_escalate_up_dispatches_reason() -> None: """POST /api/v2/flow/cell_pm/escalate_up forwards task_id and reason.""" mock_chore = MagicMock() mock_chore.escalate_up = AsyncMock( return_value=_make_envelope(status="awaiting_pm_review", task_id=_TASK_ID) ) client = TestClient(_build_app(mock_chore)) resp = client.post( "/api/v2/flow/cell_pm/escalate_up", json={"task_id": _TASK_ID, "reason": "Cross-cell dependency needs Main PM."}, headers=_HEADERS, ) assert resp.status_code == _HTTP_200 mock_chore.escalate_up.assert_awaited_once() call_args = mock_chore.escalate_up.call_args assert call_args.args[2] == "Cross-cell dependency needs Main PM." @pytest.mark.asyncio async def test_i_am_idle_dispatches_agent_id() -> None: """POST /api/v2/flow/cell_pm/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/v2/flow/cell_pm/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() def test_complete_rejects_empty_notes() -> None: """POST complete rejects empty notes (min_length=1).""" mock_chore = MagicMock() client = TestClient(_build_app(mock_chore)) resp = client.post( "/api/v2/flow/cell_pm/complete", json={"task_id": _TASK_ID, "notes": ""}, headers=_HEADERS, ) assert resp.status_code == _HTTP_422 def test_escalate_up_rejects_empty_reason() -> None: """POST escalate_up rejects empty reason (min_length=1).""" mock_chore = MagicMock() client = TestClient(_build_app(mock_chore)) resp = client.post( "/api/v2/flow/cell_pm/escalate_up", json={"task_id": _TASK_ID, "reason": ""}, headers=_HEADERS, ) assert resp.status_code == _HTTP_422