Files
FabledScribe/tests/test_mcp_tool_events.py
T

159 lines
5.4 KiB
Python

"""Tests for fable_*_event tools."""
from datetime import datetime
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from fabledassistant.mcp._context import _user_id_ctx
from fabledassistant.mcp.tools.events import (
list_events, create_event, get_event,
update_event, delete_event,
)
@pytest.fixture(autouse=True)
def _bind_user():
token = _user_id_ctx.set(7)
yield
_user_id_ctx.reset(token)
def _fake_event(**overrides) -> MagicMock:
e = MagicMock()
base = {
"id": 1, "title": "ev", "start_dt": "2026-06-01T10:00:00",
"duration_minutes": 30, "all_day": False,
"location": "", "description": "",
}
base.update(overrides)
e.to_dict.return_value = base
return e
@pytest.mark.asyncio
async def test_list_events_passes_timezone_aware_range():
"""Range must be tz-aware (UTC) and date_to inclusive at end-of-day —
Event.start_dt is tz-aware in the DB; naive comparisons raise TypeError."""
from datetime import timezone
mock = AsyncMock(return_value=[
{"id": 1, "title": "morning standup"},
])
with patch("fabledassistant.mcp.tools.events.events_svc.list_events", mock):
out = await list_events(date_from="2026-06-01", date_to="2026-06-08")
args, _ = mock.call_args
assert args[0] == 7 # user_id
assert args[1] == datetime(2026, 6, 1, tzinfo=timezone.utc)
# date_to is end-of-day inclusive → start of 2026-06-09 (24h past start of 2026-06-08)
assert args[2] == datetime(2026, 6, 9, tzinfo=timezone.utc)
assert out["total"] == 1
@pytest.mark.asyncio
async def test_create_event_combines_date_and_time():
e = _fake_event()
mock = AsyncMock(return_value=e)
with patch("fabledassistant.mcp.tools.events.events_svc.create_event", mock):
await create_event(
title="standup", start_date="2026-06-01", start_time="09:30",
duration_minutes=15,
)
kwargs = mock.call_args.kwargs
assert kwargs["start_dt"] == datetime(2026, 6, 1, 9, 30)
assert kwargs["duration_minutes"] == 15
@pytest.mark.asyncio
async def test_create_event_zero_duration_means_point_event():
"""duration_minutes=0 must map to None at the service layer (NULL = point)."""
e = _fake_event()
mock = AsyncMock(return_value=e)
with patch("fabledassistant.mcp.tools.events.events_svc.create_event", mock):
await create_event(title="x", start_date="2026-06-01")
assert mock.call_args.kwargs["duration_minutes"] is None
@pytest.mark.asyncio
async def test_get_event_raises_when_not_found():
with patch(
"fabledassistant.mcp.tools.events.events_svc.get_event",
AsyncMock(return_value=None),
):
with pytest.raises(ValueError, match="event 999 not found"):
await get_event(event_id=999)
@pytest.mark.asyncio
async def test_update_event_only_sends_non_default_fields():
e = _fake_event()
mock = AsyncMock(return_value=e)
with patch("fabledassistant.mcp.tools.events.events_svc.update_event", mock):
await update_event(event_id=1, title="new title")
args, kwargs = mock.call_args
assert args == (7, 1)
assert kwargs == {"title": "new title"}
@pytest.mark.asyncio
async def test_update_event_duration_minus_one_means_unchanged():
e = _fake_event()
mock = AsyncMock(return_value=e)
with patch("fabledassistant.mcp.tools.events.events_svc.update_event", mock):
await update_event(event_id=1, duration_minutes=-1)
assert "duration_minutes" not in mock.call_args.kwargs
@pytest.mark.asyncio
async def test_update_event_duration_zero_clears_to_point():
"""duration_minutes=0 means "set to point event" (NULL)."""
e = _fake_event()
mock = AsyncMock(return_value=e)
with patch("fabledassistant.mcp.tools.events.events_svc.update_event", mock):
await update_event(event_id=1, duration_minutes=0)
assert mock.call_args.kwargs["duration_minutes"] is None
@pytest.mark.asyncio
async def test_update_event_requires_both_date_and_time_to_move():
e = _fake_event()
mock = AsyncMock(return_value=e)
with patch("fabledassistant.mcp.tools.events.events_svc.update_event", mock):
await update_event(event_id=1, start_date="2026-06-02")
# Only start_date, no start_time → start_dt NOT in fields
assert "start_dt" not in mock.call_args.kwargs
mock.reset_mock()
await update_event(
event_id=1, start_date="2026-06-02", start_time="11:00",
)
assert mock.call_args.kwargs["start_dt"] == datetime(2026, 6, 2, 11)
@pytest.mark.asyncio
async def test_update_event_raises_when_not_found():
with patch(
"fabledassistant.mcp.tools.events.events_svc.update_event",
AsyncMock(return_value=None),
):
with pytest.raises(ValueError, match="event 999 not found"):
await update_event(event_id=999, title="x")
@pytest.mark.asyncio
async def test_delete_event_soft_deletes_and_returns_batch():
with patch(
"fabledassistant.mcp.tools.events.trash_svc.delete",
AsyncMock(return_value="batch-1"),
):
result = await delete_event(event_id=7)
assert result["deleted_batch_id"] == "batch-1"
@pytest.mark.asyncio
async def test_delete_event_raises_when_not_found():
with patch(
"fabledassistant.mcp.tools.events.trash_svc.delete",
AsyncMock(return_value=None),
):
with pytest.raises(ValueError, match="event 999 not found"):
await delete_event(event_id=999)