diff --git a/steam_backlog_enforcer/_ctl_ops.py b/steam_backlog_enforcer/_ctl_ops.py index 0fc43e9..c312f8f 100644 --- a/steam_backlog_enforcer/_ctl_ops.py +++ b/steam_backlog_enforcer/_ctl_ops.py @@ -28,7 +28,7 @@ from steam_backlog_enforcer._ctl_reset import RESET_OPS from steam_backlog_enforcer._ctl_restart import RESTART_OPS, restart_available_at from steam_backlog_enforcer._fast_install_progress import fast_install_status from steam_backlog_enforcer._playtime_block import release_block -from steam_backlog_enforcer._store_verify import StoreVerifyError +from steam_backlog_enforcer._store_check_report import StoreVerifyError from steam_backlog_enforcer._store_window import ( MAX_WINDOW_MINUTES, open_store_window, diff --git a/steam_backlog_enforcer/_store_check_report.py b/steam_backlog_enforcer/_store_check_report.py new file mode 100644 index 0000000..037c5d5 --- /dev/null +++ b/steam_backlog_enforcer/_store_check_report.py @@ -0,0 +1,90 @@ +"""The result of a store unblock / re-block check, and its failure report. + +Kept apart from the checks themselves (``_store_verify``) so the report a +todo task is filed from has one plain home: which checks ran, which failed, +and the evidence gathered for a failure. +""" + +from __future__ import annotations + +from dataclasses import dataclass, field +from typing import Final + +UNBLOCK: Final = "unblock" +REBLOCK: Final = "re-block" +NETWORK_DOWN: Final = "network down" + + +@dataclass(frozen=True) +class Check: + """One check's outcome: its name, whether it held, and what was seen.""" + + name: str + ok: bool + detail: str + + +@dataclass +class StoreCheckReport: + """Every check of one action, plus the evidence a failure report needs.""" + + action: str + at: str + checks: list[Check] + network_down: bool = False + repaired: bool = False + evidence: dict[str, str] = field(default_factory=dict) + + @property + def ok(self) -> bool: + """Whether every check held.""" + return not self.network_down and all(c.ok for c in self.checks) + + @property + def failed_check(self) -> str | None: + """The failure: "network down", the first failed check, or None.""" + if self.network_down: + return NETWORK_DOWN + return next((c.name for c in self.checks if not c.ok), None) + + @property + def title(self) -> str: + """The todo task title for this failure.""" + return f"Store {self.action} verification failed: {self.failed_check}" + + def body(self) -> str: + """The failure report as markdown, for the todo task.""" + rows = [ + f"- {c.name}: {'ok' if c.ok else 'FAILED'} ({c.detail})" + for c in self.checks + ] + parts = [ + f"Time: {self.at}", + f"Action: {self.action}", + f"Repair pass run: {'yes' if self.repaired else 'no'}", + f"Result: {self.failed_check or 'ok'}", + "", + "## Checks", + *rows, + ] + for name, text in self.evidence.items(): + parts += ["", f"## {name}", "```", text.rstrip() or "(empty)", "```"] + return "\n".join(parts) + "\n" + + def to_wire(self) -> dict[str, object]: + """What the daemon hands the web server for filing.""" + return { + "action": self.action, + "check": self.failed_check, + "title": self.title, + "body": self.body(), + } + + +class StoreVerifyError(RuntimeError): + """A store action ran but its checks failed, even after the repair pass.""" + + def __init__(self, report: StoreCheckReport) -> None: + """Keep *report* for the caller to show and file.""" + super().__init__(f"Store {report.action} failed: {report.failed_check}") + self.report = report diff --git a/steam_backlog_enforcer/_store_verify.py b/steam_backlog_enforcer/_store_verify.py index 4659e13..b64dcaf 100644 --- a/steam_backlog_enforcer/_store_verify.py +++ b/steam_backlog_enforcer/_store_verify.py @@ -19,7 +19,6 @@ Re-block checks (the reverse): from __future__ import annotations -from dataclasses import dataclass, field from datetime import datetime import shutil import subprocess @@ -31,15 +30,18 @@ from steam_backlog_enforcer._hosts_lines import ( live_store_lines, store_lines, ) +from steam_backlog_enforcer._store_check_report import ( + REBLOCK, + UNBLOCK, + Check, + StoreCheckReport, +) from steam_backlog_enforcer._store_tools import IPTABLES, IPTABLES_CHAIN, SUDO from steam_backlog_enforcer.config import BLOCKED_DOMAINS, HOSTS_FILE if TYPE_CHECKING: from collections.abc import Callable -UNBLOCK: Final = "unblock" -REBLOCK: Final = "re-block" -NETWORK_DOWN: Final = "network down" STORE_URL: Final = "https://store.steampowered.com/" CONTROL_URL: Final = "https://steamcommunity.com/" @@ -60,81 +62,6 @@ SETTLE_ATTEMPTS: Final = 3 _SETTLE_GAP_SECONDS: Final = 2.0 -@dataclass(frozen=True) -class Check: - """One check's outcome: its name, whether it held, and what was seen.""" - - name: str - ok: bool - detail: str - - -@dataclass -class StoreCheckReport: - """Every check of one action, plus the evidence a failure report needs.""" - - action: str - at: str - checks: list[Check] - network_down: bool = False - repaired: bool = False - evidence: dict[str, str] = field(default_factory=dict) - - @property - def ok(self) -> bool: - """Whether every check held.""" - return not self.network_down and all(c.ok for c in self.checks) - - @property - def failed_check(self) -> str | None: - """The failure: "network down", the first failed check, or None.""" - if self.network_down: - return NETWORK_DOWN - return next((c.name for c in self.checks if not c.ok), None) - - @property - def title(self) -> str: - """The todo task title for this failure.""" - return f"Store {self.action} verification failed: {self.failed_check}" - - def body(self) -> str: - """The failure report as markdown, for the todo task.""" - rows = [ - f"- {c.name}: {'ok' if c.ok else 'FAILED'} ({c.detail})" - for c in self.checks - ] - parts = [ - f"Time: {self.at}", - f"Action: {self.action}", - f"Repair pass run: {'yes' if self.repaired else 'no'}", - f"Result: {self.failed_check or 'ok'}", - "", - "## Checks", - *rows, - ] - for name, text in self.evidence.items(): - parts += ["", f"## {name}", "```", text.rstrip() or "(empty)", "```"] - return "\n".join(parts) + "\n" - - def to_wire(self) -> dict[str, object]: - """What the daemon hands the web server for filing.""" - return { - "action": self.action, - "check": self.failed_check, - "title": self.title, - "body": self.body(), - } - - -class StoreVerifyError(RuntimeError): - """A store action ran but its checks failed, even after the repair pass.""" - - def __init__(self, report: StoreCheckReport) -> None: - """Keep *report* for the caller to show and file.""" - super().__init__(f"Store {report.action} failed: {report.failed_check}") - self.report = report - - def _run(cmd: list[str], timeout: float) -> tuple[int, str]: """Run *cmd*; its exit code and combined output (``-1`` if it never ran).""" try: diff --git a/steam_backlog_enforcer/_store_window.py b/steam_backlog_enforcer/_store_window.py index 0e383a5..518ed38 100644 --- a/steam_backlog_enforcer/_store_window.py +++ b/steam_backlog_enforcer/_store_window.py @@ -20,8 +20,8 @@ import logging import threading from typing import TYPE_CHECKING +from steam_backlog_enforcer._store_check_report import StoreVerifyError from steam_backlog_enforcer._store_verify import ( - StoreVerifyError, check_reblock, check_unblock, verify, diff --git a/steam_backlog_enforcer/main/misc.py b/steam_backlog_enforcer/main/misc.py index b2bd9bd..2e47f88 100644 --- a/steam_backlog_enforcer/main/misc.py +++ b/steam_backlog_enforcer/main/misc.py @@ -18,7 +18,7 @@ from steam_backlog_enforcer._serve_startup import ( ensure_port_available, parse_serve_args, ) -from steam_backlog_enforcer._store_verify import StoreVerifyError +from steam_backlog_enforcer._store_check_report import StoreVerifyError from steam_backlog_enforcer._store_window import ( DEFAULT_WINDOW_MINUTES, MAX_WINDOW_MINUTES, diff --git a/steam_backlog_enforcer/tests/test_ctl_store_verify.py b/steam_backlog_enforcer/tests/test_ctl_store_verify.py index 09bd24d..700a49c 100644 --- a/steam_backlog_enforcer/tests/test_ctl_store_verify.py +++ b/steam_backlog_enforcer/tests/test_ctl_store_verify.py @@ -9,7 +9,7 @@ import pytest from steam_backlog_enforcer import _ctl_ops from steam_backlog_enforcer._ctl_protocol import CtlError -from steam_backlog_enforcer._store_verify import ( +from steam_backlog_enforcer._store_check_report import ( Check, StoreCheckReport, StoreVerifyError, diff --git a/steam_backlog_enforcer/tests/test_store_verify.py b/steam_backlog_enforcer/tests/test_store_verify.py index 6d1745e..e7c7160 100644 --- a/steam_backlog_enforcer/tests/test_store_verify.py +++ b/steam_backlog_enforcer/tests/test_store_verify.py @@ -8,6 +8,7 @@ from unittest.mock import MagicMock, patch import pytest from steam_backlog_enforcer import _store_verify as sv +from steam_backlog_enforcer._store_check_report import NETWORK_DOWN, StoreVerifyError from steam_backlog_enforcer._store_verify import _run as run_command from steam_backlog_enforcer.config import BLOCKED_DOMAINS @@ -114,7 +115,7 @@ def test_unblock_both_unreachable_is_network_down(host: FakeHost) -> None: host.reachable = dict.fromkeys(host.reachable, False) report = sv.check_unblock() assert not report.ok - assert report.failed_check == sv.NETWORK_DOWN + assert report.failed_check == NETWORK_DOWN assert report.title == "Store unblock verification failed: network down" @@ -201,7 +202,7 @@ def test_run_survives_missing_binary() -> None: def test_error_carries_report(host: FakeHost) -> None: """StoreVerifyError keeps the report and names the check.""" host.chain = True - err = sv.StoreVerifyError(sv.check_unblock()) + err = StoreVerifyError(sv.check_unblock()) assert str(err) == "Store unblock failed: iptables" assert err.report.failed_check == "iptables" diff --git a/steam_backlog_enforcer/tests/test_store_window.py b/steam_backlog_enforcer/tests/test_store_window.py index db907c2..e98261c 100644 --- a/steam_backlog_enforcer/tests/test_store_window.py +++ b/steam_backlog_enforcer/tests/test_store_window.py @@ -9,7 +9,7 @@ from unittest.mock import MagicMock, patch import pytest from steam_backlog_enforcer import _store_window -from steam_backlog_enforcer._store_verify import ( +from steam_backlog_enforcer._store_check_report import ( Check, StoreCheckReport, StoreVerifyError, diff --git a/steam_backlog_enforcer/tests/test_web_store_report.py b/steam_backlog_enforcer/tests/test_web_store_report.py index 0aa3c02..3877ac0 100644 --- a/steam_backlog_enforcer/tests/test_web_store_report.py +++ b/steam_backlog_enforcer/tests/test_web_store_report.py @@ -9,7 +9,7 @@ import pytest from steam_backlog_enforcer import _web_privileged, _web_store_report from steam_backlog_enforcer._ctl_protocol import CtlError, DaemonUnreachableError -from steam_backlog_enforcer._store_verify import ( +from steam_backlog_enforcer._store_check_report import ( Check, StoreCheckReport, StoreVerifyError,