wake-alarm/wake_alarm/tests/test_ring_main.py
Krzysztof kuhy Rudnicki 899a2c5581
Some checks failed
dependency freshness / dependencies on newest stable (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint,free-days-interop) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / free-days-interop (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
release-apk / release (APK) (push) Has been cancelled
Fix RTC +1 s mismatch and busy-ring fixture
rtcwake -t shifts the alarm by the RTC/system clock offset (vmbox: +1 s),
so the exact epoch compare in arm()/disarm_ours() forgot our record and
left the alarm armed. Match within 60 s instead (a local-time RTC is off
by hours and still never matches).

_ring_main now emits ring_backend FAILED when pw-play dies mid-ring, not
only when it never starts.

vmbox: the busy-card holder ran as the ssh user (no ACL on a fresh card)
and was aplay, which handles TERM; it now holds the node as root from a
TERM-ignoring sleep and fails the case as a fixture when nothing holds.
Guest repo path derived from the basename, so worktree runs work.
Records the S5 vmbox result; host S5 check dropped by kuhy.

vmbox harness: 16/16 PASS, ring-alsa-busy-fallback 10/10.

Todo: 364208fc
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:07:41 +01:00

95 lines
2.9 KiB
Python

"""The user-side ring entry the root ALSA script falls back to."""
from __future__ import annotations
from pathlib import Path
import signal
from typing import TYPE_CHECKING
from unittest.mock import MagicMock, patch
from wake_alarm import _ring_main
from wake_alarm._ring_main import main
if TYPE_CHECKING:
import pytest
if TYPE_CHECKING:
from collections.abc import Callable
WAVS = ["rise.wav", "loop.wav"]
def _backend(*, start: tuple[bool, str] = (True, "ok"), pumps: list[bool]) -> MagicMock:
backend = MagicMock()
backend.name = "pipewire"
backend.start.return_value = start
backend.pump.side_effect = pumps
return backend
def _run(backend: MagicMock, argv: list[str] | None = None) -> tuple[int, MagicMock]:
with (
patch.object(_ring_main, "PipeWireBackend", return_value=backend),
patch.object(_ring_main, "configure_logging"),
patch.object(_ring_main, "emit") as emitted,
patch.object(_ring_main.time, "sleep"),
):
return main(WAVS if argv is None else argv), emitted
def test_start_failure_emits_failed_and_exits_1() -> None:
backend = _backend(start=(False, "no pw"), pumps=[])
code, emitted = _run(backend)
assert code == 1
assert "FAILED" in emitted.call_args.args[1]
backend.stop.assert_not_called()
def test_pump_failure_exits_1_and_stops() -> None:
backend = _backend(pumps=[True, False])
code, emitted = _run(backend)
assert code == 1
assert "started" in emitted.call_args_list[0].args[1]
# pw-play dying mid-ring is a failure the audit must see, not silence.
assert "FAILED" in emitted.call_args.args[1]
assert emitted.call_args.kwargs["started"] is False
backend.stop.assert_called_once()
def test_sigterm_ends_the_ring_cleanly() -> None:
backend = _backend(pumps=[True, True])
handler: list[Callable[..., object]] = []
def capture(sig: int, fn: Callable[..., object]) -> None:
assert sig == signal.SIGTERM
handler.append(fn)
def stop_after_first(_: float) -> None:
handler[0](signal.SIGTERM, None)
with (
patch.object(_ring_main.signal, "signal", capture),
patch.object(_ring_main, "PipeWireBackend", return_value=backend),
patch.object(_ring_main, "configure_logging"),
patch.object(_ring_main, "emit"),
patch.object(_ring_main.time, "sleep", stop_after_first),
):
assert main(WAVS) == 0
backend.stop.assert_called_once()
def test_wrong_usage_exits_2(capsys: pytest.CaptureFixture[str]) -> None:
assert main(["only-one"]) == 2
assert "Usage" in capsys.readouterr().err
def test_paths_reach_the_backend() -> None:
backend = _backend(pumps=[False])
with (
patch.object(_ring_main, "PipeWireBackend", return_value=backend) as cls,
patch.object(_ring_main, "configure_logging"),
patch.object(_ring_main, "emit"),
):
main(WAVS)
cls.assert_called_once_with(Path("rise.wav"), Path("loop.wav"))