"""Event parsing, and a stream that must fail loudly when it stops. The payload cases here are the ones that bite: a title full of commas (the whole reason the last field absorbs the rest of the line), an event with no payload at all, an event this module has never heard of, and a line delivered in two pieces by the kernel. """ from __future__ import annotations import threading import time import pytest from hhstreamd.ipc import CompositorUnavailable, HyprlandIPC, parse_event def test_openwindow_names_its_fields(): event = parse_event("openwindow>>0x5d3d1c0,1000,foot,hermes: ~") assert event.name == "openwindow" assert event.data["address"] == "0x5d3d1c0" assert event.data["workspace"] == "1000" assert event.data["class"] == "foot" assert event.data["title"] == "hermes: ~" assert event.data["args"] == ["0x5d3d1c0", "1000", "foot", "hermes: ~"] assert event.data["raw"] == "0x5d3d1c0,1000,foot,hermes: ~" def test_a_title_keeps_its_commas(): """A naive split(",") loses the end of the title; this is that regression.""" event = parse_event("openwindow>>0x1,1000,foot,hermes: ~, build, and notes") assert event.data["title"] == "hermes: ~, build, and notes" assert len(event.data["args"]) == 4 def test_windowtitle_v2_absorbs_commas_too(): event = parse_event("windowtitlev2>>0x1,a,b,c") assert event.data == { "raw": "0x1,a,b,c", "args": ["0x1", "a,b,c"], "address": "0x1", "title": "a,b,c", } def test_an_empty_payload_names_nothing(): """``configreloaded>>`` carried no values; "" is not a value.""" event = parse_event("configreloaded>>") assert event.name == "configreloaded" assert event.data == {"raw": "", "args": []} def test_a_single_field_event(): event = parse_event("monitorremoved>>HEADLESS-3") assert event.data["name"] == "HEADLESS-3" assert event.data["args"] == ["HEADLESS-3"] def test_a_short_payload_names_only_what_arrived(): """An older Hyprland sending fewer fields must not invent the rest.""" event = parse_event("openwindow>>0x1,1000") assert event.data["address"] == "0x1" assert event.data["workspace"] == "1000" assert "class" not in event.data assert "title" not in event.data def test_an_unknown_event_is_still_usable(): event = parse_event("somethingnew>>a,b,c") assert event.name == "somethingnew" assert event.data == {"raw": "a,b,c", "args": ["a", "b", "c"]} def test_trailing_newline_is_not_part_of_the_payload(): assert parse_event("workspace>>1000\n").data["name"] == "1000" assert parse_event("workspace>>1000\r\n").data["name"] == "1000" @pytest.mark.parametrize("line", ["workspace", "", " ", ">>1000", "workspace>1000"]) def test_a_line_with_no_separator_is_malformed(line): with pytest.raises(CompositorUnavailable, match="name>>payload"): parse_event(line) @pytest.fixture def ipc(instance): client = HyprlandIPC(instance / ".socket.sock", instance / ".socket2.sock") yield client client.close() def test_the_stream_yields_events_then_raises_at_eof(servers, ipc): chunks = [ "openwindow>>0x1,1000,foot,hermes: ~, build\n", # One line split across two writes: the client must reassemble it # rather than parse half of it. "closewin", "dow>>0x1\nworkspacev2>>1000,1000\n", ] servers(event_chunks=chunks) stream = ipc.events() first = next(stream) assert first.name == "openwindow" assert first.data["title"] == "hermes: ~, build" assert next(stream).name == "closewindow" third = next(stream) assert (third.data["id"], third.data["name"]) == ("1000", "1000") # The compositor closed. An iterator that merely stopped would be # indistinguishable from an idle desktop. with pytest.raises(CompositorUnavailable, match="event stream ended"): next(stream) def test_an_unterminated_tail_is_reported(servers, ipc): servers(event_chunks=["openwindow>>0x1,1000,foot,t\nhalf-an-ev"]) stream = ipc.events() assert next(stream).name == "openwindow" with pytest.raises(CompositorUnavailable, match="unterminated"): next(stream) def test_a_malformed_line_in_the_stream(servers, ipc): servers(event_chunks=["this is not an event\n"]) stream = ipc.events() with pytest.raises(CompositorUnavailable, match="name>>payload"): next(stream) def test_a_missing_event_socket(instance, ipc): with pytest.raises(CompositorUnavailable) as caught: ipc.events() assert ".socket2.sock" in str(caught.value) def test_no_event_socket_configured(instance): ipc = HyprlandIPC(instance / ".socket.sock") with pytest.raises(CompositorUnavailable, match="no event socket"): ipc.events() def test_the_connection_is_made_by_events_not_by_the_first_next(servers, ipc): """So the caller that asked for the stream is the one told it failed.""" _, events = servers(event_chunks=["workspace>>1000\n"], close_after=False) stream = ipc.events() # ``connect`` returns as soon as the listen backlog takes it, so the # server's own accept lags by a scheduling quantum; the claim is that the # connection exists before anything iterates, not that it is instantaneous. deadline = time.monotonic() + 5.0 while events.connections == 0 and time.monotonic() < deadline: time.sleep(0.01) assert events.connections == 1 assert next(stream).name == "workspace" def test_close_unblocks_a_waiting_reader(servers, ipc): """A blocked reader must end when the broker shuts the client down. ``close()`` shuts the socket down rather than only closing it, because a bare close does not wake a thread already sitting in ``recv``. """ servers(event_chunks=["workspace>>1000\n"], close_after=False) stream = ipc.events() assert next(stream).name == "workspace" outcome = [] def read_until_it_fails(): try: next(stream) except BaseException as exc: # noqa: BLE001 - recorded, then asserted outcome.append(exc) reader = threading.Thread(target=read_until_it_fails, daemon=True) reader.start() # The reader is blocked in recv on an open, silent socket. reader.join(timeout=0.2) assert reader.is_alive() ipc.close() reader.join(timeout=5.0) assert not reader.is_alive() assert isinstance(outcome[0], CompositorUnavailable) def test_close_is_idempotent(servers, ipc): servers(event_chunks=["workspace>>1000\n"], close_after=False) ipc.events() ipc.close() ipc.close()