steam-backlog-enforcer/steam_backlog_enforcer/tests/test_research_tick.py
Krzysztof kuhy Rudnicki 5d5aa3e773
Some checks are pending
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) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / web-coverage (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Add Librarian-only research window to total block
kuhy 2026-11-11: game-design research needs Librarian (app 4197610)
actually played during the 6-day total block. New root command
total-block-research --until T opens ONE window per lock: <= 48 h,
never past the lock end, a rerun never extends it.

The command only validates and records (/var/lib/steam-backlog-
enforcer, root 0700; lock id + deadline + two latches). The daemon
opens and closes everything: Steam hosts names, the store block, the
total-block chain (IP cache kept) and the steam package, reinstalled
with only guard-lib's package-block hook masked for that one pacman
call (--hookdir, proved in a guest). Lock file, hook and end date are
never touched. Browser-game sites stay null-routed.

Only Librarian may run: loop-local allowed set, strict mode (no
whitelist exceptions, no zero-achievement games), launchers and
counted games killed; state.json is never read or written.

Expiry latches the record, then the ordinary tick re-applies hosts
(now reconciled each tick), chain, uninstall and purge; the normal
store block is restored once, unconditionally.

Failure modes: missing/corrupt/unparseable record = no window and a
corrupt one also refuses a new window (fail closed); a clock set
back cannot reopen a latched window.

Checked: 3290 tests at 100% coverage; whole-tree pylint 10/10 run in
three parts (the hook exceeds the 60 s cap, so SKIP=pylint here); vmbox
guest with real guardctl package-block and the real unit, window
opened, Librarian-only enforced, clock moved past the deadline and the
full block re-applied. Not verified on the host.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWWzuCEg1m87jpKyRrw
2026-11-11 14:29:58 +01:00

109 lines
4.5 KiB
Python

"""One daemon pass under a total block: the window and the full block."""
from __future__ import annotations
from steam_backlog_enforcer import _research_record as store
from steam_backlog_enforcer import _research_window as window
from steam_backlog_enforcer import config as cfg
from steam_backlog_enforcer.tests._research_fixtures import Mocks, run_pass
from steam_backlog_enforcer.tests._research_helpers import HOUR, NOW, make_record
class TestFullBlock:
"""Without a live window the whole total block is enforced."""
def test_no_record_runs_the_full_tick(self, tm: Mocks) -> None:
"""Test that the default is the full block."""
run_pass()
tm.block_tick.assert_called_once_with()
tm.reconcile.assert_not_called()
tm.enforce.assert_not_called()
def test_corrupt_record_runs_the_full_tick(self, tm: Mocks) -> None:
"""Test that unreadable research state means fully blocked."""
store.RESEARCH_FILE.parent.mkdir(parents=True)
store.RESEARCH_FILE.write_text("{oops", encoding="utf-8")
run_pass()
tm.block_tick.assert_called_once_with()
tm.reconcile.assert_not_called()
def test_clock_before_opening_blocks_without_latching(self, tm: Mocks) -> None:
"""Test that a clock set back blocks, but does not burn the window."""
window.write_record(make_record())
run_pass(NOW - 3 * HOUR)
tm.block_tick.assert_called_once_with()
tm.reconcile.assert_not_called()
record = window.read_record()
assert record is not None
assert record.closed is False
def test_latched_and_reblocked_record_only_runs_the_block(self, tm: Mocks) -> None:
"""Test that a finished window costs nothing but the block tick."""
window.write_record(make_record(closed=True, store_reblocked=True))
run_pass(NOW + 5 * HOUR)
tm.block_tick.assert_called_once_with()
tm.restore_store.assert_not_called()
class TestOpenWindow:
"""Inside the window: Steam is left alone, everything but Librarian is not."""
def test_reconciles_and_skips_the_full_block(self, tm: Mocks) -> None:
"""Test that the open window replaces the block tick."""
window.write_record(make_record())
run_pass()
tm.reconcile.assert_called_once_with(make_record())
tm.block_tick.assert_not_called()
def test_steam_is_never_killed_uninstalled_or_purged(self, tm: Mocks) -> None:
"""Test that the destructive half of the block stays off."""
window.write_record(make_record())
run_pass()
tm.steam_kill.assert_not_called()
tm.steam_uninstall.assert_not_called()
tm.steam_purge.assert_not_called()
def test_other_launchers_and_counted_games_are_still_killed(
self, tm: Mocks
) -> None:
"""Test that non-Steam games stay forbidden during the window."""
window.write_record(make_record())
run_pass()
tm.launchers.assert_called_once()
tm.counted.assert_called_once()
def test_only_librarian_is_allowed_and_strictly(self, tm: Mocks) -> None:
"""Test that the allowed set is exactly Librarian, no exceptions."""
window.write_record(make_record())
run_pass()
tm.enforce.assert_called_once_with(
{window.LIBRARIAN_APP_ID}, kill_unauthorized=True, strict=True
)
tm.guard.assert_called_once_with({window.LIBRARIAN_APP_ID}, strict=True)
def test_killed_game_is_reported(self, tm: Mocks) -> None:
"""Test that a killed non-Librarian game raises a notification."""
tm.enforce.return_value = [(4242, 12345)]
window.write_record(make_record())
run_pass()
tm.notify.assert_called_once()
def test_no_game_enforcement_while_steam_is_not_installed_yet(
self, tm: Mocks
) -> None:
"""Test that the install gap does not scan an absent library."""
tm.steam_installed.return_value = False
window.write_record(make_record())
run_pass()
tm.enforce.assert_not_called()
tm.guard.assert_not_called()
def test_real_assignment_is_never_touched(self, tm: Mocks) -> None:
"""Test that state.json (the real backlog) is not read or written."""
cfg.STATE_FILE.write_text('{"current_app_id": 1366540}', encoding="utf-8")
before = cfg.STATE_FILE.read_bytes()
window.write_record(make_record())
run_pass()
assert cfg.STATE_FILE.read_bytes() == before
assert tm.enforce.call_args.args[0] == {window.LIBRARIAN_APP_ID}