wake-alarm/wake_alarm/tests/test_ring_probe.py
Krzysztof kuhy Rudnicki 0781c42222
Some checks are pending
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,dependency-freshness) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / free-days-interop (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Drop type-ignores, trim CLAUDE.md to 250 lines
The bypass commit left three type: ignore comments in its tests and
pushed AGENTS.md 5 lines over the cap; both fail the CI mirror.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 12:44:21 +01:00

70 lines
2.1 KiB
Python

"""The silent monitor probe runs once, at A - 5, with no graphical session."""
from __future__ import annotations
from datetime import datetime, timedelta
from typing import TYPE_CHECKING
from unittest.mock import patch
import pytest
from wake_alarm import _ring_probe
if TYPE_CHECKING:
from collections.abc import Generator
from pathlib import Path
A = datetime(2026, 11, 23, 7, 0).astimezone()
# The argv lists the last ``_run`` handed to ``_ok``, newest run only.
CALLS: list[object] = []
@pytest.fixture(autouse=True)
def done_file(tmp_path: Path) -> Generator[Path]:
path = tmp_path / "fresh" / "ring_probe_done"
with patch.object(_ring_probe, "PROBE_DONE_FILE", path):
yield path
def _run(minutes: float, *, graphical: bool, started: bool = True) -> str | None:
with (
patch.object(_ring_probe, "graphical_session", return_value=(graphical, "why")),
patch.object(_ring_probe, "_ok", return_value=(started, "out")) as ok,
):
result = _ring_probe.maybe_probe(A + timedelta(minutes=minutes), A)
CALLS[:] = [call.args[0] for call in ok.call_args_list]
return result
def test_it_starts_the_unit_at_a_minus_five_with_no_session(done_file: Path) -> None:
assert _run(-5, graphical=False) == "started"
assert CALLS[0] == [
"sudo",
"-n",
"systemctl",
"start",
"wake-alarm-ring-probe.service",
]
assert done_file.exists()
def test_it_runs_only_once(done_file: Path) -> None:
done_file.parent.mkdir()
done_file.write_text("x")
assert _run(-4, graphical=False) is None
@pytest.mark.parametrize("minutes", [-6, 0, 30])
def test_only_in_the_five_minutes_before_the_alarm(minutes: float) -> None:
assert _run(minutes, graphical=False) is None
def test_it_waits_for_a_morning_with_no_session(done_file: Path) -> None:
assert _run(-3, graphical=True) == "waiting_for_no_session"
assert not done_file.exists()
def test_a_refused_start_is_retried_next_morning(done_file: Path) -> None:
assert _run(-3, graphical=False, started=False) == "failed"
assert not done_file.exists()