Files
hyprland-headless-stream-pl…/tests/sessions/unit/test_broker_protocol.py
T
Hermenix d45219994e tests: make the test basenames unique so the whole tree collects
The union of main and the four slices cannot be collected by pytest: an
import-file-mismatch on test_protocol.py (the frame wire's, the broker's) and
test_server.py (the frame server's, the pointer driver's). Per-directory runs
never hit it, so this is what the union run is for.

The tempting fix is --import-mode=importlib, and it is wrong: importlib does not
put a test file's directory on sys.path, which the slices' tests rely on for
sibling imports (from conftest import HANG, from test_ops import RecordingSink).
That trades a collection error for an ImportError. Renaming the two colliding
modules keeps the import behaviour and fixes the actual problem:

  tests/pointer/test_server.py       -> test_pointer_server.py
  tests/sessions/unit/test_protocol.py -> test_broker_protocol.py

tests: 551 passed.
2026-10-02 18:07:48 +00:00

111 lines
3.6 KiB
Python

"""Protocol framing tests (DESIGN.md §3.4)."""
from __future__ import annotations
import json
import pytest
from hhstreamd import protocol
from hhstreamd.protocol import MAX_LINE_BYTES, PROTOCOL_VERSION, ProtocolError
def test_a_message_is_exactly_one_newline_terminated_line():
frame = protocol.encode({"ok": True, "id": "foot-aaaaaa"})
assert frame.endswith(b"\n")
assert frame.count(b"\n") == 1
assert json.loads(frame) == {"ok": True, "id": "foot-aaaaaa"}
def test_round_trip():
message = {"v": 1, "op": "start", "app": "foot", "argv": ["--a", "--b"]}
assert protocol.decode(protocol.encode(message)) == message
def test_embedded_newlines_cannot_break_the_framing():
"""A session id or an argv element must never be able to split a frame."""
message = {"ok": False, "reason": "line one\nline two\r\nline three"}
frame = protocol.encode(message)
assert frame.count(b"\n") == 1
assert protocol.decode(frame) == message
def test_non_ascii_is_escaped_so_the_frame_stays_ascii():
frame = protocol.encode({"reason": "café — ✓"})
frame.decode("ascii") # must not raise
assert protocol.decode(frame)["reason"] == "café — ✓"
def test_decode_tolerates_a_missing_trailing_newline():
assert protocol.decode(b'{"ok":true}') == {"ok": True}
@pytest.mark.parametrize("line", [b"", b"\n", b" \n"])
def test_an_empty_frame_is_a_bad_request(line):
with pytest.raises(ProtocolError) as exc:
protocol.decode(line)
assert exc.value.error == "bad_request"
def test_malformed_json_is_a_bad_request():
with pytest.raises(ProtocolError) as exc:
protocol.decode(b"{not json\n")
assert exc.value.error == "bad_request"
assert "malformed JSON" in exc.value.reason
@pytest.mark.parametrize("payload", [b"[1,2,3]", b'"a string"', b"42", b"null"])
def test_a_frame_that_is_not_an_object_is_rejected(payload):
with pytest.raises(ProtocolError) as exc:
protocol.decode(payload)
assert exc.value.error == "bad_request"
def test_an_oversized_frame_is_refused_on_both_sides():
with pytest.raises(ProtocolError) as exc:
protocol.decode(b"x" * (MAX_LINE_BYTES + 1))
assert exc.value.error == "message_too_large"
with pytest.raises(ProtocolError) as exc:
protocol.encode({"argv": ["x" * MAX_LINE_BYTES]})
assert exc.value.error == "message_too_large"
def test_decode_request_requires_an_op():
with pytest.raises(ProtocolError) as exc:
protocol.decode_request(b'{"v":1}')
assert exc.value.error == "bad_request"
assert "no 'op'" in exc.value.reason
def test_decode_request_rejects_an_unknown_protocol_version():
with pytest.raises(ProtocolError) as exc:
protocol.decode_request(b'{"v":99,"op":"list"}')
assert exc.value.error == "unsupported_version"
def test_a_request_without_a_version_is_assumed_to_be_current():
assert protocol.decode_request(b'{"op":"list"}')["op"] == "list"
def test_request_attaches_the_version_and_drops_unset_fields():
message = protocol.request("start", app="foot", argv=None, idle_timeout_s=30.0)
assert message == {"v": PROTOCOL_VERSION, "op": "start", "app": "foot", "idle_timeout_s": 30.0}
def test_error_replies_carry_both_a_machine_field_and_a_human_field():
reply = protocol.error(
"not_allowlisted", "app 'sh' is not in the allowlist", hint="see the config"
)
assert reply == {
"ok": False,
"error": "not_allowlisted",
"reason": "app 'sh' is not in the allowlist",
"hint": "see the config",
}
def test_ok_replies_are_flagged():
assert protocol.ok(id="a")["ok"] is True