mirror of
https://github.com/kuhyx/steam-backlog-enforcer.git
synced 2026-11-22 12:02:20 +01:00
A manual pick had no escape hatch: once locked in, every command except done/check/status/enforce/setup/serve exited 1 for 14 days or until 100% achievements. A wrong pick meant two weeks stuck with it. Adds a short mistake-correction window: - MANUAL_GRACE_DAYS = 4 — within 4 days, 'abandon-pick <app_id>' backs out of the pick. Outside it the command refuses and exits 1. - The app_id must be passed explicitly and must match the active pick, so an abandon cannot be triggered by muscle memory. - 'abandon-pick' joins _MANUAL_LOCK_EXEMPT_COMMANDS — the pre-dispatch lock check would otherwise block the only way out. It is dropped from the advertised command list once the window closes. - Abandoning clears the lock and the assignment, uninstalls the game, and reuses the existing skipped_until cooldown for ABANDON_COOLDOWN_DAYS=30 so 'scan' does not hand it straight back. - _actions.abandon_manual_pick is state-only (no uninstall cascade), matching apply_manual_pick, so the gated MCP abandon_pick tool reuses it. Also fixes a crash surfaced while picking a replacement game: the SteamUnavailableError family (including SteamUpdateInProgressError, raised when a game update defers a Steam restart) was only caught in the enforce loop. cmd_hide, cmd_pick, cmd_pick_manual and both cmd_done reconciliation points called hide_other_games bare, so an undrivable Steam aborted the command with a traceback — after the pick had already been saved. All six sites now go through the new stdout-free try_hide_other_games wrapper and report "Library hiding: skipped (<reason>)" instead. Verified live: lock message advertises the window, expired/wrong-id/no-arg paths exit 1, and abandoning released the AoE2 lock, uninstalled it and recorded the 30-day cooldown. 1030 tests pass at 100% coverage. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
269 lines
9.2 KiB
Python
269 lines
9.2 KiB
Python
"""Tests for the MCP server tools in ``_mcp``.
|
|
|
|
Tools are patched at the ``_mcp`` module namespace (where the helpers are
|
|
imported), keeping each tool's own logic under test while isolating the
|
|
already-tested leaf functions.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timedelta, timezone
|
|
from unittest.mock import patch
|
|
|
|
from steam_backlog_enforcer import _mcp
|
|
from steam_backlog_enforcer.config import State
|
|
from steam_backlog_enforcer.steam_api import GameInfo
|
|
|
|
|
|
def _game(app_id: int, hours: float) -> GameInfo:
|
|
return GameInfo(
|
|
app_id=app_id,
|
|
name=f"g{app_id}",
|
|
total_achievements=10,
|
|
unlocked_achievements=1,
|
|
playtime_minutes=0,
|
|
completionist_hours=hours,
|
|
)
|
|
|
|
|
|
class TestReadTools:
|
|
def test_get_dataset(self) -> None:
|
|
with (
|
|
patch.object(_mcp, "State") as state,
|
|
patch.object(_mcp, "build_web_dataset", return_value="DS") as bwd,
|
|
patch.object(_mcp, "dataset_to_payload", return_value={"x": 1}) as dtp,
|
|
):
|
|
assert _mcp.get_dataset() == {"x": 1}
|
|
bwd.assert_called_once_with(state.load.return_value)
|
|
dtp.assert_called_once_with("DS")
|
|
|
|
def test_get_status(self) -> None:
|
|
with (
|
|
patch.object(_mcp, "State") as state,
|
|
patch.object(_mcp, "status_payload", return_value={"ok": 1}) as sp,
|
|
):
|
|
assert _mcp.get_status() == {"ok": 1}
|
|
sp.assert_called_once_with(state.load.return_value)
|
|
|
|
def test_get_stats_subsets_dataset(self) -> None:
|
|
payload = {
|
|
"default_summary": {"qualifying": 3},
|
|
"pace_vs_hltb": None,
|
|
"games": ["ignored"],
|
|
}
|
|
with (
|
|
patch.object(_mcp, "State"),
|
|
patch.object(_mcp, "build_web_dataset"),
|
|
patch.object(_mcp, "dataset_to_payload", return_value=payload),
|
|
):
|
|
out = _mcp.get_stats()
|
|
assert out == {"default_summary": {"qualifying": 3}, "pace_vs_hltb": None}
|
|
|
|
|
|
class TestListBacklog:
|
|
def test_no_snapshot_returns_note(self) -> None:
|
|
with patch.object(_mcp, "load_snapshot", return_value=None):
|
|
out = _mcp.list_backlog()
|
|
assert out["total"] == 0
|
|
assert out["games"] == []
|
|
assert "note" in out
|
|
|
|
def test_sorts_shortest_first_excludes_complete_and_caps(self) -> None:
|
|
snap = [
|
|
{
|
|
"app_id": 440,
|
|
"name": "TF2",
|
|
"total_achievements": 100,
|
|
"unlocked_achievements": 10,
|
|
"completionist_hours": 50.0,
|
|
},
|
|
# complete → excluded
|
|
{
|
|
"app_id": 620,
|
|
"name": "Portal 2",
|
|
"total_achievements": 50,
|
|
"unlocked_achievements": 50,
|
|
"completionist_hours": 20.0,
|
|
},
|
|
{
|
|
"app_id": 70,
|
|
"name": "HL",
|
|
"total_achievements": 10,
|
|
"unlocked_achievements": 1,
|
|
"completionist_hours": 12.0,
|
|
},
|
|
# unknown hours → sorted last
|
|
{
|
|
"app_id": 30,
|
|
"name": "NoHrs",
|
|
"total_achievements": 10,
|
|
"unlocked_achievements": 0,
|
|
"completionist_hours": -1,
|
|
},
|
|
]
|
|
with patch.object(_mcp, "load_snapshot", return_value=snap):
|
|
out = _mcp.list_backlog(limit=2)
|
|
assert out["total"] == 3
|
|
assert out["returned"] == 2
|
|
assert [g["app_id"] for g in out["games"]] == [70, 440]
|
|
assert out["games"][0]["completion_pct"] == 10.0
|
|
|
|
def test_negative_limit_returns_none(self) -> None:
|
|
snap = [
|
|
{
|
|
"app_id": 70,
|
|
"name": "HL",
|
|
"total_achievements": 10,
|
|
"unlocked_achievements": 1,
|
|
}
|
|
]
|
|
with patch.object(_mcp, "load_snapshot", return_value=snap):
|
|
out = _mcp.list_backlog(limit=-5)
|
|
assert out["returned"] == 0
|
|
assert out["games"] == []
|
|
|
|
|
|
class TestResolveAndSort:
|
|
def test_resolve_found(self) -> None:
|
|
with patch.object(
|
|
_mcp, "load_snapshot", return_value=[{"app_id": 440, "name": "TF2"}]
|
|
):
|
|
assert _mcp._resolve_game_name(440) == "TF2"
|
|
|
|
def test_resolve_missing(self) -> None:
|
|
with patch.object(
|
|
_mcp, "load_snapshot", return_value=[{"app_id": 1, "name": "X"}]
|
|
):
|
|
assert _mcp._resolve_game_name(440) is None
|
|
|
|
def test_resolve_no_snapshot(self) -> None:
|
|
with patch.object(_mcp, "load_snapshot", return_value=None):
|
|
assert _mcp._resolve_game_name(440) is None
|
|
|
|
def test_sort_key_branches(self) -> None:
|
|
assert _mcp._backlog_sort_key(_game(1, 5.0)) == (0, 5.0)
|
|
assert _mcp._backlog_sort_key(_game(2, -1)) == (1, 0.0)
|
|
|
|
|
|
class TestPickManualGate:
|
|
def test_not_found(self) -> None:
|
|
with patch.object(_mcp, "load_snapshot", return_value=[]):
|
|
out = _mcp.pick_manual(440)
|
|
assert out["ok"] is False
|
|
assert "not found" in out["reason"]
|
|
|
|
def test_preview_does_not_mutate(self) -> None:
|
|
with (
|
|
patch.object(
|
|
_mcp, "load_snapshot", return_value=[{"app_id": 440, "name": "TF2"}]
|
|
),
|
|
patch.object(_mcp, "apply_manual_pick") as amp,
|
|
):
|
|
out = _mcp.pick_manual(440)
|
|
assert out["preview"] is True
|
|
assert out["game_name"] == "TF2"
|
|
amp.assert_not_called()
|
|
|
|
def test_confirm_applies(self) -> None:
|
|
with (
|
|
patch.object(
|
|
_mcp, "load_snapshot", return_value=[{"app_id": 440, "name": "TF2"}]
|
|
),
|
|
patch.object(_mcp, "State") as state,
|
|
patch.object(_mcp, "apply_manual_pick") as amp,
|
|
):
|
|
out = _mcp.pick_manual(440, confirm=True)
|
|
assert out["applied"] is True
|
|
assert out["app_id"] == 440
|
|
amp.assert_called_once_with(state.load.return_value, 440, "TF2")
|
|
|
|
|
|
class TestAbandonPickGate:
|
|
"""The MCP escape hatch mirrors the CLI grace rules, state-only."""
|
|
|
|
def _state(self, *, days_ago: float = 1.0, app_id: int = 440) -> State:
|
|
started = (datetime.now(timezone.utc) - timedelta(days=days_ago)).isoformat()
|
|
return State(
|
|
manual_pick_app_id=app_id,
|
|
manual_pick_game_name="TF2",
|
|
manual_pick_started_at=started,
|
|
current_app_id=app_id,
|
|
current_game_name="TF2",
|
|
)
|
|
|
|
def test_no_active_pick(self) -> None:
|
|
with patch.object(_mcp.State, "load", return_value=State()):
|
|
out = _mcp.abandon_pick(440)
|
|
assert out["ok"] is False
|
|
assert "No manual pick" in out["reason"]
|
|
|
|
def test_wrong_app_id(self) -> None:
|
|
with patch.object(_mcp.State, "load", return_value=self._state()):
|
|
out = _mcp.abandon_pick(999)
|
|
assert out["ok"] is False
|
|
assert "not the active manual pick" in out["reason"]
|
|
|
|
def test_expired_grace(self) -> None:
|
|
state = self._state(days_ago=_mcp.MANUAL_GRACE_DAYS + 1)
|
|
with patch.object(_mcp.State, "load", return_value=state):
|
|
out = _mcp.abandon_pick(440)
|
|
assert out["ok"] is False
|
|
assert "grace period has expired" in out["reason"]
|
|
|
|
def test_preview_does_not_mutate(self) -> None:
|
|
with (
|
|
patch.object(_mcp.State, "load", return_value=self._state()),
|
|
patch.object(_mcp, "abandon_manual_pick") as amp,
|
|
):
|
|
out = _mcp.abandon_pick(440)
|
|
assert out["preview"] is True
|
|
assert out["game_name"] == "TF2"
|
|
assert out["grace_days_left"] > 0
|
|
amp.assert_not_called()
|
|
|
|
def test_confirm_applies(self) -> None:
|
|
state = self._state()
|
|
with (
|
|
patch.object(_mcp.State, "load", return_value=state),
|
|
patch.object(_mcp, "abandon_manual_pick") as amp,
|
|
):
|
|
out = _mcp.abandon_pick(440, confirm=True)
|
|
assert out["applied"] is True
|
|
assert out["app_id"] == 440
|
|
amp.assert_called_once_with(state)
|
|
|
|
|
|
class TestBlockGamingGate:
|
|
def test_invalid_days(self) -> None:
|
|
out = _mcp.block_gaming(0)
|
|
assert out["ok"] is False
|
|
|
|
def test_preview(self) -> None:
|
|
out = _mcp.block_gaming(3)
|
|
assert out["preview"] is True
|
|
assert out["requires_root"] is True
|
|
|
|
def test_confirm_success(self) -> None:
|
|
with patch.object(_mcp, "start_total_block", return_value=True):
|
|
out = _mcp.block_gaming(3, confirm=True)
|
|
assert out["applied"] is True
|
|
assert out["days"] == 3
|
|
|
|
def test_confirm_unprivileged_returns_gracefully(self) -> None:
|
|
with patch.object(_mcp, "start_total_block", return_value=False):
|
|
out = _mcp.block_gaming(3, confirm=True)
|
|
assert out["ok"] is False
|
|
assert "privileges" in out["reason"]
|
|
|
|
def test_confirm_oserror_returns_gracefully(self) -> None:
|
|
with patch.object(_mcp, "start_total_block", side_effect=OSError("boom")):
|
|
out = _mcp.block_gaming(3, confirm=True)
|
|
assert out["ok"] is False
|
|
assert "privileges" in out["reason"]
|
|
|
|
|
|
def test_main_runs_stdio_server() -> None:
|
|
with patch.object(_mcp.mcp, "run") as run:
|
|
_mcp.main()
|
|
run.assert_called_once_with()
|