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

415 lines
15 KiB
Python

"""Tests for diet_guard's entry <-> crdt_sync.Record adapters.
``TestUnionById`` through ``TestAlgebraicProperties`` are the exact same
behavioral assertions the pre-migration ``_sync_merge.merge_logs`` had --
routed through ``daylog_to_log -> crdt_sync.merge_logs -> log_to_daylog``
instead, to prove the migration preserves diet_guard's merge semantics
exactly, not just "some CRDT library now does something similar."
"""
from __future__ import annotations
import json
from crdt_sync import Record, merge_logs
import pytest
from diet_guard._budget_history import BudgetEntry
from diet_guard._sync_merge import (
_budget_hlc,
_entry_hlc,
_legacy_entry_id,
budget_to_log,
daylog_to_log,
entry_to_record,
log_to_budget,
log_to_daylog,
log_to_history,
parse_remote_budget,
parse_remote_log,
record_to_entry,
)
def _entry(**overrides: object) -> dict[str, object]:
"""Build a minimal valid entry, overriding only what a test cares about."""
entry: dict[str, object] = {
"id": "id-1",
"time": "2026-06-22T08:00:00+02:00",
"desc": "oatmeal",
"kcal": 300.0,
"protein_g": 10.0,
"carbs_g": 50.0,
"fat_g": 5.0,
"grams": 200.0,
"source": "manual",
}
entry.update(overrides)
return entry
def _merge_daylogs(a: dict, b: dict) -> dict:
"""Merge two DayLogs through the new crdt_sync-backed pipeline."""
return log_to_daylog(merge_logs(daylog_to_log(a), daylog_to_log(b)))
class TestUnionById:
def test_disjoint_logs_union_into_one(self) -> None:
a = {"2026-06-22": [_entry(id="a", time="2026-06-22T08:00:00+02:00")]}
b = {"2026-06-22": [_entry(id="b", time="2026-06-22T12:00:00+02:00")]}
merged = _merge_daylogs(a, b)
assert {e["id"] for e in merged["2026-06-22"]} == {"a", "b"}
def test_same_id_in_both_logs_is_not_duplicated(self) -> None:
shared = _entry(id="shared")
merged = _merge_daylogs({"2026-06-22": [shared]}, {"2026-06-22": [shared]})
assert len(merged["2026-06-22"]) == 1
def test_legacy_entries_without_id_dedup_by_time_and_desc(self) -> None:
legacy_a = _entry(id=None, time="2026-06-20T08:00:00+02:00", desc="toast")
legacy_a.pop("id")
legacy_b = dict(legacy_a)
merged = _merge_daylogs({"2026-06-20": [legacy_a]}, {"2026-06-20": [legacy_b]})
assert len(merged["2026-06-20"]) == 1
def test_legacy_and_id_entries_with_different_keys_both_survive(self) -> None:
legacy = _entry(time="2026-06-20T08:00:00+02:00", desc="toast")
legacy.pop("id")
with_id = _entry(id="x", time="2026-06-20T09:00:00+02:00", desc="eggs")
merged = _merge_daylogs({"2026-06-20": [legacy]}, {"2026-06-20": [with_id]})
assert len(merged["2026-06-20"]) == 2
class TestTombstoneWins:
def test_tombstone_beats_a_non_deleted_copy_either_order(self) -> None:
normal = _entry(id="x", deleted=False)
tombstoned = _entry(id="x", deleted=True)
forward = _merge_daylogs(
{"2026-06-22": [normal]},
{"2026-06-22": [tombstoned]},
)
backward = _merge_daylogs(
{"2026-06-22": [tombstoned]},
{"2026-06-22": [normal]},
)
assert forward["2026-06-22"][0]["deleted"] is True
assert backward["2026-06-22"][0]["deleted"] is True
def test_two_tombstoned_copies_stay_tombstoned(self) -> None:
tombstoned = _entry(id="x", deleted=True)
merged = _merge_daylogs(
{"2026-06-22": [tombstoned]},
{"2026-06-22": [dict(tombstoned)]},
)
assert merged["2026-06-22"][0]["deleted"] is True
class TestRebucketingAndOrdering:
def test_entry_is_filed_under_its_own_times_date_not_the_arrival_bucket(
self,
) -> None:
misfiled = _entry(id="x", time="2026-06-21T23:00:00+02:00")
merged = _merge_daylogs({"2026-06-22": [misfiled]}, {})
assert merged == {"2026-06-21": [misfiled]}
def test_a_days_entries_are_sorted_oldest_first(self) -> None:
late = _entry(id="late", time="2026-06-22T20:00:00+02:00")
early = _entry(id="early", time="2026-06-22T08:00:00+02:00")
merged = _merge_daylogs({"2026-06-22": [late]}, {"2026-06-22": [early]})
assert [e["id"] for e in merged["2026-06-22"]] == ["early", "late"]
class TestAlgebraicProperties:
def test_merge_is_commutative(self) -> None:
a = {"2026-06-22": [_entry(id="a")]}
b = {"2026-06-22": [_entry(id="b", time="2026-06-22T09:00:00+02:00")]}
assert _merge_daylogs(a, b) == _merge_daylogs(b, a)
def test_merge_is_idempotent(self) -> None:
canonical = {"2026-06-22": [_entry(id="a")]}
assert _merge_daylogs(canonical, canonical) == canonical
def test_merging_with_an_empty_log_is_a_no_op(self) -> None:
log = {"2026-06-22": [_entry(id="a")]}
assert _merge_daylogs(log, {}) == log
assert _merge_daylogs({}, log) == log
def test_merging_two_empty_logs_is_empty(self) -> None:
assert _merge_daylogs({}, {}) == {}
class TestEntryHlc:
def test_same_entry_always_yields_the_same_hlc(self) -> None:
entry = _entry()
assert _entry_hlc(entry) == _entry_hlc(dict(entry))
def test_malformed_time_still_yields_a_valid_hlc(self) -> None:
entry = _entry(time="not-a-timestamp")
assert _entry_hlc(entry).wall_time_ms == 0
def test_missing_time_still_yields_a_valid_hlc(self) -> None:
entry = _entry()
del entry["time"]
assert _entry_hlc(entry).wall_time_ms == 0
class TestLegacyEntryId:
def test_same_time_and_desc_yields_the_same_id(self) -> None:
a = _entry(time="2026-06-20T08:00:00+02:00", desc="toast")
b = _entry(time="2026-06-20T08:00:00+02:00", desc="toast", kcal=999.0)
assert _legacy_entry_id(a) == _legacy_entry_id(b)
def test_different_desc_yields_a_different_id(self) -> None:
a = _entry(time="2026-06-20T08:00:00+02:00", desc="toast")
b = _entry(time="2026-06-20T08:00:00+02:00", desc="eggs")
assert _legacy_entry_id(a) != _legacy_entry_id(b)
class TestEntryRecordRoundTrip:
def test_round_trip_preserves_all_fields(self) -> None:
entry = _entry(id="x")
assert record_to_entry(entry_to_record(entry)) == entry
def test_round_trip_of_a_deleted_entry_preserves_the_tombstone(self) -> None:
entry = _entry(id="x", deleted=True)
assert record_to_entry(entry_to_record(entry))["deleted"] is True
def test_legacy_entry_gets_a_derived_id_on_round_trip(self) -> None:
entry = _entry(time="2026-06-20T08:00:00+02:00", desc="toast")
entry.pop("id")
round_tripped = record_to_entry(entry_to_record(entry))
assert round_tripped["id"] == _legacy_entry_id(entry)
class TestParseRemoteLog:
def test_parses_new_format_wire_content(self) -> None:
entry = _entry(id="x")
pushed = {"x": entry_to_record(entry).to_dict()}
log = parse_remote_log(_dumps(pushed))
assert log["x"].id == "x"
def test_parses_old_daylog_format_for_backward_compatibility(self) -> None:
entry = _entry(id="x")
old_format = {"2026-06-22": [entry]}
log = parse_remote_log(_dumps(old_format))
assert log["x"].id == "x"
def test_empty_object_parses_as_empty_log(self) -> None:
assert parse_remote_log("{}") == {}
def test_non_object_top_level_raises_type_error(self) -> None:
with pytest.raises(TypeError):
parse_remote_log("[1, 2, 3]")
def test_old_format_day_not_a_list_raises_type_error(self) -> None:
with pytest.raises(TypeError):
parse_remote_log(_dumps({"2026-06-22": "not-a-list"}))
def test_old_format_entry_not_an_object_raises_type_error(self) -> None:
with pytest.raises(TypeError):
parse_remote_log(_dumps({"2026-06-22": ["not-a-dict"]}))
def test_invalid_json_raises(self) -> None:
with pytest.raises(json.JSONDecodeError):
parse_remote_log("not json{{{")
def _budget_record(**overrides: object) -> dict[str, object]:
"""Build a minimal valid raw budget record, overriding what a test needs."""
record: dict[str, object] = {
"v": 2,
"b": 2000,
"t": "2026-06-22T08:00:00+02:00",
}
record.update(overrides)
return record
_HIST_SEED = BudgetEntry("1970-01-01", 2200, "2026-07-13T21:15:09+02:00")
_HIST_CUT = BudgetEntry("2026-07-26", 2000, "2026-07-26T10:00:00+02:00")
class TestBudgetHlc:
def test_same_record_always_yields_the_same_hlc(self) -> None:
record = _budget_record()
assert _budget_hlc(record) == _budget_hlc(dict(record))
def test_malformed_t_still_yields_a_valid_hlc(self) -> None:
record = _budget_record(t="not-a-timestamp")
assert _budget_hlc(record).wall_time_ms == 0
def test_a_later_t_yields_a_greater_hlc(self) -> None:
earlier = _budget_record(t="2020-01-01T00:00:00+00:00")
later = _budget_record(t="2030-01-01T00:00:00+00:00")
assert _budget_hlc(later) > _budget_hlc(earlier)
class TestBudgetLogRoundTrip:
def test_none_record_yields_an_empty_log(self) -> None:
assert budget_to_log(None) == {}
def test_round_trip_preserves_the_budget(self) -> None:
record = _budget_record()
round_tripped = log_to_budget(budget_to_log(record))
assert round_tripped is not None
assert round_tripped["b"] == 2000
def test_weight_travels_as_its_own_field(self) -> None:
"""``w`` is shared state, carried as a field of its own.
Inside ``value`` it was collateral damage of whole-map LWW: the phone
rebuilds that map without ``w``, so any phone budget edit silently
deleted the stored weight and the protein target with it.
"""
log = budget_to_log(_budget_record(w=80.0))
assert "w" not in log["budget"].fields["value"][0]
assert log["budget"].fields["weight"][0] == 80.0
def test_weight_round_trips(self) -> None:
round_tripped = log_to_budget(budget_to_log(_budget_record(w=80.0)))
assert round_tripped is not None
assert round_tripped["w"] == 80.0
def test_no_weight_round_trips_to_no_weight(self) -> None:
round_tripped = log_to_budget(budget_to_log(_budget_record()))
assert round_tripped is not None
assert "w" not in round_tripped
def test_a_non_numeric_weight_is_not_emitted(self) -> None:
assert "weight" not in budget_to_log(_budget_record(w="heavy"))["budget"].fields
def test_a_boolean_weight_is_not_emitted(self) -> None:
assert "weight" not in budget_to_log(_budget_record(w=True))["budget"].fields
def test_a_non_numeric_weight_field_is_ignored_on_read(self) -> None:
log = {
"budget": Record(
id="budget",
fields={"weight": ("heavy", _budget_hlc({}))},
),
}
assert "w" not in (log_to_budget(log) or {})
def test_a_weightless_peer_cannot_delete_the_weight(self) -> None:
"""The whole point: the phone never sets ``w`` and must not drop it."""
ours = budget_to_log(_budget_record(w=80.0))
phone = budget_to_log(
{"v": 2, "b": 1800, "t": "2099-01-01T00:00:00+00:00"},
)
merged = log_to_budget(merge_logs(ours, phone))
assert merged is not None
assert merged["b"] == 1800
assert merged["w"] == 80.0
def test_round_tripped_t_reflects_the_winning_hlc(self) -> None:
record = _budget_record(t="2026-06-22T08:00:00+02:00")
round_tripped = log_to_budget(budget_to_log(record))
assert round_tripped is not None
assert round_tripped["t"] != ""
def test_empty_log_has_no_budget(self) -> None:
assert log_to_budget({}) is None
def test_record_with_no_value_field_has_no_hlc_in_result(self) -> None:
"""A record present but missing the "value" field is a defensive
edge case (should not occur from budget_to_log itself) -- the
result still comes back without crashing, and with no ``t``.
"""
log = {"budget": Record(id="budget", fields={})}
round_tripped = log_to_budget(log)
assert round_tripped == {}
class TestBudgetHistoryFields:
"""The effective-from history riding along on the budget record."""
def test_history_becomes_one_field_per_date(self) -> None:
log = budget_to_log(_budget_record(), (_HIST_SEED, _HIST_CUT))
fields = log["budget"].fields
assert "value" in fields
assert fields["hist:1970-01-01"][0] == 2200
assert fields["hist:2026-07-26"][0] == 2000
def test_round_trips_back_to_entries(self) -> None:
entries = log_to_history(
budget_to_log(_budget_record(), (_HIST_SEED, _HIST_CUT))
)
assert [e.effective_from for e in entries] == ["1970-01-01", "2026-07-26"]
assert [e.kcal for e in entries] == [2200, 2000]
def test_no_history_round_trips_to_nothing(self) -> None:
assert log_to_history(budget_to_log(_budget_record())) == ()
def test_empty_log_has_no_history(self) -> None:
assert log_to_history({}) == ()
def test_non_history_fields_are_ignored(self) -> None:
log = {
"budget": Record(
id="budget", fields={"value": ({"b": 2000}, _budget_hlc({}))}
)
}
assert log_to_history(log) == ()
def test_non_int_history_value_is_skipped(self) -> None:
log = {
"budget": Record(
id="budget",
fields={"hist:2026-07-26": ("nonsense", _budget_hlc({}))},
),
}
assert log_to_history(log) == ()
def test_unparsable_edit_time_still_yields_a_field(self) -> None:
broken = BudgetEntry("2026-07-26", 2000, "not a timestamp")
log = budget_to_log(_budget_record(), (broken,))
assert "hist:2026-07-26" in log["budget"].fields
def test_history_survives_a_merge_with_a_history_free_peer(self) -> None:
"""The rollout guarantee: an old device cannot clobber the history.
merge_record is per-field LWW over the *union* of field names, so a
peer that only pushes ``value`` leaves ``hist:*`` untouched -- which
is why this needed no coordinated release.
"""
ours = budget_to_log(_budget_record(), (_HIST_SEED, _HIST_CUT))
legacy = {
"budget": Record(
id="budget",
fields={
"value": (
{"v": 2, "b": 2200},
_budget_hlc({"t": "2099-01-01T00:00:00+00:00"}),
)
},
),
}
merged = merge_logs(ours, legacy)
assert len(log_to_history(merged)) == 2
class TestParseRemoteBudget:
def test_parses_pushed_budget_wire_content(self) -> None:
record = _budget_record()
pushed = budget_to_log(record)
wire = json.dumps({rid: rec.to_dict() for rid, rec in pushed.items()})
log = parse_remote_budget(wire)
assert log["budget"].id == "budget"
def test_empty_object_parses_as_empty_log(self) -> None:
assert parse_remote_budget("{}") == {}
def test_non_object_top_level_raises_type_error(self) -> None:
with pytest.raises(TypeError):
parse_remote_budget("[1, 2, 3]")
def test_invalid_json_raises(self) -> None:
with pytest.raises(json.JSONDecodeError):
parse_remote_budget("not json{{{")
def _dumps(data: object) -> str:
return json.dumps(data)