diet-guard/diet_guard/tests/test_daystatus.py
Krzysztof kuhy Rudnicki f7f06aee53
Some checks failed
pre-commit / pre-commit (push) Has been cancelled
Tests / test (3.10) (push) Has been cancelled
Tests / test (3.11) (push) Has been cancelled
Tests / test (3.12) (push) Has been cancelled
Remove unused features; fix reminder false alarms; share all state
Four asks, plus the fixes two review passes turned up.

Removals (all unused):
- "Repeat last meal" button, and the reward/temptation-bundling prompt it
  was the sole trigger for. Replaced with a post-log progress card showing
  kcal against budget, macros, and the adherence streak.
- The multi-item meal builder, on both the gate and the phone. The stored
  `components` field stays: nothing writes it now, but historical composite
  entries and the food bank still read it and it is part of the wire format.
- Meal photos entirely: 13 files, `imagePath`, the photo filter, the
  wrapper's /blobs/ route, image_picker, and the CAMERA permission.

Reminder false alarms had three real causes, all fixed:
- Logging never reconciled notifications, so a reminder sat on screen up to
  15 minutes after the meal was logged -- on the phone itself.
- The background check never pulled, so a PC-logged meal nagged forever
  until the app was opened. Now mirrors _cli_gate._should_lock: cheap local
  check, network only when a slot looks due, then re-check.
- Meals before 08:00 / after 22:00 were attributed to no slot at all. New
  slot_for_log/slotForLog clamps them to the first/last slot, byte-identical
  across Python and Dart. Deliberately separate from elapsed_slots, which
  would otherwise make every slot fall due at 23:00.

Budget history, so lowering the budget stops rewriting the past:
- Forward-only (effective_from, kcal) entries; each day is judged against
  the budget in force then. Verified live: setting 2000 left a 36-day
  adherence streak and the whole calendar pixel-identical.
- Rides as hist:<date> fields on the existing budget record. crdt_sync
  merges per-field over the union of field names and both devices push the
  merged log, so a device that predates the feature relays them untouched --
  no coordinated rollout, no second document.
- The seed guard tests emptiness, not file presence: _sync can write an
  empty history after merging with a pre-feature peer, and a presence check
  would then never seed again, silently reclassifying every past day.

One shared source of truth for every piece of data:
- Body weight now travels as its own `weight` CRDT field. Inside `value` it
  was collateral damage of whole-map LWW -- any phone budget edit deleted it,
  and the protein target with it.
- Both halves of the food bank sync. The curated half (foods added without
  eating them) was the last device-local state. The derived half merges with
  count as the clock, so LWW means max-count-wins; the log stays
  authoritative for membership, because a CRDT union never shrinks and a
  peer's stale copy would otherwise resurrect a deleted food permanently.
- Bank adapters honour tombstones, and Dart parses bank records defensively:
  a cast failure there is an Error, not an Exception, so one malformed
  remote record would have broken phone sync permanently.

_test_guard.py blocks any pathlib write to real user state while pytest is
in the process, whatever conftest is loaded. A throwaway test run outside
the package conftest destroyed a real GitHub token during this work; this
makes that class of accident impossible rather than merely discouraged.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-26 16:39:52 +02:00

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
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.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