"""End to end: virtual pad -> padcontroller -> QLC+ (test show) -> Art-Net -> simulator. Needs the rig up (CLAUDE.md): the padcontroller and simulator services, and QLC+ running with shows/test.qxw open. It drives the live simulator, so it only runs when asked: PAD_E2E=1 .venv/bin/python -m unittest tests.test_e2e_pad -v The helper tests at the bottom run offline every time. """ from __future__ import annotations import json import os import sys import tempfile import time import unittest from pathlib import Path from urllib.error import URLError from urllib.request import urlopen ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) from padcontroller.virtualpad import PAD_CHANNEL, VirtualPad, check_test_show, last_workspace, pad_note # noqa: E402 SIMULATOR = "http://127.0.0.1:8765/api/state" SHOW = ROOT / "shows" / "test.show.json" # Colour 1 (cans) is on notes 52-67; Play shifts them to 68-83, Colour 2 (movers). RED, GREEN, BLUE, MAGENTA = 52, 57, 61, 63 RGB = {RED: (255, 0, 0), GREEN: (0, 255, 0), BLUE: (0, 0, 255), MAGENTA: (255, 0, 255)} FADE_TIMEOUT = 4.0 # the colour scenes fade in over 1 s def state() -> dict: with urlopen(SIMULATOR, timeout=2) as r: return json.load(r) def scene_fixtures(key: str) -> list[str]: """The fixtures a scene in the test show sets, e.g. colour_1_1 -> ['#1', ..., '#15'].""" spec = json.loads(SHOW.read_text()) return sorted(next(f for f in spec["functions"] if f["key"] == key)["values"]) @unittest.skipUnless(os.environ.get("PAD_E2E") == "1", "set PAD_E2E=1 to drive the live rig") class PadToSimulator(unittest.TestCase): @classmethod def setUpClass(cls) -> None: check_test_show() try: s = state() except URLError as e: raise AssertionError(f"Simulator not answering on {SIMULATOR}: {e}") from e assert s["age"] < 1.0, f"Simulator has had no Art-Net for {s['age']:.1f} s; is QLC+ running?" cls.pad = VirtualPad() @classmethod def tearDownClass(cls) -> None: cls.pad.close() def wait_for(self, fixtures: list[str], rgb: tuple[int, int, int]) -> None: """Poll the simulator until every fixture shows rgb, or fail with what it shows.""" deadline = time.monotonic() + FADE_TIMEOUT while True: got = {k: (v["r"], v["g"], v["b"]) for k, v in state()["fixtures"].items() if k in fixtures} wrong = {k: v for k, v in got.items() if v != rgb} if not wrong and len(got) == len(fixtures): return if time.monotonic() > deadline: missing = sorted(set(fixtures) - set(got)) self.fail(f"wanted {rgb} on {fixtures}; wrong: {wrong}; missing: {missing}") time.sleep(0.1) def press(self, note: int, play: bool = False) -> None: if play: with self.pad.play(): self.pad.press(note, PAD_CHANNEL) else: self.pad.press(note, PAD_CHANNEL) # Each test presses two different buttons in the solo frame. A tap on the # button that's already on would turn it off, but the second press is always # a different button, so it always turns on and sets the colour. def test_bank2_pads_set_can_colour(self) -> None: cans = scene_fixtures("colour_1_1") self.assertTrue(cans and all(k.startswith("#") for k in cans)) self.press(RED) self.press(BLUE) self.wait_for(cans, RGB[BLUE]) self.press(GREEN) self.wait_for(cans, RGB[GREEN]) def test_play_layer_sets_mover_colour(self) -> None: movers = scene_fixtures("colour_2_1") self.assertTrue(movers and all(k.startswith("M") for k in movers)) self.press(BLUE, play=True) self.press(MAGENTA, play=True) self.wait_for(movers, RGB[MAGENTA]) self.press(BLUE, play=True) self.wait_for(movers, RGB[BLUE]) class Helpers(unittest.TestCase): def test_last_workspace_reads_recent0(self) -> None: with tempfile.TemporaryDirectory() as d: conf = Path(d) / "qlc.conf" conf.write_text("[workspace]\nrecent0=/x/shows/test.qxw\nrecent1=shows/other.qxw\n") self.assertEqual(last_workspace(conf), Path("/x/shows/test.qxw")) self.assertIsNone(last_workspace(Path(d) / "missing.conf")) def test_check_test_show_refuses_other_shows(self) -> None: with tempfile.TemporaryDirectory() as d: conf = Path(d) / "qlc.conf" conf.write_text(f"[workspace]\nrecent0={ROOT / 'shows' / 'cabaret.qxw'}\n") with self.assertRaises(SystemExit): check_test_show(conf) conf.write_text(f"[workspace]\nrecent0={ROOT / 'shows' / 'test.qxw'}\n") check_test_show(conf) def test_pad_note_from_midi_map(self) -> None: self.assertEqual(pad_note(1), (9, 36)) with self.assertRaises(SystemExit): pad_note(999) if __name__ == "__main__": unittest.main()