feat(rulebook): MCP tools — 16 tools for rulebook/topic/rule/subscription
This commit is contained in:
@@ -0,0 +1,185 @@
|
||||
"""Tests for MCP rulebook tools — patches the service layer."""
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from fabledassistant.mcp._context import _user_id_ctx
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _bind_user():
|
||||
token = _user_id_ctx.set(7)
|
||||
yield
|
||||
_user_id_ctx.reset(token)
|
||||
|
||||
|
||||
def _fake_rulebook(id=1, title="t"):
|
||||
rb = MagicMock()
|
||||
rb.id = id
|
||||
rb.title = title
|
||||
rb.to_dict.return_value = {"id": id, "title": title}
|
||||
return rb
|
||||
|
||||
|
||||
def _fake_topic(id=10, title="git"):
|
||||
t = MagicMock()
|
||||
t.id = id
|
||||
t.title = title
|
||||
t.to_dict.return_value = {"id": id, "title": title}
|
||||
return t
|
||||
|
||||
|
||||
def _fake_rule(id=100, title="r", statement="s"):
|
||||
r = MagicMock()
|
||||
r.id = id
|
||||
r.title = title
|
||||
r.topic_id = 10
|
||||
r.statement = statement
|
||||
r.to_dict.return_value = {"id": id, "title": title, "statement": statement}
|
||||
return r
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_rulebooks_wraps_in_dict():
|
||||
rows = [_fake_rulebook(id=1), _fake_rulebook(id=2)]
|
||||
with patch(
|
||||
"fabledassistant.mcp.tools.rulebooks.rulebooks_svc.list_rulebooks",
|
||||
AsyncMock(return_value=rows),
|
||||
):
|
||||
from fabledassistant.mcp.tools.rulebooks import list_rulebooks
|
||||
out = await list_rulebooks()
|
||||
assert len(out["rulebooks"]) == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_rulebook_includes_topics():
|
||||
rb = _fake_rulebook(id=1)
|
||||
topics = [_fake_topic(id=10), _fake_topic(id=11)]
|
||||
with patch(
|
||||
"fabledassistant.mcp.tools.rulebooks.rulebooks_svc.get_rulebook",
|
||||
AsyncMock(return_value=rb),
|
||||
), patch(
|
||||
"fabledassistant.mcp.tools.rulebooks.rulebooks_svc.list_topics",
|
||||
AsyncMock(return_value=topics),
|
||||
):
|
||||
from fabledassistant.mcp.tools.rulebooks import get_rulebook
|
||||
out = await get_rulebook(rulebook_id=1)
|
||||
assert out["id"] == 1
|
||||
assert len(out["topics"]) == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_rulebook_raises_when_not_found():
|
||||
with patch(
|
||||
"fabledassistant.mcp.tools.rulebooks.rulebooks_svc.get_rulebook",
|
||||
AsyncMock(return_value=None),
|
||||
):
|
||||
from fabledassistant.mcp.tools.rulebooks import get_rulebook
|
||||
with pytest.raises(ValueError, match="rulebook 999 not found"):
|
||||
await get_rulebook(rulebook_id=999)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_rule_passes_required_fields():
|
||||
rule = _fake_rule()
|
||||
mock = AsyncMock(return_value=rule)
|
||||
with patch("fabledassistant.mcp.tools.rulebooks.rulebooks_svc.create_rule", mock):
|
||||
from fabledassistant.mcp.tools.rulebooks import create_rule
|
||||
await create_rule(
|
||||
topic_id=10, title="dev is home", statement="Work directly on dev",
|
||||
)
|
||||
kwargs = mock.call_args.kwargs
|
||||
assert kwargs["user_id"] == 7
|
||||
assert kwargs["topic_id"] == 10
|
||||
assert kwargs["statement"] == "Work directly on dev"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_rule_only_sends_non_default_fields():
|
||||
rule = _fake_rule()
|
||||
mock = AsyncMock(return_value=rule)
|
||||
with patch("fabledassistant.mcp.tools.rulebooks.rulebooks_svc.update_rule", mock):
|
||||
from fabledassistant.mcp.tools.rulebooks import update_rule
|
||||
await update_rule(rule_id=1, statement="new statement")
|
||||
args, kwargs = mock.call_args
|
||||
assert args == (1, 7)
|
||||
assert kwargs == {"statement": "new statement"}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_rule_without_confirmed_returns_warning():
|
||||
"""delete_rule with confirmed=False returns a preview, not an action."""
|
||||
rule = _fake_rule()
|
||||
with patch(
|
||||
"fabledassistant.mcp.tools.rulebooks.rulebooks_svc.get_rule",
|
||||
AsyncMock(return_value=rule),
|
||||
), patch(
|
||||
"fabledassistant.mcp.tools.rulebooks.rulebooks_svc.delete_rule",
|
||||
AsyncMock(),
|
||||
) as mock_delete:
|
||||
from fabledassistant.mcp.tools.rulebooks import delete_rule
|
||||
out = await delete_rule(rule_id=1, confirmed=False)
|
||||
assert out.get("confirmed_required") is True
|
||||
assert "confirmed=True" in out.get("warning", "")
|
||||
assert not mock_delete.called # service NOT called
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_rule_with_confirmed_calls_service():
|
||||
rule = _fake_rule()
|
||||
mock_delete = AsyncMock()
|
||||
with patch(
|
||||
"fabledassistant.mcp.tools.rulebooks.rulebooks_svc.get_rule",
|
||||
AsyncMock(return_value=rule),
|
||||
), patch(
|
||||
"fabledassistant.mcp.tools.rulebooks.rulebooks_svc.delete_rule",
|
||||
mock_delete,
|
||||
):
|
||||
from fabledassistant.mcp.tools.rulebooks import delete_rule
|
||||
out = await delete_rule(rule_id=1, confirmed=True)
|
||||
assert out == {"deleted": 1}
|
||||
assert mock_delete.called
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_subscribe_project_to_rulebook_calls_service():
|
||||
mock = AsyncMock()
|
||||
with patch(
|
||||
"fabledassistant.mcp.tools.rulebooks.rulebooks_svc.subscribe_project", mock,
|
||||
):
|
||||
from fabledassistant.mcp.tools.rulebooks import subscribe_project_to_rulebook
|
||||
out = await subscribe_project_to_rulebook(project_id=3, rulebook_id=1)
|
||||
assert out["subscribed"] is True
|
||||
assert mock.called
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_unsubscribe_project_from_rulebook_calls_service():
|
||||
mock = AsyncMock()
|
||||
with patch(
|
||||
"fabledassistant.mcp.tools.rulebooks.rulebooks_svc.unsubscribe_project", mock,
|
||||
):
|
||||
from fabledassistant.mcp.tools.rulebooks import unsubscribe_project_from_rulebook
|
||||
out = await unsubscribe_project_from_rulebook(project_id=3, rulebook_id=1)
|
||||
assert out["subscribed"] is False
|
||||
assert mock.called
|
||||
|
||||
|
||||
def test_register_attaches_all_sixteen_tools():
|
||||
"""register(mcp) should call mcp.tool(name=...) for all 16 tools."""
|
||||
from fabledassistant.mcp.tools.rulebooks import register
|
||||
registered: list[str] = []
|
||||
|
||||
class FakeMCP:
|
||||
def tool(self, name=None):
|
||||
def decorator(fn):
|
||||
registered.append(name)
|
||||
return fn
|
||||
return decorator
|
||||
|
||||
register(FakeMCP())
|
||||
assert len(registered) == 16
|
||||
# spot-check a few names
|
||||
assert "list_rulebooks" in registered
|
||||
assert "create_rule" in registered
|
||||
assert "subscribe_project_to_rulebook" in registered
|
||||
Reference in New Issue
Block a user