Files
roboco/tests/unit/api/routes/v2/test_flow_cell_pm.py
T
Renn F ed828a719b feat(gateway): substantive-plan gate — approach >=150 + real sub_task descriptions (#171)
Plans were vague because the gate accepted the bare minimum: PM
approach >=20 chars and title-only sub_tasks. minimax wrote exactly
the minimum.

- IWillPlanRequest.approach min_length 20 -> 150 (kept in sync with
  _PM_APPROACH_MIN_LEN; the gate enforces it at the choreographer
  layer too so direct/MCP callers can't bypass the HTTP boundary).
- New _thin_subtask_hint: every PM sub_task must have a title and a
  description >= _PM_SUBTASK_DESC_MIN_LEN (60) saying what the step
  does — each sub_task is both a delegate target AND a
  progress-checklist item, so a title alone is not a plan.
- cell_pm/main_pm role prompts: explicit "the gate REJECTS thin plans"
  framing + concrete sub_task example + the new minimums.
- Updated all affected test fixtures across the suite to use
  substantive approaches/descriptions; added thin-sub_task rejection
  coverage.

Commit 1 of 3 for the plan/progress quality work (#171/#172/#173).
2026-05-16 10:14:45 +02:00

360 lines
11 KiB
Python

"""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, "X-Agent-Role": "cell_pm"}
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.
Cell PM's give_me_work routes to ``pm_give_me_work`` so the response
surfaces non-pending PM tasks (paused, awaiting_pm_review) too.
"""
mock_chore = MagicMock()
mock_chore.pm_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.pm_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
@pytest.mark.asyncio
async def test_i_will_plan_dispatches_to_choreographer() -> None:
"""POST /api/v2/flow/cell_pm/i_will_plan forwards task_id and plan."""
mock_chore = MagicMock()
mock_chore.i_will_plan = 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/i_will_plan",
json={
"task_id": _TASK_ID,
"plan": "break into 3 subtasks for backend",
"approach": (
"Decompose into a backend API slice that be-dev-1 owns end "
"to end; QA reviews after the PR opens, documentation follows, "
"then be-pm completes and submits up. Single-cell — no "
"frontend or ux work; strict sequencing with no cross-cell "
"dependencies for this planning task."
),
"sub_tasks": [
{
"title": "Backend API slice",
"description": (
"be-dev-1 implements the endpoint with tests, commits "
"with the task-id prefix, opens the leaf PR for QA."
),
}
],
},
headers=_HEADERS,
)
assert resp.status_code == _HTTP_200
mock_chore.i_will_plan.assert_awaited_once()
@pytest.mark.asyncio
async def test_delegate_dispatches_inputs_bundle() -> None:
"""POST /api/v2/flow/cell_pm/delegate forwards body via DelegateInputs."""
mock_chore = MagicMock()
mock_chore.delegate = AsyncMock(
return_value=_make_envelope(status="created", task_id=_TASK_ID)
)
client = TestClient(_build_app(mock_chore))
resp = client.post(
"/api/v2/flow/cell_pm/delegate",
json={
"parent_task_id": _TASK_ID,
"title": "Implement /v1/foo",
"description": "Add the foo endpoint with passing tests.",
"assigned_to": "be-dev-1",
"team": "backend",
"task_type": "code",
"nature": "technical",
"estimated_complexity": "medium",
"acceptance_criteria": ["GET /v1/foo returns 200 with body"],
},
headers=_HEADERS,
)
assert resp.status_code == _HTTP_200
mock_chore.delegate.assert_awaited_once()
inputs = mock_chore.delegate.await_args.args[2]
assert inputs.task_type == "code"
@pytest.mark.asyncio
async def test_submit_up_dispatches_notes() -> None:
"""POST /api/v2/flow/cell_pm/submit_up forwards task_id and notes."""
mock_chore = MagicMock()
mock_chore.submit_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/submit_up",
json={
"task_id": _TASK_ID,
"notes": "cell finished all subtasks, ready for main pm review",
},
headers=_HEADERS,
)
assert resp.status_code == _HTTP_200
mock_chore.submit_up.assert_awaited_once()
def test_submit_up_rejects_empty_notes() -> None:
"""POST /api/v2/flow/cell_pm/submit_up rejects empty notes."""
mock_chore = MagicMock()
client = TestClient(_build_app(mock_chore))
resp = client.post(
"/api/v2/flow/cell_pm/submit_up",
json={"task_id": _TASK_ID, "notes": ""},
headers=_HEADERS,
)
assert resp.status_code == _HTTP_422
@pytest.mark.asyncio
async def test_unclaim_dispatches() -> None:
mock_chore = MagicMock()
mock_chore.unclaim = 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/unclaim",
json={"task_id": _TASK_ID},
headers=_HEADERS,
)
assert resp.status_code == _HTTP_200
mock_chore.unclaim.assert_awaited_once()
@pytest.mark.asyncio
async def test_resume_dispatches() -> None:
mock_chore = MagicMock()
mock_chore.resume = 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/resume",
json={"task_id": _TASK_ID},
headers=_HEADERS,
)
assert resp.status_code == _HTTP_200
mock_chore.resume.assert_awaited_once()