611c940527
A user reported "next Friday at 8am" landing on the wrong day. The current `start` parameter accepts a combined ISO datetime string — when the model emits something like `"2026-05-01T00:00:00Z"`, the parser correctly honors the UTC tag and stores `2026-05-01 00:00 UTC`, which displays as `2026-04-30 19:00` for a UTC-5 user. The bug isn't in our parser; it's that we let the model TZ-tag the calendar day at all. The fix moves the foot-gun: `create_event` and `update_event` now prefer split fields (`start_date` + `start_time`, plus end variants). A `YYYY-MM-DD` string carries no TZ metadata for a model to mis-tag, and the backend builds the local datetime explicitly via `datetime.combine(date, time, tzinfo=user_tz).astimezone(UTC)`. Strict regex validation rejects anything with a TZ suffix on either field. The legacy combined `start` / `end` fields are kept as a fallback so saved tool-call payloads in conversation history still replay; new calls are steered toward the split shape via the tool description. 7 new regression tests cover Eastern, Pacific, Tokyo (positive offset), all-day inference, strict-shape rejection on both fields, backcompat with the legacy `start` field, and the same fix for `update_event`. 27 of the event-related tests pass; ruff clean. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
398 lines
14 KiB
Python
398 lines
14 KiB
Python
"""Regression tests: calendar tool respects the user's configured timezone."""
|
|
|
|
from datetime import timezone
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_event_all_day_bare_date_interprets_local_midnight():
|
|
"""Bare date like '2026-09-30' for a NY user = 2026-09-30 00:00 NY,
|
|
stored as 2026-09-30 04:00 UTC — NOT 2026-09-30 00:00 UTC (which would
|
|
be 2026-09-29 19:00 NY and land on the wrong day)."""
|
|
from fabledassistant.services.tools.calendar import create_event_tool
|
|
|
|
captured = {}
|
|
|
|
async def fake_create_event(**kwargs):
|
|
captured.update(kwargs)
|
|
event = AsyncMock()
|
|
event.to_dict.return_value = {"id": 1, **{k: v for k, v in kwargs.items() if not callable(v)}}
|
|
return event
|
|
|
|
with patch(
|
|
"fabledassistant.services.tools.calendar.get_user_tz",
|
|
AsyncMock(return_value=__import__("zoneinfo").ZoneInfo("America/New_York")),
|
|
), patch(
|
|
"fabledassistant.services.tools.calendar.events_create_event",
|
|
side_effect=fake_create_event,
|
|
):
|
|
result = await create_event_tool(
|
|
user_id=1,
|
|
arguments={"title": "Birthday", "start": "2026-09-30"},
|
|
)
|
|
|
|
assert result["success"] is True
|
|
start_dt = captured["start_dt"]
|
|
assert start_dt.tzinfo is not None
|
|
# Converted to UTC: 00:00 NY (EDT, UTC-4) == 04:00 UTC on the same day
|
|
utc = start_dt.astimezone(timezone.utc)
|
|
assert (utc.year, utc.month, utc.day) == (2026, 9, 30)
|
|
assert utc.hour == 4 # EDT offset; would be 5 in EST
|
|
assert captured["all_day"] is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_event_naive_datetime_is_user_local():
|
|
"""'2026-03-15T14:30' for a NY user means 2:30pm NY, not 2:30pm UTC."""
|
|
from fabledassistant.services.tools.calendar import create_event_tool
|
|
|
|
captured = {}
|
|
|
|
async def fake_create_event(**kwargs):
|
|
captured.update(kwargs)
|
|
event = AsyncMock()
|
|
event.to_dict.return_value = {"id": 1}
|
|
return event
|
|
|
|
with patch(
|
|
"fabledassistant.services.tools.calendar.get_user_tz",
|
|
AsyncMock(return_value=__import__("zoneinfo").ZoneInfo("America/New_York")),
|
|
), patch(
|
|
"fabledassistant.services.tools.calendar.events_create_event",
|
|
side_effect=fake_create_event,
|
|
):
|
|
await create_event_tool(
|
|
user_id=1,
|
|
arguments={"title": "Meeting", "start": "2026-03-15T14:30"},
|
|
)
|
|
|
|
utc = captured["start_dt"].astimezone(timezone.utc)
|
|
# 14:30 NY on 2026-03-15 (after DST start) = 18:30 UTC
|
|
assert (utc.year, utc.month, utc.day, utc.hour, utc.minute) == (2026, 3, 15, 18, 30)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_event_explicit_offset_is_respected():
|
|
"""Model-supplied timezone offsets must not be overridden by user TZ."""
|
|
from fabledassistant.services.tools.calendar import create_event_tool
|
|
|
|
captured = {}
|
|
|
|
async def fake_create_event(**kwargs):
|
|
captured.update(kwargs)
|
|
event = AsyncMock()
|
|
event.to_dict.return_value = {"id": 1}
|
|
return event
|
|
|
|
with patch(
|
|
"fabledassistant.services.tools.calendar.get_user_tz",
|
|
AsyncMock(return_value=__import__("zoneinfo").ZoneInfo("America/New_York")),
|
|
), patch(
|
|
"fabledassistant.services.tools.calendar.events_create_event",
|
|
side_effect=fake_create_event,
|
|
):
|
|
await create_event_tool(
|
|
user_id=1,
|
|
arguments={"title": "Meeting", "start": "2026-03-15T14:30+00:00"},
|
|
)
|
|
|
|
utc = captured["start_dt"].astimezone(timezone.utc)
|
|
assert (utc.hour, utc.minute) == (14, 30)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_events_bare_date_range_covers_local_day():
|
|
"""date_from/date_to as bare dates should cover the full LOCAL day."""
|
|
from fabledassistant.services.tools.calendar import list_events_tool
|
|
|
|
captured = {}
|
|
|
|
async def fake_list_events(*, user_id, date_from, date_to):
|
|
captured["date_from"] = date_from
|
|
captured["date_to"] = date_to
|
|
return []
|
|
|
|
with patch(
|
|
"fabledassistant.services.tools.calendar.get_user_tz",
|
|
AsyncMock(return_value=__import__("zoneinfo").ZoneInfo("America/New_York")),
|
|
), patch(
|
|
"fabledassistant.services.tools.calendar.events_list_events",
|
|
side_effect=fake_list_events,
|
|
):
|
|
await list_events_tool(
|
|
user_id=1,
|
|
arguments={"date_from": "2026-09-30", "date_to": "2026-09-30"},
|
|
)
|
|
|
|
df = captured["date_from"].astimezone(timezone.utc)
|
|
dt = captured["date_to"].astimezone(timezone.utc)
|
|
# 2026-09-30 00:00 NY (EDT) = 04:00 UTC same day
|
|
assert (df.year, df.month, df.day, df.hour) == (2026, 9, 30, 4)
|
|
# 2026-09-30 23:59:59 NY (EDT) = 03:59:59 UTC next day
|
|
assert (dt.year, dt.month, dt.day, dt.hour) == (2026, 10, 1, 3)
|
|
|
|
|
|
# ── Split date/time field tests (durable shape) ──────────────────────────────
|
|
#
|
|
# These exercise the start_date + start_time path that the model is now
|
|
# steered toward. The split-field shape is structurally immune to the
|
|
# class of bugs where a model emits a TZ-tagged combined datetime that
|
|
# the parser correctly honors but lands on the wrong calendar day for
|
|
# negative-offset users.
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_event_split_fields_friday_8am_no_drift_eastern():
|
|
"""The reported bug: 'next Friday at 8am' for a NY user must land on
|
|
2026-05-01 08:00 NY, never 04-30 19:00. With split fields the model
|
|
can't TZ-tag the date string, so the calendar day is fixed."""
|
|
from fabledassistant.services.tools.calendar import create_event_tool
|
|
|
|
captured = {}
|
|
|
|
async def fake_create_event(**kwargs):
|
|
captured.update(kwargs)
|
|
event = AsyncMock()
|
|
event.to_dict.return_value = {"id": 1}
|
|
return event
|
|
|
|
with patch(
|
|
"fabledassistant.services.tools.calendar.get_user_tz",
|
|
AsyncMock(return_value=__import__("zoneinfo").ZoneInfo("America/New_York")),
|
|
), patch(
|
|
"fabledassistant.services.tools.calendar.events_create_event",
|
|
side_effect=fake_create_event,
|
|
):
|
|
result = await create_event_tool(
|
|
user_id=1,
|
|
arguments={
|
|
"title": "Meeting",
|
|
"start_date": "2026-05-01",
|
|
"start_time": "08:00",
|
|
},
|
|
)
|
|
|
|
assert result["success"] is True
|
|
utc = captured["start_dt"].astimezone(timezone.utc)
|
|
# 08:00 NY (EDT, UTC-4) on 2026-05-01 = 12:00 UTC same day
|
|
assert (utc.year, utc.month, utc.day, utc.hour, utc.minute) == (2026, 5, 1, 12, 0)
|
|
assert captured["all_day"] is False
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_event_split_fields_no_drift_pacific():
|
|
"""Same scenario for a UTC-8 user — the calendar day must be 5/1
|
|
regardless of how big the offset gets."""
|
|
from fabledassistant.services.tools.calendar import create_event_tool
|
|
|
|
captured = {}
|
|
|
|
async def fake_create_event(**kwargs):
|
|
captured.update(kwargs)
|
|
event = AsyncMock()
|
|
event.to_dict.return_value = {"id": 1}
|
|
return event
|
|
|
|
with patch(
|
|
"fabledassistant.services.tools.calendar.get_user_tz",
|
|
AsyncMock(return_value=__import__("zoneinfo").ZoneInfo("America/Los_Angeles")),
|
|
), patch(
|
|
"fabledassistant.services.tools.calendar.events_create_event",
|
|
side_effect=fake_create_event,
|
|
):
|
|
await create_event_tool(
|
|
user_id=1,
|
|
arguments={
|
|
"title": "Standup",
|
|
"start_date": "2026-05-01",
|
|
"start_time": "08:00",
|
|
},
|
|
)
|
|
|
|
utc = captured["start_dt"].astimezone(timezone.utc)
|
|
# 08:00 LA (PDT, UTC-7) = 15:00 UTC same day
|
|
assert (utc.year, utc.month, utc.day, utc.hour) == (2026, 5, 1, 15)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_event_split_fields_no_drift_positive_offset():
|
|
"""A positive-offset user (Tokyo, UTC+9) — 08:00 Tokyo on 2026-05-01
|
|
is 23:00 UTC on 2026-04-30, but the local calendar day must still be
|
|
stored as 2026-05-01 from the user's perspective."""
|
|
from fabledassistant.services.tools.calendar import create_event_tool
|
|
|
|
captured = {}
|
|
|
|
async def fake_create_event(**kwargs):
|
|
captured.update(kwargs)
|
|
event = AsyncMock()
|
|
event.to_dict.return_value = {"id": 1}
|
|
return event
|
|
|
|
with patch(
|
|
"fabledassistant.services.tools.calendar.get_user_tz",
|
|
AsyncMock(return_value=__import__("zoneinfo").ZoneInfo("Asia/Tokyo")),
|
|
), patch(
|
|
"fabledassistant.services.tools.calendar.events_create_event",
|
|
side_effect=fake_create_event,
|
|
):
|
|
await create_event_tool(
|
|
user_id=1,
|
|
arguments={
|
|
"title": "Sync",
|
|
"start_date": "2026-05-01",
|
|
"start_time": "08:00",
|
|
},
|
|
)
|
|
|
|
utc = captured["start_dt"].astimezone(timezone.utc)
|
|
# 08:00 Tokyo (UTC+9) = 23:00 UTC previous day; the round-trip back
|
|
# to Tokyo TZ recovers 2026-05-01 08:00.
|
|
assert (utc.year, utc.month, utc.day, utc.hour) == (2026, 4, 30, 23)
|
|
tokyo = captured["start_dt"].astimezone(__import__("zoneinfo").ZoneInfo("Asia/Tokyo"))
|
|
assert (tokyo.year, tokyo.month, tokyo.day, tokyo.hour) == (2026, 5, 1, 8)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_event_split_date_only_is_all_day():
|
|
"""Omitting start_time means the event is all-day; cache row uses
|
|
local midnight."""
|
|
from fabledassistant.services.tools.calendar import create_event_tool
|
|
|
|
captured = {}
|
|
|
|
async def fake_create_event(**kwargs):
|
|
captured.update(kwargs)
|
|
event = AsyncMock()
|
|
event.to_dict.return_value = {"id": 1}
|
|
return event
|
|
|
|
with patch(
|
|
"fabledassistant.services.tools.calendar.get_user_tz",
|
|
AsyncMock(return_value=__import__("zoneinfo").ZoneInfo("America/New_York")),
|
|
), patch(
|
|
"fabledassistant.services.tools.calendar.events_create_event",
|
|
side_effect=fake_create_event,
|
|
):
|
|
await create_event_tool(
|
|
user_id=1,
|
|
arguments={"title": "Birthday", "start_date": "2026-09-30"},
|
|
)
|
|
|
|
assert captured["all_day"] is True
|
|
utc = captured["start_dt"].astimezone(timezone.utc)
|
|
assert (utc.year, utc.month, utc.day, utc.hour) == (2026, 9, 30, 4)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_event_split_rejects_tz_suffix_in_date():
|
|
"""The whole point of split fields: a TZ suffix on the date string
|
|
must be rejected, not silently honored."""
|
|
from fabledassistant.services.tools.calendar import create_event_tool
|
|
|
|
with patch(
|
|
"fabledassistant.services.tools.calendar.get_user_tz",
|
|
AsyncMock(return_value=__import__("zoneinfo").ZoneInfo("America/New_York")),
|
|
):
|
|
result = await create_event_tool(
|
|
user_id=1,
|
|
arguments={"title": "x", "start_date": "2026-05-01Z", "start_time": "08:00"},
|
|
)
|
|
|
|
assert result["success"] is False
|
|
assert "YYYY-MM-DD" in result["error"]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_event_split_rejects_tz_suffix_in_time():
|
|
"""A TZ suffix on the time string must also be rejected."""
|
|
from fabledassistant.services.tools.calendar import create_event_tool
|
|
|
|
with patch(
|
|
"fabledassistant.services.tools.calendar.get_user_tz",
|
|
AsyncMock(return_value=__import__("zoneinfo").ZoneInfo("America/New_York")),
|
|
):
|
|
result = await create_event_tool(
|
|
user_id=1,
|
|
arguments={"title": "x", "start_date": "2026-05-01", "start_time": "08:00 UTC"},
|
|
)
|
|
|
|
assert result["success"] is False
|
|
assert "HH:MM" in result["error"]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_event_legacy_combined_start_still_works():
|
|
"""Backcompat: saved tool-call payloads using the old `start` field
|
|
must still produce the same UTC datetime as before."""
|
|
from fabledassistant.services.tools.calendar import create_event_tool
|
|
|
|
captured = {}
|
|
|
|
async def fake_create_event(**kwargs):
|
|
captured.update(kwargs)
|
|
event = AsyncMock()
|
|
event.to_dict.return_value = {"id": 1}
|
|
return event
|
|
|
|
with patch(
|
|
"fabledassistant.services.tools.calendar.get_user_tz",
|
|
AsyncMock(return_value=__import__("zoneinfo").ZoneInfo("America/New_York")),
|
|
), patch(
|
|
"fabledassistant.services.tools.calendar.events_create_event",
|
|
side_effect=fake_create_event,
|
|
):
|
|
result = await create_event_tool(
|
|
user_id=1,
|
|
arguments={"title": "Legacy", "start": "2026-05-01T08:00"},
|
|
)
|
|
|
|
assert result["success"] is True
|
|
utc = captured["start_dt"].astimezone(timezone.utc)
|
|
assert (utc.year, utc.month, utc.day, utc.hour) == (2026, 5, 1, 12)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update_event_split_fields_reschedule_no_drift():
|
|
"""update_event with split fields must drift no calendar day either."""
|
|
from fabledassistant.services.tools.calendar import update_event_tool
|
|
|
|
captured = {}
|
|
|
|
async def fake_find(*, user_id, query):
|
|
ev = AsyncMock()
|
|
ev.id = 99
|
|
ev.title = "Coffee"
|
|
return [ev]
|
|
|
|
async def fake_update(*, user_id, event_id, **fields):
|
|
captured.update(fields)
|
|
ev = AsyncMock()
|
|
ev.to_dict.return_value = {"id": event_id, **fields}
|
|
return ev
|
|
|
|
with patch(
|
|
"fabledassistant.services.tools.calendar.get_user_tz",
|
|
AsyncMock(return_value=__import__("zoneinfo").ZoneInfo("America/New_York")),
|
|
), patch(
|
|
"fabledassistant.services.tools.calendar.find_events_by_query",
|
|
side_effect=fake_find,
|
|
), patch(
|
|
"fabledassistant.services.tools.calendar.events_update_event",
|
|
side_effect=fake_update,
|
|
):
|
|
result = await update_event_tool(
|
|
user_id=1,
|
|
arguments={
|
|
"query": "Coffee",
|
|
"start_date": "2026-05-01",
|
|
"start_time": "08:00",
|
|
},
|
|
)
|
|
|
|
assert result["success"] is True
|
|
utc = captured["start_dt"].astimezone(timezone.utc)
|
|
assert (utc.year, utc.month, utc.day, utc.hour) == (2026, 5, 1, 12)
|