"""The broker's end of the driver socket, against a real driver socket. Every test here goes over an actual ``AF_UNIX`` connection to the shipped :class:`hhstream_pointer.server.PointerServer` running the shipped :class:`hhstream_pointer.ops.Dispatcher`. Only the Wayland sink is a recorder. Mocking the socket would have tested that this module can call ``json.dumps``; what it has to be right about is the half of the protocol it does not own. """ from __future__ import annotations import socket import threading from pathlib import Path import pytest from fakes import FakeDriver from hhstream_pointer.backend import CompositorUnavailable as DriverDied from hhstream_pointer.backend import OutputUnknown from hhstreamd.driver import DriverRejected, DriverUnavailable, PointerDriver @pytest.fixture def driver(short_tmpdir: Path): fake = FakeDriver(short_tmpdir / "p.sock") client = PointerDriver(fake.socket_path, timeout_s=5.0) try: yield client, fake finally: client.close() fake.stop() def test_a_warp_reaches_the_sink_as_output_local_coordinates(driver): client, fake = driver client.warp("hh-a", 0.25, 0.75) assert fake.sink.calls == [("warp", "hh-a", 0.25, 0.75)] def test_a_gesture_arrives_in_order_and_is_committed_by_a_frame(driver): """Nothing the driver is sent reaches the compositor until a frame.""" client, fake = driver client.warp("hh-a", 0.5, 0.5) client.button("left", "down") client.button("left", "up") client.frame() assert fake.sink.calls == [ ("warp", "hh-a", 0.5, 0.5), ("button", "hh-a", 0x110, 1), ("button", "hh-a", 0x110, 0), ("frame", "hh-a"), ] def test_pressed_reports_what_the_driver_believes_is_down(driver): client, _ = driver client.warp("hh-a", 0.5, 0.5) assert client.pressed() == [] client.button("right", "down") client.button("middle", "down") assert client.pressed() == ["right", "middle"] client.button("right", "up") assert client.pressed() == ["middle"] def test_release_all_lifts_everything_and_says_what_it_lifted(driver): client, fake = driver client.warp("hh-a", 0.5, 0.5) client.button("left", "down") client.button("right", "down") # One call, two lists: there is one `release_all` op and one expiry call # site, so buttons and keys come back together rather than needing a # second request that could be forgotten. assert client.release_all() == (["left", "right"], []) assert client.pressed() == [] # Lifted through the pointer each went down on, then one frame for it. assert fake.sink.calls[-3:] == [ ("button", "hh-a", 0x110, 0), ("button", "hh-a", 0x111, 0), ("frame", "hh-a"), ] def test_release_all_on_an_idle_driver_is_an_empty_list_not_an_error(driver): """The expiry path calls this unconditionally; it must be safe.""" client, _ = driver assert client.release_all() == ([], []) def test_a_refusal_is_carried_back_and_is_not_a_lost_connection(driver): """``ok: false`` is the driver working, so the client must stay usable.""" client, fake = driver with pytest.raises(DriverRejected) as exc: client.warp("hh-a", 1.5, 0.5) assert "0..1" in str(exc.value) client.warp("hh-a", 0.5, 0.5) assert fake.sink.calls == [("warp", "hh-a", 0.5, 0.5)] def test_a_button_before_any_warp_is_refused_rather_than_aimed_at_nothing(driver): client, _ = driver with pytest.raises(DriverRejected) as exc: client.button("left", "down") assert "warp" in str(exc.value) def test_an_unknown_output_is_a_refusal_and_leaves_the_driver_usable(driver): """Routine: the broker can be racing a container it just tore down.""" client, fake = driver fake.sink.fail = OutputUnknown("no output hh-gone") with pytest.raises(DriverRejected) as exc: client.warp("hh-gone", 0.5, 0.5) assert "hh-gone" in str(exc.value) fake.sink.fail = None client.warp("hh-a", 0.5, 0.5) assert fake.sink.of("warp") == [("warp", "hh-a", 0.5, 0.5)] def test_a_driver_that_dies_mid_gesture_is_unavailable_not_a_refusal(driver): """A lost compositor takes the driver's connection with it (by design). The broker has to be able to tell this from a refusal, because the cursor is now in an unknown state and the lease has to go rather than carry on. """ client, fake = driver fake.sink.fail = DriverDied("the compositor hung up") with pytest.raises(DriverUnavailable): client.warp("hh-a", 0.5, 0.5) def test_no_driver_at_all_is_unavailable_and_never_a_silent_no_op(short_tmpdir: Path): client = PointerDriver(short_tmpdir / "absent.sock", timeout_s=0.5) with pytest.raises(DriverUnavailable) as exc: client.release_all() assert "absent.sock" in str(exc.value) def test_a_driver_that_restarts_is_reconnected_to_rather_than_lost(short_tmpdir: Path): """The held connection dies with the driver; the next verb must not.""" path = short_tmpdir / "p.sock" first = FakeDriver(path) client = PointerDriver(path, timeout_s=5.0) client.warp("hh-a", 0.1, 0.1) first.stop() second = FakeDriver(path) try: client.warp("hh-a", 0.2, 0.2) assert second.sink.calls == [("warp", "hh-a", 0.2, 0.2)] finally: client.close() second.stop() def test_a_driver_that_answers_nothing_is_unavailable_within_the_timeout(short_tmpdir: Path): """A wedged driver must time out, not hang the verb that called it.""" path = short_tmpdir / "mute.sock" listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) listener.bind(str(path)) listener.listen(1) accepted: list[socket.socket] = [] def accept_and_say_nothing() -> None: conn, _ = listener.accept() accepted.append(conn) thread = threading.Thread(target=accept_and_say_nothing, daemon=True) thread.start() client = PointerDriver(path, timeout_s=0.3) try: with pytest.raises(DriverUnavailable): client.pressed() finally: client.close() thread.join(timeout=5.0) for conn in accepted: conn.close() listener.close() def test_a_reply_that_is_not_the_protocol_is_unavailable_not_an_empty_answer( short_tmpdir: Path, ): """"I could not read the answer" must never render as "nothing was down".""" path = short_tmpdir / "liar.sock" listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) listener.bind(str(path)) listener.listen(1) def answer_nonsense() -> None: conn, _ = listener.accept() with conn: conn.recv(4096) conn.sendall(b'{"ok":true,"buttons":"left"}\n') thread = threading.Thread(target=answer_nonsense, daemon=True) thread.start() client = PointerDriver(path, timeout_s=2.0) try: with pytest.raises(DriverUnavailable) as exc: client.pressed() assert "list of names" in str(exc.value) finally: client.close() thread.join(timeout=5.0) listener.close()