steam-backlog-enforcer/steam_backlog_enforcer/tests/test_leisure_kill.py
Krzysztof kuhy Rudnicki d2565416aa Pylint gate covers tests, no suppressions
Tests are now linted by the 10.00 gate (ignore-paths dropped), with no
per-test disables. CtlServer/DaemonControl members the tests drive are
public (renamed, behaviour unchanged); SteamAPIClient._get takes its
params explicitly, as every caller already did.

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

199 lines
7.8 KiB
Python

"""Phase 2 at 0: how leisure pids die and what the audit log says."""
from __future__ import annotations
from dataclasses import replace
import os
import signal
from typing import TYPE_CHECKING
from unittest.mock import patch
from leisure_watch.facts import Reason
import pytest
from steam_backlog_enforcer import _leisure_kill
from steam_backlog_enforcer._leisure_kill import EVENT_LEISURE_KILL, LeisureKiller
from steam_backlog_enforcer._playtime_state import PlaytimeRules, PlaytimeState
from steam_backlog_enforcer.tests._leisure_fake_proc import FakeProc, add_proc
from steam_backlog_enforcer.tests._leisure_kill_world import (
RULES as _RULES,
)
from steam_backlog_enforcer.tests._leisure_kill_world import (
SPENT as _SPENT,
)
from steam_backlog_enforcer.tests._leisure_kill_world import (
StubLog,
build_proc,
)
from steam_backlog_enforcer.tests._leisure_kill_world import (
now_with as _now,
)
from steam_backlog_enforcer.tests._leisure_kill_world import (
verdict_of as _verdict,
)
if TYPE_CHECKING:
from collections.abc import Iterator
from pathlib import Path
from unittest.mock import MagicMock
@pytest.fixture(name="proc")
def fixture_proc(tmp_path: Path) -> Path:
"""Isolated /proc with mpv, helium, chromium and a root-owned app."""
return build_proc(tmp_path)
@pytest.fixture(name="kill")
def fixture_kill() -> Iterator[MagicMock]:
"""Replace ``os.kill`` so no real process is ever signalled."""
with patch.object(os, "kill") as mock:
yield mock
class TestEnforce:
"""Kill escalation, sparing rules and failure handling."""
def test_mpv_is_terminated_then_killed_after_30_s(
self, proc: Path, kill: MagicMock
) -> None:
"""mpv gets SIGTERM, then SIGKILL after 30 s, each logged once."""
del proc
log = StubLog()
killer = LeisureKiller(log)
verdict = _verdict(
_now(
Reason("media", "app:mpv", 10, "mpv"),
Reason("focus", "domain:x.com", 20, "x"),
)
)
for stamp in (1000.0, 1003.0, 1029.0, 1030.0, 1033.0):
killer.enforce(_SPENT, _RULES, verdict=verdict, stamp=stamp)
assert [c.args for c in kill.call_args_list] == [
(10, signal.SIGTERM),
(10, signal.SIGKILL),
(10, signal.SIGKILL),
]
assert [(e, f["pid"], f["signal"]) for e, f in log.records] == [
(EVENT_LEISURE_KILL, 10, "SIGTERM"),
(EVENT_LEISURE_KILL, 10, "SIGKILL"),
]
first = log.records[0][1]
assert first["key"] == "app:mpv"
assert first["name"] == "mpv"
assert first["why"] == "now.json"
assert first["day_key"] == "2026-11-02"
def test_untrusted_watcher_sweeps_but_spares_root(
self, proc: Path, kill: MagicMock
) -> None:
"""Untrusted for 30 s: user leisure pids are swept, root ones spared."""
add_proc(proc, FakeProc(41, exe="/usr/bin/mpv", uid=0))
killer = LeisureKiller(StubLog())
for stamp in range(0, 31, 3): # untrusted for the whole 30 s window
killer.enforce(_SPENT, _RULES, verdict=_verdict(None), stamp=stamp)
assert sorted(c.args[0] for c in kill.call_args_list) == [10, 20, 30]
def test_paused_is_not_stale(self, proc: Path, kill: MagicMock) -> None:
"""A paused watcher is not treated as stale, so nothing dies."""
del proc
LeisureKiller(StubLog()).enforce(
_SPENT, _RULES, verdict=_verdict(None, paused="i3lock"), stamp=1.0
)
kill.assert_not_called()
def test_under_budget_forgets_the_timers(self, proc: Path, kill: MagicMock) -> None:
"""Dropping under budget resets the SIGKILL timer."""
del proc
killer = LeisureKiller(None)
verdict = _verdict(_now(Reason("media", "app:mpv", 10, "mpv")))
killer.enforce(_SPENT, _RULES, verdict=verdict, stamp=0.0)
under = PlaytimeState(day_key="2026-11-02", seconds=50.0)
killer.enforce(under, _RULES, verdict=verdict, stamp=10.0)
killer.enforce(_SPENT, _RULES, verdict=verdict, stamp=40.0)
assert [c.args[1] for c in kill.call_args_list] == [signal.SIGTERM] * 2
def test_a_target_that_drops_out_restarts_its_timer(
self, proc: Path, kill: MagicMock
) -> None:
"""A target missing from one fact starts its timer over."""
del proc
killer = LeisureKiller(None)
listed = _verdict(_now(Reason("media", "app:mpv", 10, "mpv")))
killer.enforce(_SPENT, _RULES, verdict=listed, stamp=0.0)
killer.enforce(_SPENT, _RULES, verdict=_verdict(_now()), stamp=3.0)
killer.enforce(_SPENT, _RULES, verdict=listed, stamp=40.0)
assert [c.args[1] for c in kill.call_args_list] == [signal.SIGTERM] * 2
def test_a_pid_reused_by_a_new_process_starts_over(
self, proc: Path, kill: MagicMock
) -> None:
"""A new start time on the same pid restarts the timer."""
killer = LeisureKiller(None)
verdict = _verdict(_now(Reason("media", "app:mpv", 10, "mpv")))
killer.enforce(_SPENT, _RULES, verdict=verdict, stamp=0.0)
(proc / "10" / "stat").write_text(
"10 (mpv) S 1 " + "0 " * 17 + "999 0", encoding="utf-8"
)
killer.enforce(_SPENT, _RULES, verdict=verdict, stamp=40.0)
assert [c.args[1] for c in kill.call_args_list] == [signal.SIGTERM] * 2
def test_a_vanished_or_unsignallable_pid_is_not_logged(
self, proc: Path, kill: MagicMock
) -> None:
"""Pids that cannot be signalled leave no audit record."""
add_proc(proc, FakeProc(11, exe="/usr/bin/vlc"))
(proc / "12").mkdir()
(proc / "12" / "status").write_text("Uid:\t1000\t1000\n", encoding="utf-8")
kill.side_effect = ProcessLookupError
log = StubLog()
verdict = _verdict(
_now(
Reason("media", "app:vlc", 11, "vlc"),
Reason("media", "app:ghost", 12, "?"),
)
)
LeisureKiller(log).enforce(_SPENT, _RULES, verdict=verdict, stamp=0.0)
assert [c.args[0] for c in kill.call_args_list] == [11]
assert log.records == []
@pytest.mark.parametrize(
"rules",
[
replace(_RULES, enforcement=False),
replace(_RULES, demo=True),
replace(_RULES, leisure=False),
],
)
def test_never_with_enforcement_off_in_a_demo_or_dormant(
self, proc: Path, kill: MagicMock, rules: PlaytimeRules
) -> None:
"""Enforcement off, demo or dormant Phase 2 never kills."""
del proc
LeisureKiller(None).enforce(_SPENT, rules, verdict=_verdict(None), stamp=0.0)
kill.assert_not_called()
def test_a_bug_never_escapes_into_the_loop(
self, caplog: pytest.LogCaptureFixture
) -> None:
"""An internal error is logged once and never raised."""
killer = LeisureKiller(None)
with patch.object(_leisure_kill, "sweep_targets", side_effect=RecursionError):
for stamp in range(0, 34, 3): # sweeps (and fails) at 30 and 33
killer.enforce(_SPENT, _RULES, verdict=_verdict(None), stamp=stamp)
assert sum("Leisure kill failed" in r.message for r in caplog.records) == 1
def test_a_failed_sigkill_is_retried_and_logged_once_delivered(
self, proc: Path, kill: MagicMock
) -> None:
"""A failed SIGKILL is retried and logged only once it lands."""
del proc
kill.side_effect = [None, ProcessLookupError, None, None]
log = StubLog()
killer = LeisureKiller(log)
verdict = _verdict(_now(Reason("media", "app:mpv", 10, "mpv")))
for stamp in (0.0, 30.0, 33.0, 36.0):
killer.enforce(_SPENT, _RULES, verdict=verdict, stamp=stamp)
assert [f["signal"] for _e, f in log.records] == ["SIGTERM", "SIGKILL"]
assert kill.call_count == 4