mirror of
https://github.com/kuhyx/wake-alarm.git
synced 2026-11-23 10:26:34 +01:00
_reactivate.py shipped in phase 2a with tests but nothing called it, so the
HDMI sink fix was never actually in the alarm's path. WakeAlarm.__init__ now
starts the retry next to _activate_all_outputs(), stopped by the same
_stop_beep event the beep loop watches.
Verified on the host, not just in tests. Starting state was the exact bug:
alsa_card.pci-0000_01_00.1 Active Profile: off
sinks: alsa_output.pci-0000_0d_00.6.analog-stereo
After the retry ran, both passes logged:
2 sink(s) present: alsa_output.pci-0000_01_00.1.hdmi-stereo,
alsa_output.pci-0000_0d_00.6.analog-stereo
default=alsa_output.pci-0000_01_00.1.hdmi-stereo
and the captured card profiles restored cleanly afterwards.
Two wiring details worth keeping:
- _activate_outputs_only() drops what _activate_all_outputs() returns. Only
the first call sees the user's real audio state; every retry captures the
alarm's own. Returning one would let a later caller restore the alarm's
settings as if they were the user's.
- _list_real_sinks_now() re-resolves pactl each pass and reports no sinks
when it is absent. The retry outlives the startup lookup and runs on a
thread nobody is watching, where an exception would vanish silently.
_alarm.py was exactly at the 250-line cap, so the flash timers move to
_alarm_flash.py as a mixin, mirroring AlarmTimersMixin. AlarmTimersMixin's
abstract _start_flash_countdown stub had to go: it is first in the MRO and
would shadow the real implementation. main() stays in _alarm.py --
morning_routine/_orchestrator.py:39 hardcodes that path.
conftest stubs the reactivation autouse; without it every WakeAlarm test
would reshape the host's audio routing.
409 tests pass, 100% branch coverage.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EckWmyfetYq3Mi8MAnUudh
115 lines
4.6 KiB
Python
115 lines
4.6 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.
|
|
|
|
Sink reactivation adds a fourth: ``WakeAlarm.__init__`` starts a background
|
|
thread that really shells out to ``pactl set-card-profile`` and would reshape
|
|
the *host's* audio routing mid-test. It is stubbed here and re-patched
|
|
explicitly by the tests that assert the wiring.
|
|
|
|
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("wake_alarm._alarm.start_alarm_reactivation"),
|
|
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
|