Files
hyprland-headless-stream-pl…/tests/ipc/test_discovery.py
T
HermenixandClaude Opus 5 9b77c77ee8 ipc: a client that asks the compositor instead of guessing
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
2026-10-02 00:12:44 +00:00

132 lines
4.6 KiB
Python

"""Finding the instance, and refusing to pick one when there is a choice.
Two instances and no signature is the dangerous case: a broker that guessed
would aim its queries — and later its pointer — at someone else's desktop.
"""
from __future__ import annotations
import socket
import pytest
from hhstreamd.ipc import SIGNATURE_ENV, CompositorUnavailable, HyprlandIPC
def bind(path):
"""A bound, listening socket at ``path``, so discovery sees a real one."""
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.bind(str(path))
sock.listen(1)
return sock
def test_single_instance_is_discovered(runtime_dir, instance, monkeypatch):
monkeypatch.delenv(SIGNATURE_ENV, raising=False)
sock = bind(instance / ".socket.sock")
try:
ipc = HyprlandIPC.discover(runtime_dir)
finally:
sock.close()
assert ipc.request_socket == instance / ".socket.sock"
assert ipc.event_socket == instance / ".socket2.sock"
def test_no_instance_directory_at_all(runtime_dir, monkeypatch):
monkeypatch.delenv(SIGNATURE_ENV, raising=False)
with pytest.raises(CompositorUnavailable) as caught:
HyprlandIPC.discover(runtime_dir)
assert str(runtime_dir / "hypr") in str(caught.value)
def test_zero_instances_under_hypr(runtime_dir, monkeypatch):
monkeypatch.delenv(SIGNATURE_ENV, raising=False)
(runtime_dir / "hypr").mkdir()
with pytest.raises(CompositorUnavailable, match="no instance"):
HyprlandIPC.discover(runtime_dir)
def test_two_instances_without_a_signature_refuses(runtime_dir, instance, monkeypatch):
monkeypatch.delenv(SIGNATURE_ENV, raising=False)
other = runtime_dir / "hypr" / "deadbee_1700000001"
other.mkdir()
bind(instance / ".socket.sock").close()
with pytest.raises(CompositorUnavailable) as caught:
HyprlandIPC.discover(runtime_dir)
message = str(caught.value)
assert "2 instances" in message
assert instance.name in message and other.name in message
assert SIGNATURE_ENV in message
def test_two_instances_with_an_explicit_signature(runtime_dir, instance, monkeypatch):
monkeypatch.delenv(SIGNATURE_ENV, raising=False)
other = runtime_dir / "hypr" / "deadbee_1700000001"
other.mkdir()
sock = bind(other / ".socket.sock")
try:
ipc = HyprlandIPC.discover(runtime_dir, signature=other.name)
finally:
sock.close()
assert ipc.request_socket == other / ".socket.sock"
def test_signature_from_the_environment(runtime_dir, instance, monkeypatch):
other = runtime_dir / "hypr" / "deadbee_1700000001"
other.mkdir()
monkeypatch.setenv(SIGNATURE_ENV, other.name)
sock = bind(other / ".socket.sock")
try:
ipc = HyprlandIPC.discover(runtime_dir)
finally:
sock.close()
assert ipc.request_socket == other / ".socket.sock"
def test_explicit_signature_beats_the_environment(runtime_dir, instance, monkeypatch):
monkeypatch.setenv(SIGNATURE_ENV, "not_this_one")
sock = bind(instance / ".socket.sock")
try:
ipc = HyprlandIPC.discover(runtime_dir, signature=instance.name)
finally:
sock.close()
assert ipc.request_socket == instance / ".socket.sock"
def test_signature_naming_a_missing_instance(runtime_dir, instance, monkeypatch):
monkeypatch.setenv(SIGNATURE_ENV, "gone_1700000002")
with pytest.raises(CompositorUnavailable, match="request socket"):
HyprlandIPC.discover(runtime_dir)
def test_instance_without_a_request_socket(runtime_dir, instance, monkeypatch):
monkeypatch.delenv(SIGNATURE_ENV, raising=False)
with pytest.raises(CompositorUnavailable) as caught:
HyprlandIPC.discover(runtime_dir)
assert ".socket.sock" in str(caught.value)
def test_a_regular_file_is_not_a_socket(runtime_dir, instance, monkeypatch):
monkeypatch.delenv(SIGNATURE_ENV, raising=False)
(instance / ".socket.sock").write_text("not a socket")
with pytest.raises(CompositorUnavailable, match="is not a socket"):
HyprlandIPC.discover(runtime_dir)
def test_a_signature_cannot_be_a_path(runtime_dir, instance):
with pytest.raises(CompositorUnavailable, match="not a directory name"):
HyprlandIPC.discover(runtime_dir, signature="../../elsewhere")
def test_an_empty_environment_signature_falls_through(runtime_dir, instance, monkeypatch):
"""An exported-but-empty variable is no signature, not a signature of ""."""
monkeypatch.setenv(SIGNATURE_ENV, "")
sock = bind(instance / ".socket.sock")
try:
ipc = HyprlandIPC.discover(runtime_dir)
finally:
sock.close()
assert ipc.request_socket == instance / ".socket.sock"