mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-12 07:50:50 +01:00
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>
189 lines
7.1 KiB
Python
189 lines
7.1 KiB
Python
"""Tests for the food-bank Record adapters.
|
|
|
|
Split out of ``test_sync_merge.py`` to keep both files under the repo's
|
|
500-line ceiling. The derived bank clocks on ``count`` (so last-writer-wins
|
|
means max-count-wins); the curated bank clocks on an edit stamp.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
from crdt_sync import Record, merge_logs
|
|
import pytest
|
|
|
|
from diet_guard._sync_merge import (
|
|
_budget_hlc,
|
|
food_bank_to_log,
|
|
log_to_food_bank,
|
|
log_to_manual_bank,
|
|
manual_bank_to_log,
|
|
parse_remote_food_bank,
|
|
parse_remote_manual_bank,
|
|
)
|
|
|
|
_MANUAL_REC: dict[str, object] = {
|
|
"desc": "Skyr",
|
|
"kcal": 120.0,
|
|
"count": 0,
|
|
"t": "2026-07-26T10:00:00+02:00",
|
|
}
|
|
|
|
|
|
class TestFoodBankAdapters:
|
|
"""The log-derived food bank's max-count merge."""
|
|
|
|
@staticmethod
|
|
def _rec(count: float, kcal: float = 95.0) -> dict[str, object]:
|
|
return {"desc": "Apple", "kcal": kcal, "count": count}
|
|
|
|
def test_round_trips_a_record(self) -> None:
|
|
back = log_to_food_bank(food_bank_to_log({"apple": self._rec(5)}))
|
|
assert back["apple"]["count"] == 5
|
|
|
|
def test_count_is_the_clock(self) -> None:
|
|
log = food_bank_to_log({"apple": self._rec(5)})
|
|
assert log["apple"].fields["body"][1].wall_time_ms == 5
|
|
|
|
def test_a_missing_count_clocks_at_zero(self) -> None:
|
|
log = food_bank_to_log({"apple": {"desc": "Apple"}})
|
|
assert log["apple"].fields["body"][1].wall_time_ms == 0
|
|
|
|
def test_the_higher_count_wins(self) -> None:
|
|
"""A device that has replayed more of the log knows the truer count."""
|
|
behind = food_bank_to_log({"apple": self._rec(3, kcal=90)})
|
|
ahead = food_bank_to_log({"apple": self._rec(9, kcal=95)})
|
|
merged = log_to_food_bank(merge_logs(behind, ahead))
|
|
assert merged["apple"]["count"] == 9
|
|
assert merged["apple"]["kcal"] == 95
|
|
|
|
def test_the_merge_is_order_independent(self) -> None:
|
|
a = food_bank_to_log({"apple": self._rec(3)})
|
|
b = food_bank_to_log({"apple": self._rec(9)})
|
|
assert log_to_food_bank(merge_logs(a, b)) == log_to_food_bank(
|
|
merge_logs(b, a),
|
|
)
|
|
|
|
def test_re_merging_is_idempotent(self) -> None:
|
|
"""The count only moves when the log does, so a tick is a no-op."""
|
|
once = food_bank_to_log({"apple": self._rec(5)})
|
|
assert log_to_food_bank(merge_logs(once, once)) == log_to_food_bank(once)
|
|
|
|
def test_different_foods_union(self) -> None:
|
|
a = food_bank_to_log({"apple": self._rec(1)})
|
|
b = food_bank_to_log({"pear": self._rec(1)})
|
|
assert set(log_to_food_bank(merge_logs(a, b))) == {"apple", "pear"}
|
|
|
|
def test_a_non_dict_record_is_skipped(self) -> None:
|
|
assert food_bank_to_log({"apple": "nope"}) == {}
|
|
|
|
def test_a_non_dict_body_is_skipped_on_read(self) -> None:
|
|
log = {"apple": Record(id="apple", fields={"body": ("x", _budget_hlc({}))})}
|
|
assert log_to_food_bank(log) == {}
|
|
|
|
def test_a_tombstoned_record_is_dropped_on_read(self) -> None:
|
|
"""A tombstone must not be laundered back into a live bank record."""
|
|
log = {
|
|
"apple": Record(
|
|
id="apple",
|
|
fields={"body": (self._rec(5), _budget_hlc({}))},
|
|
deleted=True,
|
|
),
|
|
}
|
|
assert log_to_food_bank(log) == {}
|
|
|
|
def test_parses_pushed_wire_content(self) -> None:
|
|
pushed = food_bank_to_log({"apple": self._rec(5)})
|
|
wire = json.dumps({rid: rec.to_dict() for rid, rec in pushed.items()})
|
|
assert "apple" in parse_remote_food_bank(wire)
|
|
|
|
def test_non_object_top_level_raises_type_error(self) -> None:
|
|
with pytest.raises(TypeError):
|
|
parse_remote_food_bank("[1, 2, 3]")
|
|
|
|
def test_invalid_json_raises(self) -> None:
|
|
with pytest.raises(json.JSONDecodeError):
|
|
parse_remote_food_bank("not json{{{")
|
|
|
|
|
|
_MANUAL_REC: dict[str, object] = {
|
|
"desc": "Skyr",
|
|
"kcal": 120.0,
|
|
"count": 0,
|
|
"t": "2026-07-26T10:00:00+02:00",
|
|
}
|
|
|
|
|
|
class TestManualBankAdapters:
|
|
"""The hand-curated food bank's Record round trip."""
|
|
|
|
def test_round_trips_a_record(self) -> None:
|
|
back = log_to_manual_bank(manual_bank_to_log({"skyr": _MANUAL_REC}))
|
|
assert back["skyr"]["desc"] == "Skyr"
|
|
assert back["skyr"]["kcal"] == 120.0
|
|
|
|
def test_edit_time_is_reconstructed_from_the_hlc(self) -> None:
|
|
back = log_to_manual_bank(manual_bank_to_log({"skyr": _MANUAL_REC}))
|
|
assert back["skyr"]["t"].startswith("2026-07-26T")
|
|
|
|
def test_the_edit_stamp_does_not_travel_inside_the_body(self) -> None:
|
|
log = manual_bank_to_log({"skyr": _MANUAL_REC})
|
|
assert "t" not in log["skyr"].fields["body"][0]
|
|
|
|
def test_a_missing_edit_stamp_falls_back_to_the_epoch(self) -> None:
|
|
log = manual_bank_to_log({"skyr": {"desc": "Skyr"}})
|
|
assert log["skyr"].fields["body"][1].wall_time_ms == 0
|
|
|
|
def test_an_unparsable_edit_stamp_falls_back_to_the_epoch(self) -> None:
|
|
log = manual_bank_to_log({"skyr": {"desc": "Skyr", "t": "nonsense"}})
|
|
assert log["skyr"].fields["body"][1].wall_time_ms == 0
|
|
|
|
def test_a_non_dict_record_is_skipped(self) -> None:
|
|
assert manual_bank_to_log({"skyr": "not a record"}) == {}
|
|
|
|
def test_a_non_dict_body_is_skipped_on_read(self) -> None:
|
|
log = {"skyr": Record(id="skyr", fields={"body": ("nope", _budget_hlc({}))})}
|
|
assert log_to_manual_bank(log) == {}
|
|
|
|
def test_a_record_without_a_body_reads_as_an_empty_entry(self) -> None:
|
|
"""Defensive: not produced by manual_bank_to_log, but must not crash."""
|
|
assert log_to_manual_bank({"skyr": Record(id="skyr", fields={})}) == {
|
|
"skyr": {},
|
|
}
|
|
|
|
def test_a_newer_edit_wins_the_merge(self) -> None:
|
|
older = manual_bank_to_log({"skyr": _MANUAL_REC})
|
|
newer = manual_bank_to_log(
|
|
{"skyr": {"desc": "Skyr", "kcal": 999.0, "t": "2999-01-01T00:00:00+02:00"}},
|
|
)
|
|
merged = log_to_manual_bank(merge_logs(older, newer))
|
|
assert merged["skyr"]["kcal"] == 999.0
|
|
|
|
def test_different_foods_union_rather_than_replace(self) -> None:
|
|
a = manual_bank_to_log({"skyr": _MANUAL_REC})
|
|
b = manual_bank_to_log({"kefir": {"desc": "Kefir", "t": _MANUAL_REC["t"]}})
|
|
assert set(log_to_manual_bank(merge_logs(a, b))) == {"skyr", "kefir"}
|
|
|
|
def test_a_tombstoned_record_is_dropped_on_read(self) -> None:
|
|
log = {
|
|
"skyr": Record(
|
|
id="skyr",
|
|
fields={"body": (dict(_MANUAL_REC), _budget_hlc({}))},
|
|
deleted=True,
|
|
),
|
|
}
|
|
assert log_to_manual_bank(log) == {}
|
|
|
|
def test_parses_pushed_wire_content(self) -> None:
|
|
pushed = manual_bank_to_log({"skyr": _MANUAL_REC})
|
|
wire = json.dumps({rid: rec.to_dict() for rid, rec in pushed.items()})
|
|
assert "skyr" in parse_remote_manual_bank(wire)
|
|
|
|
def test_non_object_top_level_raises_type_error(self) -> None:
|
|
with pytest.raises(TypeError):
|
|
parse_remote_manual_bank("[1, 2, 3]")
|
|
|
|
def test_invalid_json_raises(self) -> None:
|
|
with pytest.raises(json.JSONDecodeError):
|
|
parse_remote_manual_bank("not json{{{")
|