diff --git a/syncall/caldav/caldav_side.py b/syncall/caldav/caldav_side.py index 4e97d07..46f2647 100644 --- a/syncall/caldav/caldav_side.py +++ b/syncall/caldav/caldav_side.py @@ -136,6 +136,8 @@ def set_(key: str, val: Any): # noqa: ANN401 set_(key, vText(value)) if key == "categories": set_(key, vCategory([vText(cat) for cat in value])) + if key == SYNCALL_TW_WAITING: + set_(key, vText(value)) todo.save() diff --git a/syncall/caldav/caldav_utils.py b/syncall/caldav/caldav_utils.py index 131b4f8..e8c7988 100644 --- a/syncall/caldav/caldav_utils.py +++ b/syncall/caldav/caldav_utils.py @@ -48,6 +48,10 @@ def _convert_one(name: str) -> str: for name in ["description", "summary"]: todo_item[name] = _convert_one(name) todo_item["uuid"] = _convert_one("x-syncall-tw-uuid") + # Read the waiting flag back too: the TW -> CalDAV conversion always sets it, so without it a + # changed item never compares equal to its counterpart and is updated again on every run. + if vtodo.get("x-syncall-tw-waiting") is not None: + todo_item["x-syncall-tw-waiting"] = str(vtodo.get("x-syncall-tw-waiting")) for date_field in ("due", "created", "completed", "last-modified"): if vtodo.get(date_field): diff --git a/syncall/taskwarrior/taskwarrior_side.py b/syncall/taskwarrior/taskwarrior_side.py index 228aeb0..cb9f216 100644 --- a/syncall/taskwarrior/taskwarrior_side.py +++ b/syncall/taskwarrior/taskwarrior_side.py @@ -278,6 +278,10 @@ def items_are_identical( tw_duration_key, ] if k not in ignore_keys + # Fields that the other side may not carry (e.g. CalDAV has no "scheduled" or + # duration) only count when both items have them; otherwise such a task never + # compares equal and is updated on every run. + and not (k in ("scheduled", tw_duration_key) and ((k in item1) != (k in item2))) ] # special care for the annotations key diff --git a/tests/test_tw_caldav_stable_sync.py b/tests/test_tw_caldav_stable_sync.py new file mode 100644 index 0000000..5534afa --- /dev/null +++ b/tests/test_tw_caldav_stable_sync.py @@ -0,0 +1,29 @@ +"""A task that was changed once must compare equal to its counterpart afterwards, +otherwise it is updated again on every run, alternating sides.""" + +import datetime + +from icalendar import Todo +from syncall.caldav.caldav_utils import map_ics_to_item +from syncall.taskwarrior.taskwarrior_side import TaskWarriorSide +from syncall.tw_caldav_utils import SYNCALL_TW_WAITING, convert_tw_to_caldav + + +def test_waiting_flag_is_read_back_from_caldav(): + tw_task = {"description": "t", "status": "pending", "uuid": "3925e236-3b27-4888-b41c-8c53610d722b"} + sent = convert_tw_to_caldav(tw_task) + todo = Todo() + todo.add("uid", "x") + todo.add("summary", "t") + todo.add("status", "NEEDS-ACTION") + todo.add(SYNCALL_TW_WAITING, sent[SYNCALL_TW_WAITING]) + assert map_ics_to_item(todo)[SYNCALL_TW_WAITING] == sent[SYNCALL_TW_WAITING] + + +def test_scheduled_only_on_one_side_is_not_a_difference(): + due = datetime.datetime(2026, 11, 30, 23, tzinfo=datetime.timezone.utc) + task = {"description": "t", "status": "pending", "due": due, "uuid": "u", + "scheduled": due - datetime.timedelta(days=1)} + from_caldav = {k: v for k, v in task.items() if k != "scheduled"} + assert TaskWarriorSide.items_are_identical(task, from_caldav) + assert not TaskWarriorSide.items_are_identical(task, {**task, "scheduled": due})