diet-guard/diet_guard/tests/_tk_fakes_canvas.py
Krzysztof kuhy Rudnicki af461d3157 Split _state.py and _tk_fakes.py; make the food-log redirect fail closed
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
2026-08-15 20:21:26 +02:00

99 lines
3.3 KiB
Python

"""Canvas and scrollbar fakes for the gate's scroll-viewport tests.
Extracted from ``conftest.py`` purely for size: the widget tree the gate builds
now spans several modules plus ``gatelock``, and the fakes grew past this repo's
500-line file cap when the scroll viewport and the radio-button unit selector
were added.
Stays under ``tests/`` deliberately. It was briefly moved into the package to
satisfy the ``test_*.py`` naming hook, but that lost the ruff ``ARG`` exemption
that ``**/tests/**`` already grants -- and a fake widget's whole job is to
accept arguments it ignores. Weakening ruff for package code to accommodate
test scaffolding is the wrong trade; excluding one clearly-named support file
from a naming convention is the cheaper one.
The fakes are *functional* where behaviour matters -- selecting a radio button
really sets its variable and fires its command, and the canvas really tracks a
scroll offset -- so a test can drive the gate the way a keyboard user now can,
rather than merely asserting that a widget was constructed.
"""
from __future__ import annotations
from diet_guard.tests._tk_fakes_containers import FakeWidget
__all__ = ["FakeCanvas", "FakeScrollbar"]
class FakeCanvas(FakeWidget):
"""A functional ``tk.Canvas`` stand-in for ``gatelock.ScrollableSurface``.
Tracks the scroll offset so a test can assert the viewport actually moved,
which is the whole point of the widget being there: the gate's content can
exceed a 768px screen, and before the viewport existed the overflow was
silently clipped off both edges at once.
"""
def __init__(self, *args: object, **kwargs: object) -> None:
super().__init__(*args, **kwargs)
self.items: list[object] = []
self.offset = 0.0
self._children: list[object] = []
def create_window(self, *args: object, **kwargs: object) -> int:
window = kwargs.get("window")
if window is not None:
self.items.append(window)
self._children.append(window)
return len(self.items)
def itemconfigure(self, *args: object, **kwargs: object) -> None:
pass
def coords(self, *args: object, **kwargs: object) -> None:
pass
def bbox(self, *_args: object) -> tuple[int, int, int, int]:
return (0, 0, 0, 0)
def yview(self) -> tuple[float, float]:
return (self.offset, 1.0)
def yview_scroll(self, number: int, _what: str) -> None:
self.offset = max(0.0, self.offset + number)
def yview_moveto(self, fraction: float) -> None:
self.offset = fraction
def canvasy(self, screen_y: float) -> float:
return screen_y
def winfo_children(self) -> list[object]:
return list(self._children)
def winfo_height(self) -> int:
return 0
def winfo_toplevel(self) -> FakeWidget:
return self
def update_idletasks(self) -> None:
pass
def cget(self, key: str) -> object:
return self.configured.get(key, "")
def focus_get(self) -> None:
"""Nothing holds focus in a fake tree."""
class FakeScrollbar(FakeWidget):
"""A ``tk.Scrollbar`` stand-in that records the thumb position."""
def __init__(self, *args: object, **kwargs: object) -> None:
super().__init__(*args, **kwargs)
self.fractions: tuple[str, ...] = ()
def set(self, *fractions: str) -> None:
self.fractions = fractions