steam-backlog-enforcer/steam_backlog_enforcer/tests/test_research_expiry.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

84 lines
3.5 KiB
Python

"""The window's end: latch, full block back, store block back once."""
from __future__ import annotations
from unittest.mock import patch
from steam_backlog_enforcer import _research_window as window
from steam_backlog_enforcer._research_net import RETRY_AFTER_SECONDS
from steam_backlog_enforcer.tests._research_fixtures import Mocks, run_pass
from steam_backlog_enforcer.tests._research_helpers import HOUR, NOW, make_record
class TestExpiry:
"""At the deadline the full block returns, once, and the window stays shut."""
def test_expiry_latches_then_reapplies_the_full_block(self, tm: Mocks) -> None:
"""Test that a passed deadline closes the record and runs the block."""
window.write_record(make_record())
run_pass(NOW + 2 * HOUR)
record = window.read_record()
assert record is not None
assert record.closed is True
tm.block_tick.assert_called_once_with()
tm.reconcile.assert_not_called()
def test_store_is_reblocked_once(self, tm: Mocks) -> None:
"""Test that the normal store block returns once and is remembered."""
window.write_record(make_record())
run_pass(NOW + 2 * HOUR)
tm.restore_store.assert_called_once_with()
record = window.read_record()
assert record is not None
assert record.store_reblocked is True
run_pass(NOW + 3 * HOUR)
tm.restore_store.assert_called_once_with()
def test_failed_store_reblock_is_retried_after_a_pause(self, tm: Mocks) -> None:
"""Test that the flag is only set once the store is really blocked."""
clock = [1000.0]
tm.restore_store.return_value = False
window.write_record(make_record())
with patch(
"steam_backlog_enforcer._research_net.monotonic",
side_effect=lambda: clock[0],
):
run_pass(NOW + 2 * HOUR)
record = window.read_record()
assert record is not None
assert record.store_reblocked is False
run_pass(NOW + 2 * HOUR + 3)
assert tm.restore_store.call_count == 1
clock[0] += RETRY_AFTER_SECONDS + 1
tm.restore_store.return_value = True
run_pass(NOW + 2 * HOUR + 70)
assert tm.restore_store.call_count == 2
record = window.read_record()
assert record is not None
assert record.store_reblocked is True
def test_store_not_managed_is_just_flagged(self, tm: Mocks) -> None:
"""Test that block_store=False in config leaves the store alone."""
window.write_record(make_record())
run_pass(NOW + 2 * HOUR, block_store=False)
tm.restore_store.assert_not_called()
record = window.read_record()
assert record is not None
assert record.store_reblocked is True
def test_clock_rolled_back_after_expiry_cannot_reopen(self, tm: Mocks) -> None:
"""Test that the latch beats a clock set back into the old window."""
window.write_record(make_record())
run_pass(NOW + 2 * HOUR)
tm.reconcile.reset_mock()
run_pass(NOW)
tm.reconcile.assert_not_called()
def test_latch_write_failure_still_blocks(self, tm: Mocks) -> None:
"""Test that an unwritable record cannot keep the window open."""
window.write_record(make_record())
with patch.object(window, "write_record", side_effect=OSError("ro")):
run_pass(NOW + 2 * HOUR)
tm.block_tick.assert_called_once_with()
tm.reconcile.assert_not_called()