steam-backlog-enforcer/steam_backlog_enforcer/tests/test_research_tick.py
Krzysztof kuhy Rudnicki 87c6f200e3 Research window: keep Steam dir, extend once
kuhy 2026-11-11, multi-day research without re-setup.

W1: once a window was used in a lock, the block's purge spares
~/.local/share/Steam (Librarian's install and local saves) for the
rest of the lock; ~/steam, ~/.steam and the rest still go. The
package stays removed, hosts/iptables are re-applied, and since the
kept install could otherwise be launched without the package, every
Steam game is killed on sight. A record that cannot be read also
keeps the folder (an irreversible delete never rests on unknown
state); the block itself stays fully applied.

W2: the 48 h cap is gone; --until is bounded by the lock's end. An
open window may be extended ONCE, to the lock end at most, never
shortened, never reopened after it ended. The extension is a
separate extended_until field beside until, so a daemon still on
the previous code reads the original window instead of rejecting the
record as corrupt (which would have purged the folder).

Failure modes unchanged: missing/corrupt record = no window, and a
corrupt one refuses a new window or extension.

Checked: 3325 tests at 100% coverage; pylint 10/10 in parts; vmbox:
extension accepted once, refused beyond the lock end, earlier or
equal, second time and non-root; extended end honoured past the
original end; at close the Steam dir and a save survive, ~/steam and
~/.steam go, a game started after close is killed; extension to
exactly the lock end accepted. 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 16:28:43 +01:00

143 lines
5.7 KiB
Python

"""One daemon pass under a total block: the window and the full block."""
from __future__ import annotations
import subprocess
import pytest
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(keep_steam_dir=False)
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(keep_steam_dir=True)
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(keep_steam_dir=True)
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(keep_steam_dir=True)
tm.restore_store.assert_not_called()
class TestOpenWindow:
"""Inside the window: Steam is left alone, everything but Librarian is not."""
def test_extended_window_stays_open_past_its_first_deadline(
self, tm: Mocks
) -> None:
"""Test that the extension keeps the research pass running."""
window.write_record(make_record(extended_until=NOW + 9 * HOUR))
run_pass(NOW + 4 * HOUR)
tm.reconcile.assert_called_once()
tm.block_tick.assert_not_called()
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}
class TestSlowSystem:
"""A command that times out costs one tick, never the daemon."""
@pytest.mark.parametrize(
"error",
[subprocess.TimeoutExpired(["pacman", "-Qi", "steam"], 10), OSError("busy")],
)
def test_failed_reconcile_is_retried_next_tick(
self, tm: Mocks, error: Exception
) -> None:
"""Test that a timed-out pacman/iptables call does not crash the loop."""
tm.reconcile.side_effect = error
window.write_record(make_record())
run_pass()
tm.block_tick.assert_not_called()
tm.enforce.assert_not_called()
tm.reconcile.side_effect = None
run_pass()
tm.enforce.assert_called_once()