mirror of
https://github.com/kuhyx/wake-alarm.git
synced 2026-11-21 06:22:18 +01:00
Per-output locking: the alarm now paints one full challenge per live monitor instead of one window on the primary. `_ViewGroup` presents the N copies as one -- every label update fans out, and because all entries share a single root-mastered StringVar, answering on whichever screen you are actually looking at dismisses the alarm everywhere. The escape hatch is the reason this repo could not stay unmigrated. The alarm was the only locker with no way out but four correct rounds against a 30-second refresh, which is a lot to ask of someone who has just woken up and nothing at all to fall back on if the alarm misfires at the wrong hour. It is deliberately exactly as hard as sick mode: hidden for the first five minutes so it is never the easy first move, gone entirely once a rolling budget is spent, and costing a written justification plus a wait that doubles with each recent use. It grants no workout skip. `EscapeHatch` owns the whole flow so open and close stay symmetric. That symmetry is the fix for a real bug: `_open_escape` used to pack_forget the challenge widgets and cancel only flipped a bool, so "Back to the alarm" left a dead form on a blank screen, leaked the dialog frame, and made the next open sweep the dialog instead of the challenge. pack_forget does not remember its options, so the challenge now lives in one `challenge_frame` that is forgotten and re-packed as a unit. Tests: the single-output default made every "on every monitor" assertion degenerate into 1 == 1, and a shared MagicMock return_value made N surfaces N references to one widget -- a loop cropped to containers[:1] passed the whole suite. A `dual_output` fixture plus distinct-identity mocks now cover all five fan-out loops; each was mutation-checked by cropping it and confirming the suite goes red. The suite also no longer reads the real escape history or signs with the production HMAC key. 326 tests, 100% branch coverage, pre-commit clean, no lint suppressions.
109 lines
4.3 KiB
Python
109 lines
4.3 KiB
Python
"""Shared fixtures keeping the wake_alarm suite off the real machine.
|
|
|
|
Since gatelock v0.2.0 the lock builds one real ``tk.Toplevel`` per live
|
|
output. Two consequences for tests, both of which bite silently:
|
|
|
|
* ``tk.Toplevel`` over a ``MagicMock`` master sends real tkinter into infinite
|
|
mock recursion -- the run **hangs** rather than failing, which is far harder
|
|
to diagnose than an error.
|
|
* An unpatched lock setup shells out to ``setxkbmap`` and really disables VT
|
|
switching on the machine running the tests, and reaches the real
|
|
``$XDG_RUNTIME_DIR``, where it could stand a *production* locker down.
|
|
|
|
The escape hatch adds a third: ``WakeAlarm.__init__`` builds its tracker over
|
|
the real ``ESCAPE_HISTORY_FILE``, so an unpatched run both reads a *production*
|
|
budget -- making "the button appears" tests fail on any day the real budget is
|
|
spent -- and could write to it.
|
|
|
|
So this is autouse and deliberately broad.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from gatelock import Output, OutputRect
|
|
import pytest
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Generator
|
|
from pathlib import Path
|
|
|
|
|
|
FAKE_OUTPUTS = (
|
|
Output(
|
|
"VIRTUAL-1",
|
|
connected=True,
|
|
rect=OutputRect(0, 0, 1920, 1080),
|
|
primary=True,
|
|
),
|
|
)
|
|
"""One live output, so the Tk-screen fallback (which would call int() on a
|
|
MagicMock) is never reached."""
|
|
|
|
TWO_OUTPUTS = (
|
|
Output("DP-0", connected=True, rect=OutputRect(0, 0, 3840, 2160), primary=True),
|
|
Output("HDMI-0", connected=True, rect=OutputRect(3840, 0, 2560, 1440)),
|
|
)
|
|
"""The real desk. Opt into it with the ``dual_output`` fixture for anything
|
|
asserting that work fans out: against ``FAKE_OUTPUTS`` a loop that only ever
|
|
touched the first surface would pass every assertion."""
|
|
|
|
|
|
def make_toplevel(_parent: object = None, **_kwargs: object) -> MagicMock:
|
|
"""A Toplevel stand-in that remembers the geometry it was given."""
|
|
window = MagicMock()
|
|
state = {"x": 0, "y": 0, "w": 0, "h": 0, "mapped": False}
|
|
|
|
def geometry(spec: str) -> None:
|
|
size, x, y = spec.split("+")
|
|
width, height = size.split("x")
|
|
state.update(x=int(x), y=int(y), w=int(width), h=int(height))
|
|
|
|
window.geometry.side_effect = geometry
|
|
window.deiconify.side_effect = lambda: state.update(mapped=True)
|
|
window.withdraw.side_effect = lambda: state.update(mapped=False)
|
|
window.winfo_ismapped.side_effect = lambda: state["mapped"]
|
|
window.winfo_rootx.side_effect = lambda: state["x"]
|
|
window.winfo_rooty.side_effect = lambda: state["y"]
|
|
window.winfo_width.side_effect = lambda: state["w"]
|
|
window.winfo_height.side_effect = lambda: state["h"]
|
|
return window
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _hermetic(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Generator[None]:
|
|
"""Block every path from the lock to the real display or machine."""
|
|
monkeypatch.setenv("GATELOCK_RUNTIME_DIR", str(tmp_path / "gatelock-runtime"))
|
|
key_file = tmp_path / "hmac.key"
|
|
key_file.write_bytes(b"0" * 32)
|
|
with (
|
|
# The real key is root-owned but world-readable, so an unpatched run
|
|
# signs test entries with the *production* key on this machine and
|
|
# with none at all anywhere else. Pin it so the suite is portable.
|
|
patch("gatelock.log_integrity.DEFAULT_HMAC_KEY_FILE", key_file),
|
|
patch(
|
|
"wake_alarm._escape_flow.ESCAPE_HISTORY_FILE",
|
|
tmp_path / "escape_history.json",
|
|
),
|
|
patch("gatelock._surfaces.tk.Toplevel", side_effect=make_toplevel),
|
|
patch("gatelock._outputs.RandrBackend.create", return_value=None),
|
|
patch("gatelock._outputs.scan_xrandr", return_value=FAKE_OUTPUTS),
|
|
patch("gatelock._detect._RandrEventSource.start", return_value=False),
|
|
patch("gatelock._vt.shutil.which", return_value="/usr/bin/setxkbmap"),
|
|
patch("gatelock._vt.subprocess.run"),
|
|
):
|
|
yield
|
|
|
|
|
|
@pytest.fixture
|
|
def dual_output(_hermetic: None) -> Generator[None]:
|
|
"""Re-scan as a two-monitor desk, layered over the single-output default.
|
|
|
|
Depends on ``_hermetic`` so pytest is obliged to apply it second; the
|
|
``scan_xrandr`` patch here has to be the one that wins.
|
|
"""
|
|
with patch("gatelock._outputs.scan_xrandr", return_value=TWO_OUTPUTS):
|
|
yield
|