mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-11 15:33:57 +01:00
Ninth batch of the 250-line sweep. Every Python file in the repo is now under cap (over-cap count 25 -> 18; the 18 remaining are all Dart). _foodbank.py 365 -> 210 (+ _foodbank_search, _foodbank_rebuild) test_averages.py 351 -> 222 (+ test_averages_report.py) _estimator.py 325 -> 137 (+ _estimator_off.py) _budget.py 323 -> 227 (+ _budget_biometrics, _budget_derived) test_foodbank.py 311 -> 117 (+ test_foodbank_rebuild.py) _averages.py 298 -> 174 (+ _averages_periods, _averages_report) test_budget_history.py 288 -> 162 (+ test_budget_history_io.py) The split boundaries are constrained by which module owns each conftest-patched state path, not by line count. BUDGET_FILE stays in _budget, FOOD_BANK_FILE in _foodbank: the derived/search/rebuild halves reach the file only through their parent's private helpers. Two of the new modules had copied FOOD_BANK_FILE into their headers as an incidental import, which is exactly the leak the guard is for -- both cleaned. test_state_redirect.py now covers all five redirected paths (FOOD_LOG_FILE, BUDGET_FILE, BUDGET_HISTORY_FILE, FOOD_BANK_FILE, MANUAL_BANK_FILE) rather than just the food log, in both directions: only the owning module may name each constant, and each redirect demonstrably moves a real write off the live path. Two rewires fail-closed rather than silently: `_budget.budget_weight` and `_estimator.requests` were module-attribute references no import rewrite touches, and both raised AttributeError immediately. The latter appeared only after ruff removed the now-unused `requests` import from _estimator -- the import was load-bearing solely as a patch target. Verified: 707 Python tests / 100% branch coverage, 5 layout-fits tests under xvfb, check_patch_targets 0, pre-commit clean, ~/.local/share/diet_guard byte-identical. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RA9ynjqXPnexRQnR5oAuCC
249 lines
10 KiB
Python
249 lines
10 KiB
Python
"""Tests for the food bank's half of the sync tick.
|
|
|
|
Split out of ``test_sync.py`` to keep both files under the repo's 500-line
|
|
ceiling. Covers the derived bank (merged max-count-wins, with the log
|
|
authoritative for which foods exist) and the hand-curated bank (merged
|
|
last-writer-wins per food name).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from unittest.mock import patch
|
|
|
|
from diet_guard import _sync, _sync_client
|
|
from diet_guard._device import device_id
|
|
from diet_guard._estimator import Nutrition
|
|
from diet_guard._foodbank import read_food_bank
|
|
from diet_guard._foodbank_manual import add_manual_entry, read_manual_bank
|
|
from diet_guard._foodbank_search import lookup_food
|
|
from diet_guard._state import log_meal
|
|
from diet_guard.sync_merge import food_bank_to_log, manual_bank_to_log
|
|
from diet_guard.tests._sync_fakes import _mock_client, _write_token
|
|
|
|
|
|
class TestSyncFoodBank:
|
|
"""The log-derived food bank's cross-device merge."""
|
|
|
|
def test_the_local_bank_is_pushed(self) -> None:
|
|
_write_token()
|
|
# Through the log, not remember_food: run_sync rebuilds the derived
|
|
# bank from the merged log first, so a bank entry with no log entry
|
|
# behind it is (correctly) discarded before the push.
|
|
log_meal("apple", Nutrition(95, 0.5, 25, 0.3, 180, "manual"), 8)
|
|
client = _mock_client(devices=())
|
|
with patch.object(_sync_client, "GitHubSyncClient", return_value=client):
|
|
_sync.run_sync()
|
|
pushed = [call.args[0] for call in client.put_file_text.call_args_list]
|
|
assert f"diet-guard-sync/devices/{device_id()}/food_bank.json" in pushed
|
|
|
|
def test_a_food_the_phone_logged_reaches_the_gate(self) -> None:
|
|
"""The payoff: a food eaten on the phone autocompletes on the PC.
|
|
|
|
It arrives through the *log* (which merges first in the same tick),
|
|
and the bank rebuild picks it up -- membership follows the log, so
|
|
this is the path that actually delivers cross-device autocomplete.
|
|
"""
|
|
_write_token()
|
|
remote_log_json = json.dumps(
|
|
{
|
|
"2026-06-22": [
|
|
{
|
|
"id": "phone-skyr",
|
|
"time": "2026-06-22T09:00:00+02:00",
|
|
"desc": "Skyr",
|
|
"kcal": 120.0,
|
|
"protein_g": 20.0,
|
|
"carbs_g": 5.0,
|
|
"fat_g": 0.5,
|
|
"grams": 150.0,
|
|
"source": "manual",
|
|
},
|
|
],
|
|
},
|
|
)
|
|
client = _mock_client(
|
|
devices=("phone",),
|
|
files={"diet-guard-sync/devices/phone/food_log.json": remote_log_json},
|
|
)
|
|
with patch.object(_sync_client, "GitHubSyncClient", return_value=client):
|
|
_sync.run_sync()
|
|
found = lookup_food("Skyr")
|
|
assert found is not None
|
|
assert found.kcal == 120.0
|
|
|
|
def test_a_deleted_food_is_not_resurrected_by_a_peer(self) -> None:
|
|
"""Regression: a CRDT union never shrinks.
|
|
|
|
Without the log staying authoritative for membership, a peer's stale
|
|
copy of a food whose entries were all undone out-clocks the local
|
|
absence, gets written back AND re-pushed -- permanently un-deletable.
|
|
"""
|
|
_write_token()
|
|
# This device's log knows nothing of "skyr" (its entries were undone).
|
|
log_meal("apple", Nutrition(95, 0.5, 25, 0.3, 180, "manual"), 8)
|
|
remote = json.dumps(
|
|
{
|
|
rid: rec.to_dict()
|
|
for rid, rec in food_bank_to_log(
|
|
{"skyr": {"desc": "Skyr", "kcal": 120.0, "count": 3}},
|
|
).items()
|
|
},
|
|
)
|
|
client = _mock_client(
|
|
devices=("phone",),
|
|
files={"diet-guard-sync/devices/phone/food_bank.json": remote},
|
|
)
|
|
with patch.object(_sync_client, "GitHubSyncClient", return_value=client):
|
|
_sync.run_sync()
|
|
|
|
assert lookup_food("Skyr") is None
|
|
pushed = [
|
|
call.args[1]
|
|
for call in client.put_file_text.call_args_list
|
|
if call.args[0].endswith("food_bank.json")
|
|
]
|
|
assert pushed
|
|
assert "skyr" not in pushed[0]
|
|
|
|
def test_a_higher_remote_count_still_refines_a_known_food(self) -> None:
|
|
"""Membership defers to the log; content still takes the better count."""
|
|
_write_token()
|
|
log_meal("apple", Nutrition(95, 0.5, 25, 0.3, 180, "manual"), 8)
|
|
remote = json.dumps(
|
|
{
|
|
rid: rec.to_dict()
|
|
for rid, rec in food_bank_to_log(
|
|
{"apple": {"desc": "Apple", "kcal": 95.0, "count": 99}},
|
|
).items()
|
|
},
|
|
)
|
|
client = _mock_client(
|
|
devices=("phone",),
|
|
files={"diet-guard-sync/devices/phone/food_bank.json": remote},
|
|
)
|
|
with patch.object(_sync_client, "GitHubSyncClient", return_value=client):
|
|
_sync.run_sync()
|
|
assert read_food_bank()["apple"]["count"] == 99
|
|
|
|
def test_unparsable_remote_food_bank_is_skipped(self) -> None:
|
|
_write_token()
|
|
log_meal("apple", Nutrition(95, 0.5, 25, 0.3, 180, "manual"), 8)
|
|
client = _mock_client(
|
|
devices=("phone",),
|
|
files={"diet-guard-sync/devices/phone/food_bank.json": "{not json"},
|
|
)
|
|
with patch.object(_sync_client, "GitHubSyncClient", return_value=client):
|
|
_sync.run_sync()
|
|
assert lookup_food("apple") is not None
|
|
|
|
def test_nothing_pushed_when_no_device_has_a_bank(self) -> None:
|
|
_write_token()
|
|
client = _mock_client(devices=())
|
|
with patch.object(_sync_client, "GitHubSyncClient", return_value=client):
|
|
_sync.run_sync()
|
|
pushed = [call.args[0] for call in client.put_file_text.call_args_list]
|
|
assert f"diet-guard-sync/devices/{device_id()}/food_bank.json" not in pushed
|
|
|
|
|
|
class TestSyncManualBank:
|
|
"""The hand-curated food bank's cross-device merge."""
|
|
|
|
def test_nothing_pushed_when_no_device_has_curated_anything(self) -> None:
|
|
_write_token()
|
|
client = _mock_client(devices=())
|
|
with patch.object(_sync_client, "GitHubSyncClient", return_value=client):
|
|
_sync.run_sync()
|
|
pushed = [call.args[0] for call in client.put_file_text.call_args_list]
|
|
assert not any("food_bank_manual" in path for path in pushed)
|
|
|
|
def test_local_curated_entry_is_pushed(self) -> None:
|
|
_write_token()
|
|
add_manual_entry("Skyr", {"desc": "Skyr", "kcal": 120.0, "count": 0})
|
|
client = _mock_client(devices=())
|
|
with patch.object(_sync_client, "GitHubSyncClient", return_value=client):
|
|
_sync.run_sync()
|
|
pushed = [call.args[0] for call in client.put_file_text.call_args_list]
|
|
assert f"diet-guard-sync/devices/{device_id()}/food_bank_manual.json" in pushed
|
|
|
|
def test_a_remote_curated_entry_is_adopted_locally(self) -> None:
|
|
"""The point of the whole feature: a food added on the phone is usable
|
|
on the PC, in the gate's autocomplete, without ever being eaten."""
|
|
_write_token()
|
|
remote = json.dumps(
|
|
{
|
|
record_id: record.to_dict()
|
|
for record_id, record in manual_bank_to_log(
|
|
{
|
|
"skyr": {
|
|
"desc": "Skyr",
|
|
"kcal": 120.0,
|
|
"protein_g": 20.0,
|
|
"carbs_g": 5.0,
|
|
"fat_g": 0.5,
|
|
"grams": 150.0,
|
|
"count": 0,
|
|
"t": "2026-07-26T10:00:00+02:00",
|
|
},
|
|
},
|
|
).items()
|
|
},
|
|
)
|
|
client = _mock_client(
|
|
devices=("phone",),
|
|
files={"diet-guard-sync/devices/phone/food_bank_manual.json": remote},
|
|
)
|
|
with patch.object(_sync_client, "GitHubSyncClient", return_value=client):
|
|
_sync.run_sync()
|
|
assert "skyr" in read_manual_bank()
|
|
found = lookup_food("Skyr")
|
|
assert found is not None
|
|
assert found.kcal == 120.0
|
|
|
|
def test_a_newer_remote_edit_wins(self) -> None:
|
|
_write_token()
|
|
add_manual_entry("Skyr", {"desc": "Skyr", "kcal": 120.0, "count": 0})
|
|
remote = json.dumps(
|
|
{
|
|
record_id: record.to_dict()
|
|
for record_id, record in manual_bank_to_log(
|
|
{
|
|
"skyr": {
|
|
"desc": "Skyr",
|
|
"kcal": 999.0,
|
|
"count": 0,
|
|
"t": "2999-01-01T00:00:00+02:00",
|
|
},
|
|
},
|
|
).items()
|
|
},
|
|
)
|
|
client = _mock_client(
|
|
devices=("phone",),
|
|
files={"diet-guard-sync/devices/phone/food_bank_manual.json": remote},
|
|
)
|
|
with patch.object(_sync_client, "GitHubSyncClient", return_value=client):
|
|
_sync.run_sync()
|
|
assert read_manual_bank()["skyr"]["kcal"] == 999.0
|
|
|
|
def test_unparsable_remote_curated_bank_is_skipped(self) -> None:
|
|
_write_token()
|
|
add_manual_entry("Skyr", {"desc": "Skyr", "kcal": 120.0, "count": 0})
|
|
client = _mock_client(
|
|
devices=("phone",),
|
|
files={
|
|
"diet-guard-sync/devices/phone/food_bank_manual.json": "{not json",
|
|
},
|
|
)
|
|
with patch.object(_sync_client, "GitHubSyncClient", return_value=client):
|
|
_sync.run_sync()
|
|
assert read_manual_bank()["skyr"]["kcal"] == 120.0
|
|
|
|
def test_a_device_with_no_pushed_curated_bank_is_skipped(self) -> None:
|
|
_write_token()
|
|
add_manual_entry("Skyr", {"desc": "Skyr", "kcal": 120.0, "count": 0})
|
|
client = _mock_client(devices=("phone",))
|
|
with patch.object(_sync_client, "GitHubSyncClient", return_value=client):
|
|
_sync.run_sync()
|
|
assert read_manual_bank()["skyr"]["kcal"] == 120.0
|