mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-11 14:07:40 +01:00
The periodic diet-guard-sync.timer paid a network round-trip every ~15 min to approximate the two moments the shared log actually changes on this device. Sync now fires on those events instead: * before a lock -- already present in _cli_gate._should_lock, which pulls only when a lock is otherwise due, so a phone-logged meal clears the lock and a not-due tick never touches the network; * after a meal is logged here -- new _sync_events.publish_after_log, wired into all three write paths (_cli_log, _cli_gate, _mcp). This is what stops the PHONE nagging for a slot already logged on the PC, the mirror image of the rule that keeps the phone's own tick unconditional. The gate's publish sits in cmd_gate AFTER MealGate.run() returns, never in the unlock handler: that runs on the Tk thread with the window still up, so a hanging network call would keep the user locked in behind a successful log. It also covers the "Synced from another device" unlock, which hooking log_meal would miss. Accepted trade: a PC that is on but neither logging nor locking no longer pulls, so an inbound phone edit lands at the next log or lock rather than within 15 min. Bounded by slot spacing -- an unlogged day raises a lock. install.sh now disables and removes the installed timer (guarded, since a fresh machine has no such unit and `disable` would abort under `set -euo pipefail`); the .service stays for manual one-off ticks. Every test that logs a meal would otherwise reach the network through the new call sites, so conftest patches publish_after_log at each of the three call sites -- not on _sync_events, so test_sync_events still exercises the real helper. Suite stays at ~8s, which is the actual leak detector. Two files had to be split to commit at all, since file-length passes filenames and both were already over the 250-line cap: _cli.py 466 -> 207, + _cli_args.py, _cli_init.py, _cli_log.py _mcp.py 370 -> 194, + _mcp_server.py (singleton + annotations), _mcp_read.py Their conftest/test patch targets moved in the same commit; a stale target redirects nothing and fails open. Verified: 701 python tests at 100% branch coverage, 555 flutter tests, `python -m diet_guard sync` -> 402 entries/exit 0, `gate --check` -> exit 0, all five MCP tools still register, live ~/.local/share/diet_guard byte-identical. Over-cap files: 44 -> 42. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RA9ynjqXPnexRQnR5oAuCC
92 lines
3.2 KiB
Python
92 lines
3.2 KiB
Python
"""Tests for the get_averages MCP tool, including its budget-secrecy invariant."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import patch
|
|
|
|
from diet_guard import _mcp, _mcp_read
|
|
from diet_guard._averages import AverageBand, PeriodAverage
|
|
from diet_guard._budget import BudgetNotInitializedError
|
|
|
|
|
|
def _period(
|
|
*,
|
|
avg_kcal: float | None = 2100.0,
|
|
band: AverageBand | None = AverageBand.SLIGHTLY_OVER,
|
|
) -> PeriodAverage:
|
|
return PeriodAverage(
|
|
start="2026-01-05",
|
|
end="2026-01-11",
|
|
logged_days=7,
|
|
elapsed_days=7,
|
|
avg_kcal=avg_kcal,
|
|
avg_budget=2000.0,
|
|
band=band,
|
|
)
|
|
|
|
|
|
class TestPeriodView:
|
|
def test_never_carries_the_average_budget(self) -> None:
|
|
# The module invariant: no tool hands an automated caller the budget.
|
|
view = _mcp_read._period_view(_period())
|
|
assert "avg_budget" not in view
|
|
assert 2000.0 not in view.values()
|
|
|
|
def test_carries_intake_band_and_coverage(self) -> None:
|
|
view = _mcp_read._period_view(_period())
|
|
assert view["avg_kcal"] == 2100.0
|
|
assert view["band"] == "slightly_over"
|
|
assert view["band_label"] == "slightly over"
|
|
assert view["logged_days"] == 7
|
|
assert view["elapsed_days"] == 7
|
|
assert view["start"] == "2026-01-05"
|
|
assert view["end"] == "2026-01-11"
|
|
|
|
def test_empty_period_serializes_as_null_not_zero(self) -> None:
|
|
view = _mcp_read._period_view(_period(avg_kcal=None, band=None))
|
|
assert view["avg_kcal"] is None
|
|
assert view["band"] is None
|
|
assert view["band_label"] == "no data"
|
|
|
|
|
|
class TestGetAverages:
|
|
def test_returns_all_four_periods(self) -> None:
|
|
with (
|
|
patch.object(_mcp_read, "daily_budget", return_value=2000),
|
|
patch.object(_mcp_read, "current_schedule"),
|
|
patch.object(_mcp_read, "load_log", return_value={}),
|
|
patch.object(_mcp_read, "weekly_average", return_value=_period()),
|
|
patch.object(_mcp_read, "monthly_average", return_value=_period()),
|
|
):
|
|
out = _mcp.get_averages()
|
|
assert out["budget_initialized"] is True
|
|
assert out["excludes_today"] is True
|
|
assert set(out["periods"]) == {
|
|
"this_week",
|
|
"last_week",
|
|
"this_month",
|
|
"last_month",
|
|
}
|
|
|
|
def test_no_budget_degrades_to_empty_periods(self) -> None:
|
|
with patch.object(
|
|
_mcp_read,
|
|
"daily_budget",
|
|
side_effect=BudgetNotInitializedError,
|
|
):
|
|
out = _mcp.get_averages()
|
|
assert out == {"budget_initialized": False, "periods": {}}
|
|
|
|
def test_no_response_field_leaks_the_budget_number(self) -> None:
|
|
with (
|
|
patch.object(_mcp_read, "daily_budget", return_value=2000),
|
|
patch.object(_mcp_read, "current_schedule"),
|
|
patch.object(_mcp_read, "load_log", return_value={}),
|
|
patch.object(_mcp_read, "weekly_average", return_value=_period()),
|
|
patch.object(_mcp_read, "monthly_average", return_value=_period()),
|
|
):
|
|
out = _mcp.get_averages()
|
|
flat = repr(out)
|
|
assert "2000" not in flat
|
|
assert "avg_budget" not in flat
|