Files
FabledScribe/tests/test_mcp_tool_milestones.py
bvandeusen 1f6c592226
CI & Build / Python lint (push) Successful in 3s
CI & Build / TypeScript typecheck (push) Successful in 22s
CI & Build / Python tests (push) Successful in 42s
CI & Build / Build & push image (push) Successful in 59s
feat(plans): milestone-as-plan-container; retire kind=plan (T3)
The milestone becomes the plan container: a new nullable milestones.body
holds the design/intent (Goal/Approach/Verification) and individual steps
live as first-class child tasks (milestone_id) instead of checkboxes crammed
into one kind=plan task body. start_planning now creates a MILESTONE seeded
with the body template (not a kind=plan task) and returns it with applicable
rules; a new get_milestone MCP tool reads the plan back (body + steps + rules).

kind=plan is hard-retired going forward — start_planning never creates one.
The 'plan' task_kind enum value stays valid so the 11 historical plan-tasks
remain readable in place; no body-shredding backfill (corpus review showed
auto-splitting their checklists into tasks would be lossy: embedded code
blocks, a non-binary [~] state, tables, ID-encoded hierarchy).

- migration 0066: add milestones.body
- model/service/route/MCP: body passthrough on create+update; get_milestone
- server _INSTRUCTIONS: "plan" = milestone w/ body + child step-tasks
- UI: ProjectView shows/edits a milestone's plan body; start_planning expands
  the new milestone and opens its plan editor
- tests updated to the milestone contract + new body/get_milestone coverage

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-14 12:22:22 -04:00

157 lines
6.0 KiB
Python

"""Tests for fable_*_milestone tools."""
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from scribe.mcp._context import _user_id_ctx
from scribe.mcp.tools.milestones import (
list_milestones, get_milestone, create_milestone, update_milestone,
)
@pytest.fixture(autouse=True)
def _bind_user():
token = _user_id_ctx.set(7)
yield
_user_id_ctx.reset(token)
def _fake_ms(**overrides) -> MagicMock:
m = MagicMock()
base = {"id": 1, "project_id": 1, "title": "MS", "description": None,
"status": "active", "order_index": 0}
base.update(overrides)
m.to_dict.return_value = base
return m
@pytest.mark.asyncio
async def test_list_milestones_returns_dict_with_progress():
rows = [{"id": 1, "title": "MS1", "status": "active", "task_count": 2}]
with patch(
"scribe.mcp.tools.milestones.milestones_svc.get_project_milestone_summary",
AsyncMock(return_value=rows),
):
out = await list_milestones(project_id=1)
assert out["milestones"] == rows
@pytest.mark.asyncio
async def test_create_milestone_passes_through():
m = _fake_ms(id=5)
mock = AsyncMock(return_value=m)
with patch("scribe.mcp.tools.milestones.milestones_svc.create_milestone", mock):
out = await create_milestone(project_id=1, title="new", description="d")
assert out["id"] == 5
assert mock.call_args.kwargs["project_id"] == 1
assert mock.call_args.kwargs["title"] == "new"
assert mock.call_args.kwargs["description"] == "d"
@pytest.mark.asyncio
async def test_create_milestone_empty_description_becomes_none():
m = _fake_ms()
mock = AsyncMock(return_value=m)
with patch("scribe.mcp.tools.milestones.milestones_svc.create_milestone", mock):
await create_milestone(project_id=1, title="t", description="")
assert mock.call_args.kwargs["description"] is None
@pytest.mark.asyncio
async def test_create_milestone_passes_body_through():
"""The milestone-as-plan body is forwarded to the service."""
m = _fake_ms(id=5)
mock = AsyncMock(return_value=m)
with patch("scribe.mcp.tools.milestones.milestones_svc.create_milestone", mock):
await create_milestone(project_id=1, title="t", body="## Goal\n\nship")
assert mock.call_args.kwargs["body"] == "## Goal\n\nship"
@pytest.mark.asyncio
async def test_create_milestone_empty_body_becomes_none():
m = _fake_ms()
mock = AsyncMock(return_value=m)
with patch("scribe.mcp.tools.milestones.milestones_svc.create_milestone", mock):
await create_milestone(project_id=1, title="t", body="")
assert mock.call_args.kwargs["body"] is None
@pytest.mark.asyncio
async def test_update_milestone_sends_body():
m = _fake_ms()
mock = AsyncMock(return_value=m)
with patch("scribe.mcp.tools.milestones.milestones_svc.update_milestone", mock):
await update_milestone(project_id=1, milestone_id=5, body="new plan")
assert mock.call_args.kwargs == {"body": "new plan"}
@pytest.mark.asyncio
async def test_get_milestone_returns_body_steps_and_rules():
m = _fake_ms(id=5, project_id=3, body="## Goal")
step = MagicMock()
step.to_dict.return_value = {"id": 9, "title": "step 1", "status": "todo"}
applicable = {"rules": [{"id": 1, "title": "r"}], "truncated": False,
"subscribed_rulebooks": [{"id": 2, "title": "rb"}]}
with patch("scribe.mcp.tools.milestones.milestones_svc.get_milestone",
AsyncMock(return_value=m)), \
patch("scribe.mcp.tools.milestones.milestones_svc.get_milestone_progress",
AsyncMock(return_value={"total": 1, "completed": 0, "pct": 0.0})), \
patch("scribe.mcp.tools.milestones.notes_svc.list_notes",
AsyncMock(return_value=([step], 1))), \
patch("scribe.mcp.tools.milestones.rulebooks_svc.get_applicable_rules",
AsyncMock(return_value=applicable)):
out = await get_milestone(milestone_id=5)
assert out["milestone"]["body"] == "## Goal"
assert out["milestone"]["total"] == 1
assert out["steps"] == [{"id": 9, "title": "step 1", "status": "todo"}]
assert out["applicable_rules"] == [{"id": 1, "title": "r"}]
@pytest.mark.asyncio
async def test_get_milestone_raises_when_not_found():
with patch("scribe.mcp.tools.milestones.milestones_svc.get_milestone",
AsyncMock(return_value=None)):
with pytest.raises(ValueError, match="milestone 999 not found"):
await get_milestone(milestone_id=999)
@pytest.mark.asyncio
async def test_update_milestone_only_sends_non_default_fields():
m = _fake_ms()
mock = AsyncMock(return_value=m)
with patch("scribe.mcp.tools.milestones.milestones_svc.update_milestone", mock):
await update_milestone(project_id=1, milestone_id=5, status="done")
args, kwargs = mock.call_args
assert args == (7, 5)
assert kwargs == {"status": "done"}
@pytest.mark.asyncio
async def test_update_milestone_order_index_negative_is_omitted():
"""order_index=-1 sentinel means leave unchanged."""
m = _fake_ms()
mock = AsyncMock(return_value=m)
with patch("scribe.mcp.tools.milestones.milestones_svc.update_milestone", mock):
await update_milestone(project_id=1, milestone_id=5, order_index=-1)
assert "order_index" not in mock.call_args.kwargs
@pytest.mark.asyncio
async def test_update_milestone_order_index_zero_is_explicit():
"""order_index=0 is a real value (top of list), not a sentinel."""
m = _fake_ms()
mock = AsyncMock(return_value=m)
with patch("scribe.mcp.tools.milestones.milestones_svc.update_milestone", mock):
await update_milestone(project_id=1, milestone_id=5, order_index=0)
assert mock.call_args.kwargs["order_index"] == 0
@pytest.mark.asyncio
async def test_update_milestone_raises_when_not_found():
with patch(
"scribe.mcp.tools.milestones.milestones_svc.update_milestone",
AsyncMock(return_value=None),
):
with pytest.raises(ValueError, match="milestone 999 not found"):
await update_milestone(project_id=1, milestone_id=999, title="x")