utils/crdt-sync/crdt_sync/tests/test_sync.py
Krzysztof kuhy Rudnicki 6eef99d661
Some checks are pending
crdt-sync pre-commit / pre-commit (push) Waiting to run
crdt-sync tests / test (3.10) (push) Waiting to run
crdt-sync tests / test (3.11) (push) Waiting to run
crdt-sync tests / test (3.12) (push) Waiting to run
Add DeviceIdentity and a legacy device id for the uuid migration
Groundwork for moving diet-guard, screen-locker and wake-alarm off fixed
role ids ('pc'/'phone'/'desktop') onto per-install uuids, the scheme todo
and home_inventory already use. A role constant collides the moment a
second machine takes the same role, and a reinstall silently inherits the
previous install's CRDT identity.

The migration cannot rewrite history: the device id is baked into every HLC
stamp already written, is the record path segment and the revision key. So
a device that has just switched ids must still recognise its former id as
its own -- otherwise skip-own-writes stops matching the old path, and the
device pulls back and re-merges its own pre-migration log as though a peer
had written it. Idempotent under LWW, but pure wasted transfer on every
tick.

Both halves therefore take an optional legacy id and test set membership
(own_ids / ownIds) rather than equality against a single id:

- crdt-sync (Python) 0.5.1 -> 0.6.0: new crdt_sync._device with
  DeviceIdentity + load_device_identity (generate-or-load, persisted); the
  mkdir is deliberately unguarded, since handing back an unpersisted id
  would mint a fresh uuid every restart and strand a device dir each time.
  sync_log gains legacy_device_id.
- crdt_sync_dart 0.6.0 -> 0.7.0: DeviceIdentity value type; syncLog gains
  legacyDeviceId. Persistence stays app-side, where the existing uuid apps
  already keep it in SharedPreferences under 'crdt.nodeId'.

Both changes are backward compatible: the parameter defaults to null, which
reproduces the previous single-id behaviour exactly.

Each half has a positive test and a negative control proving the old path
*is* pulled when no legacy id is declared -- so the skip is verified to be
the parameter's doing, not an artifact of the fixture.

Python: 239 tests, 100% branch coverage. Dart: 248 tests, analyzer clean,
device.dart 11/11 and sync.dart 57/57 lines covered.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GZH89WEqAjuJT8WZdqc7AP
2026-08-09 16:54:33 +02:00

223 lines
6.4 KiB
Python

"""Tests for the pull/merge/push sync orchestration."""
from __future__ import annotations
import json
import logging
from typing import TYPE_CHECKING
from unittest.mock import MagicMock
from crdt_sync import GitHubSyncClient, Record, sync_log
if TYPE_CHECKING:
from collections.abc import Callable
import pytest
from crdt_sync import Hlc, Log
def _encode(log: Log) -> str:
return json.dumps(
{record_id: record.to_dict() for record_id, record in log.items()}
)
def _decode(text: str) -> Log:
return {
record_id: Record.from_dict(data)
for record_id, data in json.loads(text).items()
}
def _mock_client(
*,
devices: tuple[str, ...] = (),
files: dict[str, str] | None = None,
) -> MagicMock:
"""Build a mock ``GitHubSyncClient`` covering the methods sync_log calls."""
client = MagicMock(spec=GitHubSyncClient)
client.list_directory.return_value = list(devices)
resolved_files = files or {}
client.get_file_text.side_effect = resolved_files.get
return client
class TestSyncLog:
def test_pushes_local_log_when_no_other_devices_have_synced(
self, make_hlc: Callable[..., Hlc]
) -> None:
local_log = {"a": Record(id="a", fields={"text": ("hello", make_hlc(100))})}
client = _mock_client()
merged = sync_log(
client=client,
device_id="pc",
path_prefix="devices",
local_log=local_log,
encode=_encode,
decode=_decode,
)
assert merged == local_log
client.put_file_text.assert_called_once()
assert client.put_file_text.call_args.args[0] == "devices/pc/log.json"
def test_skips_its_own_device_id_when_listing(self) -> None:
client = _mock_client(
devices=("pc", "phone"), files={"devices/phone/log.json": "{}"}
)
sync_log(
client=client,
device_id="pc",
path_prefix="devices",
local_log={},
encode=_encode,
decode=_decode,
)
client.get_file_text.assert_called_once_with("devices/phone/log.json")
def test_skips_its_legacy_device_id_as_well_as_its_current_one(self) -> None:
"""A migrated device must not re-merge its own pre-migration log.
Without this the old path looks like a peer, so the device pulls
back everything it pushed under its former id every single tick.
"""
client = _mock_client(
devices=("pc", "new-uuid", "phone"),
files={"devices/phone/log.json": "{}"},
)
sync_log(
client=client,
device_id="new-uuid",
path_prefix="devices",
local_log={},
encode=_encode,
decode=_decode,
legacy_device_id="pc",
)
client.get_file_text.assert_called_once_with("devices/phone/log.json")
def test_pulls_the_old_path_when_no_legacy_id_is_declared(self) -> None:
"""Sanity check the previous test: absent the legacy id, it *is* pulled."""
client = _mock_client(
devices=("pc", "new-uuid"), files={"devices/pc/log.json": "{}"}
)
sync_log(
client=client,
device_id="new-uuid",
path_prefix="devices",
local_log={},
encode=_encode,
decode=_decode,
)
client.get_file_text.assert_called_once_with("devices/pc/log.json")
def test_skips_a_device_with_no_pushed_file_yet(self) -> None:
client = _mock_client(devices=("phone",), files={})
merged = sync_log(
client=client,
device_id="pc",
path_prefix="devices",
local_log={},
encode=_encode,
decode=_decode,
)
assert merged == {}
def test_skips_a_device_whose_pushed_file_is_corrupt(
self, caplog: pytest.LogCaptureFixture
) -> None:
"""An interrupted/truncated push must not crash every other device's
merge -- it is treated the same as a device that hasn't pushed yet.
"""
client = _mock_client(
devices=("phone",),
files={"devices/phone/log.json": "{not valid json"},
)
with caplog.at_level(logging.WARNING):
merged = sync_log(
client=client,
device_id="pc",
path_prefix="devices",
local_log={},
encode=_encode,
decode=_decode,
)
assert merged == {}
assert "Unparsable log" in caplog.text
def test_skips_a_device_whose_pushed_json_has_the_wrong_shape(self) -> None:
"""Valid JSON that isn't a record map (e.g. from an incompatible
writer) must be skipped like corrupt JSON, not crash the whole
sync -- this is what the broad ``(ValueError, KeyError, TypeError)``
catch in ``_pull_remote_logs`` is for, not just JSON syntax errors.
"""
client = _mock_client(
devices=("phone",),
files={"devices/phone/log.json": '{"a": 5}'},
)
merged = sync_log(
client=client,
device_id="pc",
path_prefix="devices",
local_log={},
encode=_encode,
decode=_decode,
)
assert merged == {}
def test_merges_in_a_remote_devices_entries(
self, make_hlc: Callable[..., Hlc]
) -> None:
remote_log = {
"b": Record(id="b", fields={"text": ("from phone", make_hlc(100))})
}
client = _mock_client(
devices=("phone",),
files={"devices/phone/log.json": _encode(remote_log)},
)
merged = sync_log(
client=client,
device_id="pc",
path_prefix="devices",
local_log={},
encode=_encode,
decode=_decode,
)
assert merged == remote_log
def test_uses_a_custom_filename_and_commit_message(self) -> None:
client = _mock_client()
sync_log(
client=client,
device_id="pc",
path_prefix="devices",
local_log={},
encode=_encode,
decode=_decode,
filename="notes.json",
commit_message="custom message",
)
client.put_file_text.assert_called_once_with(
"devices/pc/notes.json",
"{}",
message="custom message",
)