mirror of
https://github.com/kuhyx/steam-backlog-enforcer.git
synced 2026-11-22 00:32:18 +01:00
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
192 lines
7.0 KiB
Python
192 lines
7.0 KiB
Python
"""Tests for the stdout-free, state-only cores in ``_actions``."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
import pytest
|
|
|
|
from steam_backlog_enforcer import _allowed_games
|
|
from steam_backlog_enforcer._actions import (
|
|
apply_manual_pick,
|
|
is_manual_pick_locked,
|
|
manual_pick_age_days,
|
|
)
|
|
from steam_backlog_enforcer.config import State
|
|
|
|
|
|
def _iso_days_ago(days: float) -> str:
|
|
return (datetime.now(timezone.utc) - timedelta(days=days)).isoformat()
|
|
|
|
|
|
def _pick(app_id: int = 440, name: str = "TF2", days_ago: float = 1.0) -> dict:
|
|
return {
|
|
"app_id": app_id,
|
|
"game_name": name,
|
|
"started_at": _iso_days_ago(days_ago),
|
|
}
|
|
|
|
|
|
class TestIsManualPickLocked:
|
|
"""Tests for Is Manual Pick Locked."""
|
|
|
|
def test_no_pick_is_unlocked(self) -> None:
|
|
"""Test no pick is unlocked."""
|
|
assert is_manual_pick_locked(State()) is False
|
|
|
|
def test_finished_game_releases_lock(self) -> None:
|
|
"""Test finished game releases lock."""
|
|
state = State(
|
|
manual_picks=[{"app_id": 5, "game_name": "G", "started_at": ""}],
|
|
finished_app_ids=[5],
|
|
)
|
|
assert is_manual_pick_locked(state) is False
|
|
|
|
def test_empty_timestamp_stays_locked(self) -> None:
|
|
# Pick set but no started_at → locked (no expiry to evaluate).
|
|
"""Test empty timestamp stays locked."""
|
|
state = State(manual_picks=[{"app_id": 5, "game_name": "G", "started_at": ""}])
|
|
assert is_manual_pick_locked(state) is True
|
|
|
|
def test_recent_pick_is_locked(self) -> None:
|
|
"""Test recent pick is locked."""
|
|
state = State(
|
|
manual_picks=[
|
|
{"app_id": 5, "game_name": "G", "started_at": _iso_days_ago(1)}
|
|
]
|
|
)
|
|
assert is_manual_pick_locked(state) is True
|
|
|
|
def test_expired_pick_releases_lock(self) -> None:
|
|
"""Test expired pick releases lock."""
|
|
state = State(
|
|
manual_picks=[
|
|
{
|
|
"app_id": 5,
|
|
"game_name": "G",
|
|
"started_at": _iso_days_ago(_allowed_games.MANUAL_LOCK_DAYS + 1),
|
|
}
|
|
]
|
|
)
|
|
assert is_manual_pick_locked(state) is False
|
|
|
|
def test_one_active_pick_holds_the_lock_for_all(self) -> None:
|
|
# Multi-pick: the lock only lifts when every pick is done/expired.
|
|
"""Test one active pick holds the lock for all."""
|
|
state = State(
|
|
manual_picks=[
|
|
{"app_id": 5, "game_name": "Done", "started_at": _iso_days_ago(1)},
|
|
{"app_id": 6, "game_name": "Live", "started_at": _iso_days_ago(1)},
|
|
],
|
|
finished_app_ids=[5],
|
|
)
|
|
assert is_manual_pick_locked(state) is True
|
|
|
|
|
|
class TestLegacyManualPickMigration:
|
|
"""A lock written by the old single-slot code must survive the upgrade."""
|
|
|
|
def test_legacy_pick_is_migrated_on_load(self) -> None:
|
|
"""Test legacy pick is migrated on load."""
|
|
started = _iso_days_ago(1)
|
|
State(
|
|
manual_pick_app_id=5,
|
|
manual_pick_game_name="Legacy",
|
|
manual_pick_started_at=started,
|
|
).save()
|
|
|
|
loaded = State.load()
|
|
assert loaded.manual_picks == [
|
|
{"app_id": 5, "game_name": "Legacy", "started_at": started}
|
|
]
|
|
assert loaded.manual_pick_app_id is None
|
|
assert loaded.manual_pick_game_name == ""
|
|
assert loaded.manual_pick_started_at == ""
|
|
assert is_manual_pick_locked(loaded) is True
|
|
|
|
def test_new_format_is_left_alone(self) -> None:
|
|
"""Test new format is left alone."""
|
|
State(manual_picks=[{"app_id": 9, "game_name": "New", "started_at": ""}]).save()
|
|
assert [p["app_id"] for p in State.load().manual_picks] == [9]
|
|
|
|
def test_no_pick_needs_no_migration(self) -> None:
|
|
"""Test no pick needs no migration."""
|
|
State().save()
|
|
assert State.load().manual_picks == []
|
|
|
|
|
|
class TestApplyManualPick:
|
|
"""Tests for Apply Manual Pick."""
|
|
|
|
def test_sets_all_fields_and_enforcement_start(self) -> None:
|
|
"""Test sets all fields and enforcement start."""
|
|
state = State()
|
|
assert apply_manual_pick(state, 440, "Team Fortress 2") is None
|
|
assert [p["app_id"] for p in state.manual_picks] == [440]
|
|
assert state.manual_picks[0]["game_name"] == "Team Fortress 2"
|
|
assert state.current_app_id == 440
|
|
assert state.current_game_name == "Team Fortress 2"
|
|
assert state.manual_picks[0]["started_at"] != ""
|
|
# enforcement_started_at was empty, so it is set now.
|
|
assert state.enforcement_started_at != ""
|
|
|
|
def test_preserves_existing_enforcement_start(self) -> None:
|
|
"""Test preserves existing enforcement start."""
|
|
state = State(enforcement_started_at="2020-01-01T00:00:00+00:00")
|
|
apply_manual_pick(state, 620, "Portal 2")
|
|
assert state.enforcement_started_at == "2020-01-01T00:00:00+00:00"
|
|
|
|
def test_persists_to_disk(self) -> None:
|
|
"""Test persists to disk."""
|
|
state = State()
|
|
apply_manual_pick(state, 70, "Half-Life")
|
|
assert State.load().current_app_id == 70
|
|
|
|
def test_default_cap_is_one(self) -> None:
|
|
# Callers that do not opt in keep the historical single-slot behaviour.
|
|
"""Test default cap is one."""
|
|
state = State()
|
|
apply_manual_pick(state, 440, "TF2")
|
|
assert apply_manual_pick(state, 620, "Portal 2") is not None
|
|
|
|
|
|
class TestManualPickAgeDays:
|
|
"""Tests for Manual Pick Age Days."""
|
|
|
|
def test_no_pick_returns_none(self) -> None:
|
|
"""Test no pick returns none."""
|
|
assert manual_pick_age_days(State(), 440) is None
|
|
|
|
def test_unknown_app_id_returns_none(self) -> None:
|
|
"""Test unknown app id returns none."""
|
|
state = State(manual_picks=[_pick(440)])
|
|
assert manual_pick_age_days(state, 999) is None
|
|
|
|
def test_missing_timestamp_returns_none(self) -> None:
|
|
"""Test missing timestamp returns none."""
|
|
state = State(manual_picks=[{"app_id": 440, "game_name": "TF2"}])
|
|
assert manual_pick_age_days(state, 440) is None
|
|
|
|
def test_malformed_timestamp_returns_none(self) -> None:
|
|
"""Test malformed timestamp returns none."""
|
|
state = State(
|
|
manual_picks=[
|
|
{"app_id": 440, "game_name": "TF2", "started_at": "not-a-date"}
|
|
]
|
|
)
|
|
assert manual_pick_age_days(state, 440) is None
|
|
|
|
def test_fresh_pick_is_almost_zero_days_old(self) -> None:
|
|
"""Test fresh pick is almost zero days old."""
|
|
state = State(manual_picks=[_pick(days_ago=0)])
|
|
age = manual_pick_age_days(state, 440)
|
|
assert age is not None
|
|
assert age == pytest.approx(0.0, abs=0.01)
|
|
|
|
def test_old_pick_reports_its_age(self) -> None:
|
|
"""Test old pick reports its age."""
|
|
state = State(manual_picks=[_pick(days_ago=8)])
|
|
age = manual_pick_age_days(state, 440)
|
|
assert age is not None
|
|
assert age == pytest.approx(8.0, abs=0.01)
|