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

101 lines
4.0 KiB
Python

"""The Steam folder survives the purge once a research window was used."""
from __future__ import annotations
import json
from typing import TYPE_CHECKING
from unittest.mock import patch
from steam_backlog_enforcer._total_block import enforce_total_block_tick
from steam_backlog_enforcer._total_block_purge import (
_remove_steam_remnants,
purge_steam_and_proton,
)
if TYPE_CHECKING:
from pathlib import Path
from steam_backlog_enforcer.tests._total_block_paths import Paths
PKG = "steam_backlog_enforcer._total_block_purge"
BLOCK = "steam_backlog_enforcer._total_block"
def _populate(paths: Paths) -> tuple[Path, Path, Path]:
"""Create the Steam folder (with a save), ~/.steam and ~/steam."""
save = paths.steam_dir / "steamapps" / "compatdata" / "4197610" / "save.dat"
save.parent.mkdir(parents=True)
save.write_text("progress", encoding="utf-8")
dot_steam, portable = paths.remnant_paths[0], paths.remnant_paths[1]
dot_steam.mkdir(parents=True)
portable.mkdir(parents=True)
return save, dot_steam, portable
class TestKeepSteamDir:
"""Only the Steam data folder is spared; every other remnant still goes."""
def test_default_purges_the_folder_too(self, total_block_paths: Paths) -> None:
"""Test that without research nothing changed."""
save, dot_steam, portable = _populate(total_block_paths)
removed = _remove_steam_remnants()
assert str(total_block_paths.steam_dir) in removed
assert not save.exists()
assert not dot_steam.exists()
assert not portable.exists()
def test_kept_folder_and_its_saves_survive(self, total_block_paths: Paths) -> None:
"""Test that the install and local saves are not touched."""
save, dot_steam, portable = _populate(total_block_paths)
removed = _remove_steam_remnants(keep_steam_dir=True)
assert str(total_block_paths.steam_dir) not in removed
assert save.read_text(encoding="utf-8") == "progress"
assert not dot_steam.exists()
assert not portable.exists()
def test_purge_passes_the_flag_and_logs_only_what_went(
self, total_block_paths: Paths
) -> None:
"""Test that the whole purge honours the flag and logs honestly."""
save, _, _ = _populate(total_block_paths)
with patch(f"{PKG}.is_package_installed", return_value=False):
purge_steam_and_proton(keep_steam_dir=True)
assert save.exists()
logged = json.loads(total_block_paths.purge_log_file.read_text("utf-8"))
assert str(total_block_paths.steam_dir) not in logged[0]["removed_paths"]
def test_default_purge_still_removes_the_folder(
self, total_block_paths: Paths
) -> None:
"""Test that the old call signature keeps its old behaviour."""
save, _, _ = _populate(total_block_paths)
with patch(f"{PKG}.is_package_installed", return_value=False):
purge_steam_and_proton()
assert not save.exists()
class TestTickPassesTheFlag:
"""enforce_total_block_tick is the only caller on the hot path."""
def _tick(self, **kwargs: bool) -> object:
with (
patch(f"{BLOCK}.kill_steam_and_launchers", return_value=[]),
patch(f"{BLOCK}.kill_processes_by_name", return_value=[]),
patch(f"{BLOCK}.is_steam_installed", return_value=False),
patch(f"{BLOCK}.apply_total_block_iptables", return_value=True),
patch(f"{BLOCK}.refresh_total_block_hosts", return_value=True),
patch(f"{BLOCK}.purge_steam_and_proton") as purge,
):
enforce_total_block_tick(**kwargs)
return purge
def test_default_purges_everything(self) -> None:
"""Test that the default is the full purge."""
purge = self._tick()
purge.assert_called_once_with(keep_steam_dir=False)
def test_keep_flag_reaches_the_purge(self) -> None:
"""Test that the research exemption reaches the purge."""
purge = self._tick(keep_steam_dir=True)
purge.assert_called_once_with(keep_steam_dir=True)