mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-11 13:47:38 +01:00
Eighth batch of the 250-line sweep (over-cap count 28 -> 25). _state.py 366 -> 197 (+ _state_today.py, _state_sync.py) test_state.py 400 -> 221 (+ test_state_today.py, test_state_sync.py) _tk_fakes.py 368 -> 167 (+ _tk_fakes_containers.py, _tk_fakes_canvas.py) _state.py is the file conftest patches FOOD_LOG_FILE on, so the split is constrained rather than free: `FOOD_LOG_FILE` and the two functions that open it stay put, and the derived read model (_state_today) and the sync/undo helpers (_state_sync) reach the file only through _state's private _read_raw_log/_write_log. Callers were rewired to import from the new modules directly rather than via a circular re-export. That constraint is now enforced instead of merely documented. Moving a reader of FOOD_LOG_FILE into a sibling would leave conftest's patch pointing at a name nothing reads: check_patch_targets.py still exits 0 (the path resolves) and the runtime is unchanged (a file write, not a network call), so the suite would go green while writing to the real ~/.local/share/diet_guard and the next sync tick would publish it. test_state_redirect.py asserts both halves -- only _constants and _state may name the constant, and the redirect demonstrably moves a real write off the live path. Verified it fails closed by adding a FOOD_LOG_FILE reader to _state_today: it named that module exactly. _tk_fakes.py keeps _FAKE_TK/_FAKE_TTK and gains an explicit __all__, since the re-exports are load-bearing and ruff would otherwise strip them as unused. The identity canary from the previous commit covers the split: breaking one entry in the assembly (Canvas=object) fails 8 gate tests loudly rather than silently building real widgets. Verified: 706 Python tests / 100% branch coverage, 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
89 lines
2.8 KiB
Python
89 lines
2.8 KiB
Python
"""Tests for _state.py — the HMAC-signed daily food log.
|
|
|
|
State files are redirected into ``tmp_path`` and a deterministic HMAC key is
|
|
provided by the autouse conftest fixtures, so signing, verification, and the
|
|
defensive read paths are all exercised in isolation.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from diet_guard import _state
|
|
from diet_guard._budget import BudgetNotInitializedError, write_budget
|
|
from diet_guard._estimator import Nutrition
|
|
from diet_guard._state import (
|
|
log_meal,
|
|
)
|
|
from diet_guard._state_today import (
|
|
consumption_band,
|
|
logged_slots_today,
|
|
remaining_budget,
|
|
)
|
|
|
|
|
|
def _nut(
|
|
kcal: float, *, protein: float = 0, carbs: float = 0, fat: float = 0
|
|
) -> Nutrition:
|
|
"""Build a Nutrition for a logged meal."""
|
|
return Nutrition(kcal, protein, carbs, fat, 100, "manual")
|
|
|
|
|
|
def _raw() -> dict[str, list[dict[str, object]]]:
|
|
"""Read the raw log file as parsed JSON (no verification)."""
|
|
return json.loads(_state.FOOD_LOG_FILE.read_text(encoding="utf-8"))
|
|
|
|
|
|
class TestLoggedSlots:
|
|
"""Which slots today's log has satisfied."""
|
|
|
|
def test_int_slots_counted(self) -> None:
|
|
"""Integer slot tags are reported."""
|
|
log_meal("a", _nut(1), slot=8)
|
|
log_meal("b", _nut(1), slot=12)
|
|
assert logged_slots_today() == {8, 12}
|
|
|
|
def test_bool_slot_excluded(self) -> None:
|
|
"""A bool masquerading as a slot is ignored."""
|
|
log_meal("a", _nut(1), slot=8)
|
|
raw = _raw()
|
|
day = next(iter(raw))
|
|
raw[day].append({"kcal": 1, "slot": True})
|
|
_state.FOOD_LOG_FILE.write_text(json.dumps(raw), encoding="utf-8")
|
|
assert logged_slots_today() == {8}
|
|
|
|
|
|
class TestBudgetViews:
|
|
"""Remaining budget and the qualitative band."""
|
|
|
|
def test_remaining_requires_budget(self) -> None:
|
|
"""With no budget sealed, remaining_budget raises."""
|
|
with pytest.raises(BudgetNotInitializedError):
|
|
remaining_budget()
|
|
|
|
def test_remaining_value(self) -> None:
|
|
"""Remaining is budget minus today's total."""
|
|
write_budget(2000)
|
|
log_meal("lunch", _nut(500), slot=12)
|
|
assert remaining_budget() == 1500.0
|
|
|
|
def test_band_on_track(self) -> None:
|
|
"""Well under the warn fraction is 'on track'."""
|
|
write_budget(2000)
|
|
log_meal("a", _nut(500), slot=8)
|
|
assert consumption_band() == "on track"
|
|
|
|
def test_band_approaching(self) -> None:
|
|
"""At or above the warn fraction but under budget is 'approaching limit'."""
|
|
write_budget(2000)
|
|
log_meal("a", _nut(1700), slot=8)
|
|
assert consumption_band() == "approaching limit"
|
|
|
|
def test_band_over(self) -> None:
|
|
"""At or above budget is 'OVER BUDGET'."""
|
|
write_budget(2000)
|
|
log_meal("a", _nut(2100), slot=8)
|
|
assert consumption_band() == "OVER BUDGET"
|