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

146 lines
5.8 KiB
Python

"""The research window: lock binding, fail-closed live record, the clock."""
from __future__ import annotations
import json
import logging
from typing import TYPE_CHECKING
from unittest.mock import patch
from steam_backlog_enforcer import _research_record as store
from steam_backlog_enforcer import _research_window as window
from steam_backlog_enforcer.tests._research_helpers import (
HOUR,
LOCK_END,
LOCK_START,
NOW,
lock,
make_record,
)
if TYPE_CHECKING:
import pytest
from steam_backlog_enforcer.tests._total_block_paths import Paths
NOW_PATCH = "steam_backlog_enforcer._research_window.now"
class TestLiveRecord:
"""live_record is the daemon's fail-closed view of the file."""
def test_no_lock_means_no_window(self) -> None:
"""Test that a record without an active total block is ignored."""
window.write_record(make_record())
assert window.live_record() is None
def test_no_record(self, total_block_paths: Paths) -> None:
"""Test that a lock without a record has no window."""
lock(total_block_paths)
with patch(NOW_PATCH, return_value=NOW):
assert window.live_record() is None
def test_matching_record(self, total_block_paths: Paths) -> None:
"""Test that a record for the live lock is returned."""
lock(total_block_paths)
window.write_record(make_record())
with patch(NOW_PATCH, return_value=NOW):
assert window.live_record() == make_record()
def test_other_locks_record_is_ignored(self, total_block_paths: Paths) -> None:
"""Test that last month's record does not open this lock."""
lock(total_block_paths)
window.write_record(make_record(lock_started_at=LOCK_START - 99.0))
with patch(NOW_PATCH, return_value=NOW):
assert window.live_record() is None
def test_corrupt_record_is_no_window_and_logged_once(
self, total_block_paths: Paths, caplog: pytest.LogCaptureFixture
) -> None:
"""Test that corruption fails closed and does not spam every tick."""
lock(total_block_paths)
store.RESEARCH_FILE.parent.mkdir(parents=True)
store.RESEARCH_FILE.write_text("{oops", encoding="utf-8")
with patch(NOW_PATCH, return_value=NOW), caplog.at_level(logging.ERROR):
assert window.live_record() is None
assert window.live_record() is None
assert len([r for r in caplog.records if "research" in r.message]) == 1
def test_corruption_is_logged_again_after_recovery(
self, total_block_paths: Paths, caplog: pytest.LogCaptureFixture
) -> None:
"""Test that a record that heals and breaks again is reported again."""
lock(total_block_paths)
store.RESEARCH_FILE.parent.mkdir(parents=True)
store.RESEARCH_FILE.write_text("{oops", encoding="utf-8")
with patch(NOW_PATCH, return_value=NOW), caplog.at_level(logging.ERROR):
window.live_record()
window.write_record(make_record())
assert window.live_record() is not None
store.RESEARCH_FILE.write_text("{oops", encoding="utf-8")
window.live_record()
assert len([r for r in caplog.records if "research" in r.message]) == 2
class TestOpenNow:
"""live_record plus is_open is the daemon's whole decision."""
@staticmethod
def _open_at(at: float) -> bool:
with patch(NOW_PATCH, return_value=at):
record = window.live_record()
return record is not None and record.is_open(at)
def test_open_window(self, total_block_paths: Paths) -> None:
"""Test that an open window on the live lock is open."""
lock(total_block_paths)
window.write_record(make_record())
assert self._open_at(NOW) is True
def test_expired_window(self, total_block_paths: Paths) -> None:
"""Test that a passed deadline is not open."""
lock(total_block_paths)
window.write_record(make_record())
assert self._open_at(NOW + 2 * HOUR) is False
def test_clock_in_the_past_is_not_open(self, total_block_paths: Paths) -> None:
"""Test that a clock before opening is fully blocked."""
lock(total_block_paths)
window.write_record(make_record())
assert self._open_at(NOW - 3 * HOUR) is False
def test_closed_latch_is_not_open(self, total_block_paths: Paths) -> None:
"""Test that a latched window never reopens."""
lock(total_block_paths)
window.write_record(make_record(closed=True))
assert self._open_at(NOW) is False
def test_missing_state_is_fully_blocked(self, total_block_paths: Paths) -> None:
"""Test that no record at all is the blocked default."""
lock(total_block_paths)
assert self._open_at(NOW) is False
def test_lock_identity_tolerates_float_rounding(
self, total_block_paths: Paths
) -> None:
"""Test that sub-second noise still matches, whole seconds do not."""
lock(total_block_paths)
window.write_record(make_record(lock_started_at=LOCK_START + 0.4))
assert self._open_at(NOW) is True
window.write_record(make_record(lock_started_at=LOCK_START + 2.0))
assert self._open_at(NOW) is False
def test_lock_without_start_is_no_lock(self, total_block_paths: Paths) -> None:
"""Test that a lock file lacking started_at cannot host a window."""
total_block_paths.lock_file.write_text(
json.dumps({"until": LOCK_END}), encoding="utf-8"
)
window.write_record(make_record())
assert self._open_at(NOW) is False
def test_now_is_the_wall_clock(self) -> None:
"""Test that now() reads time.time()."""
with patch("steam_backlog_enforcer._research_window.time.time") as clock:
clock.return_value = 42.5
assert window.now() == 42.5