steam-backlog-enforcer/steam_backlog_enforcer/tests/test_x_probe.py
Krzysztof kuhy Rudnicki b3e629e3f0 Bill gaming time only while the user is actually playing
A running game process was treated as proof of play. On 2026-08-28 the budget
billed ~2h23m while a gatelock screen locker covered the display and the
machine could not be touched at all -- the game was simply still resident.

A tick now bills only when all three hold, evaluated together rather than
short-circuited so the audit log can answer "was the screen ALSO locked?":

  * no gatelock guard owns the screen  (_screen_hold, via /proc/locks)
  * the focused window belongs to a qualifying process or its ancestry
  * some input source has been used within idle_grace_seconds (default 300)

Any probe that cannot answer BILLS the tick and is recorded in `degraded`.
A broken detector must never become an unlimited-gaming exploit.

Three findings from measuring this machine rather than assuming:

  * /dev/input/js0 here is an *ASRock LED Controller* -- the kernel exposes
    the motherboard RGB device as a joystick. A presence check would have
    disabled the idle criterion permanently, so _controller_input watches for
    real events instead (skipping joydev's synthetic open burst).
  * Aseprite's window carries no _NET_WM_PID at all, so focus attribution
    falls back to XRes, which asks the server who owns the window rather than
    trusting the client to have announced itself.
  * /proc/locks reports gatelock's hold without taking it; gatelock's own
    read_claim_if_held detects held-ness by acquiring, which would race a
    guard every 3 seconds.

Idle pauses also refund the grace period once per walk-away, on the
engaged->paused edge only, never across a day rollover, and never on the first
tick after a restart -- the only place the counter moves backwards.

Everything is recorded to /var/log/steam-backlog-enforcer/budget.jsonl, on
change plus a heartbeat rather than one line per 3s tick. Records name the
qualifying PIDs: reconstructing this incident from journald alone was only
possible by inferring accrual from the spacing of two warning notifications.

Also: refuse to bounce Steam while a game is running. ensure_steam_debug_port
relaunches Steam when the CDP port is closed, which kills the running game --
that is how restarting the daemon to deploy this change ended a live session.
It now defers exactly as it already did for in-flight game updates.

Talks to X through ctypes over one cached connection: at a 3s tick, forking
xprintidle and xprop would cost ~40 forks a minute.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019CEsG6BzHtYR9cHcdxPZqG
2026-08-28 19:24:14 +02:00

121 lines
4.0 KiB
Python

"""Tests for the cached X connection and the input-idle query.
Every case runs against fake library handles, so the suite never needs a
display and never depends on whether the developer happens to be at the
keyboard.
"""
from __future__ import annotations
from unittest.mock import MagicMock, patch
import pytest
from steam_backlog_enforcer._x_probe import (
XProbe,
XProbeError,
_ignore_x_error,
load_libraries,
root_window,
)
from steam_backlog_enforcer.tests._fake_xlib import fake_x11, fake_xss
XP = "steam_backlog_enforcer._x_probe"
class TestLoadLibraries:
def test_returns_all_three_when_present(self) -> None:
x11, xss, xres = MagicMock(), MagicMock(), MagicMock()
with patch(f"{XP}.ctypes.CDLL", side_effect=[x11, xss, xres]):
loaded = load_libraries()
assert loaded[0] is x11
assert loaded[1] is xss
assert loaded[2] is xres
def test_missing_xres_is_tolerated(self) -> None:
x11, xss = MagicMock(), MagicMock()
with patch(f"{XP}.ctypes.CDLL", side_effect=[x11, xss, OSError("no")]):
loaded = load_libraries()
assert loaded[0] is x11
assert loaded[2] is None
def test_missing_core_libraries_raise(self) -> None:
with (
patch(f"{XP}.ctypes.CDLL", side_effect=OSError("no libX11")),
pytest.raises(XProbeError),
):
load_libraries()
class TestConnect:
def test_opens_once_and_caches(self) -> None:
x11 = fake_x11()
probe = XProbe(libraries=(x11, fake_xss(), None))
first = probe.connect()
assert probe.connect() is first
assert x11.XOpenDisplay.call_count == 1
def test_loads_libraries_when_none_were_injected(self) -> None:
libs = (fake_x11(), fake_xss(), None)
with patch(f"{XP}.load_libraries", return_value=libs) as loader:
XProbe().connect()
loader.assert_called_once_with()
def test_a_null_display_raises(self) -> None:
probe = XProbe(libraries=(fake_x11(display=0), fake_xss(), None))
with pytest.raises(XProbeError, match="XOpenDisplay"):
probe.connect()
def test_an_error_handler_is_installed(self) -> None:
# Xlib's default handler calls exit(1); a window vanishing mid-query
# must not be able to take the daemon down.
x11 = fake_x11()
XProbe(libraries=(x11, fake_xss(), None)).connect()
x11.XSetErrorHandler.assert_called_once()
class TestClose:
def test_closing_an_open_connection_releases_it(self) -> None:
x11 = fake_x11()
probe = XProbe(libraries=(x11, fake_xss(), None))
probe.connect()
probe.close()
x11.XCloseDisplay.assert_called_once()
probe.connect()
assert x11.XOpenDisplay.call_count == 2
def test_closing_an_unopened_connection_is_a_no_op(self) -> None:
x11 = fake_x11()
XProbe(libraries=(x11, fake_xss(), None)).close()
x11.XCloseDisplay.assert_not_called()
class TestIdleSeconds:
def test_converts_milliseconds_to_seconds(self) -> None:
probe = XProbe(libraries=(fake_x11(), fake_xss(idle_ms=4500), None))
assert probe.idle_seconds() == 4.5
def test_a_failed_allocation_raises(self) -> None:
probe = XProbe(libraries=(fake_x11(), fake_xss(allocated=False), None))
with pytest.raises(XProbeError, match="AllocInfo"):
probe.idle_seconds()
def test_a_failed_query_raises_and_still_frees(self) -> None:
x11 = fake_x11()
probe = XProbe(libraries=(x11, fake_xss(ok=False), None))
with pytest.raises(XProbeError, match="QueryInfo"):
probe.idle_seconds()
x11.XFree.assert_called_once()
class TestErrorHandler:
def test_swallows_the_error_and_reports_success(self) -> None:
assert _ignore_x_error(None, None) == 0
class TestRootWindow:
def test_returns_the_default_root(self) -> None:
conn = MagicMock()
conn.x11.XDefaultRootWindow.return_value = 0x99
assert root_window(conn) == 0x99