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

183 lines
6.9 KiB
Python

"""Opening a research window: the 48 h cap, once per lock, fail closed."""
from __future__ import annotations
from datetime import UTC, datetime
import os
import time
from typing import TYPE_CHECKING
from unittest.mock import patch
import pytest
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:
from collections.abc import Iterator
from steam_backlog_enforcer._research_record import ResearchRecord
from steam_backlog_enforcer.tests._total_block_paths import Paths
NOW_PATCH = "steam_backlog_enforcer._research_window.now"
def _at(offset: float) -> datetime:
"""The instant NOW + *offset* seconds, timezone-aware."""
return datetime.fromtimestamp(NOW + offset, tz=UTC)
def _open(offset: float) -> ResearchRecord:
with patch(NOW_PATCH, return_value=NOW):
return window.open_research_window(_at(offset))
@pytest.fixture(name="live")
def _live(total_block_paths: Paths) -> None:
"""An active 6-day lock, two days in."""
lock(total_block_paths)
@pytest.mark.usefixtures("live")
class TestOpen:
"""The happy path and the bounds around it."""
def test_records_the_window_for_this_lock(self) -> None:
"""Test that a valid expiry is stored with the lock's identity."""
record = _open(10 * HOUR)
assert record == window.read_record()
assert (record.lock_started_at, record.lock_until) == (LOCK_START, LOCK_END)
assert (record.opened_at, record.until) == (NOW, NOW + 10 * HOUR)
assert (record.closed, record.store_reblocked) == (False, False)
def test_exactly_48_hours_is_allowed(self) -> None:
"""Test that the cap is inclusive."""
assert _open(48 * HOUR).until == NOW + 48 * HOUR
def test_more_than_48_hours_is_refused(self) -> None:
"""Test that one second over the cap is refused and nothing written."""
with pytest.raises(window.ResearchRefusedError, match="48"):
_open(48 * HOUR + 1)
assert window.read_record() is None
@pytest.mark.parametrize("offset", [0.0, -1.0, -3 * HOUR])
def test_not_in_the_future_is_refused(self, offset: float) -> None:
"""Test that now and the past are refused."""
with pytest.raises(window.ResearchRefusedError, match="future"):
_open(offset)
assert window.read_record() is None
def test_window_may_not_outlive_the_lock(self) -> None:
"""Test that an expiry after the lock end is refused."""
with (
patch(NOW_PATCH, return_value=LOCK_END - HOUR),
pytest.raises(window.ResearchRefusedError, match="lock"),
):
window.open_research_window(datetime.fromtimestamp(LOCK_END + 1, tz=UTC))
assert window.read_record() is None
def test_write_failure_is_a_refusal(self) -> None:
"""Test that an unwritable state dir is reported, not a traceback."""
with (
patch.object(window, "write_record", side_effect=OSError("ro")),
pytest.raises(window.ResearchRefusedError, match="write"),
):
_open(HOUR)
class TestNoLock:
"""Without an active total block there is nothing to research."""
def test_no_lock_file(self) -> None:
"""Test that the command has no meaning without a block."""
with pytest.raises(window.ResearchRefusedError, match="total block"):
_open(HOUR)
def test_expired_lock(self, total_block_paths: Paths) -> None:
"""Test that a lock that already ended cannot host a window."""
lock(total_block_paths)
with (
patch(NOW_PATCH, return_value=LOCK_END + 1),
pytest.raises(window.ResearchRefusedError, match="total block"),
):
window.open_research_window(datetime.fromtimestamp(LOCK_END + 2, UTC))
@pytest.mark.usefixtures("live")
class TestOncePerLock:
"""One window per lock, however it ended; reruns never extend."""
def test_second_open_is_refused_and_changes_nothing(self) -> None:
"""Test that a rerun with a later expiry never extends the window."""
_open(2 * HOUR)
before = store.RESEARCH_FILE.read_bytes()
with pytest.raises(window.ResearchRefusedError, match="already"):
_open(40 * HOUR)
assert store.RESEARCH_FILE.read_bytes() == before
def test_used_window_stays_used_after_it_closed(self) -> None:
"""Test that an expired, latched window is not a free second window."""
window.write_record(make_record(closed=True, store_reblocked=True))
with pytest.raises(window.ResearchRefusedError, match="already"):
_open(HOUR)
def test_a_new_lock_gets_its_own_window(self, total_block_paths: Paths) -> None:
"""Test that last lock's record does not burn the next lock's window."""
window.write_record(make_record(lock_started_at=LOCK_START - 7 * 86400))
lock(total_block_paths)
assert _open(HOUR).lock_started_at == LOCK_START
def test_corrupt_state_refuses_instead_of_resetting(self) -> None:
"""Test that corruption cannot be used to get a second window."""
store.RESEARCH_FILE.parent.mkdir(parents=True)
store.RESEARCH_FILE.write_text("{oops", encoding="utf-8")
with pytest.raises(window.ResearchRefusedError, match="unreadable"):
_open(HOUR)
assert store.RESEARCH_FILE.read_text(encoding="utf-8") == "{oops"
@pytest.fixture(name="warsaw")
def _warsaw(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]:
"""Run the test in Europe/Warsaw local time, restoring the zone after."""
monkeypatch.setenv("TZ", "Europe/Warsaw")
time.tzset()
yield
monkeypatch.undo()
time.tzset()
class TestParseUntil:
"""--until is local time unless it carries an offset."""
@pytest.mark.usefixtures("warsaw")
def test_naive_means_local_time(self) -> None:
"""Test that 2026-11-13T00:00 is midnight in the local zone."""
parsed = window.parse_until("2026-11-13T00:00")
assert parsed == datetime(2026, 11, 12, 23, 0, tzinfo=UTC)
assert os.environ["TZ"] == "Europe/Warsaw"
def test_offset_is_honoured(self) -> None:
"""Test that an explicit offset is not reinterpreted."""
parsed = window.parse_until("2026-11-13T00:00:00+02:00")
assert parsed == datetime(2026, 11, 12, 22, 0, tzinfo=UTC)
def test_z_suffix(self) -> None:
"""Test that Z means UTC."""
assert window.parse_until("2026-11-13T00:00:00Z") == datetime(
2026, 11, 13, tzinfo=UTC
)
@pytest.mark.parametrize("raw", ["", "tomorrow", "2026-13-40", "13:00"])
def test_garbage_is_a_value_error(self, raw: str) -> None:
"""Test that an unparsable expiry is rejected."""
with pytest.raises(ValueError, match="until"):
window.parse_until(raw)