mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-10 19:27:14 +01:00
Every Python consumer now speaks slot minutes:
- satisfaction is nearest-slot snapping (_slot_wire.satisfied_by_entries)
at every "is this slot logged?" site: gate, CLI status, MCP, catering
occupancy and dedup. Nothing on disk is rewritten; 07:00/10:00 entries
keep covering 07:15/10:15 after a schedule move.
- log_meal writes slot_fields(minute) before signing (slot + slot_min).
- schedule store and sync encode via schedule_to_wire/from_wire; a
whole-hour schedule stays byte-identical (f/l/n only).
- CLI ate --hour takes HH or HH:MM; MCP log_meal takes "HH:MM" or a
legacy int hour, validated against today's slots; get_slots returns
{minute, label}.
- gate History tab: first/last are HH:MM ttk.Comboboxes (quarter-hour
list, typed times, Up/Down step without posting the popdown), locked
as disabled; validation mirrors normalized().
- gate timer fires every 15 min; docs rewritten for minutes.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015npsYVB2PdtWkG7MPAi4tn
165 lines
6.9 KiB
Python
165 lines
6.9 KiB
Python
"""Tests for the budget's half of the sync tick.
|
|
|
|
Split out of ``test_sync.py`` to keep both files under the repo's 250-line
|
|
cap, along the same seam as the module they cover
|
|
(``diet_guard.sync_merge._budget``).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import patch
|
|
|
|
from diet_guard import _sync, _sync_client
|
|
from diet_guard._budget import daily_budget, write_budget
|
|
from diet_guard._budget_derived import budget_weight
|
|
from diet_guard._budget_history import load_entries
|
|
from diet_guard._device import device_id
|
|
from diet_guard._meal_schedule import MealSchedule
|
|
from diet_guard._meal_schedule_store import (
|
|
load_entries as load_schedule_entries,
|
|
)
|
|
from diet_guard._meal_schedule_store import record_schedule_change
|
|
from diet_guard.tests._sync_fakes import (
|
|
_mock_client,
|
|
_remote_budget_json,
|
|
_write_token,
|
|
)
|
|
|
|
|
|
class TestSyncBudget:
|
|
"""The daily budget's last-writer-wins sync, folded into run_sync()."""
|
|
|
|
def test_pushes_local_budget_when_no_other_devices_have_synced(self) -> None:
|
|
_write_token()
|
|
write_budget(2000)
|
|
client = _mock_client(devices=())
|
|
with patch.object(_sync_client, "GitHubSyncClient", return_value=client):
|
|
_sync.run_sync()
|
|
pushed_paths = {call.args[0] for call in client.put_file_text.call_args_list}
|
|
assert f"diet-guard-sync/devices/{device_id()}/budget.json" in pushed_paths
|
|
|
|
def test_refreshes_the_weight_from_the_log_after_the_merge(self) -> None:
|
|
"""The hook runs last, on every successful tick, into the sync log."""
|
|
_write_token()
|
|
write_budget(2000)
|
|
client = _mock_client(devices=())
|
|
with (
|
|
patch.object(_sync_client, "GitHubSyncClient", return_value=client),
|
|
patch.object(_sync, "refresh_weight_from_log") as refresh,
|
|
):
|
|
_sync.run_sync()
|
|
refresh.assert_called_once()
|
|
(sink,) = refresh.call_args.args
|
|
with patch.object(_sync._logger, "info") as info:
|
|
sink("weight: 72.4 kg from the phone (2026-09-17).")
|
|
info.assert_called_once_with(
|
|
"%s", "weight: 72.4 kg from the phone (2026-09-17)."
|
|
)
|
|
|
|
def test_nothing_pushed_when_no_budget_ever_set(self) -> None:
|
|
"""An uninitialized device contributes nothing -- no push, no crash."""
|
|
_write_token()
|
|
client = _mock_client(devices=())
|
|
with patch.object(_sync_client, "GitHubSyncClient", return_value=client):
|
|
_sync.run_sync()
|
|
pushed_paths = {call.args[0] for call in client.put_file_text.call_args_list}
|
|
assert f"diet-guard-sync/devices/{device_id()}/budget.json" not in pushed_paths
|
|
|
|
def test_remote_only_budget_is_adopted_locally(self) -> None:
|
|
"""Only the phone has ever set a budget -- the PC adopts it."""
|
|
_write_token()
|
|
remote_json = _remote_budget_json(kcal=1800, t="2026-01-01T09:00:00")
|
|
client = _mock_client(
|
|
devices=("phone",),
|
|
files={"diet-guard-sync/devices/phone/budget.json": remote_json},
|
|
)
|
|
with patch.object(_sync_client, "GitHubSyncClient", return_value=client):
|
|
_sync.run_sync()
|
|
assert daily_budget() == 1800
|
|
|
|
def test_local_edit_later_than_remote_wins(self) -> None:
|
|
"""A fresh local write beats a much older remote edit."""
|
|
_write_token()
|
|
write_budget(1500) # stamped with "now"
|
|
remote_json = _remote_budget_json(kcal=1800, t="2020-01-01T09:00:00")
|
|
client = _mock_client(
|
|
devices=("phone",),
|
|
files={"diet-guard-sync/devices/phone/budget.json": remote_json},
|
|
)
|
|
with patch.object(_sync_client, "GitHubSyncClient", return_value=client):
|
|
_sync.run_sync()
|
|
assert daily_budget() == 1500
|
|
|
|
def test_remote_edit_later_than_local_wins(self) -> None:
|
|
"""A remote edit far in the future beats a stale local write.
|
|
|
|
Confirms this is genuinely edit-time (not sync-time or push-order)
|
|
LWW: whichever side has the later ``t`` wins regardless of which
|
|
device happens to run its sync tick first.
|
|
"""
|
|
_write_token()
|
|
write_budget(1500) # stamped with "now"
|
|
remote_json = _remote_budget_json(kcal=1800, t="2999-01-01T09:00:00")
|
|
client = _mock_client(
|
|
devices=("phone",),
|
|
files={"diet-guard-sync/devices/phone/budget.json": remote_json},
|
|
)
|
|
with patch.object(_sync_client, "GitHubSyncClient", return_value=client):
|
|
_sync.run_sync()
|
|
assert daily_budget() == 1800
|
|
|
|
def test_a_remote_edit_does_not_delete_the_local_body_weight(self) -> None:
|
|
"""The w-loss bug: ``w`` is PC-local and must survive a phone edit.
|
|
|
|
``w`` never travels (budget_to_log strips it), so before this fix a
|
|
winning remote record replaced the whole stored map and silently took
|
|
the weight -- and the protein target -- with it.
|
|
"""
|
|
_write_token()
|
|
write_budget(1500, weight_kg=78.5)
|
|
remote_json = _remote_budget_json(kcal=1800, t="2999-01-01T09:00:00")
|
|
client = _mock_client(
|
|
devices=("phone",),
|
|
files={"diet-guard-sync/devices/phone/budget.json": remote_json},
|
|
)
|
|
with patch.object(_sync_client, "GitHubSyncClient", return_value=client):
|
|
_sync.run_sync()
|
|
assert daily_budget() == 1800
|
|
assert budget_weight() == 78.5
|
|
|
|
def test_budget_history_survives_a_round_trip(self) -> None:
|
|
"""The history is written back locally after every merge."""
|
|
_write_token()
|
|
write_budget(2000)
|
|
client = _mock_client(devices=())
|
|
with patch.object(_sync_client, "GitHubSyncClient", return_value=client):
|
|
_sync.run_sync()
|
|
assert load_entries()[-1].kcal == 2000
|
|
|
|
def test_meal_schedule_survives_a_round_trip(self) -> None:
|
|
"""The meal schedule is written back locally after every merge.
|
|
|
|
Without this the schedule would be device-local: the phone could set
|
|
five meals while the PC still derived four, and the PC's gate would
|
|
nag for a checkpoint the phone never offered.
|
|
"""
|
|
_write_token()
|
|
write_budget(2000)
|
|
record_schedule_change(MealSchedule(480, 1200, 5))
|
|
client = _mock_client(devices=())
|
|
with patch.object(_sync_client, "GitHubSyncClient", return_value=client):
|
|
_sync.run_sync()
|
|
assert load_schedule_entries()[-1].schedule == MealSchedule(480, 1200, 5)
|
|
|
|
def test_malformed_remote_budget_is_skipped(self) -> None:
|
|
"""A corrupt remote budget.json is skipped, not a crash."""
|
|
_write_token()
|
|
write_budget(2000)
|
|
client = _mock_client(
|
|
devices=("phone",),
|
|
files={"diet-guard-sync/devices/phone/budget.json": "{not valid json"},
|
|
)
|
|
with patch.object(_sync_client, "GitHubSyncClient", return_value=client):
|
|
_sync.run_sync()
|
|
assert daily_budget() == 2000
|