Docstrings for the research game tests

This commit is contained in:
Krzysztof Rudnicki 2026-11-21 16:12:08 +01:00
parent 2ac30bcdaf
commit ef6d28617a
3 changed files with 35 additions and 6 deletions

View File

@ -9,6 +9,7 @@ import pytest
from steam_backlog_enforcer import _ctl_ops
from steam_backlog_enforcer import _research_game as rg
from steam_backlog_enforcer import _total_block_purge as purge
from steam_backlog_enforcer._ctl_protocol import INVALID_PARAMS, CtlError
from steam_backlog_enforcer._total_block_purge import _remove_steam_remnants
from steam_backlog_enforcer.tests._ctl_fixtures import make_ctx
@ -17,16 +18,19 @@ APP = 4197610
def _approve(app_id: int = APP) -> None:
"""Approve."""
rg.APPROVAL_FILE.parent.mkdir(parents=True, exist_ok=True)
rg.APPROVAL_FILE.write_text(json.dumps({"app_id": app_id}), encoding="utf-8")
def test_ops_are_allowlisted() -> None:
"""Ops are allowlisted."""
assert "research_game" in _ctl_ops.OPS
assert "research_game_revoke" in _ctl_ops.OPS
def test_add_takes_effect_at_once() -> None:
"""Add takes effect at once."""
_approve()
data = _ctl_ops.OPS["research_game"](make_ctx(), {"app_id": APP})
assert data["app_id"] == APP
@ -34,6 +38,7 @@ def test_add_takes_effect_at_once() -> None:
def test_add_refuses_other_app_than_the_file() -> None:
"""Add refuses other app than the file."""
_approve(APP + 1)
with pytest.raises(CtlError) as err:
_ctl_ops.OPS["research_game"](make_ctx(), {"app_id": APP})
@ -42,11 +47,13 @@ def test_add_refuses_other_app_than_the_file() -> None:
def test_add_needs_an_int_app_id() -> None:
"""Add needs an int app id."""
with pytest.raises(CtlError):
_ctl_ops.OPS["research_game"](make_ctx(), {"app_id": "x"})
def test_revoke() -> None:
"""Revoke."""
_approve()
_ctl_ops.OPS["research_game"](make_ctx(), {"app_id": APP})
assert _ctl_ops.OPS["research_game_revoke"](make_ctx(), {}) == {"app_id": APP}
@ -54,8 +61,7 @@ def test_revoke() -> None:
def test_total_block_purge_keeps_the_steam_folder() -> None:
from steam_backlog_enforcer import _total_block_purge as purge
"""Total block purge keeps the steam folder."""
purge.STEAM_DATA_DIR.mkdir(parents=True, exist_ok=True)
_approve()
rg.register(APP)

View File

@ -19,6 +19,7 @@ APP = 4197610
def _approve(path: Path, app_id: int = APP, flags: list[str] | None = None) -> None:
"""Approve."""
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(
json.dumps({"app_id": app_id, "added_at": 1.0, "flags": flags or []}),
@ -27,22 +28,26 @@ def _approve(path: Path, app_id: int = APP, flags: list[str] | None = None) -> N
def _wrapper_proc(proc: Path, pid: int, argv0: str = "game_research_desktop") -> None:
"""Wrapper proc."""
(proc / str(pid)).mkdir(parents=True)
(proc / str(pid) / "cmdline").write_bytes(f"/x/bin/{argv0}\0--serve\0".encode())
def test_no_file_means_no_research_game() -> None:
"""No file means no research game."""
assert rg.research_game_id() is None
assert rg.kept_ids() == frozenset()
assert rg.playable_ids() == frozenset()
def test_file_alone_is_not_honoured() -> None:
"""File alone is not honoured."""
_approve(rg.APPROVAL_FILE)
assert rg.research_game_id() is None
def test_file_and_record_agree() -> None:
"""File and record agree."""
_approve(rg.APPROVAL_FILE)
rg.register(APP)
assert rg.research_game_id() == APP
@ -50,6 +55,7 @@ def test_file_and_record_agree() -> None:
def test_file_changed_to_other_app_revokes() -> None:
"""File changed to other app revokes."""
_approve(rg.APPROVAL_FILE)
rg.register(APP)
_approve(rg.APPROVAL_FILE, app_id=APP + 1)
@ -61,6 +67,7 @@ def test_file_changed_to_other_app_revokes() -> None:
["", "{", "[]", '{"app_id": "x"}', '{"app_id": true}', '{"app_id": -1}'],
)
def test_malformed_file_fails_closed(text: str) -> None:
"""Malformed file fails closed."""
rg.APPROVAL_FILE.parent.mkdir(parents=True, exist_ok=True)
rg.APPROVAL_FILE.write_text(text, encoding="utf-8")
rg.RECORD_FILE.parent.mkdir(parents=True, exist_ok=True)
@ -69,6 +76,7 @@ def test_malformed_file_fails_closed(text: str) -> None:
def test_register_refuses_mismatch_and_logs_add() -> None:
"""Register refuses mismatch and logs add."""
_approve(rg.APPROVAL_FILE)
with pytest.raises(rg.ResearchGameError):
rg.register(APP + 1)
@ -79,12 +87,14 @@ def test_register_refuses_mismatch_and_logs_add() -> None:
def test_register_refuses_without_file() -> None:
"""Register refuses without file."""
with pytest.raises(rg.ResearchGameError):
rg.register(APP)
assert not rg.RECORD_FILE.exists()
def test_revoke_clears_record_and_logs() -> None:
"""Revoke clears record and logs."""
_approve(rg.APPROVAL_FILE)
rg.register(APP)
assert rg.revoke() == APP
@ -94,6 +104,7 @@ def test_revoke_clears_record_and_logs() -> None:
def test_playable_only_while_wrapper_flag_live(tmp_path: Path) -> None:
"""Playable only while wrapper flag live."""
proc = tmp_path / "proc"
flag = rg.APPROVAL_FILE.parent / "recording.pid"
_approve(rg.APPROVAL_FILE, flags=[str(flag)])
@ -109,6 +120,7 @@ def test_playable_only_while_wrapper_flag_live(tmp_path: Path) -> None:
def test_flag_owned_by_other_process_is_ignored(tmp_path: Path) -> None:
"""Flag owned by other process is ignored."""
proc = tmp_path / "proc"
flag = rg.APPROVAL_FILE.parent / "recording.pid"
_approve(rg.APPROVAL_FILE, flags=[str(flag)])
@ -120,6 +132,7 @@ def test_flag_owned_by_other_process_is_ignored(tmp_path: Path) -> None:
def test_flag_outside_home_is_ignored(tmp_path: Path) -> None:
"""Flag outside home is ignored."""
proc = tmp_path / "proc"
_wrapper_proc(proc, 5)
outside = tmp_path / "elsewhere.pid"
@ -134,6 +147,7 @@ def test_flag_outside_home_is_ignored(tmp_path: Path) -> None:
def test_stale_launch_flag_is_ignored(tmp_path: Path) -> None:
"""Stale launch flag is ignored."""
proc = tmp_path / "proc"
_wrapper_proc(proc, 9)
flag = rg.APPROVAL_FILE.parent / "research_launch.pid"

View File

@ -9,6 +9,7 @@ from unittest.mock import patch
import pytest
from steam_backlog_enforcer import _research_game as rg
from steam_backlog_enforcer import _steam_state
from steam_backlog_enforcer._enforce_guards import guard_installed_games
from steam_backlog_enforcer._scanning_tampering import _legitimately_played
from steam_backlog_enforcer.config import State
@ -49,6 +50,7 @@ def _record_live(tmp_path: Path) -> Path:
def test_uninstall_sweep_keeps_it() -> None:
"""Uninstall sweep keeps it."""
removed: list[int] = []
with (
patch(
@ -66,6 +68,7 @@ def test_uninstall_sweep_keeps_it() -> None:
@pytest.mark.parametrize("strict", [False, True])
def test_install_guard_keeps_it(*, strict: bool) -> None:
"""Install guard keeps it."""
removed: list[int] = []
pkg = "steam_backlog_enforcer._enforce_guards"
with (
@ -81,10 +84,10 @@ def test_install_guard_keeps_it(*, strict: bool) -> None:
def test_uninstall_game_refuses_its_files() -> None:
from steam_backlog_enforcer._steam_state import STEAMAPPS_PATH
manifest = STEAMAPPS_PATH / f"appmanifest_{RESEARCH}.acf"
game_dir = STEAMAPPS_PATH / "common" / "Librarian"
"""Uninstall game refuses its files."""
steamapps = _steam_state.STEAMAPPS_PATH # patched per test: read late
manifest = steamapps / f"appmanifest_{RESEARCH}.acf"
game_dir = steamapps / "common" / "Librarian"
game_dir.mkdir(parents=True)
manifest.write_text('"AppState"\n{\n\t"installdir"\t\t"Librarian"\n}\n')
assert uninstall_game(RESEARCH, "Librarian") is False
@ -93,10 +96,12 @@ def test_uninstall_game_refuses_its_files() -> None:
def test_stays_visible_in_the_library() -> None:
"""Stays visible in the library."""
pkg = "steam_backlog_enforcer.library_hider"
captured: dict[str, str] = {}
def fake_eval(js: str) -> dict[str, object]:
"""Fake eval."""
captured["js"] = js
return {}
@ -111,10 +116,12 @@ def test_stays_visible_in_the_library() -> None:
def test_tampering_check_exempts_it() -> None:
"""Tampering check exempts it."""
assert RESEARCH in _legitimately_played(State(current_app_id=ASSIGNED))
def test_killed_when_not_recording() -> None:
"""Killed when not recording."""
with (
patch(
"steam_backlog_enforcer.enforcer.get_running_steam_game_pids",
@ -127,6 +134,7 @@ def test_killed_when_not_recording() -> None:
def test_spared_while_recording(tmp_path: Path) -> None:
"""Spared while recording."""
with (
patch.object(rg, "PROC", _record_live(tmp_path)),
patch(
@ -140,6 +148,7 @@ def test_spared_while_recording(tmp_path: Path) -> None:
def test_revoked_game_is_treated_normally() -> None:
"""Revoked game is treated normally."""
rg.APPROVAL_FILE.unlink()
removed: list[int] = []
with (