diet-guard/diet_guard/tests/test_cli_sync.py
Krzysztof kuhy Rudnicki 59d1c29dd2 Migrate sync to shared crdt_sync library (Python + Dart)
Replace the bespoke GitHub-Gist sync with the shared crdt-sync /
crdt_sync_dart libraries so diet-guard uses the same CRDT transport as
the other apps.

- Python: delete _sync_github.py (+ its test); rewrite _sync.py,
  _cli_sync.py, _sync_merge.py against crdt-sync. Add the crdt-sync git
  dependency (pyproject + pylint deps) and S107 to the test ignore list.
- Dart: rewrite app sync_service.dart / sync_merge.dart against
  crdt_sync_dart (new git dep + crypto); update log_meal and settings
  screens and their tests.

Verification: pytest 347 passed at 100% coverage; flutter analyze clean;
flutter test 306 passed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RRCdR4D8SaVrN2xraMQxM
2026-07-10 23:49:43 +02:00

45 lines
1.4 KiB
Python

"""Tests for the sync subcommand's handler, split out of test_cli.py
alongside its source module (see _cli_sync.py's module docstring).
"""
from __future__ import annotations
from unittest.mock import patch
from crdt_sync import GitHubSyncError
from diet_guard import _cli_sync
from diet_guard._sync import SyncError
class TestCmdSync:
def test_reports_synced_entry_count(self) -> None:
merged = {
"2026-06-22": [{"id": "a"}, {"id": "b"}],
"2026-06-21": [{"id": "c"}],
}
lines: list[str] = []
with patch.object(_cli_sync, "run_sync", return_value=merged):
assert _cli_sync.cmd_sync(lines.append) == 0
assert lines == ["synced: 3 entries across 2 day(s)."]
def test_reports_sync_error_as_not_configured(self) -> None:
lines: list[str] = []
with patch.object(
_cli_sync,
"run_sync",
side_effect=SyncError("no token"),
):
assert _cli_sync.cmd_sync(lines.append) == 1
assert lines == ["sync not configured: no token"]
def test_reports_github_sync_error_as_failed(self) -> None:
lines: list[str] = []
with patch.object(
_cli_sync,
"run_sync",
side_effect=GitHubSyncError("network down"),
):
assert _cli_sync.cmd_sync(lines.append) == 1
assert lines == ["sync failed: network down"]