Files
Hermenix acdfb2801e the default capture command now asks for the cursor
grim's `-c` ("include cursors in the screenshot") exists because the screencopy
protocol takes the decision from the client: Hyprland's capture_output carries
an overlayCursor argument and the frame is only composited with the cursor when
it is set. Without it a frame shows that *something* changed and never where it
was clicked -- and a click on the wrong element looks exactly like a click on
the right one, which is the whole reason the pointer work is verifiable at all.

Found by reading Hyprland's Screencopy.cpp rather than guessing at a compositor
setting: `no_hardware_cursors` was already on, and the cursor was invisible
because nobody had asked for it.
2026-10-03 19:42:13 +00:00

173 lines
6.5 KiB
Python

"""The command line: the flag names the NixOS module passes, end to end.
The server and the client run as real processes here, with the fake capture
command standing in for `grim`, so nothing needs a compositor.
"""
from __future__ import annotations
import json
import os
import pathlib
import subprocess
import sys
import time
import pytest
from helpers import FAKE_CAPTURE
from hhstream import client, server
SRC = str(pathlib.Path(__file__).resolve().parents[2] / "src")
RUN_SERVER = [sys.executable, "-c",
"from hhstream.server import main; raise SystemExit(main())"]
RUN_CLIENT = [sys.executable, "-c",
"from hhstream.client import main; raise SystemExit(main())"]
def child_env() -> dict[str, str]:
env = dict(os.environ)
env["PYTHONPATH"] = SRC + (os.pathsep + env["PYTHONPATH"] if "PYTHONPATH" in env else "")
return env
def capture_flags(*flags: str) -> list[str]:
"""`--capture-command` once per argv token, as the module passes it."""
out = []
for token in (*FAKE_CAPTURE, *flags):
out += ["--capture-command", token]
return out
def wait_for_socket(path: str, proc: subprocess.Popen, timeout: float = 10.0) -> None:
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
if os.path.exists(path):
return
if proc.poll() is not None:
raise AssertionError(f"server exited {proc.returncode}: {proc.stderr.read()}")
time.sleep(0.05)
raise AssertionError(f"{path} never appeared")
# -- argument parsing ----------------------------------------------------
def test_server_defaults_match_the_contract():
args = server.build_parser().parse_args([])
assert args.socket == "/run/hypr-stream/stream.sock"
assert (args.fps, args.format, args.capture_command) == (30, "png", None)
assert server.DEFAULT_CAPTURE_COMMAND == ["grim", "-c", "-t", "png", "-"]
def test_capture_command_is_one_argv_token_per_flag():
argv = ["--capture-command", "grim", "--capture-command", "-t",
"--capture-command", "png", "--capture-command", "-"]
parsed = server.build_parser().parse_args(server._join_capture_tokens(argv))
assert parsed.capture_command == ["grim", "-t", "png", "-"]
def test_capture_command_also_accepts_the_equals_form():
argv = ["--capture-command=grim", "--capture-command=-t", "--capture-command=-"]
parsed = server.build_parser().parse_args(server._join_capture_tokens(argv))
assert parsed.capture_command == ["grim", "-t", "-"]
@pytest.mark.parametrize("bad", [["--fps", "0"], ["--fps", "-3"],
["--format", "webp"]])
def test_the_server_rejects_bad_flags(bad):
with pytest.raises(SystemExit):
server.build_parser().parse_args(bad)
def test_client_defaults():
args = client.build_parser().parse_args([])
assert (args.socket, args.count, args.out, args.timeout) == (
client.DEFAULT_SOCKET, 1, None, 10.0)
# -- the real thing ------------------------------------------------------
def test_server_and_client_processes_talk_to_each_other(workdir):
sock = str(workdir / "s.sock")
server_proc = subprocess.Popen(
[*RUN_SERVER, "--socket", sock, "--fps", "10", "--format", "png",
*capture_flags("--width", "1280", "--height", "720", "--counter",
str(workdir / "n"))],
env=child_env(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True,
)
try:
wait_for_socket(sock, server_proc)
done = subprocess.run(
[*RUN_CLIENT, "--socket", sock, "--count", "2",
"--out", str(workdir / "frame.png"), "--timeout", "10"],
env=child_env(), capture_output=True, text=True, timeout=30,
)
assert done.returncode == 0, done.stderr
lines = done.stdout.splitlines()
assert json.loads(lines[0]) == {
"magic": "HHS1", "width": 1280, "height": 720,
"format": "image/png", "fps": 10,
}
assert len(lines) == 3
first = (workdir / "frame-1.png").read_bytes()
second = (workdir / "frame-2.png").read_bytes()
assert first.startswith(b"\x89PNG\r\n\x1a\n")
assert second.startswith(b"\x89PNG\r\n\x1a\n")
assert first != second, "the two frames should be two different captures"
assert server_proc.poll() is None, "the server must still be running"
finally:
server_proc.terminate()
out, err = server_proc.communicate(timeout=10)
assert server_proc.returncode == 0
assert out == "", "the server must keep its stdout clean"
assert os.path.exists(sock) is False, "the socket file should be cleaned up"
def test_the_format_flag_becomes_the_mime_type_in_the_hello(workdir):
sock = str(workdir / "s.sock")
server_proc = subprocess.Popen(
[*RUN_SERVER, "--socket", sock, "--fps", "5", "--format", "jpeg",
*capture_flags("--width", "640", "--height", "480")],
env=child_env(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True,
)
try:
wait_for_socket(sock, server_proc)
hello, frames = client.read_stream(sock, count=1, timeout=10)
assert hello == {"magic": "HHS1", "width": 640, "height": 480,
"format": "image/jpeg", "fps": 5}
assert len(frames) == 1
finally:
server_proc.terminate()
server_proc.communicate(timeout=10)
def test_a_broken_capture_command_does_not_kill_the_server(workdir):
sock = str(workdir / "s.sock")
server_proc = subprocess.Popen(
[*RUN_SERVER, "--socket", sock, "--fps", "20", *capture_flags("--fail")],
env=child_env(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True,
)
try:
wait_for_socket(sock, server_proc)
time.sleep(1.0)
assert server_proc.poll() is None, "the server exited on a capture failure"
assert os.path.exists(sock), "the socket went away"
finally:
server_proc.terminate()
out, err = server_proc.communicate(timeout=10)
assert out == ""
assert "capture failed" in err
assert "simulated failure" in err, "the helper's stderr should be reported"
def test_the_server_exits_nonzero_when_the_socket_cannot_be_bound(workdir):
blocked = workdir / "regular-file"
blocked.write_text("not a socket")
done = subprocess.run(
[*RUN_SERVER, "--socket", str(blocked), *capture_flags()],
env=child_env(), capture_output=True, text=True, timeout=30,
)
assert done.returncode == 1
assert "cannot serve" in done.stderr
assert blocked.read_text() == "not a socket"