A session could move a cursor and press it, but not scroll. The protocol already had the request -- zwlr_virtual_pointer_v1.axis(time, axis, value) -- it had simply never been implemented, in the driver or the broker. hhstream session pointer scroll <id> --axis vertical --amount 3 Positive is down (vertical) or right (horizontal): the protocol's direction, not a choice of ours, which is why direction travels in the sign and there is deliberately no up/down vocabulary layered over it. The output is whatever the last warp aimed at, exactly like a button, so scrolling somewhere else is a warp first -- and the lease is what keeps the whole gesture atomic. Bounded at 100 wheel clicks per request on both sides, because a decimal-point slip should be a refusal rather than a session scrolled somewhere unrecoverable. The resume point is re-stamped, not moved: the cursor did not go anywhere, so the coordinates are re-affirmed and only the stamp advances. Recorded in the contract as an amendment to the frozen op set, because adding an op to a frozen interface should be visible rather than implied. Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
316 lines
11 KiB
Python
316 lines
11 KiB
Python
"""The driver's protocol (CONTRACT-sessions.md §3.4).
|
|
|
|
The dispatcher is tested against a recording sink rather than a compositor, so
|
|
these assertions are about the exact events that would go on the wire: which
|
|
pointer they go to, in what order, and which requests are refused instead.
|
|
|
|
The driver is an executor. It is *not* tested for serialisation, authority, or
|
|
any notion of who may ask — none of that is here, on purpose. The lease is in
|
|
``hhstreamd.pointer`` and the broker owns it.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from hhstream_pointer import buttons
|
|
from hhstream_pointer.ops import Dispatcher
|
|
|
|
LEFT = buttons.CODES["left"]
|
|
RIGHT = buttons.CODES["right"]
|
|
DOWN = buttons.STATES["down"]
|
|
UP = buttons.STATES["up"]
|
|
|
|
|
|
class RecordingSink:
|
|
"""Every call, in order, as a tuple. Nothing else."""
|
|
|
|
def __init__(self) -> None:
|
|
self.calls: list[tuple] = []
|
|
|
|
def motion_absolute(self, output, x, y):
|
|
self.calls.append(("motion", output, x, y))
|
|
|
|
def button(self, output, code, state):
|
|
self.calls.append(("button", output, code, state))
|
|
|
|
def axis(self, output, axis, value):
|
|
self.calls.append(("axis", output, axis, value))
|
|
|
|
def frame(self, output):
|
|
self.calls.append(("frame", output))
|
|
|
|
|
|
class FailingSink(RecordingSink):
|
|
"""A sink whose motion raises, standing in for an output that vanished."""
|
|
|
|
class Boom(Exception):
|
|
pass
|
|
|
|
def motion_absolute(self, output, x, y):
|
|
raise FailingSink.Boom(output)
|
|
|
|
|
|
@pytest.fixture
|
|
def sink() -> RecordingSink:
|
|
return RecordingSink()
|
|
|
|
|
|
@pytest.fixture
|
|
def driver(sink) -> Dispatcher:
|
|
return Dispatcher(sink)
|
|
|
|
|
|
def warp(driver, output="hh-alpha", x=0.5, y=0.5):
|
|
return driver.handle({"op": "warp", "output": output, "x": x, "y": y})
|
|
|
|
|
|
# -- happy paths ------------------------------------------------------------
|
|
|
|
|
|
def test_warp_sends_one_motion_and_no_implicit_frame(driver, sink):
|
|
assert warp(driver, x=0.42, y=0.61) == {"ok": True}
|
|
assert sink.calls == [("motion", "hh-alpha", 0.42, 0.61)]
|
|
|
|
|
|
def test_frame_is_a_separate_op(driver, sink):
|
|
warp(driver)
|
|
assert driver.handle({"op": "frame"}) == {"ok": True}
|
|
assert sink.calls[-1] == ("frame", "hh-alpha")
|
|
|
|
|
|
def test_a_click_is_warp_button_frame(driver, sink):
|
|
warp(driver, x=0.25, y=0.75)
|
|
driver.handle({"op": "button", "button": "left", "state": "down"})
|
|
driver.handle({"op": "button", "button": "left", "state": "up"})
|
|
driver.handle({"op": "frame"})
|
|
assert sink.calls == [
|
|
("motion", "hh-alpha", 0.25, 0.75),
|
|
("button", "hh-alpha", LEFT, DOWN),
|
|
("button", "hh-alpha", LEFT, UP),
|
|
("frame", "hh-alpha"),
|
|
]
|
|
assert driver.handle({"op": "pressed"}) == {"ok": True, "buttons": []}
|
|
|
|
|
|
def test_the_boundary_coordinates_are_accepted(driver, sink):
|
|
assert warp(driver, x=0.0, y=0.0) == {"ok": True}
|
|
assert warp(driver, x=1.0, y=1.0) == {"ok": True}
|
|
# Integers are numbers too; 0 and 1 are the corners.
|
|
assert warp(driver, x=0, y=1) == {"ok": True}
|
|
assert [c[2:] for c in sink.calls] == [(0.0, 0.0), (1.0, 1.0), (0.0, 1.0)]
|
|
|
|
|
|
def test_buttons_follow_the_last_warp_not_the_first(driver, sink):
|
|
warp(driver, output="hh-alpha")
|
|
warp(driver, output="hh-beta")
|
|
driver.handle({"op": "button", "button": "left", "state": "down"})
|
|
assert sink.calls[-1] == ("button", "hh-beta", LEFT, DOWN)
|
|
|
|
|
|
# -- axis (scrolling) -------------------------------------------------------
|
|
|
|
|
|
def test_axis_scrolls_the_aimed_output_and_sends_no_implicit_frame(driver, sink):
|
|
warp(driver)
|
|
sink.calls.clear()
|
|
|
|
assert driver.handle({"op": "axis", "axis": "vertical", "value": 3}) == {"ok": True}
|
|
assert sink.calls == [("axis", "hh-alpha", buttons.AXES["vertical"], 3.0)]
|
|
|
|
|
|
def test_axis_defaults_to_one_vertical_click(driver, sink):
|
|
warp(driver)
|
|
sink.calls.clear()
|
|
|
|
assert driver.handle({"op": "axis"}) == {"ok": True}
|
|
assert sink.calls == [("axis", "hh-alpha", buttons.AXES["vertical"], 1.0)]
|
|
|
|
|
|
def test_a_horizontal_scroll_is_the_other_axis_and_keeps_the_sign(driver, sink):
|
|
warp(driver)
|
|
sink.calls.clear()
|
|
|
|
assert driver.handle({"op": "axis", "axis": "horizontal", "value": -2}) == {"ok": True}
|
|
assert sink.calls == [("axis", "hh-alpha", buttons.AXES["horizontal"], -2.0)]
|
|
# Two names, two axes -- and positive is down/right, per the protocol rather
|
|
# than per us, which is why direction travels in the sign and not the name.
|
|
assert buttons.AXES["vertical"] != buttons.AXES["horizontal"]
|
|
|
|
|
|
def test_a_scroll_goes_to_the_pointer_the_last_warp_aimed(driver, sink):
|
|
warp(driver, output="hh-alpha")
|
|
warp(driver, output="hh-beta")
|
|
sink.calls.clear()
|
|
|
|
driver.handle({"op": "axis", "value": 5})
|
|
|
|
assert sink.calls == [("axis", "hh-beta", buttons.AXES["vertical"], 5.0)]
|
|
|
|
|
|
def test_scroll_before_any_warp_is_refused(driver, sink):
|
|
assert driver.handle({"op": "axis", "value": 3})["ok"] is False
|
|
assert sink.calls == [], "a scroll with nothing aimed must emit nothing"
|
|
|
|
|
|
# -- pressed / release_all --------------------------------------------------
|
|
|
|
|
|
def test_pressed_reports_what_is_down_oldest_first(driver):
|
|
warp(driver)
|
|
driver.handle({"op": "button", "button": "right", "state": "down"})
|
|
driver.handle({"op": "button", "button": "left", "state": "down"})
|
|
assert driver.handle({"op": "pressed"}) == {"ok": True, "buttons": ["right", "left"]}
|
|
|
|
|
|
def test_release_all_lifts_everything_and_frames_once(driver, sink):
|
|
warp(driver)
|
|
driver.handle({"op": "button", "button": "left", "state": "down"})
|
|
driver.handle({"op": "button", "button": "right", "state": "down"})
|
|
sink.calls.clear()
|
|
|
|
assert driver.handle({"op": "release_all"}) == {"ok": True, "lifted": ["left", "right"]}
|
|
assert sink.calls == [
|
|
("button", "hh-alpha", LEFT, UP),
|
|
("button", "hh-alpha", RIGHT, UP),
|
|
("frame", "hh-alpha"),
|
|
]
|
|
assert driver.handle({"op": "pressed"}) == {"ok": True, "buttons": []}
|
|
|
|
|
|
def test_release_all_on_a_quiet_driver_lifts_nothing(driver, sink):
|
|
assert driver.handle({"op": "release_all"}) == {"ok": True, "lifted": []}
|
|
assert sink.calls == []
|
|
|
|
|
|
def test_release_all_lifts_through_the_pointer_the_press_went_to(driver, sink):
|
|
"""The expiry path: the broker lifts after the cursor has moved on.
|
|
|
|
A button pressed on one output must come up on that output's pointer, not
|
|
on whatever is aimed now, or the first session keeps a stuck drag.
|
|
"""
|
|
|
|
warp(driver, output="hh-alpha")
|
|
driver.handle({"op": "button", "button": "left", "state": "down"})
|
|
warp(driver, output="hh-beta")
|
|
driver.handle({"op": "button", "button": "right", "state": "down"})
|
|
sink.calls.clear()
|
|
|
|
assert driver.handle({"op": "release_all"}) == {"ok": True, "lifted": ["left", "right"]}
|
|
assert sink.calls == [
|
|
("button", "hh-alpha", LEFT, UP),
|
|
("button", "hh-beta", RIGHT, UP),
|
|
("frame", "hh-alpha"),
|
|
("frame", "hh-beta"),
|
|
]
|
|
|
|
|
|
def test_a_lifted_button_is_forgotten(driver):
|
|
warp(driver)
|
|
driver.handle({"op": "button", "button": "left", "state": "down"})
|
|
driver.handle({"op": "button", "button": "left", "state": "up"})
|
|
assert driver.handle({"op": "release_all"}) == {"ok": True, "lifted": []}
|
|
|
|
|
|
def test_every_known_button_name_round_trips(driver, sink):
|
|
warp(driver)
|
|
for name in buttons.NAMES:
|
|
assert driver.handle({"op": "button", "button": name, "state": "down"}) == {"ok": True}
|
|
assert driver.handle({"op": "pressed"})["buttons"] == list(buttons.NAMES)
|
|
assert driver.handle({"op": "release_all"})["lifted"] == list(buttons.NAMES)
|
|
|
|
|
|
# -- refusals ---------------------------------------------------------------
|
|
|
|
|
|
def test_button_before_any_warp_is_refused(driver, sink):
|
|
assert driver.handle({"op": "button", "button": "left", "state": "down"}) == {
|
|
"ok": False,
|
|
"error": "no output is aimed; warp before button or frame",
|
|
}
|
|
assert sink.calls == []
|
|
|
|
|
|
def test_frame_before_any_warp_is_refused(driver, sink):
|
|
assert driver.handle({"op": "frame"})["ok"] is False
|
|
assert sink.calls == []
|
|
|
|
|
|
def test_a_failed_warp_does_not_change_the_aim():
|
|
"""Otherwise the next button lands on a session we never reached."""
|
|
|
|
driver = Dispatcher(RecordingSink())
|
|
warp(driver, output="hh-alpha")
|
|
|
|
driver._sink = FailingSink()
|
|
with pytest.raises(FailingSink.Boom):
|
|
warp(driver, output="hh-beta")
|
|
|
|
driver._sink = sink = RecordingSink()
|
|
driver.handle({"op": "button", "button": "left", "state": "down"})
|
|
assert sink.calls == [("button", "hh-alpha", LEFT, DOWN)]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"request_, fragment",
|
|
[
|
|
({}, "no 'op'"),
|
|
({"op": 7}, "no 'op'"),
|
|
({"op": "nonsense"}, "unknown op 'nonsense'"),
|
|
({"op": "warp", "x": 0.5, "y": 0.5}, "'output' must be"),
|
|
({"op": "warp", "output": "", "x": 0.5, "y": 0.5}, "'output' must be"),
|
|
({"op": "warp", "output": "hh-a", "y": 0.5}, "'x' must be a number"),
|
|
({"op": "warp", "output": "hh-a", "x": "0.5", "y": 0.5}, "'x' must be a number"),
|
|
({"op": "warp", "output": "hh-a", "x": True, "y": 0.5}, "'x' must be a number"),
|
|
({"op": "warp", "output": "hh-a", "x": 1.5, "y": 0.5}, "outside 0..1"),
|
|
({"op": "warp", "output": "hh-a", "x": 0.5, "y": -0.1}, "outside 0..1"),
|
|
({"op": "button", "state": "down"}, "'button' must be a string"),
|
|
({"op": "button", "button": "left"}, "'state' must be"),
|
|
({"op": "button", "button": "pinky", "state": "down"}, "unknown button 'pinky'"),
|
|
({"op": "button", "button": "left", "state": "sideways"}, "unknown button state"),
|
|
({"op": "axis", "axis": "diagonal"}, "unknown axis 'diagonal'"),
|
|
({"op": "axis", "axis": 0}, "'axis' must be a string"),
|
|
({"op": "axis", "value": "a lot"}, "'value' must be a number"),
|
|
({"op": "axis", "value": True}, "'value' must be a number"),
|
|
({"op": "axis", "value": float("nan")}, "'value' must be finite"),
|
|
({"op": "axis", "value": 101}, "wheel-click limit"),
|
|
({"op": "axis", "value": -1e9}, "wheel-click limit"),
|
|
],
|
|
)
|
|
def test_malformed_requests_are_answered_not_fatal(sink, request_, fragment):
|
|
driver = Dispatcher(sink)
|
|
# Aim first, so a refusal is about the field under test rather than the aim.
|
|
warp(driver)
|
|
sink.calls.clear()
|
|
|
|
response = driver.handle(request_)
|
|
assert response["ok"] is False
|
|
assert fragment in response["error"]
|
|
assert sink.calls == [], "a refused request must emit nothing"
|
|
|
|
|
|
@pytest.mark.parametrize("request_", [[], "warp", None, 7])
|
|
def test_a_non_object_request_is_refused(driver, request_):
|
|
assert driver.handle(request_)["ok"] is False
|
|
assert "must be a JSON object" in driver.handle(request_)["error"]
|
|
|
|
|
|
def test_the_contracts_ops_are_exactly_what_is_implemented() -> None:
|
|
"""The op set cannot drift quietly.
|
|
|
|
An op that exists here and nowhere in the contract -- or the reverse --
|
|
fails this test rather than surprising whoever reads the contract next.
|
|
``axis`` is the one addition since the freeze, made deliberately so that a
|
|
session can scroll; the set is edited by hand, which is the reminder to
|
|
write it down.
|
|
"""
|
|
|
|
assert set(Dispatcher._ops) == {
|
|
"warp",
|
|
"button",
|
|
"axis",
|
|
"frame",
|
|
"pressed",
|
|
"release_all",
|
|
}
|