diet-guard/diet_guard/tests/_sync_fakes.py
Krzysztof kuhy Rudnicki 1e201acbdc Split _sync.py and its tests; extract the gate fixtures from conftest
_sync.py 498 -> 165, into siblings that each own one concern:

  _sync_client.py  picking a backend and building the remote store
  _sync_banks.py   pull/merge/push for the food bank and the budget
  _sync_paths.py   the kuhyx/syncs wire layout, shared by both
  _sync_errors.py  SyncError, so _sync and _sync_client need not import
                   each other

The patch targets moved with the code they guard, which is the whole risk of
this commit. unittest.mock.patch and monkeypatch.setattr both bind to a
*module object*, so a patch left pointing at _sync after CONFIG_FILE and
SYNC_TOKEN_FILE moved to _sync_client redirects nothing. That is not
hypothetical here: the first run of the suite took 9m18s instead of 7s and
raised 401s, because tests were reaching the real GitHub API with a fake
token. Repointed:

  conftest       SYNC_TOKEN_FILE, CONFIG_FILE      -> _sync_client
  test_sync*     GitHubSyncClient (30 patch sites) -> _sync_client
  test_backend   mirror/firebase_client_for        -> _sync_client

The repo's own _test_guard caught the same leak independently, which is the
behaviour worth keeping: two guards, both fail-closed.

test_sync.py 482 -> 205, splitting the budget and pull/revision classes into
siblings and the shared fakes into _sync_fakes.py.

conftest.py 284 -> 155, moving the Tk/gatelock fixtures to _gate_fixtures.py.
Importing them back is what registers them (pytest_plugins is an error in a
non-root conftest), and they are named in __all__ because ruff's autofix
otherwise deletes the "unused" import and silently disables Tk blocking --
it did exactly that once here. Verified afterwards with a canary asserting
_gatelock.tk is still a MagicMock, not merely that the suite passes.

Verified: 692 tests / 100% coverage unchanged; ~/.local/share/diet_guard
byte-identical before and after the run; cd /tmp && python3 -m diet_guard sync
-> 402 entries, exit 0. Gate 47 -> 44.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK3HCZrM75sRHL1gL8usve
2026-08-15 18:51:18 +02:00

60 lines
2.0 KiB
Python

"""Shared fakes for the sync tests.
Not a ``test_`` module: these are fixtures-by-hand imported by
``test_sync.py``, ``test_sync_banks.py`` and ``test_sync_pull.py``, and
pytest must not collect them as tests. The repo already excludes this
naming pattern from the ``name-tests-test`` hook.
"""
from __future__ import annotations
import json
from unittest.mock import MagicMock
from diet_guard import _sync_client
from diet_guard._estimator import Nutrition
from diet_guard.sync_merge import budget_to_log
def _remote_budget_json(*, kcal: int, t: str, weight_kg: float | None = None) -> str:
"""Build the wire text a remote device would push for a given budget edit."""
record: dict[str, object] = {"v": 2, "b": kcal, "t": t}
if weight_kg is not None:
record["w"] = weight_kg
log = budget_to_log(record)
return json.dumps({rid: rec.to_dict() for rid, rec in log.items()}, indent=2)
def _nutrition(kcal: float = 200.0) -> Nutrition:
return Nutrition(
kcal=kcal,
protein_g=10.0,
carbs_g=20.0,
fat_g=5.0,
grams=100.0,
source="manual",
)
def _write_token(token: str = "fake-token") -> None:
_sync_client.SYNC_TOKEN_FILE.parent.mkdir(parents=True, exist_ok=True)
_sync_client.SYNC_TOKEN_FILE.write_text(token)
def _mock_client(
*,
devices: tuple[str, ...] = (),
files: dict[str, str] | None = None,
) -> MagicMock:
"""Build a mock ``GitHubSyncClient`` covering the methods sync calls."""
client = MagicMock()
client.list_directory.return_value = list(devices)
resolved_files = files or {}
client.get_file_text.side_effect = resolved_files.get
# The real GitHubSyncClient has no bulk-map read, and a MagicMock would
# otherwise auto-create one returning a Mock -- which then fails to
# serialise. Deleting it also exercises the documented degrade path:
# no revision map means every peer is fetched, exactly as before.
del client.get_string_map
return client