Wires a `checks` output into the flake, one file per check under checks/, all three asserting docs/CONTRACT.md rather than the implementation: - unit: pytest over tests/unit/ in the build sandbox, against the nix/package.nix build. Refuses an empty suite -- pytest exits 0 when every test was skipped or deselected, so checks/lib/check_report.py parses the JUnit report and fails on zero collected or zero executed. - vm-basic: nixosTest with captureCommand pointed at tests/fake_capture.py, which emits one fixed deterministic PNG. Because the image never changes, "two frames arrived and they are the same bytes" is an assertion about the framing rather than a coincidence. - vm-hyprland: the same machine with no captureCommand override, so grim captures the real compositor. Deliberately impatient -- bounded timeouts, no retry loops, and a full diagnostics dump (compositor log, hyprctl monitors, both journals) before any failure propagates. tests/assert_stream.py is a second, independent reader of the contract: it shares no code with hhstream.client, so a framing bug present in both the server's writer and the shipped client's reader still fails. The unit suite's negative tests use a `rejects` fixture instead of pytest.raises(Exception), which an unimplemented stub's NotImplementedError would otherwise satisfy. Three defects found in code this branch does not own, recorded in README.dev.md rather than worked around: - nix/module.nix has no config side, so neither unit exists and both VM checks fail at their first wait; - services.hyprland-headless-stream.package is declared with no default, against docs/CONTRACT.md; - src/hhstream/*.py is all stubs, so 66 of 70 unit tests fail. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017YXMoCNxzScTaKNr43GQNz
203 lines
7.4 KiB
Python
203 lines
7.4 KiB
Python
"""`FrameServer` against a real (temporary) socket.
|
|
|
|
These drive the server in-process on a daemon thread and talk to it over an
|
|
AF_UNIX socket inside `tmp_path`. Nothing here touches the host: no compositor,
|
|
no /run, no network. The reader is `hhstream.protocol`, which its own tests pin
|
|
to the document.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import socket
|
|
import time
|
|
|
|
import pytest
|
|
|
|
from hhstream import protocol
|
|
from hhstream.server import FrameServer
|
|
|
|
from conftest import make_png
|
|
|
|
|
|
def wait_for_socket(path: str, deadline: float = 10.0) -> None:
|
|
end = time.monotonic() + deadline
|
|
while time.monotonic() < end:
|
|
if os.path.exists(path):
|
|
return
|
|
time.sleep(0.02)
|
|
raise AssertionError(f"server never bound {path}")
|
|
|
|
|
|
def read_stream(sock: socket.socket):
|
|
"""A file object over the socket, for protocol.read_* to consume."""
|
|
return sock.makefile("rb")
|
|
|
|
|
|
def test_serves_the_hello_then_frames(sock_path, stub_capture, run_bg, connect):
|
|
cap = stub_capture(frames=[make_png(width=64, height=32)], size=(64, 32))
|
|
box = run_bg(FrameServer(sock_path, cap, fps=30).serve_forever)
|
|
wait_for_socket(sock_path)
|
|
|
|
with read_stream(connect(sock_path)) as stream:
|
|
hello = protocol.read_hello(stream)
|
|
assert hello["magic"] == "HHS1"
|
|
assert (hello["width"], hello["height"]) == (64, 32)
|
|
assert hello["format"] == "image/png"
|
|
assert hello["fps"] == 30
|
|
first = protocol.read_frame(stream)
|
|
second = protocol.read_frame(stream)
|
|
|
|
assert not box, f"server thread died: {box}"
|
|
assert first and second
|
|
assert len(first) > 0 and len(second) > 0
|
|
|
|
|
|
def test_hello_format_follows_the_constructor(sock_path, stub_capture, run_bg, connect):
|
|
cap = stub_capture()
|
|
run_bg(FrameServer(sock_path, cap, fps=15, fmt="image/jpeg").serve_forever)
|
|
wait_for_socket(sock_path)
|
|
with read_stream(connect(sock_path)) as stream:
|
|
hello = protocol.read_hello(stream)
|
|
assert hello["format"] == "image/jpeg"
|
|
assert hello["fps"] == 15
|
|
|
|
|
|
def test_socket_mode_is_0660(sock_path, stub_capture, run_bg):
|
|
# docs/CONTRACT.md -> Socket: "mode 0660".
|
|
run_bg(FrameServer(sock_path, stub_capture(), fps=30).serve_forever)
|
|
wait_for_socket(sock_path)
|
|
assert os.stat(sock_path).st_mode & 0o777 == 0o660
|
|
|
|
|
|
def test_a_stale_socket_file_does_not_prevent_startup(sock_path, stub_capture, run_bg, connect):
|
|
# docs/CONTRACT.md: "The socket path is removed and recreated on start."
|
|
with open(sock_path, "wb") as fh:
|
|
fh.write(b"leftover from a crashed run")
|
|
run_bg(FrameServer(sock_path, stub_capture(), fps=30).serve_forever)
|
|
wait_for_socket(sock_path)
|
|
with read_stream(connect(sock_path)) as stream:
|
|
assert protocol.read_hello(stream)["magic"] == "HHS1"
|
|
|
|
|
|
def test_every_client_gets_its_own_hello_and_frames(sock_path, stub_capture, run_bg, connect):
|
|
# docs/CONTRACT.md: "Each client gets its own copy of the header and of
|
|
# every subsequent frame."
|
|
run_bg(FrameServer(sock_path, stub_capture(size=(128, 96)), fps=30).serve_forever)
|
|
wait_for_socket(sock_path)
|
|
|
|
streams = [read_stream(connect(sock_path)) for _ in range(3)]
|
|
try:
|
|
for stream in streams:
|
|
hello = protocol.read_hello(stream)
|
|
assert (hello["width"], hello["height"]) == (128, 96)
|
|
assert len(protocol.read_frame(stream)) > 0
|
|
finally:
|
|
for stream in streams:
|
|
stream.close()
|
|
|
|
|
|
def test_a_client_disconnecting_does_not_take_the_server_down(
|
|
sock_path, stub_capture, run_bg, connect
|
|
):
|
|
# docs/CONTRACT.md: "A client disconnecting (or dying mid-frame) must never
|
|
# take the server down and must never disturb the other clients."
|
|
box = run_bg(FrameServer(sock_path, stub_capture(), fps=30).serve_forever)
|
|
wait_for_socket(sock_path)
|
|
|
|
survivor = read_stream(connect(sock_path))
|
|
assert protocol.read_hello(survivor)["magic"] == "HHS1"
|
|
|
|
for _ in range(3):
|
|
doomed = connect(sock_path)
|
|
doomed.recv(1) # take one byte of the Hello, then die mid-message
|
|
doomed.close()
|
|
|
|
assert len(protocol.read_frame(survivor)) > 0
|
|
survivor.close()
|
|
|
|
with read_stream(connect(sock_path)) as latecomer:
|
|
assert protocol.read_hello(latecomer)["magic"] == "HHS1"
|
|
assert not box, f"server thread died: {box}"
|
|
|
|
|
|
def test_a_stalled_client_does_not_stall_the_others(sock_path, stub_capture, run_bg, connect):
|
|
# docs/CONTRACT.md: "A client that stalls or reads slowly is *slowed*, not
|
|
# queued" -- observably, it must not hold up anybody else.
|
|
run_bg(FrameServer(sock_path, stub_capture(), fps=30).serve_forever)
|
|
wait_for_socket(sock_path)
|
|
|
|
stalled = connect(sock_path) # connects and never reads
|
|
with read_stream(connect(sock_path)) as stream:
|
|
assert protocol.read_hello(stream)["magic"] == "HHS1"
|
|
for _ in range(5):
|
|
assert len(protocol.read_frame(stream)) > 0
|
|
stalled.close()
|
|
|
|
|
|
def test_bytes_sent_by_a_client_are_read_and_ignored(sock_path, stub_capture, run_bg, connect):
|
|
# docs/CONTRACT.md -> "Client -> server direction": reserved in v1.
|
|
box = run_bg(FrameServer(sock_path, stub_capture(), fps=30).serve_forever)
|
|
wait_for_socket(sock_path)
|
|
|
|
sock = connect(sock_path)
|
|
sock.sendall(b"a future control channel says hello\n" * 64)
|
|
with read_stream(sock) as stream:
|
|
assert protocol.read_hello(stream)["magic"] == "HHS1"
|
|
assert len(protocol.read_frame(stream)) > 0
|
|
assert not box, f"server thread died: {box}"
|
|
|
|
|
|
def test_a_failing_capture_does_not_kill_the_server(sock_path, stub_capture, run_bg, connect):
|
|
# docs/CONTRACT.md -> Failure semantics: "the server keeps the socket up and
|
|
# retries; it does not exit and it does not send partial frames."
|
|
cap = stub_capture(raise_first=3)
|
|
box = run_bg(FrameServer(sock_path, cap, fps=30).serve_forever)
|
|
wait_for_socket(sock_path)
|
|
|
|
with read_stream(connect(sock_path)) as stream:
|
|
assert protocol.read_hello(stream)["magic"] == "HHS1"
|
|
payload = protocol.read_frame(stream)
|
|
assert payload and payload.startswith(b"\x89PNG"), "no partial frame was sent"
|
|
assert cap.grabs > 3, "the server never retried after the capture failed"
|
|
assert not box, f"server thread died: {box}"
|
|
|
|
|
|
def test_never_announces_a_zero_size_hello(sock_path, stub_capture, run_bg, connect):
|
|
# docs/CONTRACT.md -> Failure semantics: "It never sends a Hello with zero
|
|
# size." The size only becomes known after the first successful capture.
|
|
cap = stub_capture(size=(0, 0))
|
|
cap_sizes = [(0, 0), (0, 0), (1280, 720)]
|
|
calls = {"n": 0}
|
|
|
|
def size():
|
|
n = calls["n"]
|
|
calls["n"] = min(n + 1, len(cap_sizes) - 1)
|
|
return cap_sizes[n]
|
|
|
|
cap.size = size
|
|
run_bg(FrameServer(sock_path, cap, fps=30).serve_forever)
|
|
wait_for_socket(sock_path)
|
|
|
|
with read_stream(connect(sock_path)) as stream:
|
|
hello = protocol.read_hello(stream)
|
|
assert hello["width"] > 0 and hello["height"] > 0
|
|
|
|
|
|
def test_build_parser_exposes_the_options_the_module_needs():
|
|
from hhstream import server
|
|
|
|
parser = server.build_parser()
|
|
args = parser.parse_args(
|
|
["--socket", "/run/x.sock", "--fps", "24", "--format", "png", "--", "grim", "-t", "png", "-"]
|
|
)
|
|
assert args.socket == "/run/x.sock"
|
|
assert args.fps == 24
|
|
|
|
|
|
def test_main_rejects_an_unknown_flag():
|
|
from hhstream import server
|
|
|
|
with pytest.raises(SystemExit):
|
|
server.main(["--definitely-not-a-flag"])
|