Files
qlcplus/tests/test_padcontroller_midi.py
jimmyandClaude Opus 5.5 cf94c7b1ba Press the colours again when a restarted QLC+ links itself to the pad controller.
The pad controller only took a QLC+ reload as one when it had to make the
link to QLC+ itself. A restarted QLC+ often subscribes to the virtual port on
its own as it opens the show, before the next check, so the colours stayed off.
Each check now also notes the QLC+ process and port it feeds (qlc_feeds), and a
new one counts as a reload too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:57:22 +13:00

94 lines
3.0 KiB
Python

"""padcontroller.midi: picking the pad's port and keeping the ALSA links up."""
from __future__ import annotations
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
from padcontroller.midi import missing_links, parse_aconnect, pick_source, qlc_feeds # noqa: E402
BOTH = [
"Midi Through:Midi Through Port-0 14:0",
"SINCO:SINCO SMC-PAD-Private 20:0",
"SINCO:SINCO SMC-PAD-Master 20:1",
"SMC-PAD:SMC-PAD Bluetooth 128:0",
"padcontroller-out:padcontroller 130:0",
]
ACONNECT = """\
client 20: 'SINCO' [type=kernel,card=1]
0 'SINCO SMC-PAD-Private'
1 'SINCO SMC-PAD-Master'
\tConnecting To: 129:0
client 128: 'SMC-PAD' [type=user,pid=1153]
0 'SMC-PAD Bluetooth'
client 129: 'padcontroller-in' [type=user,pid=57888]
0 'RtMidi input '
\tConnected From: 20:1
client 130: 'padcontroller-out' [type=user,pid=57888]
0 'padcontroller '
client 131: 'qlcplus' [type=user,pid=57635]
0 '__QLC__ '
"""
def test_bluetooth_wins_over_usb():
assert BOTH[pick_source(BOTH)] == "SMC-PAD:SMC-PAD Bluetooth 128:0"
def test_usb_when_not_paired():
names = [n for n in BOTH if "Bluetooth" not in n]
assert names[pick_source(names)] == "SINCO:SINCO SMC-PAD-Master 20:1"
def test_no_pad_and_never_our_own_port():
assert pick_source(["padcontroller-out:padcontroller 130:0"]) is None
def test_parse_aconnect_reads_links():
ports = {p.addr: p for p in parse_aconnect(ACONNECT)}
assert ports["20:1"].to == {"129:0"}
assert ports["129:0"].sources == {"20:1"}
assert ports["131:0"].client == "qlcplus"
def test_relinks_qlc_after_a_restart():
ports = parse_aconnect(ACONNECT)
assert missing_links(ports, "SINCO:SINCO SMC-PAD-Master 20:1") == [("130:0", "131:0")]
def test_relinks_a_replugged_pad_and_only_that_one():
ports = parse_aconnect(ACONNECT.replace("\tConnected From: 20:1\n", ""))
links = missing_links(ports, "SINCO:SINCO SMC-PAD-Master 20:1")
assert ("20:1", "129:0") in links
assert not any(src == "128:0" for src, _ in links)
def test_nothing_missing_when_linked():
text = ACONNECT.replace(" 0 'padcontroller '\n", " 0 'padcontroller '\n\tConnecting To: 131:0\n")
assert missing_links(parse_aconnect(text), "SINCO:SINCO SMC-PAD-Master 20:1") == []
assert missing_links(parse_aconnect(text), None) == []
LINKED = ACONNECT.replace(" 0 'padcontroller '\n", " 0 'padcontroller '\n\tConnecting To: 131:0\n")
def test_parse_aconnect_reads_pids():
ports = {p.addr: p for p in parse_aconnect(ACONNECT)}
assert ports["131:0"].pid == "57635"
assert ports["20:0"].pid is None # a kernel client
def test_qlc_feeds_names_the_qlc_process():
assert qlc_feeds(parse_aconnect(LINKED)) == {("57635", "131:0")}
assert qlc_feeds(parse_aconnect(ACONNECT)) == set()
def test_a_restarted_qlc_that_linked_itself_is_a_new_feed():
before = qlc_feeds(parse_aconnect(LINKED))
after = qlc_feeds(parse_aconnect(LINKED.replace("pid=57635", "pid=60001")))
assert after - before == {("60001", "131:0")}