steam-backlog-enforcer/steam_backlog_enforcer/tests/test_cmd_serve.py
Krzysztof kuhy Rudnicki 47f77f5f1e
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 (push) Waiting to run
Tests / test (push) Waiting to run
Make serve re-runnable, and cover it to 100%
The serve rework was sitting untracked: a /proc-based port guard, the
start-or-defer decision, and a stale-bundle rebuild. It had no tests, and it
changed cmd_serve(_config, state) to cmd_serve(argv) without updating the one
test that called it, so the suite was red and coverage was 251 lines short.

Tests, all against fakes:
- _fake_proc builds a synthetic /proc. _NET_TCP is derived from _PROC at
  import time, so patch_proc redirects both; patching _PROC alone would have
  had these tests parse the host's real process table.
- _serve_guard is split across two test files to stay under the 250-line cap.
  os is replaced wholesale in every termination test, so no signal can reach a
  real process even if the guard's self-protection were removed.
- _web_build mocks subprocess entirely and redirects every path constant into
  tmp_path. _no_subprocess now blocks _web_build.subprocess.run too: an
  unguarded test would fire a real `npm run build`, rewriting web/dist and
  perturbing frontend_is_stale() for every later test in the run.

Source changes, both required rather than incidental:
- _serve_guard's wildcard match was a `# noqa: S104` on a "0.0.0.0" literal,
  which the no-noqa hook rejects outright. It is now the all-zero hex column
  /proc actually prints, so the dotted-quad literal never appears.
- main/__init__.py's serve dispatch arm was itself uncovered; it now has a
  test asserting the raw argv passes through unparsed.

Also drops the document.hidden guard from useBudgetPoll: it skipped the very
first load, so the budget panel sat on "Reading budget state…" forever in a
background tab, which is every tab under browser automation.

Verified: 1712 python tests, 100% branch coverage; 128 vitest, 100%. The
guard's /proc parser was checked read-only against a real listener on :8000
and resolved the same pid as `ss`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MKFzwzwRU1JD7SFLHtQBsz
2026-08-29 18:52:11 +02:00

100 lines
3.8 KiB
Python

"""Tests for the main.cmd_serve wiring — 100% branch coverage.
Split from test_web_server.py to stay inside the 250-line file cap: that file
covers the HTTP server itself, this one covers the command that starts it.
"""
from __future__ import annotations
import errno
from unittest.mock import patch
import pytest
from steam_backlog_enforcer import main as main_mod
from steam_backlog_enforcer._web_server import DEFAULT_HOST
from steam_backlog_enforcer.main import misc as misc_mod
_CALLS: list[str] = []
def _record_build() -> bool:
"""Stand in for a successful build, recording when it ran."""
_CALLS.append("build")
return True
def _record_guard(*_args: object) -> None:
"""Stand in for the port guard, recording when it ran."""
_CALLS.append("guard")
class TestCmdServe:
"""Tests for the main.cmd_serve wiring.
Every patch targets the module that *defines* cmd_serve, not the main
package that re-exports it: cmd_serve resolves these names from its own
globals, so patching the re-export would not bite.
"""
def test_invokes_serve_with_parsed_args(self) -> None:
with (
patch.object(misc_mod, "frontend_is_stale", return_value=False),
patch.object(misc_mod, "ensure_port_available") as guard,
patch.object(misc_mod, "serve") as mock_serve,
):
main_mod.cmd_serve(["--port", "8123"])
guard.assert_called_once_with(DEFAULT_HOST, 8123)
mock_serve.assert_called_once_with(DEFAULT_HOST, 8123)
def test_stale_frontend_is_rebuilt_before_binding(self) -> None:
# Order matters: a failed build must not cost the caller the server
# that was already running, so the build happens before the port check.
_CALLS.clear()
with (
patch.object(misc_mod, "frontend_is_stale", return_value=True),
patch.object(misc_mod, "build_frontend", side_effect=_record_build),
patch.object(misc_mod, "ensure_port_available", side_effect=_record_guard),
patch.object(misc_mod, "serve"),
):
main_mod.cmd_serve([])
assert _CALLS == ["build", "guard"]
def test_failed_build_exits_1(self) -> None:
with (
patch.object(misc_mod, "frontend_is_stale", return_value=True),
patch.object(misc_mod, "build_frontend", return_value=False),
patch.object(misc_mod, "ensure_port_available") as guard,
patch.object(misc_mod, "serve") as mock_serve,
pytest.raises(SystemExit) as exc,
):
main_mod.cmd_serve([])
assert exc.value.code == 1
guard.assert_not_called()
mock_serve.assert_not_called()
def test_port_stolen_after_the_check_exits_1(self) -> None:
# Covers the race between the /proc scan and the actual bind.
taken = OSError(errno.EADDRINUSE, "Address already in use")
with (
patch.object(misc_mod, "frontend_is_stale", return_value=False),
patch.object(misc_mod, "ensure_port_available"),
patch.object(misc_mod, "serve", side_effect=taken),
patch.object(misc_mod, "_echo") as echo,
pytest.raises(SystemExit) as exc,
):
main_mod.cmd_serve([])
assert exc.value.code == 1
assert "was taken while starting up" in echo.call_args[0][0]
def test_unrelated_oserror_propagates(self) -> None:
# Swallowing this would turn a real fault into a bare "try again".
denied = OSError(errno.EACCES, "Permission denied")
with (
patch.object(misc_mod, "frontend_is_stale", return_value=False),
patch.object(misc_mod, "ensure_port_available"),
patch.object(misc_mod, "serve", side_effect=denied),
pytest.raises(OSError, match="Permission denied"),
):
main_mod.cmd_serve([])