"""Fixtures for the session-stream web app suite. Everything is standard library and everything is in-process: a real `LiveServer` on a loopback ephemeral port, fed by a stub capture, with plain `http.client` for the requests. No compositor, no `grim`, no broker. The doubles (`StubCapture`, `read_parts`, `PHRASE`) live in `live_helpers.py`, not here, so the tests import them by a plain module name -- the same shape as `tests/unit`. """ from __future__ import annotations import contextlib import http.client import pathlib import sys import pytest _HERE = pathlib.Path(__file__).resolve().parent _SRC = _HERE.parents[1] / "src" for _path in (str(_HERE), str(_SRC)): if _path not in sys.path: sys.path.insert(0, _path) from fake_jpeg import jpeg_bytes # noqa: E402 from live_helpers import PHRASE, StubCapture # noqa: E402 from hhstream_live.publisher import StreamRegistry, StreamSpec # noqa: E402 from hhstream_live.server import LiveServer, RateLimiter # noqa: E402 @pytest.fixture def jpg(): return jpeg_bytes @pytest.fixture def registry(): reg = StreamRegistry() yield reg reg.stop_all() @pytest.fixture def serve(registry): """`with serve(phrase=..., capture=..., fps=...) as (server, capture)`.""" servers: list[LiveServer] = [] @contextlib.contextmanager def _serve(*, phrase: str = PHRASE, capture: StubCapture | None = None, fps: int = 5, label: str = "demo", limit: int = 30): capture = capture or StubCapture() spec = StreamSpec(phrase=phrase, output="hh-demo", fps=fps, session="demo", label=label) registry.add(spec, capture) server = LiveServer(registry, host="127.0.0.1", port=0, limiter=RateLimiter(limit=limit)) servers.append(server) server.start() assert server.ready.wait(5) try: yield server, capture finally: server.stop() yield _serve for server in servers: with contextlib.suppress(Exception): server.stop() @pytest.fixture def http_conn(): """Open an HTTP connection to a running server; closed at teardown.""" connections: list[http.client.HTTPConnection] = [] def _connect(server: LiveServer, timeout: float = 10.0) -> http.client.HTTPConnection: conn = http.client.HTTPConnection("127.0.0.1", server.port, timeout=timeout) connections.append(conn) return conn yield _connect for conn in connections: with contextlib.suppress(Exception): conn.close()