diet-guard/diet_guard/tests/test_budget_history.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

289 lines
11 KiB
Python

"""Tests for _budget_history.py — the effective-from budget history.
The history file is redirected into ``tmp_path`` by the autouse conftest
fixture, so every read/write here is isolated from real user data.
"""
from __future__ import annotations
from datetime import datetime, timezone
import json
from diet_guard import _budget, _budget_history
from diet_guard._budget_history import (
EPOCH_DAY,
BudgetEntry,
BudgetSchedule,
history_from_json,
history_to_json,
load_entries,
read_raw_history,
record_budget_change,
seed_from_budget,
upsert,
write_raw_history,
)
def _at(iso: str) -> datetime:
return datetime.fromisoformat(iso)
def _entry(day: str, kcal: int, t: str = "2026-01-01T00:00:00+00:00") -> BudgetEntry:
return BudgetEntry(effective_from=day, kcal=kcal, edited_at=t)
class TestForDay:
"""Resolving a day to the budget that applied on it."""
def test_empty_history_falls_back_to_the_default(self) -> None:
assert BudgetSchedule((), default=2200).for_day("2026-06-01") == 2200
def test_day_before_every_entry_falls_back_to_the_default(self) -> None:
schedule = BudgetSchedule((_entry("2026-07-26", 2000),), default=2200)
assert schedule.for_day("2026-06-01") == 2200
def test_exact_effective_from_day_uses_the_new_value(self) -> None:
schedule = BudgetSchedule((_entry("2026-07-26", 2000),), default=2200)
assert schedule.for_day("2026-07-26") == 2000
def test_day_after_uses_the_new_value(self) -> None:
schedule = BudgetSchedule((_entry("2026-07-26", 2000),), default=2200)
assert schedule.for_day("2026-07-27") == 2000
def test_between_entries_uses_the_earlier_one(self) -> None:
schedule = BudgetSchedule(
(_entry(EPOCH_DAY, 2200), _entry("2026-07-26", 2000)),
default=1,
)
assert schedule.for_day("2026-07-25") == 2200
assert schedule.for_day("2026-07-26") == 2000
def test_picks_the_latest_of_several_changes(self) -> None:
schedule = BudgetSchedule(
(
_entry(EPOCH_DAY, 2400),
_entry("2026-03-01", 2200),
_entry("2026-07-26", 2000),
),
default=1,
)
assert schedule.for_day("2026-02-28") == 2400
assert schedule.for_day("2026-05-05") == 2200
assert schedule.for_day("2026-12-31") == 2000
class TestParsing:
"""history_from_json tolerates anything it is handed."""
def test_round_trips_entries(self) -> None:
entries = (_entry(EPOCH_DAY, 2200), _entry("2026-07-26", 2000))
assert history_from_json(history_to_json(entries)) == entries
def test_sorts_ascending_regardless_of_stored_order(self) -> None:
raw = {
"v": 1,
"e": {
"2026-07-26": {"b": 2000, "t": "x"},
"1970-01-01": {"b": 2200, "t": "y"},
},
}
assert [e.effective_from for e in history_from_json(raw)] == [
"1970-01-01",
"2026-07-26",
]
def test_non_dict_is_empty(self) -> None:
assert history_from_json([1, 2, 3]) == ()
def test_wrong_version_is_empty(self) -> None:
assert history_from_json({"v": 99, "e": {}}) == ()
def test_non_dict_entries_map_is_empty(self) -> None:
assert history_from_json({"v": 1, "e": "nope"}) == ()
def test_malformed_entry_is_skipped(self) -> None:
raw = {
"v": 1,
"e": {
"2026-07-26": "not a record",
"2026-07-27": {"b": "not an int"},
"2026-07-28": {"b": True},
"2026-07-29": {"b": 2000, "t": "2026-07-29T00:00:00+00:00"},
},
}
entries = history_from_json(raw)
assert [e.effective_from for e in entries] == ["2026-07-29"]
def test_missing_edit_time_falls_back_to_the_epoch(self) -> None:
entries = history_from_json({"v": 1, "e": {"2026-07-26": {"b": 2000}}})
assert entries[0].edited_at.startswith("1970-01-01")
class TestUpsert:
"""Appending and replacing entries."""
def test_appends_a_new_day(self) -> None:
entries = upsert(
(_entry(EPOCH_DAY, 2200),), kcal=2000, when=_at("2026-07-26T10:00:00+02:00")
)
assert [e.effective_from for e in entries] == [EPOCH_DAY, "2026-07-26"]
assert entries[-1].kcal == 2000
def test_second_edit_the_same_day_replaces_rather_than_appends(self) -> None:
entries = upsert((), kcal=2000, when=_at("2026-07-26T10:00:00+02:00"))
entries = upsert(entries, kcal=1900, when=_at("2026-07-26T18:30:00+02:00"))
assert len(entries) == 1
assert entries[0].kcal == 1900
def test_stamps_the_edit_time(self) -> None:
entries = upsert((), kcal=2000, when=_at("2026-07-26T10:00:00+02:00"))
assert entries[0].edited_at == "2026-07-26T10:00:00+02:00"
class TestSeedFromBudget:
"""Grandfathering an existing budget to the beginning of time."""
def test_none_record_seeds_nothing(self) -> None:
assert seed_from_budget(None) == ()
def test_record_without_a_budget_seeds_nothing(self) -> None:
assert seed_from_budget({"v": 2}) == ()
def test_boolean_budget_seeds_nothing(self) -> None:
assert seed_from_budget({"b": True}) == ()
def test_seeds_the_epoch_day_with_the_records_own_timestamp(self) -> None:
seeded = seed_from_budget({"b": 2200, "t": "2026-07-13T21:15:09+02:00"})
assert seeded == (BudgetEntry(EPOCH_DAY, 2200, "2026-07-13T21:15:09+02:00"),)
def test_missing_timestamp_falls_back_to_the_epoch(self) -> None:
seeded = seed_from_budget({"b": 2200})
assert seeded[0].edited_at.startswith("1970-01-01")
class TestFileIO:
"""Reading, writing, and lazily seeding the history file."""
def test_absent_file_reads_as_none(self) -> None:
assert read_raw_history() is None
def test_write_then_read_round_trips(self) -> None:
write_raw_history(history_to_json((_entry(EPOCH_DAY, 2200),)))
assert history_from_json(read_raw_history())[0].kcal == 2200
def test_corrupt_file_reads_as_none(self) -> None:
_budget_history.BUDGET_HISTORY_FILE.parent.mkdir(parents=True, exist_ok=True)
_budget_history.BUDGET_HISTORY_FILE.write_text("not json{{{")
assert read_raw_history() is None
def test_non_object_file_reads_as_none(self) -> None:
_budget_history.BUDGET_HISTORY_FILE.parent.mkdir(parents=True, exist_ok=True)
_budget_history.BUDGET_HISTORY_FILE.write_text("[1, 2, 3]")
assert read_raw_history() is None
def test_load_entries_is_a_pure_read_and_never_seeds(self) -> None:
"""Seeding belongs to ``_budget``; this must not reach back into it."""
_budget.write_raw_record({"v": 2, "b": 2200, "t": "2026-07-13T21:15:09+02:00"})
assert load_entries() == ()
assert read_raw_history() is None
def test_load_entries_is_empty_without_a_budget(self) -> None:
assert load_entries() == ()
def test_seeding_never_overwrites_a_pulled_history(self) -> None:
write_raw_history(history_to_json((_entry("2026-07-26", 2000),)))
_budget.write_raw_record({"v": 2, "b": 2200, "t": "2026-07-13T21:15:09+02:00"})
schedule = _budget.current_schedule(default=1)
assert [e.effective_from for e in schedule.entries] == ["2026-07-26"]
def test_record_budget_change_defaults_to_now(self) -> None:
record_budget_change(1900)
entries = load_entries()
today = datetime.now(tz=timezone.utc).astimezone().date().isoformat()
assert entries[-1].effective_from == today
assert entries[-1].kcal == 1900
class TestEmptyHistoryDocument:
"""An empty-but-present history must still seed.
Regression guard: the seed used to be gated on the *file* being absent,
so an empty ``{"v": 1, "e": {}}`` -- which ``_sync`` could write back
after merging with a pre-feature peer -- permanently disabled seeding and
every past day silently adopted the newest budget.
"""
def test_empty_document_still_grandfathers_on_write(self) -> None:
_budget.write_raw_record({"v": 2, "b": 2200, "t": "2026-07-13T21:15:09+02:00"})
write_raw_history({"v": 1, "e": {}})
_budget.write_budget(2000)
schedule = _budget.current_schedule(default=1)
assert schedule.for_day("2026-06-01") == 2200
def test_empty_document_still_grandfathers_on_read(self) -> None:
_budget.write_raw_record({"v": 2, "b": 2200, "t": "2026-07-13T21:15:09+02:00"})
write_raw_history({"v": 1, "e": {}})
assert _budget.current_schedule(default=1).for_day("2026-06-01") == 2200
def test_current_schedule_seeds_a_missing_history(self) -> None:
_budget.write_raw_record({"v": 2, "b": 2200, "t": "2026-07-13T21:15:09+02:00"})
assert _budget.current_schedule(default=1).for_day("2026-06-01") == 2200
assert read_raw_history() is not None
def test_current_schedule_without_a_budget_uses_the_default(self) -> None:
assert _budget.current_schedule(default=1900).for_day("2026-06-01") == 1900
class TestWriteBudgetIntegration:
"""write_budget is the funnel that keeps the history correct."""
def test_first_write_grandfathers_the_previous_value(self) -> None:
"""The load-bearing ordering test.
A today-only assertion passes even when the seed happens *after* the
upsert; this asserts a PAST day, which is the case that breaks if the
two are ever reordered.
"""
_budget.write_raw_record({"v": 2, "b": 2200, "t": "2026-07-13T21:15:09+02:00"})
_budget.write_budget(2000)
schedule = _budget.current_schedule(default=1)
assert schedule.for_day("2026-06-01") == 2200
assert (
schedule.for_day(
datetime.now(tz=timezone.utc).astimezone().date().isoformat(),
)
== 2000
)
def test_current_budget_still_reads_back_as_the_new_value(self) -> None:
_budget.write_budget(2000)
assert _budget.daily_budget() == 2000
def test_weight_is_preserved_alongside_the_history(self) -> None:
_budget.write_budget(2000, weight_kg=78.5)
assert _budget.budget_weight() == 78.5
assert load_entries()[-1].kcal == 2000
def test_a_fresh_install_seeds_no_phantom_history(self) -> None:
"""With no prior budget there is nothing to grandfather."""
_budget.write_budget(2000)
entries = load_entries()
assert [e.effective_from for e in entries] == [
datetime.now(tz=timezone.utc).astimezone().date().isoformat(),
]
def test_the_history_file_is_plain_readable_json(self) -> None:
_budget.write_budget(2000)
document = json.loads(
_budget_history.BUDGET_HISTORY_FILE.read_text(encoding="utf-8"),
)
assert document["v"] == 1
assert isinstance(document["e"], dict)