"""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"