Files
Hermenix e8b3855a05 move every live default to /run/hypr-stream, and resolve the slices' contract gaps
Slice A renamed the runtime directory in the module, but the old path was still
live in the code and checks it does not own, so the VM gates pointed at a
directory the module no longer creates. This moves the 21 remaining live
occurrences (client/server defaults, the unit tests that pin them, all three VM
checks, the diagnostics script, both READMEs) and deliberately leaves the three
mentions that are history rather than configuration: the plan's problem
statement, module.nix's note about the previous name, and cli.py's explanation
for spelling its default out.

Also here: docs/CONTRACT-sessions.md is now v2 with every gap the four slices
reported resolved as a decision (Monitor.id, stable_id, pid -1, Event.data,
mark(), the quarantine argv type, the corrected wlr XML path, and the state
dir/frame socket rows), and docs/CONTRACT.md is v2 because its documented socket
default moved -- wire format unchanged.

Two comments that stated the old, wrong diagnosis (a 'misnamed' IPC socket) now
state the measured one: the event socket did not fit in sun_path.
2026-10-02 18:11:46 +00:00

148 lines
5.5 KiB
Python

"""The client, against the real server and against a hand-written stub server."""
from __future__ import annotations
import contextlib
import json
import socket
import threading
import pytest
from helpers import FakeCapture, connect, distinct_frames
from hhstream import client, protocol
@contextlib.contextmanager
def stub_server(path: str, payload: bytes):
"""A server that writes exactly `payload` to one client, then hangs up."""
listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
listener.bind(path)
listener.listen(1)
def run():
try:
conn, _ = listener.accept()
except OSError:
return
with conn:
try:
conn.sendall(payload)
except OSError:
pass
thread = threading.Thread(target=run, daemon=True)
thread.start()
try:
yield
finally:
listener.close()
thread.join(5)
def test_fetch_returns_the_payloads(sock_path, serve):
frames = distinct_frames(30)
with serve(sock_path, FakeCapture(frames=frames), fps=50):
got = client.fetch(sock_path, count=3, timeout=5)
assert len(got) == 3
assert all(payload in frames for payload in got)
def test_read_stream_also_returns_the_hello(sock_path, serve):
with serve(sock_path, FakeCapture(frames=distinct_frames(5)), fps=25):
hello, frames = client.read_stream(sock_path, count=1, timeout=5)
assert hello == {"magic": "HHS1", "width": 1280, "height": 720,
"format": "image/png", "fps": 25}
assert len(frames) == 1
def test_fetch_returns_fewer_frames_when_the_server_hangs_up(sock_path):
wire = protocol.hello(4, 2, "image/png", 1) + protocol.frame(b"only one")
with stub_server(sock_path, wire):
assert client.fetch(sock_path, count=5, timeout=5) == [b"only one"]
def test_fetch_on_a_missing_socket_raises_oserror(sock_path):
with pytest.raises(OSError):
client.fetch(sock_path, count=1, timeout=1)
def test_fetch_rejects_a_bad_hello(sock_path):
with stub_server(sock_path, b'{"magic":"NOPE"}\n'):
with pytest.raises(ValueError):
client.fetch(sock_path, count=1, timeout=5)
def test_fetch_rejects_a_truncated_frame(sock_path):
wire = protocol.hello(4, 2, "image/png", 1) + b"100\nshort"
with stub_server(sock_path, wire):
with pytest.raises(EOFError):
client.fetch(sock_path, count=1, timeout=5)
# -- the CLI -------------------------------------------------------------
def test_main_prints_the_hello_and_writes_one_frame(sock_path, serve, workdir, capsys):
frames = distinct_frames(5)
with serve(sock_path, FakeCapture(frames=frames), fps=25):
out = workdir / "frame.png"
assert client.main(["--socket", sock_path, "--count", "1",
"--out", str(out), "--timeout", "5"]) == 0
printed = capsys.readouterr().out.splitlines()
assert json.loads(printed[0])["magic"] == "HHS1"
assert f"-> {out}" in printed[1]
assert out.read_bytes() in frames
def test_main_suffixes_the_output_when_count_is_more_than_one(sock_path, serve,
workdir, capsys):
with serve(sock_path, FakeCapture(frames=distinct_frames(20)), fps=50):
assert client.main(["--socket", sock_path, "--count", "2",
"--out", str(workdir / "shot.png"), "--timeout", "5"]) == 0
assert (workdir / "shot-1.png").exists()
assert (workdir / "shot-2.png").exists()
assert not (workdir / "shot.png").exists()
assert len(capsys.readouterr().out.splitlines()) == 3 # hello + two frames
def test_main_writes_payloads_to_stdout_with_out_dash(sock_path, serve, capsysbinary):
frame = distinct_frames(1)[0]
with serve(sock_path, FakeCapture(frames=[frame]), fps=25):
assert client.main(["--socket", sock_path, "--count", "2", "--out", "-",
"--timeout", "5"]) == 0
captured = capsysbinary.readouterr()
assert captured.out == frame * 2, "payloads must be the only thing on stdout"
assert b'"magic":"HHS1"' in captured.err
def test_main_reports_a_missing_socket_without_a_traceback(sock_path, capsys):
assert client.main(["--socket", sock_path, "--timeout", "1"]) == 1
assert "hhstream-client:" in capsys.readouterr().err
def test_main_reports_a_short_stream(sock_path, capsys):
wire = protocol.hello(4, 2, "image/png", 1) + protocol.frame(b"one")
with stub_server(sock_path, wire):
assert client.main(["--socket", sock_path, "--count", "3",
"--timeout", "5"]) == 1
assert "wanted 3 frames, got 1" in capsys.readouterr().err
def test_main_rejects_a_negative_count(sock_path, capsys):
assert client.main(["--socket", sock_path, "--count", "-1"]) == 2
assert "--count" in capsys.readouterr().err
def test_default_socket_matches_the_contract():
assert client.DEFAULT_SOCKET == "/run/hypr-stream/stream.sock"
assert client.build_parser().parse_args([]).socket == client.DEFAULT_SOCKET
def test_a_client_can_be_interrupted_without_disturbing_the_server(sock_path, serve):
"""Two sequential fetches: the first client's exit leaves the socket usable."""
with serve(sock_path, FakeCapture(frames=distinct_frames(40)), fps=50):
assert len(client.fetch(sock_path, count=2, timeout=5)) == 2
conn = connect(sock_path)
conn.close() # leave mid-stream
assert len(client.fetch(sock_path, count=2, timeout=5)) == 2