mirror of
https://github.com/kuhyx/steam-backlog-enforcer.git
synced 2026-11-21 03:52:26 +01:00
The budget tab answered "how much did I play" but not "what did I play":
playtime_state.json and playtime_history.json each stored one aggregate
seconds figure per gaming day, so the 14-day chart was a row of featureless
bars. Separately, qualifying_pids had exactly two sources — the SteamAppId
env var and a hardcoded 16-launcher frozenset — so osu!lazer, which sets no
SteamAppId and matches no launcher name, was never billed at all.
qualifying_pids now returns pid -> attribution key instead of a bare pid set.
The app id was already in hand and thrown away on its first line; it now
survives into PlaytimeState.per_game, alongside a user-editable
counted_processes list in config.json seeded with osu!lazer.
Two things the parts/whole relationship forces:
- per_game only ever sums to AT MOST seconds, never equals it. backdate
refunds the idle grace on every engaged-to-paused edge, and
_state_or_recover synthesises a full budget with no breakdown. The
refund is debited from last_credited_key, and the remainder is rendered
as "Unattributed" rather than stored — one path then covers legacy days,
focus-probe failures and drift alike.
- counted_processes is kept apart from LAUNCHER_PROCESS_NAMES. That
frozenset is also the total-block kill list, and it routes through
kill_and_uninstall_launchers, which pacman -R's the owning package.
Counted entries are killed on total block, never uninstalled.
Both storage formats stay at schema 1: load_state already filters to
dataclass fields, so a record without per_game migrates for free, and the
history reader has to accept both a bare float and an object regardless, so
a version bump would distinguish nothing.
Chart segments use the shared design system's categorical ramp (--cat-1..6),
capped at six games plus "Other" because a seventh distinguishable hue does
not exist under simulated CVD. The red over-budget fill is gone: colour now
encodes which game, and the dashed budget line alone says over or under.
Snapshot persistence moves to _snapshot.py to keep config.py under the
250-line cap. Three modules bound config paths at import time, which
bypasses the conftest redirect — they now reach them through the module.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MKFzwzwRU1JD7SFLHtQBsz
243 lines
8.8 KiB
Python
243 lines
8.8 KiB
Python
"""Tests for the per-tick engagement verdict and the idle backdate.
|
|
|
|
The backdate is deliberately hard to trigger: it is the only place the budget
|
|
counter ever moves backwards, so every guard around it is tested here.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import replace
|
|
from pathlib import Path
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from steam_backlog_enforcer._engagement_types import (
|
|
CAUSE_FOCUS,
|
|
CAUSE_IDLE,
|
|
STATE_ENGAGED,
|
|
STATE_NOT_APPLICABLE,
|
|
STATE_PAUSED,
|
|
CauseTally,
|
|
EngagementVerdict,
|
|
)
|
|
from steam_backlog_enforcer._playtime_engagement import EngagementTracker
|
|
from steam_backlog_enforcer._playtime_state import PlaytimeRules, PlaytimeState
|
|
|
|
PE = "steam_backlog_enforcer._playtime_engagement"
|
|
|
|
RULES = PlaytimeRules(
|
|
budget_seconds=28800.0,
|
|
warn_at=(3600,),
|
|
sigkill_after=30.0,
|
|
count_launchers=True,
|
|
enforcement=True,
|
|
demo=False,
|
|
engagement_gate=True,
|
|
idle_grace_seconds=300.0,
|
|
require_game_focus=True,
|
|
)
|
|
|
|
|
|
def _tracker(**kwargs: object) -> EngagementTracker:
|
|
return EngagementTracker(
|
|
uid=1000,
|
|
probe=MagicMock(name="XProbe"),
|
|
controller=MagicMock(name="ControllerActivity"),
|
|
holder_path=Path("/nonexistent/holder.lock"),
|
|
**kwargs,
|
|
)
|
|
|
|
|
|
def _engaged() -> EngagementVerdict:
|
|
return EngagementVerdict(state=STATE_ENGAGED)
|
|
|
|
|
|
def _idle_paused() -> EngagementVerdict:
|
|
return EngagementVerdict(state=STATE_PAUSED, causes=(CAUSE_IDLE,))
|
|
|
|
|
|
class TestAssess:
|
|
def test_nothing_qualifying_is_not_applicable(self) -> None:
|
|
verdict = _tracker().assess(RULES, qualifying=set(), now_monotonic=1.0)
|
|
assert verdict.state == STATE_NOT_APPLICABLE
|
|
assert verdict.engaged is True
|
|
|
|
def test_a_disabled_gate_bills_without_probing(self) -> None:
|
|
tracker = _tracker()
|
|
with patch(f"{PE}.assess_screen") as screen:
|
|
verdict = tracker.assess(
|
|
replace(RULES, engagement_gate=False),
|
|
qualifying={7},
|
|
now_monotonic=1.0,
|
|
)
|
|
assert verdict.state == STATE_ENGAGED
|
|
assert verdict.qualifying == (7,)
|
|
screen.assert_not_called()
|
|
|
|
def test_every_cause_is_recorded_not_just_the_first(self) -> None:
|
|
# The log has to be able to answer "was the screen ALSO locked?", and
|
|
# the backdate must be able to tell an idle-only pause from a compound.
|
|
tracker = _tracker()
|
|
with (
|
|
patch(f"{PE}.assess_screen", side_effect=_add("screen_held", (True, 5))),
|
|
patch(f"{PE}.assess_focus", side_effect=_add_focus("focus", 9)),
|
|
patch(f"{PE}.assess_idle", side_effect=_add_idle("idle", (900.0, None))),
|
|
):
|
|
verdict = tracker.assess(RULES, qualifying={7}, now_monotonic=1.0)
|
|
assert verdict.state == STATE_PAUSED
|
|
assert set(verdict.causes) == {"screen_held", "focus", "idle"}
|
|
assert verdict.holder_pid == 5
|
|
assert verdict.focus_pid == 9
|
|
|
|
def test_no_causes_means_engaged(self) -> None:
|
|
tracker = _tracker()
|
|
with (
|
|
patch(f"{PE}.assess_screen", return_value=(False, None)),
|
|
patch(f"{PE}.assess_focus", return_value=7),
|
|
patch(f"{PE}.assess_idle", return_value=(1.0, None)),
|
|
):
|
|
verdict = tracker.assess(RULES, qualifying={7}, now_monotonic=1.0)
|
|
assert verdict.state == STATE_ENGAGED
|
|
|
|
|
|
class TestClose:
|
|
def test_releases_both_resources(self) -> None:
|
|
probe = MagicMock()
|
|
controller = MagicMock()
|
|
tracker = EngagementTracker(
|
|
uid=1000, probe=probe, controller=controller, holder_path=Path("x")
|
|
)
|
|
tracker.close()
|
|
probe.close.assert_called_once()
|
|
controller.close.assert_called_once()
|
|
|
|
|
|
class TestBackdate:
|
|
def _state(self, seconds: float = 1000.0) -> PlaytimeState:
|
|
return PlaytimeState(day_key="2026-08-28", seconds=seconds)
|
|
|
|
def test_refunds_the_grace_on_the_engaged_to_idle_edge(self) -> None:
|
|
tracker = _tracker()
|
|
state = tracker.backdate(self._state(), _engaged(), rules=RULES)
|
|
state = tracker.backdate(state, _idle_paused(), rules=RULES)
|
|
assert state.seconds == 700.0
|
|
|
|
def test_refunds_only_once_per_walk_away(self) -> None:
|
|
tracker = _tracker()
|
|
state = tracker.backdate(self._state(), _engaged(), rules=RULES)
|
|
state = tracker.backdate(state, _idle_paused(), rules=RULES)
|
|
state = tracker.backdate(state, _idle_paused(), rules=RULES)
|
|
assert state.seconds == 700.0
|
|
|
|
def test_debits_the_refund_from_the_credited_game(self) -> None:
|
|
"""Otherwise the parts drift above the whole on every walk-away.
|
|
|
|
This edge fires many times a day, so leaving `per_game` untouched would
|
|
make stacked segments overflow their bar within minutes.
|
|
"""
|
|
tracker = _tracker()
|
|
state = PlaytimeState(
|
|
day_key="2026-08-28",
|
|
seconds=1000.0,
|
|
per_game={"app:440": 1000.0},
|
|
last_credited_key="app:440",
|
|
)
|
|
state = tracker.backdate(state, _engaged(), rules=RULES)
|
|
state = tracker.backdate(state, _idle_paused(), rules=RULES)
|
|
assert state.seconds == 700.0
|
|
assert state.per_game == {"app:440": 700.0}
|
|
assert sum(state.per_game.values()) <= state.seconds
|
|
|
|
def test_a_refund_larger_than_the_key_holds_floors_at_zero(self) -> None:
|
|
tracker = _tracker()
|
|
state = PlaytimeState(
|
|
day_key="2026-08-28",
|
|
seconds=1000.0,
|
|
per_game={"app:440": 100.0},
|
|
last_credited_key="app:440",
|
|
)
|
|
state = tracker.backdate(state, _engaged(), rules=RULES)
|
|
state = tracker.backdate(state, _idle_paused(), rules=RULES)
|
|
assert state.per_game == {"app:440": 0.0}
|
|
|
|
def test_an_unattributed_refund_leaves_the_breakdown_alone(self) -> None:
|
|
"""Nothing was credited, so there is nothing to take back."""
|
|
tracker = _tracker()
|
|
state = PlaytimeState(
|
|
day_key="2026-08-28", seconds=1000.0, per_game={"app:440": 100.0}
|
|
)
|
|
state = tracker.backdate(state, _engaged(), rules=RULES)
|
|
state = tracker.backdate(state, _idle_paused(), rules=RULES)
|
|
assert state.per_game == {"app:440": 100.0}
|
|
|
|
def test_never_refunds_below_zero(self) -> None:
|
|
tracker = _tracker()
|
|
state = tracker.backdate(self._state(seconds=10.0), _engaged(), rules=RULES)
|
|
state = tracker.backdate(state, _idle_paused(), rules=RULES)
|
|
assert state.seconds == 0.0
|
|
|
|
def test_no_refund_on_the_first_tick_after_a_restart(self) -> None:
|
|
# With no previous verdict there is no edge to recognise, and inventing
|
|
# one would hand out a free refund on every daemon restart.
|
|
tracker = _tracker()
|
|
state = tracker.backdate(self._state(), _idle_paused(), rules=RULES)
|
|
assert state.seconds == 1000.0
|
|
|
|
def test_no_refund_across_a_day_rollover(self) -> None:
|
|
tracker = _tracker()
|
|
tracker.backdate(self._state(), _engaged(), rules=RULES)
|
|
tomorrow = PlaytimeState(day_key="2026-08-29", seconds=1000.0)
|
|
assert tracker.backdate(tomorrow, _idle_paused(), rules=RULES).seconds == 1000.0
|
|
|
|
def test_no_refund_when_something_else_also_paused_the_tick(self) -> None:
|
|
tracker = _tracker()
|
|
state = tracker.backdate(self._state(), _engaged(), rules=RULES)
|
|
compound = EngagementVerdict(
|
|
state=STATE_PAUSED, causes=(CAUSE_IDLE, CAUSE_FOCUS)
|
|
)
|
|
assert tracker.backdate(state, compound, rules=RULES).seconds == 1000.0
|
|
|
|
def test_no_refund_while_still_engaged(self) -> None:
|
|
tracker = _tracker()
|
|
state = tracker.backdate(self._state(), _engaged(), rules=RULES)
|
|
assert tracker.backdate(state, _engaged(), rules=RULES).seconds == 1000.0
|
|
|
|
def test_reports_the_measured_idle_time(self) -> None:
|
|
tracker = _tracker()
|
|
tracker.backdate(self._state(), _engaged(), rules=RULES)
|
|
verdict = EngagementVerdict(
|
|
state=STATE_PAUSED, causes=(CAUSE_IDLE,), idle_seconds=301.0
|
|
)
|
|
assert tracker.backdate(self._state(), verdict, rules=RULES).seconds == 700.0
|
|
|
|
|
|
def _add(cause: str, result: tuple[bool | None, int | None]) -> object:
|
|
def impl(_path: object, tally: CauseTally) -> object:
|
|
tally.causes.append(cause)
|
|
return result
|
|
|
|
return impl
|
|
|
|
|
|
def _add_focus(cause: str, pid: int) -> object:
|
|
def impl(
|
|
_probe: object, _rules: object, _qualifying: object, tally: CauseTally
|
|
) -> int:
|
|
tally.causes.append(cause)
|
|
return pid
|
|
|
|
return impl
|
|
|
|
|
|
def _add_idle(cause: str, result: tuple[float | None, float | None]) -> object:
|
|
def impl(
|
|
_probe: object,
|
|
_controller: object,
|
|
_rules: object,
|
|
tally: CauseTally,
|
|
_now: float,
|
|
) -> object:
|
|
tally.causes.append(cause)
|
|
return result
|
|
|
|
return impl
|