"""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/hyprland-headless-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