mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-19 23:14:25 +01:00
A meal logged on the phone while the gate window was up changed nothing until Fetch was pressed: the new credit watch only fires on a local write to the food log, and the PC never pulled on its own. While a non-demo window is open it now pulls peer logs every 30 s, with the Fetch button's own narrow pull on a worker thread. One pull is in flight at a time (shared with the button), failures are logged and retried, and the window stays up. The pull's write trips the watch, which closes through the usual reconcile path. gatelock moves to 0.14.0 for watch_credit. refresh_peer_logs also read the local log before the peer downloads and wrote it after, so a meal logged during the pull was overwritten. It now merges the peers first and reads the local log just before the write. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WzxdWWfb63jncj9Lca2jeX
52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
"""A meal logged while a peer pull is in flight must survive it.
|
|
|
|
The gate now pulls on a timer, so a pull's network reads can overlap the user's
|
|
own "Log & Continue". The merge must therefore read the local log *after* the
|
|
peers have been fetched, immediately before the write -- not seconds earlier.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
from unittest.mock import patch
|
|
|
|
from diet_guard import _sync_client, _sync_refresh
|
|
from diet_guard._state import log_meal
|
|
from diet_guard._state_sync import read_raw_log
|
|
from diet_guard.tests._sync_fakes import _mock_client, _nutrition, _write_token
|
|
from diet_guard.tests.test_sync_refresh import _peer_log_text
|
|
|
|
if TYPE_CHECKING:
|
|
from crdt_sync import RemoteStore
|
|
|
|
|
|
def _descs() -> set[str]:
|
|
return {
|
|
str(entry.get("desc"))
|
|
for entries in read_raw_log().values()
|
|
for entry in entries
|
|
}
|
|
|
|
|
|
def test_a_meal_logged_during_the_peer_read_is_not_overwritten() -> None:
|
|
_write_token()
|
|
text = _peer_log_text()
|
|
client = _mock_client(
|
|
devices=("phone",),
|
|
files={"diet-guard-sync/devices-e2e/phone/food_log.json": text},
|
|
)
|
|
real_read = _sync_refresh.read_sealed
|
|
|
|
def read_then_user_logs(store: RemoteStore, path: str) -> str | None:
|
|
got = real_read(store, path)
|
|
log_meal("typed meanwhile", _nutrition(), 12) # lands mid-pull
|
|
return got
|
|
|
|
with (
|
|
patch.object(_sync_client, "GitHubSyncClient", return_value=client),
|
|
patch.object(_sync_refresh, "read_sealed", read_then_user_logs),
|
|
):
|
|
_sync_refresh.refresh_peer_logs()
|
|
|
|
assert _descs() == {"peer porridge", "typed meanwhile"}
|