mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-07-04 16:53:00 +02:00
Pulls every other device's pushed log from GitHub-backed dumb storage, merges it with the local log, and pushes this device's own merged copy back -- the PC half of the diet-guard-app sync plan. - _sync_merge.py: pure union-by-id merge, tombstone always wins, legacy (time, desc) dedup for pre-id entries. Commutative and idempotent. - _sync_github.py: minimal GitHub Contents API client (list/get/put), distinguishing a 404 on an unused path from the repo itself being unreachable. - _sync.py: orchestration -- pull, merge, re-sign every persisted entry regardless of origin, write, rebuild the food bank, push. Re-signing unconditionally is load-bearing: an unsigned phone-origin entry would otherwise be silently dropped on the very next read once a machine holds the shared HMAC key. - _foodbank.rebuild_food_bank(): the "replay a full log into a fresh bank" entrypoint the Python side was missing (the Dart port already had its equivalent). Backs sync's bank-rebuild step. - New diet-guard-sync.service/.timer (15-minute cadence, headless, a separate unit from the gate so a held lock can't stall sync) and a new install.sh step to install them. - Created the private kuhyx/diet-guard-sync GitHub repo for storage. Incidental to this feature: adding the `sync` subcommand pushed _cli.py past the repo's 500-line cap, so `gate`'s CLI glue moved out alongside sync's into _cli_gate.py/_cli_sync.py -- same split pattern already used for the gate window logic itself, not a sync-specific design choice. 338 tests, 100% branch coverage. Verified importing and running cleanly under /usr/bin/python (the production interpreter), not just the dev venv -- the gap that caused the earlier 3-day outage. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FU3f5KQ1GHXsbbSecfVEyF
86 lines
3.7 KiB
Python
86 lines
3.7 KiB
Python
"""Tests for the gate subcommand's handler, split out of test_cli.py
|
|
alongside its source module (see _cli_gate.py's module docstring).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from diet_guard import _cli_gate
|
|
from diet_guard._cli_gate import cmd_gate
|
|
|
|
|
|
class TestCmdGate:
|
|
"""The gate subcommand's three modes."""
|
|
|
|
def test_check_due(self) -> None:
|
|
"""--check exits 1 and announces a due lock."""
|
|
lines: list[str] = []
|
|
with patch.object(_cli_gate, "gate_is_due", return_value=True):
|
|
assert cmd_gate(lines.append, check=True, demo=False) == 1
|
|
assert "due" in lines[0]
|
|
|
|
def test_check_not_due(self) -> None:
|
|
"""--check exits 0 when no lock is needed."""
|
|
with patch.object(_cli_gate, "gate_is_due", return_value=False):
|
|
assert cmd_gate([].append, check=True, demo=False) == 0
|
|
|
|
def test_demo_opens_window(self) -> None:
|
|
"""--demo always builds and runs the gate window."""
|
|
gate = MagicMock()
|
|
with (
|
|
patch.object(_cli_gate, "MealGate", return_value=gate) as factory,
|
|
patch.object(_cli_gate, "acquire_gate_lock", return_value=MagicMock()),
|
|
patch.object(_cli_gate, "release_gate_lock"),
|
|
patch.object(_cli_gate, "wait_for_display", return_value=True),
|
|
):
|
|
assert cmd_gate([].append, check=False, demo=True) == 0
|
|
factory.assert_called_once_with(demo_mode=True)
|
|
gate.run.assert_called_once()
|
|
|
|
def test_bare_gate_not_due(self) -> None:
|
|
"""A bare gate with nothing due just reports and exits."""
|
|
lines: list[str] = []
|
|
with patch.object(_cli_gate, "gate_is_due", return_value=False):
|
|
assert cmd_gate(lines.append, check=False, demo=False) == 0
|
|
assert "no lock needed" in lines[0]
|
|
|
|
def test_bare_gate_due_opens_window(self) -> None:
|
|
"""A bare gate that is due opens the real window."""
|
|
gate = MagicMock()
|
|
with (
|
|
patch.object(_cli_gate, "gate_is_due", return_value=True),
|
|
patch.object(_cli_gate, "MealGate", return_value=gate),
|
|
patch.object(_cli_gate, "acquire_gate_lock", return_value=MagicMock()),
|
|
patch.object(_cli_gate, "release_gate_lock"),
|
|
patch.object(_cli_gate, "wait_for_display", return_value=True),
|
|
):
|
|
assert cmd_gate([].append, check=False, demo=False) == 0
|
|
gate.run.assert_called_once()
|
|
|
|
def test_gate_already_running(self) -> None:
|
|
"""A held single-instance lock means a second window is not opened."""
|
|
lines: list[str] = []
|
|
with (
|
|
patch.object(_cli_gate, "gate_is_due", return_value=True),
|
|
patch.object(_cli_gate, "acquire_gate_lock", return_value=None),
|
|
patch.object(_cli_gate, "MealGate") as factory,
|
|
):
|
|
assert cmd_gate(lines.append, check=False, demo=False) == 0
|
|
factory.assert_not_called()
|
|
assert "already running" in lines[0]
|
|
|
|
def test_gate_due_but_display_not_ready_defers(self) -> None:
|
|
"""A due gate whose display never comes up defers without a window."""
|
|
lines: list[str] = []
|
|
with (
|
|
patch.object(_cli_gate, "gate_is_due", return_value=True),
|
|
patch.object(_cli_gate, "acquire_gate_lock", return_value=MagicMock()),
|
|
patch.object(_cli_gate, "release_gate_lock"),
|
|
patch.object(_cli_gate, "wait_for_display", return_value=False),
|
|
patch.object(_cli_gate, "MealGate") as factory,
|
|
):
|
|
assert cmd_gate(lines.append, check=False, demo=False) == 0
|
|
factory.assert_not_called()
|
|
assert "display not ready" in lines[0]
|