Files
hyprland-headless-stream-pl…/tests/sessions/unit/test_driver_client.py
T
HermenixandClaude Opus 5 129430bffd wire the broker to the compositor, containers and pointer
The four slices existed and nothing called them. Now the broker builds a
spatial container per session, reads back what the compositor actually did,
and aims the cursor into one container under a broker-wide lease.

Record schema 3 (registry.py): `container` {output, workspace, mode,
created_by, state}, `pointer` {bookmark, last_released_at, released_by} and
the compositor's `signature`. A v2 record is migrated on load, not refused —
its application is still running. Every field that could not be verified is
null; `container: null` means "no container", never "probably something".

Containers (new container_host.py, manager.py, launcher.py): `start`
allocates a slot from `WorkspaceAllocator`, runs `container_plan()`'s argv,
launches the app with the container stamp in its environment, then reads the
workspace, output and window back out of `clients -j`. Teardown removes the
output, releases the slot and reports survivors rather than claiming success
— and runs for an unprompted exit and for `gc` too, so no container is ever
left unattended. Broker start-up reconciles: adopt `hh-*` outputs a live
session claims, remove the rest, `contains_output()` being the only filter.
No IPC means `container: degraded` and lifecycle verbs unchanged.

The plans' argv are dispatched over the compositor's request socket rather
than by executing `hyprctl`, which is what hyprctl does with the same words;
the reply is checked, so a refused command is not recorded as a container.

Pointer (new driver.py, manager.py): a client for the driver's private
socket, and `pointer.acquire|move|button|click|release|state` on top of
`PointerLease`. The broker owns the lease, the driver executes. The bookmark
is marked on every move and before release and stored in the record, so it
survives a restart. On expiry `pressed()` is read before `expire()` and
`release_all` goes to the driver, because the lease cannot lift a button
itself; a sweep runs unprompted so the TTL is a promise.

containers.py to v2: `quarantine_plan()` -> `quarantine_argv()`, with the
before-any-per-session-rule ordering stated where the caller could get it
wrong. protocol.py: 9 NO_LEASE, 10 COMPOSITOR, the verb table, and the
`request_id` cache moved out of the server so the CLI agrees with it.

Tests: real AF_UNIX everywhere — a stateful fake compositor and the shipped
pointer driver behind a recording sink. No mocked sockets, no compositor, no
VM. 551 -> 693.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017mUq7QvQ9EZb2Wi35xj4cs
2026-10-02 19:47:33 +00:00

223 lines
6.8 KiB
Python

"""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")
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 button names" in str(exc.value)
finally:
client.close()
thread.join(timeout=5.0)
listener.close()