steam-backlog-enforcer/steam_backlog_enforcer/tests/_fake_session.py
Krzysztof kuhy Rudnicki e82ddd9a4b playtime: shadow leisure billing (C4); /proc scan from leisure_watch
The enforcer now imports the Steam/launcher/counted-process scan from
leisure_watch.procscan (v0.1.0, shared with the leisure watcher) and drops
its own copies (_playtime_procs, _counted_procs, _total_block_launchers and
enforcer's two scanners). Every counted-process read passes
config.CONFIG_FILE explicitly, as before.

Each tick also bills a shadow leisure budget beside the enforced one, from
the same incoming stamp and delta clamp, before accumulate: paused (held
gatelock grab or i3lock) -> shadow_paused_seconds; else a qualifying game,
a "leisure" now.json, or an untrusted/missing now.json (fail closed) bills
shadow_seconds once, split over shadow_per_source. shadow_budget_seconds is
earned_time.resolve(leisure=True) from the same answers, held as a same-day
high-water mark. Not billed under a total block. Shadow verdict changes go
to budget.jsonl as shadow_change (the live game view skips them); /api/budget
and MCP get_gaming_time carry a "shadow" block. Enforced fields, cutoff,
warnings and existing log records are unchanged.

Also fixes two tests that assumed earned_time < 0.14 internals.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dsh4Znwh2KnC7q6vbGpX1N
2026-10-19 12:37:01 +02:00

95 lines
2.6 KiB
Python

"""A :class:`PlaytimeSession` stub for tests that are not about the log.
Most budget tests care only about accounting, warnings and the cutoff, not
about what the audit journal records. They get a session with an in-memory
journal and history so nothing touches the log file or disk.
Deliberately hand-written rather than ``MagicMock``: a mock satisfies every
attribute lookup, including ones a later refactor renamed, so a stub that must
actually match the protocol is the point.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from steam_backlog_enforcer._playtime_session import PlaytimeSession
if TYPE_CHECKING:
from steam_backlog_enforcer._playtime_state import PlaytimeRules, PlaytimeState
from steam_backlog_enforcer._shadow import ShadowVerdict
class StubJournal:
"""A journal that keeps records in memory instead of on disk."""
def __init__(self) -> None:
"""Start with nothing recorded."""
self.observed: list[set[int]] = []
def observe(
self,
qualifying: set[int],
state: PlaytimeState,
*,
rules: PlaytimeRules,
now_monotonic: float,
) -> None:
"""Remember *qualifying*.
Args:
qualifying: This tick's qualifying PIDs.
state: Ignored.
rules: Ignored.
now_monotonic: Ignored.
"""
del state, rules, now_monotonic
self.observed.append(qualifying)
class StubHistory:
"""A per-day history writer that records in memory instead of on disk."""
def __init__(self) -> None:
self.recorded: list[tuple[str, float]] = []
def observe(self, state: PlaytimeState, *, demo: bool) -> None:
"""Remember the day and total.
Args:
state: This tick's state.
demo: Ignored; the real writer skips demo runs.
"""
del demo
self.recorded.append((state.day_key, state.seconds))
class StubShadow:
"""A shadow journal that keeps verdicts in memory instead of on disk."""
def __init__(self) -> None:
self.observed: list[ShadowVerdict] = []
def observe(self, verdict: ShadowVerdict, state: PlaytimeState) -> None:
"""Remember *verdict*.
Args:
verdict: This tick's shadow verdict.
state: Ignored.
"""
del state
self.observed.append(verdict)
def fake_session() -> PlaytimeSession:
"""Build a session that never touches the log file or disk.
Returns:
The session.
"""
return PlaytimeSession(
journal=StubJournal(),
history=StubHistory(),
shadow=StubShadow(),
)