* feat: CalDAV write-back — push local event create/update/delete to the remote (#800) CalDAV sync was pull-only (src/caldav_sync.py), so events created, edited, or deleted in Odysseus on a CalDAV-backed calendar only changed local SQLite and never reached the server — they silently vanished on the next pull and never appeared on the user's phone (iCloud, etc.). This adds the missing write half: - src/caldav_writeback.py builds the VEVENT, re-discovers the remote calendar by the same URL-hash the local id was derived from (the remote URL isn't stored), and PUTs/DELETEs the event by UID via the caldav lib. The pure pieces (build_event_ical, find_remote_calendar, push_event) take inputs by argument so they unit-test against a fake client with no network. - create/update/delete event handlers (routes/calendar_routes.py) call it best-effort for caldav-sourced calendars only: the local DB stays the source of truth, a remote failure is logged, never fatal, and local calendars are untouched. Tests: tests/test_caldav_writeback.py (9, pure logic incl. iCal serialization, hash discovery, create/update/delete orchestration) and tests/test_caldav_writeback_route.py (3, route-level: a caldav calendar pushes, a local one does not, delete pushes a delete). 12 passed. Note: write-back re-discovers the remote calendar per write (the URL isn't persisted locally); a follow-up could cache it. Live-iCloud verification needs a real account — flagging for a maintainer pass. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * test: drive #800 route regression without TestClient (fixes local hang) Same fix as the document route test: the CalDAV write-back route regression used Starlette TestClient (middleware app + threadpool) which hung in the maintainer's environment. Rework it to call the async create/delete calendar handlers directly — extracted from the router — with a minimal fake request, temp-SQLite-patched SessionLocal, and writeback_event stubbed to record calls. Same coverage (a caldav calendar pushes, a local one does not, delete pushes a delete), completes in ~0.3s with no TestClient. Verified the maintainer's exact batch: pytest tests/test_caldav_writeback.py tests/test_caldav_writeback_route.py -> 12 passed Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
119 lines
3.5 KiB
Python
119 lines
3.5 KiB
Python
"""Issue #800 — CalDAV write-back pushes local changes to the remote server.
|
|
|
|
Unit-tests the pure pieces against a fake caldav calendar (no network): the
|
|
iCalendar serialization, hash-based remote-calendar discovery, and the
|
|
create/update/delete orchestration.
|
|
"""
|
|
|
|
from datetime import datetime
|
|
|
|
from src.caldav_writeback import (
|
|
build_event_ical,
|
|
find_remote_calendar,
|
|
push_event,
|
|
_stable_cal_id,
|
|
)
|
|
|
|
REMOTE_URL = "https://p69-caldav.icloud.com/123/calendars/home/"
|
|
CAL_ID = _stable_cal_id(REMOTE_URL)
|
|
|
|
|
|
class FakeEvent:
|
|
def __init__(self):
|
|
self.data = "OLD"
|
|
self.saved = False
|
|
self.deleted = False
|
|
|
|
def save(self):
|
|
self.saved = True
|
|
|
|
def delete(self):
|
|
self.deleted = True
|
|
|
|
|
|
class FakeCalendar:
|
|
def __init__(self, url, existing=None):
|
|
self.url = url
|
|
self._existing = existing
|
|
self.saved_ical = None
|
|
|
|
def event_by_uid(self, uid):
|
|
if self._existing is None:
|
|
raise Exception("not found")
|
|
return self._existing
|
|
|
|
def save_event(self, ical):
|
|
self.saved_ical = ical
|
|
|
|
|
|
def _ev(**over):
|
|
base = dict(
|
|
uid="evt-1", summary="Dentist", description="bring x-rays",
|
|
location="Clinic", dtstart=datetime(2026, 6, 10, 14, 0),
|
|
dtend=datetime(2026, 6, 10, 15, 0), all_day=False, is_utc=True, rrule="",
|
|
)
|
|
base.update(over)
|
|
return base
|
|
|
|
|
|
def test_build_ical_timed_event_has_core_fields():
|
|
ical = build_event_ical(_ev())
|
|
assert "BEGIN:VEVENT" in ical and "END:VEVENT" in ical
|
|
assert "UID:evt-1" in ical
|
|
assert "SUMMARY:Dentist" in ical
|
|
# is_utc -> UTC instant (Z suffix)
|
|
assert "DTSTART:20260610T140000Z" in ical
|
|
assert "DTEND:20260610T150000Z" in ical
|
|
|
|
|
|
def test_build_ical_all_day_uses_date_values():
|
|
ical = build_event_ical(_ev(all_day=True, is_utc=False))
|
|
assert "DTSTART;VALUE=DATE:20260610" in ical
|
|
|
|
|
|
def test_build_ical_includes_rrule():
|
|
ical = build_event_ical(_ev(rrule="FREQ=WEEKLY;BYDAY=MO"))
|
|
assert "RRULE:FREQ=WEEKLY" in ical
|
|
|
|
|
|
def test_find_remote_calendar_matches_by_hash():
|
|
cals = [FakeCalendar("https://other/x/"), FakeCalendar(REMOTE_URL)]
|
|
found = find_remote_calendar(cals, CAL_ID)
|
|
assert found is cals[1]
|
|
assert find_remote_calendar([FakeCalendar("https://nope/")], CAL_ID) is None
|
|
|
|
|
|
def test_push_create_calls_save_event():
|
|
cal = FakeCalendar(REMOTE_URL, existing=None) # event_by_uid raises -> create
|
|
res = push_event([cal], CAL_ID, _ev(), delete=False)
|
|
assert res["ok"] and res.get("created")
|
|
assert cal.saved_ical and "UID:evt-1" in cal.saved_ical
|
|
|
|
|
|
def test_push_update_overwrites_existing():
|
|
existing = FakeEvent()
|
|
cal = FakeCalendar(REMOTE_URL, existing=existing)
|
|
res = push_event([cal], CAL_ID, _ev(summary="Moved"), delete=False)
|
|
assert res["ok"] and res.get("updated")
|
|
assert existing.saved and "SUMMARY:Moved" in existing.data
|
|
assert cal.saved_ical is None # used update path, not create
|
|
|
|
|
|
def test_push_delete_removes_existing():
|
|
existing = FakeEvent()
|
|
cal = FakeCalendar(REMOTE_URL, existing=existing)
|
|
res = push_event([cal], CAL_ID, _ev(), delete=True)
|
|
assert res["ok"] and existing.deleted
|
|
|
|
|
|
def test_push_delete_absent_is_ok():
|
|
cal = FakeCalendar(REMOTE_URL, existing=None)
|
|
res = push_event([cal], CAL_ID, _ev(), delete=True)
|
|
assert res["ok"] and "absent" in res.get("note", "")
|
|
|
|
|
|
def test_push_unknown_calendar_reports_not_found():
|
|
cal = FakeCalendar("https://different/")
|
|
res = push_event([cal], CAL_ID, _ev())
|
|
assert res["ok"] is False and "not found" in res["error"]
|