mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-11 17:53:55 +01:00
Eighth batch of the 250-line sweep (over-cap count 28 -> 25). _state.py 366 -> 197 (+ _state_today.py, _state_sync.py) test_state.py 400 -> 221 (+ test_state_today.py, test_state_sync.py) _tk_fakes.py 368 -> 167 (+ _tk_fakes_containers.py, _tk_fakes_canvas.py) _state.py is the file conftest patches FOOD_LOG_FILE on, so the split is constrained rather than free: `FOOD_LOG_FILE` and the two functions that open it stay put, and the derived read model (_state_today) and the sync/undo helpers (_state_sync) reach the file only through _state's private _read_raw_log/_write_log. Callers were rewired to import from the new modules directly rather than via a circular re-export. That constraint is now enforced instead of merely documented. Moving a reader of FOOD_LOG_FILE into a sibling would leave conftest's patch pointing at a name nothing reads: check_patch_targets.py still exits 0 (the path resolves) and the runtime is unchanged (a file write, not a network call), so the suite would go green while writing to the real ~/.local/share/diet_guard and the next sync tick would publish it. test_state_redirect.py asserts both halves -- only _constants and _state may name the constant, and the redirect demonstrably moves a real write off the live path. Verified it fails closed by adding a FOOD_LOG_FILE reader to _state_today: it named that module exactly. _tk_fakes.py keeps _FAKE_TK/_FAKE_TTK and gains an explicit __all__, since the re-exports are load-bearing and ruff would otherwise strip them as unused. The identity canary from the previous commit covers the split: breaking one entry in the assembly (Canvas=object) fails 8 gate tests loudly rather than silently building real widgets. Verified: 706 Python tests / 100% branch coverage, check_patch_targets 0, pre-commit clean, ~/.local/share/diet_guard byte-identical. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RA9ynjqXPnexRQnR5oAuCC
73 lines
2.4 KiB
Python
73 lines
2.4 KiB
Python
"""Decision logic for the diet_guard slot-based log-to-unlock gate.
|
|
|
|
This module is GUI-free and side-effect-free so the lock/no-lock decision can
|
|
be verified headlessly: the fullscreen window in ``_gatelock.py`` is only a
|
|
thin shell around :func:`gate_is_due` and :func:`due_slots`. It composes the
|
|
pure slot arithmetic in :mod:`diet_guard._slots` with the logged-slot
|
|
state in :mod:`diet_guard._state`; ``now`` is injectable so the
|
|
time-of-day rules stay deterministically testable.
|
|
|
|
The gate fires when any *elapsed* meal slot for today carries no logged meal.
|
|
Coming home late therefore surfaces several unlogged slots at once -- a single
|
|
lock that backfills the whole day before the PC is usable -- while a normal day
|
|
prompts one slot at a time, with no separate weekday code path.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from diet_guard._slots import missing_slots, slot_label
|
|
from diet_guard._state import (
|
|
now_local,
|
|
)
|
|
from diet_guard._state_today import (
|
|
logged_slots_today,
|
|
)
|
|
|
|
if TYPE_CHECKING:
|
|
from datetime import datetime
|
|
|
|
|
|
def due_slots(now: datetime | None = None) -> tuple[int, ...]:
|
|
"""Return today's elapsed-but-unlogged meal slots, ascending.
|
|
|
|
Args:
|
|
now: Reference time (defaults to the current local time); injectable.
|
|
|
|
Returns:
|
|
The slot hours that still need a meal logged (empty == nothing due).
|
|
"""
|
|
reference = now if now is not None else now_local()
|
|
return missing_slots(reference, logged_slots_today())
|
|
|
|
|
|
def gate_is_due(now: datetime | None = None) -> bool:
|
|
"""Return True if the screen should lock until the missing slots are filled.
|
|
|
|
Args:
|
|
now: Reference time (defaults to the current local time); injectable.
|
|
|
|
Returns:
|
|
True if at least one elapsed slot today is unlogged, else False.
|
|
"""
|
|
return bool(due_slots(now))
|
|
|
|
|
|
def gate_message(now: datetime | None = None) -> str:
|
|
"""Return the lock-screen reason line listing the slots to backfill.
|
|
|
|
Args:
|
|
now: Reference time (defaults to the current local time); injectable.
|
|
|
|
Returns:
|
|
A short human-readable explanation of which meals are missing.
|
|
"""
|
|
missing = due_slots(now)
|
|
if not missing:
|
|
return "All meals are logged. You're up to date."
|
|
labels = ", ".join(slot_label(slot) for slot in missing)
|
|
if len(missing) == 1:
|
|
return f"Log your {labels} meal to unlock."
|
|
return f"Log your meals for {labels} to unlock."
|