# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence. """The console entrypoint. `python main.py` used to do literally nothing: there was no __main__ block and no uvicorn.run() call anywhere in the project. """ from __future__ import annotations import runpy import pytest import uvicorn import signal_bot.__main__ as entrypoint @pytest.fixture def captured(monkeypatch: pytest.MonkeyPatch) -> dict[str, object]: seen: dict[str, object] = {} def fake_run(target: str, **kwargs: object) -> None: seen["target"] = target seen.update(kwargs) monkeypatch.setattr(entrypoint.uvicorn, "run", fake_run) return seen def test_runs_the_asgi_app_on_loopback_by_default( captured: dict[str, object], monkeypatch: pytest.MonkeyPatch ) -> None: monkeypatch.delenv("BIND_HOST", raising=False) monkeypatch.delenv("BIND_PORT", raising=False) entrypoint.main() assert captured["target"] == "signal_bot.app:app" assert captured["host"] == "127.0.0.1" assert captured["port"] == 8000 def test_bind_overrides_are_honoured( captured: dict[str, object], monkeypatch: pytest.MonkeyPatch ) -> None: # TEST-NET-1: proves the override without a bind-all literal. monkeypatch.setenv("BIND_HOST", "192.0.2.10") monkeypatch.setenv("BIND_PORT", "9001") entrypoint.main() assert captured["host"] == "192.0.2.10" assert captured["port"] == 9001 def test_module_runs_as_a_script(monkeypatch: pytest.MonkeyPatch) -> None: """`python -m signal_bot` must actually start the server.""" seen: dict[str, object] = {} monkeypatch.setattr(uvicorn, "run", lambda target, **kw: seen.update(target=target)) monkeypatch.delenv("BIND_HOST", raising=False) monkeypatch.delenv("BIND_PORT", raising=False) runpy.run_module("signal_bot", run_name="__main__", alter_sys=True) assert seen["target"] == "signal_bot.app:app"