from __future__ import annotations import socket from types import SimpleNamespace import pytest from tools import start_n7web def test_tcp_port_probe_detects_an_existing_listener() -> None: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as listener: listener.bind(("127.0.0.1", 0)) listener.listen(1) assert start_n7web._tcp_port_is_listening( # noqa: SLF001 "0.0.0.0", listener.getsockname()[1], ) def test_startup_rejects_a_duplicate_service_port(monkeypatch: pytest.MonkeyPatch) -> None: args = SimpleNamespace( host="127.0.0.1", port=8000, scene_bind="0.0.0.0", scene_port=8090, ) monkeypatch.setattr( start_n7web, "_tcp_port_is_listening", lambda _host, port: port == 8000, ) with pytest.raises(RuntimeError, match="stop the previous N7Web instance"): start_n7web._ensure_service_ports_available(args) # noqa: SLF001