mirror of
https://github.com/kuhyx/steam-backlog-enforcer.git
synced 2026-11-11 18:13:49 +01:00
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / web-coverage (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
218 lines
8.0 KiB
Python
218 lines
8.0 KiB
Python
"""Tests for config module."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC
|
|
import json
|
|
from typing import TYPE_CHECKING, Any
|
|
from unittest.mock import patch
|
|
|
|
from steam_backlog_enforcer._snapshot import load_snapshot, save_snapshot
|
|
from steam_backlog_enforcer.config import Config, State
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
|
|
class TestConfig:
|
|
"""Tests for Config dataclass."""
|
|
|
|
def test_defaults(self) -> None:
|
|
"""Test defaults."""
|
|
cfg = Config()
|
|
assert cfg.steam_api_key == ""
|
|
assert cfg.steam_id == ""
|
|
assert cfg.block_store is True
|
|
assert cfg.kill_unauthorized_games is True
|
|
assert cfg.uninstall_other_games is True
|
|
assert cfg.desktop_notifications is True
|
|
|
|
def test_save(self, tmp_path: Path) -> None:
|
|
"""Test save."""
|
|
cfg = Config(steam_api_key="abc", steam_id="123")
|
|
config_dir = tmp_path / "cfg"
|
|
config_file = config_dir / "config.json"
|
|
with (
|
|
patch("steam_backlog_enforcer.config.CONFIG_DIR", config_dir),
|
|
patch("steam_backlog_enforcer.config.CONFIG_FILE", config_file),
|
|
):
|
|
cfg.save()
|
|
data = json.loads(config_file.read_text(encoding="utf-8"))
|
|
assert data["steam_api_key"] == "abc"
|
|
assert data["steam_id"] == "123"
|
|
|
|
def test_load_existing(self, tmp_path: Path) -> None:
|
|
"""Test load existing."""
|
|
config_file = tmp_path / "config.json"
|
|
config_file.write_text(
|
|
json.dumps({"steam_api_key": "key1", "steam_id": "id1"}) + "\n",
|
|
encoding="utf-8",
|
|
)
|
|
with patch("steam_backlog_enforcer.config.CONFIG_FILE", config_file):
|
|
cfg = Config.load()
|
|
assert cfg.steam_api_key == "key1"
|
|
assert cfg.steam_id == "id1"
|
|
|
|
def test_load_missing(self, tmp_path: Path) -> None:
|
|
"""Test load missing."""
|
|
config_file = tmp_path / "nonexistent.json"
|
|
with patch("steam_backlog_enforcer.config.CONFIG_FILE", config_file):
|
|
cfg = Config.load()
|
|
assert cfg.steam_api_key == ""
|
|
|
|
def test_load_extra_fields_ignored(self, tmp_path: Path) -> None:
|
|
"""Test load extra fields ignored."""
|
|
config_file = tmp_path / "config.json"
|
|
config_file.write_text(
|
|
json.dumps({"steam_api_key": "k", "unknown_field": 42}) + "\n",
|
|
encoding="utf-8",
|
|
)
|
|
with patch("steam_backlog_enforcer.config.CONFIG_FILE", config_file):
|
|
cfg = Config.load()
|
|
assert cfg.steam_api_key == "k"
|
|
|
|
def test_load_drops_the_retired_budget_fields(self, tmp_path: Path) -> None:
|
|
"""A config.json written before earned_time still loads.
|
|
|
|
The budget numbers moved to the registry; a live config that still
|
|
carries them must not crash the daemon, and must not set anything.
|
|
"""
|
|
retired = {
|
|
"base_gaming_seconds": 1,
|
|
"workout_bonus_seconds": 2,
|
|
"leetcode_bonus_seconds": 3,
|
|
"reading_bonus_seconds": 4,
|
|
"max_gaming_seconds": 5,
|
|
}
|
|
config_file = tmp_path / "config.json"
|
|
config_file.write_text(
|
|
json.dumps({"steam_api_key": "k", **retired}), encoding="utf-8"
|
|
)
|
|
with patch("steam_backlog_enforcer.config.CONFIG_FILE", config_file):
|
|
cfg = Config.load()
|
|
assert cfg.steam_api_key == "k"
|
|
assert not any(hasattr(cfg, name) for name in retired)
|
|
|
|
|
|
class TestState:
|
|
"""Tests for State dataclass."""
|
|
|
|
def test_defaults(self) -> None:
|
|
"""Test defaults."""
|
|
state = State()
|
|
assert state.current_app_id is None
|
|
assert state.current_game_name == ""
|
|
assert state.finished_app_ids == []
|
|
|
|
def test_save(self, tmp_path: Path) -> None:
|
|
"""Test save."""
|
|
state = State(current_app_id=100, current_game_name="TestGame")
|
|
config_dir = tmp_path / "cfg"
|
|
state_file = config_dir / "state.json"
|
|
with (
|
|
patch("steam_backlog_enforcer.config.CONFIG_DIR", config_dir),
|
|
patch("steam_backlog_enforcer.config.STATE_FILE", state_file),
|
|
):
|
|
state.save()
|
|
data = json.loads(state_file.read_text(encoding="utf-8"))
|
|
assert data["current_app_id"] == 100
|
|
assert data["current_game_name"] == "TestGame"
|
|
|
|
def test_load_existing(self, tmp_path: Path) -> None:
|
|
"""Test load existing."""
|
|
state_file = tmp_path / "state.json"
|
|
state_file.write_text(
|
|
json.dumps(
|
|
{
|
|
"current_app_id": 50,
|
|
"current_game_name": "G",
|
|
"finished_app_ids": [1, 2],
|
|
}
|
|
)
|
|
+ "\n",
|
|
encoding="utf-8",
|
|
)
|
|
with patch("steam_backlog_enforcer.config.STATE_FILE", state_file):
|
|
st = State.load()
|
|
assert st.current_app_id == 50
|
|
assert st.finished_app_ids == [1, 2]
|
|
|
|
def test_load_missing(self, tmp_path: Path) -> None:
|
|
"""Test load missing."""
|
|
state_file = tmp_path / "nonexistent.json"
|
|
with patch("steam_backlog_enforcer.config.STATE_FILE", state_file):
|
|
st = State.load()
|
|
assert st.current_app_id is None
|
|
|
|
def test_load_corrupt(self, tmp_path: Path) -> None:
|
|
"""Test load corrupt."""
|
|
state_file = tmp_path / "state.json"
|
|
state_file.write_text("not valid json{{", encoding="utf-8")
|
|
with patch("steam_backlog_enforcer.config.STATE_FILE", state_file):
|
|
st = State.load()
|
|
assert st.current_app_id is None
|
|
assert st.current_game_name == ""
|
|
|
|
def test_skip_for_days_records_iso_timestamp(self) -> None:
|
|
"""Test skip for days records iso timestamp."""
|
|
state = State()
|
|
state.skip_for_days(42, 7)
|
|
assert "42" in state.skipped_until
|
|
# Round-trip parse and check ~7 days in the future.
|
|
from datetime import datetime
|
|
|
|
expiry = datetime.fromisoformat(state.skipped_until["42"])
|
|
delta = (expiry - datetime.now(UTC)).total_seconds()
|
|
assert 6 * 86400 < delta <= 7 * 86400 + 1
|
|
|
|
def test_active_skipped_ids_returns_active(self) -> None:
|
|
"""Test active skipped ids returns active."""
|
|
from datetime import datetime, timedelta
|
|
|
|
state = State()
|
|
future = datetime.now(UTC) + timedelta(days=3)
|
|
state.skipped_until["100"] = future.isoformat()
|
|
assert state.active_skipped_ids() == {100}
|
|
# Active entry retained.
|
|
assert "100" in state.skipped_until
|
|
|
|
def test_active_skipped_ids_prunes_expired(self) -> None:
|
|
"""Test active skipped ids prunes expired."""
|
|
from datetime import datetime, timedelta
|
|
|
|
state = State()
|
|
past = datetime.now(UTC) - timedelta(days=1)
|
|
state.skipped_until["50"] = past.isoformat()
|
|
assert state.active_skipped_ids() == set()
|
|
assert "50" not in state.skipped_until
|
|
|
|
def test_active_skipped_ids_prunes_malformed(self) -> None:
|
|
"""Test active skipped ids prunes malformed."""
|
|
state = State()
|
|
state.skipped_until["77"] = "not-a-date"
|
|
assert state.active_skipped_ids() == set()
|
|
assert "77" not in state.skipped_until
|
|
|
|
|
|
class TestSnapshot:
|
|
"""Tests for snapshot save/load."""
|
|
|
|
def test_save_and_load(self, tmp_path: Path) -> None:
|
|
"""Test save and load."""
|
|
config_dir = tmp_path / "cfg"
|
|
snap_file = config_dir / "snapshot.json"
|
|
with (
|
|
patch("steam_backlog_enforcer.config.CONFIG_DIR", config_dir),
|
|
patch("steam_backlog_enforcer.config.SNAPSHOT_FILE", snap_file),
|
|
):
|
|
data: list[dict[str, Any]] = [{"app_id": 1, "name": "G1"}]
|
|
save_snapshot(data)
|
|
loaded = load_snapshot()
|
|
assert loaded == data
|
|
|
|
def test_load_none(self, tmp_path: Path) -> None:
|
|
"""Test load none."""
|
|
snap_file = tmp_path / "nonexistent.json"
|
|
with patch("steam_backlog_enforcer.config.SNAPSHOT_FILE", snap_file):
|
|
assert load_snapshot() is None
|