Fill in the bodies the skeleton left as NotImplementedError, to docs/CONTRACT.md: - protocol: the `\n`-terminated JSON Hello and the `<ascii length>\n<payload>` frame, both directions. Malformed input raises (ValueError for a bad length prefix, EOFError for a truncated payload), never hangs on endless garbage and never silently truncates; a huge length prefix is not pre-allocated. - capture: CommandCapture runs the configured command and reports a missing binary, a non-zero exit, a timeout and empty output as CaptureError, with the helper's stderr folded into the message so nothing leaks to the server's own stdio. The Hello's pixel size is read from the frame's PNG/JPEG header, so no compositor query is needed and an injected fake command behaves the same. - server: one capture thread holding only the newest frame plus one thread per client, which is what drops frames for a slow client instead of queueing them. Survives a client dying mid-write, keeps the socket up across capture failures, withholds the Hello until a size is known, replaces a stale socket file (but never a non-socket one) and publishes it mode 0660. - client: fetch()/read_stream() and the CLI, with --out suffixing per frame. --capture-command folds its separated form into `--flag=value` before argparse sees it, so the contract's default `grim -t png -` can actually be passed: its tokens start with a dash. Unit tests under tests/unit cover framing round-trips (including a payload containing `\n` and a zero-length frame), truncated and garbage prefixes, a stalled client not blocking a second one, a client dying mid-frame, frames being dropped rather than queued, and every capture failure mode. A fake capture command stands in for grim, so nothing needs a compositor or a GPU. They are deliberately not wired into flake.nix: `checks` belongs to another workstream. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FYJ8ZQ5AZaDErYTwXZe7N9
60 lines
1.5 KiB
Python
60 lines
1.5 KiB
Python
"""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)
|