mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-11 16:33:56 +01:00
_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
31 lines
1.1 KiB
Python
31 lines
1.1 KiB
Python
"""Wall-clock helpers shared by every adapter in this package.
|
|
|
|
All three adapter modules derive an ``Hlc`` from some ISO timestamp -- an
|
|
entry's birth ``time``, a budget's ``t`` edit stamp, a curated food's
|
|
``editedAt``. They therefore share one parsing rule, and it lives here rather
|
|
than in whichever module happened to be written first: a second copy is how
|
|
two adapters start disagreeing about what an unparsable stamp means, which
|
|
would silently change merge outcomes rather than raising.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
_EPOCH = datetime(1970, 1, 1, tzinfo=timezone.utc)
|
|
|
|
|
|
def _wall_time_ms(stamp: str) -> int:
|
|
"""Return ``stamp``'s epoch milliseconds, or the epoch when unparsable.
|
|
|
|
Falling back to the epoch rather than raising keeps a malformed stamp from
|
|
dropping the record entirely: it sorts early in a last-writer-wins merge,
|
|
so a well-formed copy from any other device wins, which is the outcome the
|
|
user wants.
|
|
"""
|
|
try:
|
|
moment = datetime.fromisoformat(stamp)
|
|
except ValueError:
|
|
moment = _EPOCH
|
|
return int(moment.timestamp() * 1000)
|