mirror of
https://github.com/kuhyx/steam-backlog-enforcer.git
synced 2026-11-22 12:42:20 +01:00
main.py became main/ with __main__.py, so the invocation string baked into
run.sh and the systemd unit (`python -m steam_backlog_enforcer.main`) stays
byte-identical and nothing needs redeploying. Commands are grouped into
sibling modules (status, install, picks, abandon, misc), with _shared.py as
a leaf holding the lock helpers and constants both need, and _registry.py
holding the command tables. main/__init__.py keeps every public and private
name importable, so `from steam_backlog_enforcer.main import X` is unchanged.
The subtle part is patch targets. The test suite builds them as
f"{PKG}.{name}", so a grep for the literal module path found nothing -- but
once a command moves to a submodule it resolves its dependencies from that
module's globals, and patching the main package's re-export silently stops
biting. Measured which of the 36 patched symbols moved: 8 could have passed
vacuously. Rather than repair PKG per class, the five test_main*.py files
(all cap violators anyway) were regrouped to mirror the production modules,
so each file has exactly one correct PKG. Verified by mutation: breaking a
line in status/misc/picks/_shared each makes the corresponding tests fail.
test_web_server.py patched main_mod.serve, which no longer intercepts
cmd_serve; repointed at main.misc and confirmed it fails when the call is
removed. Added tests for the __main__ shim, which coverage would otherwise
report at 0% under fail_under = 100.
Shared test builders live in tests/_main_helpers.py, reachable because the
Phase 0 name-tests-test exclude allows tests/_*.py.
1290 tests pass; every main/ module reports 100% coverage; `main`, `status`,
`bogus` and `--status` produce byte-identical output and exit codes to
pre-refactor. 55 -> 49 files over the cap.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PPpmMpTSkp8UziRc5u8B7P
226 lines
8.4 KiB
Python
226 lines
8.4 KiB
Python
"""Tests for the main CLI: enforce-on-done behaviour."""
|
|
|
|
from typing import Any
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from steam_backlog_enforcer._cmd_done import (
|
|
_enforce_on_done,
|
|
cmd_done,
|
|
)
|
|
from steam_backlog_enforcer.config import Config, State
|
|
from steam_backlog_enforcer.steam_api import GameInfo
|
|
|
|
CMD_DONE_PKG = "steam_backlog_enforcer._cmd_done"
|
|
PKG = "steam_backlog_enforcer.main"
|
|
|
|
|
|
def _snap(
|
|
app_id: int = 1,
|
|
name: str = "G",
|
|
total: int = 10,
|
|
unlocked: int = 0,
|
|
hours: float = -1,
|
|
) -> dict[str, Any]:
|
|
return {
|
|
"app_id": app_id,
|
|
"name": name,
|
|
"total_achievements": total,
|
|
"unlocked_achievements": unlocked,
|
|
"playtime_minutes": 60,
|
|
"completionist_hours": hours,
|
|
}
|
|
|
|
|
|
class TestEnforceOnDone:
|
|
"""Tests for _enforce_on_done."""
|
|
|
|
def test_no_current_game(self) -> None:
|
|
_enforce_on_done(Config(), State())
|
|
|
|
def test_kills_and_uninstalls(self) -> None:
|
|
config = Config(
|
|
kill_unauthorized_games=True,
|
|
uninstall_other_games=True,
|
|
)
|
|
state = State(current_app_id=1, current_game_name="G")
|
|
with (
|
|
patch(f"{CMD_DONE_PKG}._echo"),
|
|
patch(
|
|
f"{CMD_DONE_PKG}.enforce_allowed_game",
|
|
return_value=[(1234, 999)],
|
|
),
|
|
patch(f"{CMD_DONE_PKG}.uninstall_other_games", return_value=2),
|
|
patch(f"{CMD_DONE_PKG}.is_game_installed", return_value=True),
|
|
patch(f"{CMD_DONE_PKG}.get_all_owned_app_ids", return_value=[1, 2]),
|
|
patch(f"{CMD_DONE_PKG}.try_hide_other_games", return_value=(1, None)),
|
|
):
|
|
_enforce_on_done(config, state)
|
|
|
|
def test_no_violations_no_uninstalls(self) -> None:
|
|
config = Config(
|
|
kill_unauthorized_games=True,
|
|
uninstall_other_games=True,
|
|
)
|
|
state = State(current_app_id=1, current_game_name="G")
|
|
with (
|
|
patch(f"{CMD_DONE_PKG}._echo"),
|
|
patch(f"{CMD_DONE_PKG}.enforce_allowed_game", return_value=[]),
|
|
patch(f"{CMD_DONE_PKG}.uninstall_other_games", return_value=0),
|
|
patch(f"{CMD_DONE_PKG}.is_game_installed", return_value=True),
|
|
patch(f"{CMD_DONE_PKG}.get_all_owned_app_ids", return_value=[]),
|
|
patch(f"{CMD_DONE_PKG}.try_hide_other_games", return_value=(0, None)),
|
|
):
|
|
_enforce_on_done(config, state)
|
|
|
|
def test_hide_skipped_when_steam_unreachable(self) -> None:
|
|
config = Config(kill_unauthorized_games=False, uninstall_other_games=False)
|
|
state = State(current_app_id=1, current_game_name="G")
|
|
with (
|
|
patch(f"{CMD_DONE_PKG}._echo") as mock_echo,
|
|
patch(f"{CMD_DONE_PKG}.is_game_installed", return_value=True),
|
|
patch(f"{CMD_DONE_PKG}.get_all_owned_app_ids", return_value=[1, 2]),
|
|
patch(
|
|
f"{CMD_DONE_PKG}.try_hide_other_games",
|
|
return_value=(0, "Steam is not installed"),
|
|
),
|
|
):
|
|
_enforce_on_done(config, state)
|
|
assert "skipped (Steam is not installed)" in " ".join(
|
|
str(c) for c in mock_echo.call_args_list
|
|
)
|
|
|
|
def test_reinstall_when_not_installed(self) -> None:
|
|
config = Config(
|
|
kill_unauthorized_games=False,
|
|
uninstall_other_games=False,
|
|
steam_id="s1",
|
|
)
|
|
state = State(current_app_id=1, current_game_name="G")
|
|
with (
|
|
patch(f"{CMD_DONE_PKG}._echo"),
|
|
patch(f"{CMD_DONE_PKG}.is_game_installed", return_value=False),
|
|
patch(f"{CMD_DONE_PKG}.install_game") as mock_install,
|
|
patch(f"{CMD_DONE_PKG}.get_all_owned_app_ids", return_value=[1, 2]),
|
|
patch(f"{CMD_DONE_PKG}.try_hide_other_games", return_value=(0, None)),
|
|
):
|
|
_enforce_on_done(config, state)
|
|
mock_install.assert_called_once_with(1, "G", "s1", use_steam_protocol=True)
|
|
|
|
"""Tests for cmd_done."""
|
|
|
|
def test_no_game_assigned(self) -> None:
|
|
with patch(f"{CMD_DONE_PKG}._echo") as mock_echo:
|
|
cmd_done(Config(), State())
|
|
assert any("No game" in str(c) for c in mock_echo.call_args_list)
|
|
|
|
def test_fetch_fails(self) -> None:
|
|
mock_client = MagicMock()
|
|
mock_client.refresh_single_game.return_value = None
|
|
state = State(current_app_id=1, current_game_name="G")
|
|
with (
|
|
patch(f"{CMD_DONE_PKG}.SteamAPIClient", return_value=mock_client),
|
|
patch(f"{CMD_DONE_PKG}._echo"),
|
|
):
|
|
cmd_done(Config(steam_api_key="k", steam_id="i"), state)
|
|
|
|
def test_not_complete_enforces(self) -> None:
|
|
game = GameInfo(
|
|
app_id=1,
|
|
name="G",
|
|
total_achievements=10,
|
|
unlocked_achievements=5,
|
|
playtime_minutes=60,
|
|
)
|
|
mock_client = MagicMock()
|
|
mock_client.refresh_single_game.return_value = game
|
|
state = State(current_app_id=1, current_game_name="G")
|
|
with (
|
|
patch(f"{CMD_DONE_PKG}.SteamAPIClient", return_value=mock_client),
|
|
patch(f"{CMD_DONE_PKG}._echo"),
|
|
patch(f"{CMD_DONE_PKG}.load_hltb_cache", return_value={1: 20.0}),
|
|
patch(f"{CMD_DONE_PKG}._enforce_on_done"),
|
|
):
|
|
cmd_done(Config(steam_api_key="k", steam_id="i"), state)
|
|
|
|
def test_complete_finalizes(self) -> None:
|
|
game = GameInfo(
|
|
app_id=1,
|
|
name="G",
|
|
total_achievements=10,
|
|
unlocked_achievements=10,
|
|
playtime_minutes=60,
|
|
)
|
|
mock_client = MagicMock()
|
|
mock_client.refresh_single_game.return_value = game
|
|
state = State(current_app_id=1, current_game_name="G")
|
|
with (
|
|
patch(f"{CMD_DONE_PKG}.SteamAPIClient", return_value=mock_client),
|
|
patch(f"{CMD_DONE_PKG}._echo"),
|
|
patch(f"{CMD_DONE_PKG}.load_hltb_cache", return_value={1: 10.0}),
|
|
patch(f"{CMD_DONE_PKG}._finalize_completion") as mock_final,
|
|
):
|
|
cmd_done(Config(steam_api_key="k", steam_id="i"), state)
|
|
mock_final.assert_called_once()
|
|
|
|
def test_hltb_cache_miss_fetches(self) -> None:
|
|
game = GameInfo(
|
|
app_id=1,
|
|
name="G",
|
|
total_achievements=10,
|
|
unlocked_achievements=5,
|
|
playtime_minutes=60,
|
|
)
|
|
mock_client = MagicMock()
|
|
mock_client.refresh_single_game.return_value = game
|
|
state = State(current_app_id=1, current_game_name="G")
|
|
with (
|
|
patch(f"{CMD_DONE_PKG}.SteamAPIClient", return_value=mock_client),
|
|
patch(f"{CMD_DONE_PKG}._echo"),
|
|
patch(f"{CMD_DONE_PKG}.load_hltb_cache", return_value={}),
|
|
patch(
|
|
f"{CMD_DONE_PKG}.fetch_hltb_times_cached",
|
|
return_value={1: 15.0},
|
|
),
|
|
patch(f"{CMD_DONE_PKG}._enforce_on_done"),
|
|
):
|
|
cmd_done(Config(steam_api_key="k", steam_id="i"), state)
|
|
|
|
def test_hltb_negative_no_display(self) -> None:
|
|
"""Covers the hours <= 0 branch (no HLTB estimate display)."""
|
|
game = GameInfo(
|
|
app_id=1,
|
|
name="G",
|
|
total_achievements=10,
|
|
unlocked_achievements=5,
|
|
playtime_minutes=60,
|
|
)
|
|
mock_client = MagicMock()
|
|
mock_client.refresh_single_game.return_value = game
|
|
state = State(current_app_id=1, current_game_name="G")
|
|
with (
|
|
patch(f"{CMD_DONE_PKG}.SteamAPIClient", return_value=mock_client),
|
|
patch(f"{CMD_DONE_PKG}._echo"),
|
|
patch(f"{CMD_DONE_PKG}.load_hltb_cache", return_value={1: -1.0}),
|
|
patch(f"{CMD_DONE_PKG}._enforce_on_done"),
|
|
):
|
|
cmd_done(Config(steam_api_key="k", steam_id="i"), state)
|
|
|
|
def test_reassign_returns_true(self) -> None:
|
|
game = GameInfo(
|
|
app_id=1,
|
|
name="G",
|
|
total_achievements=10,
|
|
unlocked_achievements=10,
|
|
playtime_minutes=60,
|
|
)
|
|
mock_client = MagicMock()
|
|
mock_client.refresh_single_game.return_value = game
|
|
state = State(current_app_id=1, current_game_name="G")
|
|
with (
|
|
patch(f"{CMD_DONE_PKG}.SteamAPIClient", return_value=mock_client),
|
|
patch(f"{CMD_DONE_PKG}._echo"),
|
|
patch(f"{CMD_DONE_PKG}.load_hltb_cache", return_value={1: 50.0}),
|
|
patch(f"{CMD_DONE_PKG}._finalize_completion"),
|
|
):
|
|
cmd_done(Config(steam_api_key="k", steam_id="i"), state)
|