"""The pad as it arrives on the wire, and what silence on that wire means.""" import pyarrow as pa from dora_teleop.joy import ABSENT, JoyInput, Sample, decode from tests.conftest import input_event, joy_message, stick def joy_event(sample): return input_event("joy", joy_message(sample)) def test_a_reading_survives_the_round_trip(): sample = decode(joy_event(stick(axes=[0.5, -0.5], buttons=[False, True]))) assert sample.axes[:2] == [0.5, -0.5] assert sample.buttons == [False, True] assert sample.present is True def test_an_absent_pad_decodes_as_absent_not_as_centred(): sample = decode(joy_event(ABSENT)) assert sample.present is False assert sample.axes == [] def test_a_pad_with_fewer_axes_than_the_layout_decodes_without_complaint(): # A three-axis flight stick is a strange thing to drive an arm with, but it # must not take the node down; `mapping.axis` treats a missing one as centred. sample = decode(joy_event(Sample(axes=[0.0, 0.0, 0.0], buttons=[False], present=True))) assert len(sample.axes) == 3 def test_a_message_with_no_rows_reads_as_absent(): empty = input_event("joy", pa.array([], type=joy_message(ABSENT).type)) assert decode(empty) is ABSENT def test_a_fresh_reading_is_handed_back(): joy = JoyInput(ttl_s=0.2) sample = stick(axes=[0.5]) joy.accept(sample, 100.0) assert joy.read(100.1) is sample def test_a_reading_nobody_is_refreshing_reads_as_absent(): # dora-joy publishes every tick, so silence means it died — and a dead pad # node is indistinguishable from an unplugged pad as far as the arm cares. joy = JoyInput(ttl_s=0.2) joy.accept(stick(axes=[0.5]), 100.0) assert joy.read(100.5) is ABSENT def test_nothing_ever_arriving_reads_as_absent(): # A dataflow with no `joy` wired at all, which is a browser-only console. assert JoyInput(ttl_s=0.2).read(100.0) is ABSENT