The broker reported `workspace: null` and `window: null` for every session because nothing in it could talk to Hyprland. This is that voice, and the only place that knows the compositor's wire format: one connection per request on `.socket.sock` (leading `/` so an argument containing a slash is never read as output flags, `j/` where JSON is wanted), and a held, blocking stream on `.socket2.sock`. Every failure is CompositorUnavailable -- no instance directory, two of them and no signature, a socket that is gone, a peer that refuses or closes mid-reply, a reply that is not the JSON it promised, an event stream that ends. Nothing returns an empty list or a None to stand in for "I could not ask", because a caller that cannot tell those apart writes a guess into a session record. Inside the defined shape the parse is forgiving instead: Hyprland's JSON has changed keys across releases, so a missing key is a defined value and never a KeyError reaching the broker. Tests bind real AF_UNIX sockets in a temporary directory laid out the way Hyprland lays one out and serve canned bytes, so they drive the actual connect, half-close, read-to-EOF and timeout paths rather than a mock that would pass while the sockets broke. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MLiQAZCmJf66CEcLx8siYF
241 lines
8.1 KiB
Python
241 lines
8.1 KiB
Python
"""The request socket, against a real listener.
|
|
|
|
Every test here connects over ``AF_UNIX`` to a socket the fixture bound, so the
|
|
failures that matter — a path that is gone, a peer that refuses, a peer that
|
|
closes mid-reply, a peer that never answers — are produced rather than
|
|
simulated.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import socket
|
|
|
|
import pytest
|
|
|
|
from hhstreamd.ipc import CompositorUnavailable, CursorPos, HyprlandIPC
|
|
|
|
from conftest import HANG
|
|
|
|
|
|
@pytest.fixture
|
|
def ipc(instance):
|
|
client = HyprlandIPC(instance / ".socket.sock", instance / ".socket2.sock")
|
|
yield client
|
|
client.close()
|
|
|
|
|
|
def test_version_is_the_banner_verbatim(servers, good_handler, ipc):
|
|
request, _ = servers(good_handler)
|
|
assert ipc.version().startswith("Hyprland 0.41.2 built from branch")
|
|
assert "Tag: v0.41.2" in ipc.version()
|
|
# Text, not JSON, and the command carries a leading "/" so an argument
|
|
# containing a slash could never be read as output flags.
|
|
assert request.requests[0] == ("", "version")
|
|
|
|
|
|
def test_monitors(servers, good_handler, ipc):
|
|
request, _ = servers(good_handler)
|
|
monitors = ipc.monitors()
|
|
assert [m.name for m in monitors] == ["HEADLESS-2", "HEADLESS-3"]
|
|
first = monitors[0]
|
|
assert (first.x, first.y, first.width, first.height) == (0, 0, 1280, 720)
|
|
assert first.scale == 1.0
|
|
assert first.active_workspace_id == 1000
|
|
# The second monitor carries no activeWorkspace at all: a defined None.
|
|
assert monitors[1].active_workspace_id is None
|
|
assert monitors[1].scale == 1.5
|
|
assert request.requests == [("j", "monitors")]
|
|
|
|
|
|
def test_workspaces(servers, good_handler, ipc):
|
|
servers(good_handler)
|
|
workspaces = ipc.workspaces()
|
|
assert [w.id for w in workspaces] == [1000, -99]
|
|
assert workspaces[0].monitor == "HEADLESS-2"
|
|
assert workspaces[0].windows == 1
|
|
assert workspaces[0].is_special is False
|
|
# No "special" key in this reply: the negative id and the name prefix are
|
|
# what every Hyprland has agreed on.
|
|
assert workspaces[1].is_special is True
|
|
|
|
|
|
def test_clients(servers, good_handler, ipc):
|
|
request, _ = servers(good_handler)
|
|
clients = ipc.clients()
|
|
assert len(clients) == 2
|
|
foot, chromium = clients
|
|
assert foot.address == "0x5d3d1c0"
|
|
assert foot.pid == 4242
|
|
assert foot.workspace_id == 1000
|
|
assert foot.class_ == "foot"
|
|
assert foot.initial_class == "hh-foot-v7r4li"
|
|
# A title with commas in it survives whole; the broker shows this to a user.
|
|
assert foot.title == "hermes: ~, build, and notes"
|
|
assert foot.stable_id == "hh-foot-v7r4li:4242"
|
|
assert foot.visible is True
|
|
# clients -j reports the monitor as an index, not a name; the field is text
|
|
# so that None keeps its one meaning of "the reply said nothing".
|
|
assert foot.monitor == "0"
|
|
|
|
assert chromium.visible is False
|
|
assert chromium.monitor is None
|
|
assert chromium.workspace_id is None
|
|
assert chromium.stable_id == "0x5d4aa10"
|
|
assert request.requests == [("j", "clients")]
|
|
|
|
|
|
def test_cursorpos(servers, good_handler, ipc):
|
|
servers(good_handler)
|
|
assert ipc.cursorpos() == CursorPos(x=640, y=360)
|
|
|
|
|
|
def test_query_passes_a_command_through_untouched(servers, ipc):
|
|
seen = []
|
|
|
|
def handler(flags, command):
|
|
seen.append((flags, command))
|
|
return "ok"
|
|
|
|
servers(handler)
|
|
assert ipc.query("dispatch exec /usr/bin/true") == "ok"
|
|
# The slash in the argument must not be read as a flag separator.
|
|
assert seen == [("", "dispatch exec /usr/bin/true")]
|
|
|
|
|
|
def test_query_keeps_an_explicit_flag_prefix(servers, ipc):
|
|
seen = []
|
|
|
|
def handler(flags, command):
|
|
seen.append((flags, command))
|
|
return "[]"
|
|
|
|
servers(handler)
|
|
assert ipc.query("j/devices") == "[]"
|
|
assert seen == [("j", "devices")]
|
|
|
|
|
|
def test_an_empty_request_is_refused_before_connecting(ipc):
|
|
with pytest.raises(CompositorUnavailable, match="empty request"):
|
|
ipc.query(" ")
|
|
|
|
|
|
@pytest.mark.parametrize("method", ["monitors", "workspaces", "clients", "cursorpos"])
|
|
def test_plain_text_where_json_was_promised(servers, ipc, method):
|
|
"""What Hyprland answers for a command it does not know."""
|
|
|
|
servers(lambda flags, command: "unknown request")
|
|
with pytest.raises(CompositorUnavailable, match="not JSON"):
|
|
getattr(ipc, method)()
|
|
|
|
|
|
def test_a_json_object_where_a_list_belongs(servers, ipc):
|
|
servers(lambda flags, command: '{"monitors": []}')
|
|
with pytest.raises(CompositorUnavailable, match="expected a JSON list"):
|
|
ipc.monitors()
|
|
|
|
|
|
def test_a_list_of_scalars(servers, ipc):
|
|
servers(lambda flags, command: '["HEADLESS-2"]')
|
|
with pytest.raises(CompositorUnavailable, match="expected objects"):
|
|
ipc.monitors()
|
|
|
|
|
|
def test_a_client_without_an_address_is_malformed(servers, ipc):
|
|
"""The one key whose absence makes the record meaningless."""
|
|
|
|
servers(lambda flags, command: '[{"class": "foot", "pid": 1}]')
|
|
with pytest.raises(CompositorUnavailable, match="no address"):
|
|
ipc.clients()
|
|
|
|
|
|
def test_a_monitor_without_a_name_is_malformed(servers, ipc):
|
|
servers(lambda flags, command: '[{"width": 1280}]')
|
|
with pytest.raises(CompositorUnavailable, match="no name"):
|
|
ipc.monitors()
|
|
|
|
|
|
def test_a_workspace_without_an_id_is_malformed(servers, ipc):
|
|
servers(lambda flags, command: '[{"name": "1000"}]')
|
|
with pytest.raises(CompositorUnavailable, match="no id"):
|
|
ipc.workspaces()
|
|
|
|
|
|
def test_missing_keys_degrade_rather_than_raise_keyerror(servers, ipc):
|
|
"""A reply from a Hyprland that reports less than this one expects."""
|
|
|
|
servers(lambda flags, command: '[{"address": "0x1"}]')
|
|
(client,) = ipc.clients()
|
|
assert client.pid == -1
|
|
assert client.workspace_id is None
|
|
assert client.monitor is None
|
|
assert (client.class_, client.initial_class, client.title) == ("", "", "")
|
|
assert client.stable_id == "0x1"
|
|
# Nothing said it was unmapped, so it is not reported as gone.
|
|
assert client.visible is True
|
|
|
|
|
|
def test_cursorpos_without_coordinates(servers, ipc):
|
|
servers(lambda flags, command: '{"ok": true}')
|
|
with pytest.raises(CompositorUnavailable, match="x and y"):
|
|
ipc.cursorpos()
|
|
|
|
|
|
def test_cursorpos_with_non_numeric_coordinates(servers, ipc):
|
|
servers(lambda flags, command: '{"x": "left", "y": null}')
|
|
with pytest.raises(CompositorUnavailable, match="not numbers"):
|
|
ipc.cursorpos()
|
|
|
|
|
|
def test_connection_closed_mid_reply(servers, ipc):
|
|
"""The compositor dies while writing: a truncated reply, never a guess."""
|
|
|
|
servers(lambda flags, command: '[{"address": "0x5d3d1c0", "pid": 4242, "cla')
|
|
with pytest.raises(CompositorUnavailable, match="not JSON"):
|
|
ipc.clients()
|
|
|
|
|
|
def test_connection_closed_with_no_reply_at_all(servers, ipc):
|
|
servers(lambda flags, command: None)
|
|
with pytest.raises(CompositorUnavailable, match="empty reply"):
|
|
ipc.workspaces()
|
|
with pytest.raises(CompositorUnavailable, match="empty reply"):
|
|
ipc.version()
|
|
|
|
|
|
def test_missing_socket(instance):
|
|
ipc = HyprlandIPC(instance / ".socket.sock")
|
|
with pytest.raises(CompositorUnavailable) as caught:
|
|
ipc.clients()
|
|
assert str(instance / ".socket.sock") in str(caught.value)
|
|
|
|
|
|
def test_nothing_listening(instance, ipc):
|
|
"""A socket left behind by a compositor that is gone: ECONNREFUSED."""
|
|
|
|
stale = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
stale.bind(str(instance / ".socket.sock"))
|
|
try:
|
|
with pytest.raises(CompositorUnavailable, match="request socket"):
|
|
ipc.monitors()
|
|
finally:
|
|
stale.close()
|
|
|
|
|
|
def test_a_compositor_that_never_answers(servers, ipc):
|
|
"""Accepted, then silence: the read timeout, exercised for real."""
|
|
|
|
servers(lambda flags, command: HANG)
|
|
ipc.request_timeout_s = 0.3
|
|
with pytest.raises(CompositorUnavailable, match="did not answer"):
|
|
ipc.clients()
|
|
|
|
|
|
def test_each_request_is_its_own_connection(servers, good_handler, ipc):
|
|
"""What hyprctl does, and what the compositor expects: no session state."""
|
|
|
|
request, _ = servers(good_handler)
|
|
ipc.monitors()
|
|
ipc.clients()
|
|
ipc.clients()
|
|
assert request.requests == [("j", "monitors"), ("j", "clients"), ("j", "clients")]
|