diet-guard/diet_guard/tests/test_calendar_averages.py
Krzysztof kuhy Rudnicki e07284d3e1 Show weekly and monthly average intake on both devices
The calendar already said which individual days went over budget, but not
whether the week as a whole did. A run of small overages reads as four
harmless yellow squares; averaged, it is the thing that actually moves the
needle.

Three rules are load-bearing, and are documented in CLAUDE.md because getting
any of them wrong silently changes what "under budget" means:

- The denominator is logged days, not elapsed days. An unlogged day is not a
  zero-kcal day, and averaging gaps in as zeros would make a badly-tracked
  week look like the best week of the year.
- The yardstick is the mean of the per-day budgets over those same days,
  resolved through BudgetSchedule -- never today's budget, which would
  retroactively reclassify past weeks on every budget edit.
- Today is excluded. A half-logged today drags the mean far enough to flip
  the band, so every period ends at yesterday.

Band boundaries reuse the calendar's own OVER_BUDGET_YELLOW_CEILING by
import rather than by copy, so a period and its days can never disagree
about what "over" means; that constant loses its underscore to say so.

The MCP get_averages tool returns the average and the band but drops the
average budget, keeping the module's existing invariant that the budget
number never leaves this machine. The CLI, the gate and the phone all show
both, since it is hidden from the network, not from the user.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DjJqpcEsaV5WRhPXyxCThd
2026-08-09 14:59:40 +02:00

60 lines
2.3 KiB
Python

"""Tests for _calendar_view.averages_text: the History tab's averages line."""
from __future__ import annotations
from datetime import date
from diet_guard._budget_history import EPOCH_DAY, BudgetEntry, BudgetSchedule
from diet_guard._calendar_view import averages_text
def _flat(budget: int) -> BudgetSchedule:
return BudgetSchedule(
(BudgetEntry(EPOCH_DAY, budget, "1970-01-01T00:00:00+00:00"),),
default=budget,
)
class TestAveragesText:
def test_names_both_periods(self) -> None:
text = averages_text({}, schedule=_flat(2000), today=date(2026, 1, 14))
assert "week" in text
assert "month" in text
def test_says_it_stops_at_yesterday(self) -> None:
text = averages_text({}, schedule=_flat(2000), today=date(2026, 1, 14))
assert "to yesterday" in text
def test_empty_log_reads_no_data_not_zero(self) -> None:
text = averages_text({}, schedule=_flat(2000), today=date(2026, 1, 14))
assert "no data" in text
assert "0 kcal" not in text
def test_renders_the_average_and_its_band(self) -> None:
# Mon-Tue of the week containing Wed 2026-01-14, at 3000 kcal each
# against a 2000 budget: 150% of budget, comfortably "very over".
log = {
"2026-01-12": [{"kcal": 3000.0}],
"2026-01-13": [{"kcal": 3000.0}],
}
text = averages_text(log, schedule=_flat(2000), today=date(2026, 1, 14))
assert "week 3000 kcal (very over)" in text
def test_under_budget_reads_under(self) -> None:
log = {"2026-01-12": [{"kcal": 1500.0}]}
text = averages_text(log, schedule=_flat(2000), today=date(2026, 1, 14))
assert "week 1500 kcal (under)" in text
def test_slightly_over_reads_slightly_over(self) -> None:
log = {"2026-01-12": [{"kcal": 2200.0}]}
text = averages_text(log, schedule=_flat(2000), today=date(2026, 1, 14))
assert "week 2200 kcal (slightly over)" in text
def test_today_is_not_averaged_in(self) -> None:
log = {"2026-01-14": [{"kcal": 9000.0}]}
text = averages_text(log, schedule=_flat(2000), today=date(2026, 1, 14))
assert "9000" not in text
def test_defaults_to_the_real_today(self) -> None:
assert "week" in averages_text({}, schedule=_flat(2000))