mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-11 16:53:57 +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
208 lines
7.6 KiB
Python
208 lines
7.6 KiB
Python
"""Tests for diet_guard._daystatus: status classification, streaks, tallies."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import date
|
|
|
|
from diet_guard import _daystatus, _state, _state_sync
|
|
from diet_guard._budget_history import EPOCH_DAY, BudgetEntry, BudgetSchedule
|
|
from diet_guard._daystatus import DayStatus
|
|
from diet_guard._estimator import Nutrition
|
|
|
|
|
|
def _entry(kcal: float) -> dict[str, object]:
|
|
return {"kcal": kcal}
|
|
|
|
|
|
def _flat(budget: int) -> BudgetSchedule:
|
|
"""A schedule where one budget has applied since the beginning of time."""
|
|
return BudgetSchedule(
|
|
(BudgetEntry(EPOCH_DAY, budget, "1970-01-01T00:00:00+00:00"),),
|
|
default=budget,
|
|
)
|
|
|
|
|
|
class TestDayTotalKcal:
|
|
def test_sums_entries(self) -> None:
|
|
log = {"2026-01-01": [_entry(100), _entry(50)]}
|
|
assert _daystatus.day_total_kcal(log, "2026-01-01") == 150
|
|
|
|
def test_missing_day_is_zero(self) -> None:
|
|
assert _daystatus.day_total_kcal({}, "2026-01-01") == 0
|
|
|
|
|
|
class TestDayStatus:
|
|
def test_missing_day_is_not_logged(self) -> None:
|
|
assert _daystatus.day_status({}, "2026-01-01", 2000) == DayStatus.NOT_LOGGED
|
|
|
|
def test_exactly_at_budget_is_green(self) -> None:
|
|
log = {"2026-01-01": [_entry(2000)]}
|
|
assert _daystatus.day_status(log, "2026-01-01", 2000) == DayStatus.GREEN
|
|
|
|
def test_under_budget_is_green(self) -> None:
|
|
log = {"2026-01-01": [_entry(1000)]}
|
|
assert _daystatus.day_status(log, "2026-01-01", 2000) == DayStatus.GREEN
|
|
|
|
def test_just_over_budget_is_yellow(self) -> None:
|
|
log = {"2026-01-01": [_entry(2001)]}
|
|
assert _daystatus.day_status(log, "2026-01-01", 2000) == DayStatus.YELLOW
|
|
|
|
def test_exactly_at_yellow_ceiling_is_yellow(self) -> None:
|
|
# 2400 sits right at the yellow ceiling for a 2000 budget (120%).
|
|
log = {"2026-01-01": [_entry(2400)]}
|
|
assert _daystatus.day_status(log, "2026-01-01", 2000) == DayStatus.YELLOW
|
|
|
|
def test_just_over_yellow_ceiling_is_red(self) -> None:
|
|
log = {"2026-01-01": [_entry(2400.01)]}
|
|
assert _daystatus.day_status(log, "2026-01-01", 2000) == DayStatus.RED
|
|
|
|
def test_way_over_budget_is_red(self) -> None:
|
|
log = {"2026-01-01": [_entry(5000)]}
|
|
assert _daystatus.day_status(log, "2026-01-01", 2000) == DayStatus.RED
|
|
|
|
def test_day_with_only_tombstoned_entries_is_not_logged(self) -> None:
|
|
# A tombstoned entry is kept on disk but load_log() drops it, so the
|
|
# day is entirely absent from the filtered log rather than present
|
|
# with an empty list -- confirm that reads as NOT_LOGGED, not GREEN.
|
|
_state.log_meal("snack", Nutrition(300, 5, 40, 10, 100, "manual"))
|
|
_state_sync.undo_last_today()
|
|
today = _state._today()
|
|
assert today not in _state.load_log()
|
|
assert (
|
|
_daystatus.day_status(_state.load_log(), today, 2000)
|
|
== DayStatus.NOT_LOGGED
|
|
)
|
|
|
|
|
|
class TestStatusMap:
|
|
def test_maps_every_present_day(self) -> None:
|
|
log = {
|
|
"2026-01-01": [_entry(1000)],
|
|
"2026-01-02": [_entry(5000)],
|
|
}
|
|
result = _daystatus.status_map(log, schedule=_flat(2000))
|
|
assert result == {
|
|
"2026-01-01": DayStatus.GREEN,
|
|
"2026-01-02": DayStatus.RED,
|
|
}
|
|
|
|
def test_empty_log_is_empty_map(self) -> None:
|
|
assert _daystatus.status_map({}, schedule=_flat(2000)) == {}
|
|
|
|
def test_each_day_uses_the_budget_in_force_that_day(self) -> None:
|
|
"""The whole point: a budget cut must not reclassify earlier days.
|
|
|
|
Both days total exactly 2100 kcal. Under the old 2200 budget that is
|
|
green; under the new 2000 budget it is yellow. The day before the
|
|
change must stay green.
|
|
"""
|
|
log = {
|
|
"2026-06-01": [_entry(2100)],
|
|
"2026-07-26": [_entry(2100)],
|
|
}
|
|
schedule = BudgetSchedule(
|
|
(
|
|
BudgetEntry(EPOCH_DAY, 2200, "1970-01-01T00:00:00+00:00"),
|
|
BudgetEntry("2026-07-26", 2000, "2026-07-26T10:00:00+02:00"),
|
|
),
|
|
default=2200,
|
|
)
|
|
result = _daystatus.status_map(log, schedule=schedule)
|
|
assert result["2026-06-01"] == DayStatus.GREEN
|
|
assert result["2026-07-26"] == DayStatus.YELLOW
|
|
|
|
|
|
class TestLoggingStreak:
|
|
def test_empty_map_is_zero(self) -> None:
|
|
assert _daystatus.logging_streak({}, today=date(2026, 1, 5)) == 0
|
|
|
|
def test_counts_consecutive_logged_days_including_today(self) -> None:
|
|
sm = {
|
|
"2026-01-03": DayStatus.GREEN,
|
|
"2026-01-04": DayStatus.RED,
|
|
"2026-01-05": DayStatus.YELLOW,
|
|
}
|
|
assert _daystatus.logging_streak(sm, today=date(2026, 1, 5)) == 3
|
|
|
|
def test_breaks_on_gap(self) -> None:
|
|
sm = {
|
|
"2026-01-01": DayStatus.GREEN,
|
|
"2026-01-03": DayStatus.GREEN,
|
|
"2026-01-04": DayStatus.GREEN,
|
|
}
|
|
assert _daystatus.logging_streak(sm, today=date(2026, 1, 4)) == 2
|
|
|
|
def test_today_not_logged_is_not_a_break(self) -> None:
|
|
sm = {
|
|
"2026-01-03": DayStatus.GREEN,
|
|
"2026-01-04": DayStatus.YELLOW,
|
|
}
|
|
# today (01-05) has no entry yet -- should not zero out the streak.
|
|
assert _daystatus.logging_streak(sm, today=date(2026, 1, 5)) == 2
|
|
|
|
def test_yesterday_not_logged_breaks_streak_even_if_today_is(self) -> None:
|
|
sm = {
|
|
"2026-01-03": DayStatus.GREEN,
|
|
"2026-01-05": DayStatus.GREEN,
|
|
}
|
|
assert _daystatus.logging_streak(sm, today=date(2026, 1, 5)) == 1
|
|
|
|
|
|
class TestAdherenceStreak:
|
|
def test_counts_consecutive_green_and_yellow(self) -> None:
|
|
sm = {
|
|
"2026-01-03": DayStatus.GREEN,
|
|
"2026-01-04": DayStatus.YELLOW,
|
|
"2026-01-05": DayStatus.GREEN,
|
|
}
|
|
assert _daystatus.adherence_streak(sm, today=date(2026, 1, 5)) == 3
|
|
|
|
def test_red_today_breaks_streak_immediately(self) -> None:
|
|
sm = {
|
|
"2026-01-04": DayStatus.GREEN,
|
|
"2026-01-05": DayStatus.RED,
|
|
}
|
|
assert _daystatus.adherence_streak(sm, today=date(2026, 1, 5)) == 0
|
|
|
|
def test_not_logged_today_is_not_a_break(self) -> None:
|
|
sm = {"2026-01-04": DayStatus.GREEN}
|
|
assert _daystatus.adherence_streak(sm, today=date(2026, 1, 5)) == 1
|
|
|
|
def test_red_in_history_breaks_streak(self) -> None:
|
|
sm = {
|
|
"2026-01-03": DayStatus.GREEN,
|
|
"2026-01-04": DayStatus.RED,
|
|
"2026-01-05": DayStatus.GREEN,
|
|
}
|
|
assert _daystatus.adherence_streak(sm, today=date(2026, 1, 5)) == 1
|
|
|
|
|
|
class TestYearToDateTally:
|
|
def test_counts_logged_and_adherent_days_this_year_only(self) -> None:
|
|
sm = {
|
|
"2026-01-01": DayStatus.GREEN,
|
|
"2026-01-02": DayStatus.RED,
|
|
"2026-01-03": DayStatus.NOT_LOGGED,
|
|
"2025-12-31": DayStatus.GREEN, # previous year, excluded
|
|
}
|
|
tally = _daystatus.year_to_date_tally(sm, today=date(2026, 1, 3))
|
|
assert tally.logged_days == 2
|
|
assert tally.elapsed_days == 3
|
|
assert tally.adherent_days == 1
|
|
|
|
def test_future_day_in_map_excluded(self) -> None:
|
|
sm = {
|
|
"2026-01-01": DayStatus.GREEN,
|
|
"2026-06-01": DayStatus.GREEN,
|
|
}
|
|
tally = _daystatus.year_to_date_tally(sm, today=date(2026, 1, 1))
|
|
assert tally.logged_days == 1
|
|
assert tally.elapsed_days == 1
|
|
assert tally.adherent_days == 1
|
|
|
|
def test_empty_map(self) -> None:
|
|
tally = _daystatus.year_to_date_tally({}, today=date(2026, 3, 1))
|
|
assert tally.logged_days == 0
|
|
assert tally.elapsed_days == 60
|
|
assert tally.adherent_days == 0
|