"""Shared fixtures. Nothing here touches a compositor, a GPU or the network.""" from __future__ import annotations import contextlib import pathlib import shutil import sys import tempfile import threading 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 hhstream.server import FrameServer # noqa: E402 (needs sys.path above) @pytest.fixture def workdir(): """A short-lived directory with a short path (AF_UNIX paths are ~108 bytes).""" path = tempfile.mkdtemp(prefix="hhs") try: yield pathlib.Path(path) finally: shutil.rmtree(path, ignore_errors=True) @pytest.fixture def sock_path(workdir): return str(workdir / "s.sock") @pytest.fixture def serve(): """`with serve(path, capture, fps=...) as server:` -- bound and listening.""" started: list[tuple[FrameServer, threading.Thread]] = [] @contextlib.contextmanager def _serve(path, capture, fps=20, fmt="image/png"): server = FrameServer(path, capture, fps, fmt=fmt) thread = threading.Thread(target=server.serve_forever, daemon=True) started.append((server, thread)) thread.start() assert server.ready.wait(5), "server never bound its socket" try: yield server finally: server.stop() thread.join(5) yield _serve for server, thread in started: server.stop() thread.join(5)