Files
odysseus/tests/test_static_test_server.py
T
Léo 9b2185f1cc test(harness): bind the static test server to an ephemeral port
The session-scoped autouse fixture bound 127.0.0.1:7011 and raised when the
port was taken. Because it is autouse, that raise errored every collected
test rather than the browser ones: a second worktree running its own suite
produced 10,612 errors, none of them about the code under test. 7011 is also
the application's own default port, so the suite could not run while a local
instance was up.

Bind port 0 instead and publish the resulting origin as
ODYSSEUS_TEST_STATIC_ORIGIN. The browser tests shell out to node, which
inherits the environment, so the snippets read process.env rather than
hardcoding a port. ODYSSEUS_TEST_STATIC_PORT still pins one when something
outside pytest has to reach the server; that is the only path that can now
fail to bind, and it fails with a message that says so.

Two concurrent full runs from one checkout now both pass. Only the docx export
snippet is an rf-string, so it is the only one whose JS braces needed doubling.
2026-09-25 10:45:58 +02:00

54 lines
1.7 KiB
Python

"""Contract for the loopback static server the browser tests run against.
The server used to bind a fixed port and raise if it was taken, which errored
every collected test rather than the browser ones — so a second worktree
running its own suite took the whole session down with it.
"""
import os
import re
import urllib.request
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def test_static_origin_is_published_for_node_subprocesses() -> None:
origin = os.environ.get("ODYSSEUS_TEST_STATIC_ORIGIN")
assert origin, "the session fixture must publish the origin it bound"
assert re.fullmatch(r"http://127\.0\.0\.1:\d+", origin)
def test_static_origin_does_not_reuse_the_application_port() -> None:
"""An ephemeral port keeps the suite and a running instance independent."""
origin = os.environ["ODYSSEUS_TEST_STATIC_ORIGIN"]
assert not origin.endswith(":7011")
def test_static_server_serves_this_worktree() -> None:
origin = os.environ["ODYSSEUS_TEST_STATIC_ORIGIN"]
with urllib.request.urlopen(f"{origin}/static/js/documentStats.js", timeout=5) as r:
assert r.status == 200
assert r.headers.get_content_type() == "application/javascript"
def test_no_test_hardcodes_the_static_server_origin() -> None:
"""Regression guard: a hardcoded port reintroduces the collision."""
needle = "page.goto(" + "'http://127.0.0.1:"
offenders = []
for path in sorted((ROOT / "tests").glob("*.py")):
if path.name == Path(__file__).name:
continue
if needle in path.read_text(encoding="utf-8"):
offenders.append(path.name)
assert offenders == [], (
"browser tests must read ODYSSEUS_TEST_STATIC_ORIGIN rather than a fixed port"
)