import asyncio
import builtins
import importlib
import pytest
# Import the repo top-level entrypoint module `main.py`.
# Try direct import first, then try package import, and finally load the module
# directly from the local `main.py` file so tests run under `uv` and dev installs.
try:
domin8_main = importlib.import_module("main")
except ModuleNotFoundError:
try:
domin8_main = importlib.import_module("domin8.main")
except ModuleNotFoundError:
# As a last resort, load the local `main.py` directly from the repository
from importlib.util import spec_from_file_location, module_from_spec
from pathlib import Path
repo_root = Path(__file__).resolve().parents[2]
spec = spec_from_file_location("domin8_main", str(repo_root / "main.py"))
module = module_from_spec(spec)
spec.loader.exec_module(module)
domin8_main = module
def test_refuses_non_loopback_host():
with pytest.raises(SystemExit):
domin8_main.main(["--tcp-port", "0", "--host", "0.0.0.0"])
@pytest.mark.asyncio
async def test_start_server_calls_asyncio_start_server(monkeypatch):
recorded = {}
async def fake_start_server(handler, host, port):
recorded['host'] = host
recorded['port'] = port
class DummyServer:
def __init__(self):
class FakeSocket:
def getsockname(self):
return (host, port)
self.sockets = [FakeSocket()]
async def serve_forever(self):
# Return immediately so test doesn't block
return
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc, tb):
return False
return DummyServer()
monkeypatch.setattr(domin8_main.asyncio, 'start_server', fake_start_server)
# Call the internal coroutine directly (avoids nesting event loops in tests).
await domin8_main._run_tcp_mode('127.0.0.1', 0)
assert recorded['host'] == '127.0.0.1'
assert recorded['port'] == 0