diet-guard/diet_guard/tests/test_sync_device_id.py
Krzysztof kuhy Rudnicki 617e1ec879
Some checks failed
dependency freshness / dependencies on newest stable (push) Has been cancelled
file-length / file length <= 250 lines (push) Has been cancelled
md-naming / markdown naming convention (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint) (push) Has been cancelled
Tests / test (push) Has been cancelled
release-apk / release (APK) (push) Has been cancelled
repo-contract / repo contract (push) Has been cancelled
E2E-encrypt diet_guard sync and export keys
Every sync payload (food log, banks, budget, catering credential,
Body, export keyring) now lives on diet-guard-sync/devices-e2e, sealed
with the Argon2id key from the shared crdt-sync passphrase; record
ids, tombstones and clocks stay clear. Old plaintext nodes are copied
once and never written or deleted. Locked = no pull, no push.

- PC: _sync_e2e.open_e2e over crdt-sync 0.11.0, key and copy memo in
  ~/.local/share/diet_guard/e2e; undecryptable peers are skipped.
- Phone/desktop: sync_e2e.dart, crdt_sync_flutter 0.8.1, lock icon
  in Settings; pullExportKeys goes through the same open.
- wake-alarm weigh-ins read from wake-alarm-sync/devices-e2e (own
  header), on both sides.

Todo: fda71ed1

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 10:31:44 +01:00

73 lines
2.7 KiB
Python

"""Tests for this machine's persisted sync device id.
Split out of ``test_sync.py`` to keep it under the repo's 500-line cap.
Covers the migration from the fixed ``"pc"`` role constant to a per-install
uuid -- specifically that the old path is still recognised as this device's
own, so its pre-migration log is not re-merged as a peer's.
"""
from __future__ import annotations
from unittest.mock import MagicMock
from crdt_sync import DeviceIdentity, SyncState
from diet_guard import _sync
from diet_guard._device import device_id, device_identity
def _mock_client(*, devices: tuple[str, ...] = ()) -> MagicMock:
"""Build a mock sync client that lists ``devices`` and holds no files."""
client = MagicMock()
client.list_directory.return_value = list(devices)
client.get_file_text.side_effect = lambda _path: None
del client.get_string_map
return client
class TestPullSkipsOwnLegacyPath:
"""The uuid migration leaves this device's old 'pc' log behind."""
def test_skips_the_devices_own_legacy_id(self) -> None:
"""devices/pc/ is this machine's own history, not a peer's.
Without the legacy id in the skip set, every tick would download and
re-merge the log this machine pushed before it migrated -- idempotent
under LWW, but hundreds of KB of pure waste, 96 times a day.
"""
client = _mock_client(devices=("pc", "phone"))
state = SyncState(pushed_rev=None, peer_revs={})
_, _seen = _sync._pull_remote_logs(
client,
{},
state,
identity=device_identity(),
device_ids=("pc", "phone"),
)
fetched = [call.args[0] for call in client.get_file_text.call_args_list]
assert "diet-guard-sync/devices-e2e/pc/food_log.json" not in fetched
assert "diet-guard-sync/devices-e2e/phone/food_log.json" in fetched
def test_pulls_the_legacy_path_once_it_is_disowned(self) -> None:
"""Negative control: with no legacy id, devices/pc/ IS a peer.
Proves the skip above is the legacy id's doing, not an artifact of
the fixture -- and documents what happens after the old path is
reclaimed and SYNC_LEGACY_DEVICE_ID drops to None.
"""
client = _mock_client(devices=("pc",))
state = SyncState(pushed_rev=None, peer_revs={})
_, _seen = _sync._pull_remote_logs(
client,
{},
state,
identity=DeviceIdentity(device_id=device_id()),
device_ids=("pc",),
)
fetched = [call.args[0] for call in client.get_file_text.call_args_list]
assert "diet-guard-sync/devices-e2e/pc/food_log.json" in fetched