"""The wire shape: what a consumer is promised.""" import pyarrow as pa from dora_joy.gamepad import ABSENT, Sample from dora_joy.message import JOY, pack from tests.conftest import stick def test_a_reading_survives_the_round_trip(): packed = pack(stick(axes=[0.5, -0.5], buttons=[False, True])) row = packed[0].as_py() assert row["axes"][:2] == [0.5, -0.5] assert row["buttons"] == [False, True] assert row["present"] is True def test_an_absent_pad_keeps_the_schema(): # pyarrow infers `list` from an empty list, so a pad being unplugged # would otherwise change the type of the port halfway through a run. assert pack(ABSENT).type == JOY assert pack(stick()).type == JOY def test_an_absent_pad_says_so_rather_than_reading_as_centred(): row = pack(ABSENT)[0].as_py() assert row["present"] is False assert row["axes"] == [] def test_axes_are_float32_and_buttons_are_bool(): packed = pack(stick(axes=[1.0])) assert packed.type.field("axes").type == pa.list_(pa.float32()) assert packed.type.field("buttons").type == pa.list_(pa.bool_()) def test_a_pad_with_more_axes_than_six_is_published_whole(): # A pad whose d-pad arrives as two extra axes is exactly what the probe is # for, so nothing may be trimmed on the way out. row = pack(Sample(axes=[0.0] * 8, buttons=[False] * 12, present=True))[0].as_py() assert len(row["axes"]) == 8 assert len(row["buttons"]) == 12 def test_nothing_is_shaped_on_the_way_out(): # No deadzone here: a consumer that wants one applies its own, and two # deadzones on one number is a bug nobody can see. row = pack(stick(axes=[0.01]))[0].as_py() assert row["axes"][0] != 0.0