steam-backlog-enforcer/steam_backlog_enforcer/tests/test_mcp_actions.py
Krzysztof kuhy Rudnicki 737b49246c Allow abandoning a manual pick at any time; cap picks at 2
The 4-day grace window left kuhy stuck: three picks locked in, the window
closed on two of them, so nothing could be abandoned and no new pick could
be added.

- abandon-pick now works however old the pick is. MANUAL_GRACE_DAYS,
  manual_pick_grace_remaining() and can_abandon_manual_pick() are deleted;
  abandon_manual_pick()'s existence check is the single remaining guard
  (the previous post-guard and its "race" test became unreachable).
- Config.max_manual_picks 3 -> 2. kuhy's config.json does not set the key,
  so the default change is sufficient.
- status/lock/MCP surfaces report informational age ("picked N day(s) ago",
  age_days) instead of a grace countdown. Note this is a sign flip, not a
  rename: age is now - started, where grace was deadline - now.

Verified live: abandoned Montaro (495890) and Battle Bruise (618510), both
previously refused as EXPIRED; pick-manual 3164500 now reaches its confirm
prompt at 1/2 slots used. 1344 tests, 100.00% coverage.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016A1EYiv7e7HGhET84oa9M3
2026-08-24 15:41:32 +02:00

159 lines
5.8 KiB
Python

"""Tests for the mutating MCP tools and their confirm gates."""
from __future__ import annotations
from datetime import datetime, timedelta, timezone
from unittest.mock import patch
from steam_backlog_enforcer import _mcp_actions as mcp_actions
from steam_backlog_enforcer import _mcp_query as mcp_query
from steam_backlog_enforcer import _mcp_server as mcp_server
from steam_backlog_enforcer.config import State
class TestPickManualGate:
def test_not_found(self) -> None:
with patch.object(mcp_query, "load_snapshot", return_value=[]):
out = mcp_actions.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_server,
"load_snapshot",
return_value=[{"app_id": 440, "name": "TF2"}],
),
patch.object(mcp_actions, "apply_manual_pick") as amp,
):
out = mcp_actions.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_server,
"load_snapshot",
return_value=[{"app_id": 440, "name": "TF2"}],
),
patch.object(mcp_actions, "State") as state,
patch.object(mcp_actions, "Config") as config,
patch.object(mcp_actions, "apply_manual_pick", return_value=None) as amp,
):
out = mcp_actions.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",
max_picks=config.load.return_value.max_manual_picks,
)
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_picks=[
{"app_id": app_id, "game_name": "TF2", "started_at": started}
],
current_app_id=app_id,
current_game_name="TF2",
)
def test_no_active_pick(self) -> None:
with patch.object(mcp_query.State, "load", return_value=State()):
out = mcp_actions.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_query.State, "load", return_value=self._state()):
out = mcp_actions.abandon_pick(999)
assert out["ok"] is False
assert "not one of the active manual picks" in out["reason"]
def test_old_pick_is_still_abandonable(self) -> None:
# Past the deleted 4-day grace window, inside the 14-day lock.
state = self._state(days_ago=8)
with (
patch.object(mcp_query.State, "load", return_value=state),
patch.object(mcp_actions, "abandon_manual_pick", return_value=True),
):
out = mcp_actions.abandon_pick(440, confirm=True)
assert out["ok"] is True
def test_preview_does_not_mutate(self) -> None:
with (
patch.object(mcp_query.State, "load", return_value=self._state()),
patch.object(mcp_actions, "abandon_manual_pick") as amp,
):
out = mcp_actions.abandon_pick(440)
assert out["preview"] is True
assert out["game_name"] == "TF2"
assert out["age_days"] > 0
amp.assert_not_called()
def test_confirm_applies(self) -> None:
state = self._state()
with (
patch.object(mcp_query.State, "load", return_value=state),
patch.object(mcp_actions, "abandon_manual_pick") as amp,
):
out = mcp_actions.abandon_pick(440, confirm=True)
assert out["applied"] is True
assert out["app_id"] == 440
amp.assert_called_once_with(state, 440)
def test_refused_at_cap(self) -> None:
with (
patch.object(
mcp_server,
"load_snapshot",
return_value=[{"app_id": 440, "name": "TF2"}],
),
patch.object(mcp_actions, "State"),
patch.object(mcp_actions, "Config"),
patch.object(mcp_actions, "apply_manual_pick", return_value="cap reached"),
):
out = mcp_actions.pick_manual(440, confirm=True)
assert out["ok"] is False
assert out["reason"] == "cap reached"
class TestBlockGamingGate:
def test_invalid_days(self) -> None:
out = mcp_actions.block_gaming(0)
assert out["ok"] is False
def test_preview(self) -> None:
out = mcp_actions.block_gaming(3)
assert out["preview"] is True
assert out["requires_root"] is True
def test_confirm_success(self) -> None:
with patch.object(mcp_actions, "start_total_block", return_value=True):
out = mcp_actions.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_actions, "start_total_block", return_value=False):
out = mcp_actions.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_actions, "start_total_block", side_effect=OSError("boom")
):
out = mcp_actions.block_gaming(3, confirm=True)
assert out["ok"] is False
assert "privileges" in out["reason"]