313 lines
12 KiB
Python
313 lines
12 KiB
Python
"""Unit tests for mcp-ics server (pure functions, no server, no Docker)."""
|
|||
|
|
|
||
|
|
import calendar as calmod
|
||
|
|
import logging
|
||
|
|
from datetime import date, datetime
|
||
|
|
|
||
|
|
import icalendar
|
||
|
|
import pytest
|
||
|
|
|
||
|
|
import conftest # noqa: F401 (sys.path setup)
|
||
|
|
|
||
|
|
|
||
|
|
# ---------------------------------------------------------------- discover_calendars
|
||
|
|
|
||
|
|
def test_discover_finds_calendars_with_names(server_mod, radicale_tree):
|
||
|
|
cals = server_mod.discover_calendars(str(radicale_tree))
|
||
|
|
by_path = {c["path"]: c for c in cals}
|
||
|
|
assert len(cals) == 3 # perso, taches, famille
|
||
|
|
assert by_path[str(radicale_tree / "collections/collection-root/manuel/perso")][
|
||
|
|
"name"
|
||
|
|
] == "Perso manuel"
|
||
|
|
assert by_path[str(radicale_tree / "collections/collection-root/manuel/taches")][
|
||
|
|
"name"
|
||
|
|
] == "Tâches"
|
||
|
|
assert by_path[str(radicale_tree / "collections/collection-root/robert/famille")][
|
||
|
|
"user"
|
||
|
|
] == "robert"
|
||
|
|
|
||
|
|
|
||
|
|
def test_discover_skips_vaddressbook_hidden_and_broken(server_mod, radicale_tree):
|
||
|
|
cals = server_mod.discover_calendars(str(radicale_tree))
|
||
|
|
paths = " ".join(c["path"] for c in cals)
|
||
|
|
assert "contacts" not in paths
|
||
|
|
assert ".stfolder" not in paths
|
||
|
|
assert ".trash" not in paths
|
||
|
|
assert "broken" not in paths
|
||
|
|
|
||
|
|
|
||
|
|
def test_discover_missing_collection_root(server, tmp_path):
|
||
|
|
assert server.discover_calendars(str(tmp_path / "nowhere")) == []
|
||
|
|
|
||
|
|
|
||
|
|
def test_discover_empty_path(server):
|
||
|
|
assert server.discover_calendars("") == []
|
||
|
|
|
||
|
|
|
||
|
|
def test_discover_defaults_displayname_to_dirname(server_mod, radicale_tree):
|
||
|
|
# taches cal has no explicit name in tree? it does: "Tâches". famille: "Famille".
|
||
|
|
# Build one WITHOUT displayname.
|
||
|
|
import json
|
||
|
|
|
||
|
|
cal = radicale_tree / "collections/collection-root/manuel/noname"
|
||
|
|
cal.mkdir(parents=True, exist_ok=True)
|
||
|
|
(cal / ".Radicale.props").write_text(json.dumps({"tag": "VCALENDAR"}))
|
||
|
|
try:
|
||
|
|
cals = server_mod.discover_calendars(str(radicale_tree))
|
||
|
|
noname = [c for c in cals if c["path"].endswith("noname")][0]
|
||
|
|
assert noname["name"] == "noname"
|
||
|
|
finally:
|
||
|
|
(cal / ".Radicale.props").unlink()
|
||
|
|
cal.rmdir()
|
||
|
|
|
||
|
|
|
||
|
|
# ---------------------------------------------------------------- parse_ics_files
|
||
|
|
|
||
|
|
def test_parse_ics_files_reads_component(server_mod, radicale_tree):
|
||
|
|
path = radicale_tree / "collections/collection-root/manuel/perso"
|
||
|
|
cals = server_mod.parse_ics_files(str(path))
|
||
|
|
assert len(cals) == 3
|
||
|
|
types = {sub.name for cal in cals for sub in cal.subcomponents}
|
||
|
|
assert "VEVENT" in types
|
||
|
|
|
||
|
|
|
||
|
|
def test_parse_ics_files_skips_unparseable(server_mod, radicale_tree):
|
||
|
|
import json
|
||
|
|
|
||
|
|
cal = radicale_tree / "collections/collection-root/manuel/badics"
|
||
|
|
cal.mkdir(parents=True, exist_ok=True)
|
||
|
|
(cal / ".Radicale.props").write_text(json.dumps({"tag": "VCALENDAR"}))
|
||
|
|
(cal / "x.ics").write_bytes(b"NOT AN ICS FILE \xff\xfe at all")
|
||
|
|
cals = server_mod.parse_ics_files(str(cal))
|
||
|
|
assert cals == []
|
||
|
|
|
||
|
|
|
||
|
|
def test_parse_ics_files_empty_dir(server_mod, radicale_tree):
|
||
|
|
assert server_mod.parse_ics_files(str(radicale_tree / "does/not/exist")) == []
|
||
|
|
|
||
|
|
|
||
|
|
# ---------------------------------------------------------------- component_to_dt
|
||
|
|
|
||
|
|
def test_component_to_dt_datetime_passthrough(server_mod):
|
||
|
|
dt = datetime(2026, 3, 15, 9, 0)
|
||
|
|
prop = icalendar.prop.vDatetime(dt)
|
||
|
|
assert server_mod.component_to_dt(prop) == dt
|
||
|
|
|
||
|
|
|
||
|
|
def test_component_to_dt_date_becomes_midnight(server_mod):
|
||
|
|
d = date(2026, 4, 10)
|
||
|
|
prop = icalendar.prop.vDate(d)
|
||
|
|
assert server_mod.component_to_dt(prop) == datetime(2026, 4, 10, 0, 0)
|
||
|
|
|
||
|
|
|
||
|
|
def test_component_to_dt_none(server_mod):
|
||
|
|
assert server_mod.component_to_dt(None) is None
|
||
|
|
assert server_mod.component_to_dt("not-a-date") is None
|
||
|
|
|
||
|
|
|
||
|
|
# ---------------------------------------------------------------- format_dt
|
||
|
|
|
||
|
|
def test_format_dt(server_mod):
|
||
|
|
assert server_mod.format_dt(None) is None
|
||
|
|
d = date(2026, 4, 10)
|
||
|
|
assert server_mod.format_dt(d) == "2026-04-10"
|
||
|
|
dt = datetime(2026, 3, 15, 9, 0)
|
||
|
|
assert server_mod.format_dt(dt) == dt.isoformat()
|
||
|
|
|
||
|
|
|
||
|
|
# ---------------------------------------------------------------- format_component (filters)
|
||
|
|
|
||
|
|
def _ven_event(summary, uid="x@test", dtstart=None):
|
||
|
|
if dtstart is None:
|
||
|
|
dtstart = datetime(2026, 3, 15, 9, 0)
|
||
|
|
ev = icalendar.cal.Event()
|
||
|
|
ev.add("uid", uid)
|
||
|
|
ev.add("dtstamp", datetime(2026, 1, 1, tzinfo=__import__("datetime").timezone.utc))
|
||
|
|
ev.add("dtstart", dtstart)
|
||
|
|
ev.add("summary", summary)
|
||
|
|
return ev
|
||
|
|
|
||
|
|
|
||
|
|
class TestFilters:
|
||
|
|
def test_exclude_pattern_hides_poubelle(self, server_mod):
|
||
|
|
comp = _ven_event("Poubelle verte")
|
||
|
|
assert server_mod.format_component(comp, "Perso") is None
|
||
|
|
|
||
|
|
def test_birthdays_excluded_by_default(self, server_mod):
|
||
|
|
comp = _ven_event("Anniversaire Marc", dtstart=date(2026, 4, 10))
|
||
|
|
assert server_mod.format_component(comp, "Perso") is None
|
||
|
|
|
||
|
|
def test_birthdays_included_on_demand(self, server_mod):
|
||
|
|
comp = _ven_event("Anniversaire Marc", dtstart=date(2026, 4, 10))
|
||
|
|
out = server_mod.format_component(comp, "Perso", include_birthdays=True)
|
||
|
|
assert out is not None
|
||
|
|
assert out["summary"] == "Anniversaire Marc"
|
||
|
|
|
||
|
|
def test_normal_event_passes_birthday_filter(self, server_mod):
|
||
|
|
comp = _ven_event("Réunion équipe")
|
||
|
|
assert server_mod.format_component(comp, "Perso") is not None
|
||
|
|
|
||
|
|
|
||
|
|
class TestOutput:
|
||
|
|
def test_event_shape(self, server_mod, radicale_tree):
|
||
|
|
cal_path = str(radicale_tree / "collections/collection-root/manuel/perso")
|
||
|
|
found = None
|
||
|
|
for cal in server_mod.parse_ics_files(cal_path):
|
||
|
|
for c in cal.subcomponents:
|
||
|
|
if str(c.get("UID", "")) == "evt-1@test":
|
||
|
|
found = c
|
||
|
|
out = server_mod.format_component(found, "Perso manuel")
|
||
|
|
assert out["type"] == "event"
|
||
|
|
assert out["summary"] == "Réunion équipe"
|
||
|
|
assert out["start"] == "2026-03-15T09:00:00"
|
||
|
|
assert out["end"] == "2026-03-15T10:00:00"
|
||
|
|
assert out["calendar"] == "Perso manuel"
|
||
|
|
assert out["uid"] == "evt-1@test"
|
||
|
|
assert out["location"] == "Bureau"
|
||
|
|
assert out["description"] == "Point hebdo"
|
||
|
|
assert "due" not in out
|
||
|
|
assert "status" not in out
|
||
|
|
|
||
|
|
def test_todo_shape(self, server_mod, radicale_tree):
|
||
|
|
cal_path = str(radicale_tree / "collections/collection-root/manuel/taches")
|
||
|
|
cal = server_mod.parse_ics_files(cal_path)[0]
|
||
|
|
todo = [c for c in cal.subcomponents if c.name == "VTODO"][0]
|
||
|
|
out = server_mod.format_component(todo, "Tâches")
|
||
|
|
assert out["type"] == "todo"
|
||
|
|
assert out["due"] == "2026-03-20T17:00:00"
|
||
|
|
assert out["status"] == "NEEDS-ACTION"
|
||
|
|
assert "end" not in out
|
||
|
|
|
||
|
|
def test_optional_fields_absent_when_empty(self, server_mod):
|
||
|
|
comp = _ven_event("Sans extras")
|
||
|
|
out = server_mod.format_component(comp, "C")
|
||
|
|
assert "location" not in out
|
||
|
|
assert "description" not in out
|
||
|
|
|
||
|
|
|
||
|
|
# ---------------------------------------------------------------- get_events_in_range
|
||
|
|
|
||
|
|
class TestGetEventsInRange:
|
||
|
|
def test_range_aggregates_and_sorts(self, server_mod):
|
||
|
|
items = server_mod.get_events_in_range(
|
||
|
|
datetime(2026, 3, 15), datetime(2026, 3, 21)
|
||
|
|
)
|
||
|
|
summaries = [i["summary"] for i in items]
|
||
|
|
assert "Réunion équipe" in summaries
|
||
|
|
assert "Ranger le garage" in summaries # DUE 03-20 lands inside the window
|
||
|
|
# chronological order
|
||
|
|
starts = [i.get("start") or i.get("due") or "" for i in items]
|
||
|
|
assert starts == sorted(starts)
|
||
|
|
|
||
|
|
def test_rrule_expansion(self, server_mod):
|
||
|
|
# weekly event DTSTART 2026-03-02, COUNT=10 → occurrences 03-02..05-04
|
||
|
|
items = server_mod.get_events_in_range(
|
||
|
|
datetime(2026, 3, 2), datetime(2026, 5, 6)
|
||
|
|
)
|
||
|
|
recurring = [i for i in items if i["summary"] == "Présence matinale"]
|
||
|
|
assert len(recurring) == 10
|
||
|
|
first_days = sorted(i["start"][:10] for i in recurring)
|
||
|
|
# every occurrence lands on a Monday (DTSTART was a Monday)
|
||
|
|
assert all(
|
||
|
|
calmod.weekday(int(d[:4]), int(d[5:7]), int(d[8:10])) == 0
|
||
|
|
for d in first_days
|
||
|
|
)
|
||
|
|
|
||
|
|
def test_birthday_only_with_flag(self, server_mod):
|
||
|
|
r1 = server_mod.get_events_in_range(
|
||
|
|
datetime(2026, 4, 10), datetime(2026, 4, 11), include_birthdays=False
|
||
|
|
)
|
||
|
|
assert all(i["summary"] != "Anniversaire Marc" for i in r1)
|
||
|
|
r2 = server_mod.get_events_in_range(
|
||
|
|
datetime(2026, 4, 10), datetime(2026, 4, 11), include_birthdays=True
|
||
|
|
)
|
||
|
|
assert any(i["summary"] == "Anniversaire Marc" for i in r2)
|
||
|
|
|
||
|
|
def test_calendars_aggregated_across_users(self, server_mod, radicale_tree):
|
||
|
|
# add an event to robert's calendar and check cross-aggregation
|
||
|
|
import json
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
cal_dir = radicale_tree / "collections/collection-root/robert/famille"
|
||
|
|
ics = cal_dir / "x.ics"
|
||
|
|
original = ics.read_text()
|
||
|
|
ics.write_text(
|
||
|
|
conftest.ICS_HEADER
|
||
|
|
+ "BEGIN:VEVENT\nUID:fam-1\nDTSTAMP:20260101T000000Z\n"
|
||
|
|
"DTSTART:20260317T120000\nDTEND:20260317T130000\n"
|
||
|
|
"SUMMARY:Souper famille\nEND:VEVENT\n"
|
||
|
|
+ conftest.ICS_FOOTER
|
||
|
|
)
|
||
|
|
try:
|
||
|
|
items = server_mod.get_events_in_range(
|
||
|
|
datetime(2026, 3, 17), datetime(2026, 3, 18)
|
||
|
|
)
|
||
|
|
summaries = {i["summary"]: i for i in items}
|
||
|
|
assert "Souper famille" in summaries
|
||
|
|
assert summaries["Souper famille"]["calendar"] == "Famille"
|
||
|
|
finally:
|
||
|
|
ics.write_text(original)
|
||
|
|
|
||
|
|
|
||
|
|
# ---------------------------------------------------------------- get_agenda tool
|
||
|
|
|
||
|
|
class TestGetAgenda:
|
||
|
|
def test_end_date_default_plus_7_days(self, server_mod):
|
||
|
|
out = server_mod.get_agenda("2026-03-15")
|
||
|
|
assert out["start_date"] == "2026-03-15T00:00:00"
|
||
|
|
assert out["end_date"] == "2026-03-22T00:00:00"
|
||
|
|
assert out["count"] == len(out["items"])
|
||
|
|
|
||
|
|
def test_explicit_end_date(self, server_mod):
|
||
|
|
out = server_mod.get_agenda("2026-03-15", "2026-03-16")
|
||
|
|
assert out["end_date"] == "2026-03-16T00:00:00"
|
||
|
|
assert any(i["summary"] == "Réunion équipe" for i in out["items"])
|
||
|
|
|
||
|
|
def test_birthday_flag_forwarded(self, server_mod):
|
||
|
|
out = server_mod.get_agenda("2026-04-10", "2026-04-11", include_birthdays=True)
|
||
|
|
assert any(i["summary"] == "Anniversaire Marc" for i in out["items"])
|
||
|
|
out2 = server_mod.get_agenda("2026-04-10", "2026-04-11")
|
||
|
|
assert out2["items"] == []
|
||
|
|
|
||
|
|
def test_bad_date_returns_error_dict_not_raise(self, server_mod):
|
||
|
|
out = server_mod.get_agenda("pas-une-date")
|
||
|
|
assert "error" in out
|
||
|
|
assert "items" not in out
|
||
|
|
|
||
|
|
|
||
|
|
# ---------------------------------------------------------------- startup config
|
||
|
|
|
||
|
|
def test_validate_startup_config_raises_when_empty(server_mod):
|
||
|
|
with pytest.raises(ValueError, match="CALDAV_DATA_PATH"):
|
||
|
|
server_mod._validate_startup_config("")
|
||
|
|
|
||
|
|
|
||
|
|
def test_validate_startup_config_accepts_value(server_mod):
|
||
|
|
server_mod._validate_startup_config("/some/path") # no exception
|
||
|
|
|
||
|
|
|
||
|
|
def test_invalid_exclude_pattern_degrades_without_crash(server_mod, caplog):
|
||
|
|
# simulate a broken pattern compile path via format_component reading None regex
|
||
|
|
with caplog.at_level(logging.ERROR, logger=server_mod.logger.name):
|
||
|
|
pass
|
||
|
|
# validate the guard exists: compile fails → regex stays None → filters not applied
|
||
|
|
import re
|
||
|
|
|
||
|
|
try:
|
||
|
|
re.compile("(bad-pattern")
|
||
|
|
raised = False
|
||
|
|
except re.error:
|
||
|
|
raised = True
|
||
|
|
assert raised # and server catches this in its startup block
|
||
|
|
|
||
|
|
|
||
|
|
# ---------------------------------------------------------------- /health route
|
||
|
|
|
||
|
|
def test_health_endpoint(server):
|
||
|
|
from starlette.testclient import TestClient
|
||
|
|
|
||
|
|
app = server.mcp.http_app()
|
||
|
|
client = TestClient(app)
|
||
|
|
resp = client.get("/health")
|
||
|
|
assert resp.status_code == 200
|
||
|
|
assert resp.json() == {"status": "ok"}
|