| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- import ast
- import hashlib
- import json
- import shutil
- import sys
- import tempfile
- import unittest
- from pathlib import Path
- from types import SimpleNamespace
- ROOT = Path(__file__).resolve().parents[1]
- sys.path.insert(0, str(ROOT / "build" / "arm64"))
- import patch_mod_ui_rigforge
- from mod_ui_pipewire import (
- boundary_connection,
- connect_link,
- disconnect_link,
- pedalboard_connection,
- resolve_pedalboard_port,
- )
- class FakePwLink:
- def __init__(self, links=(), transient_failures=0):
- self.links = set(links)
- self.commands = []
- self.transient_failures = transient_failures
- def __call__(self, command, **_kwargs):
- self.commands.append(command)
- if command == ["pw-link", "-l"]:
- output = "\n".join(
- f"{source}\n |-> {destination}"
- for source, destination in sorted(self.links)
- )
- return SimpleNamespace(returncode=0, stdout=output, stderr="")
- if len(command) == 3 and command[0] == "pw-link" and command[1] != "-d":
- if self.transient_failures:
- self.transient_failures -= 1
- return SimpleNamespace(
- returncode=1, stdout="", stderr="port not registered yet"
- )
- edge = (command[1], command[2])
- if edge in self.links:
- return SimpleNamespace(returncode=1, stdout="", stderr="already linked")
- self.links.add(edge)
- return SimpleNamespace(returncode=0, stdout="", stderr="")
- if len(command) == 4 and command[:2] == ["pw-link", "-d"]:
- edge = (command[2], command[3])
- if edge not in self.links:
- return SimpleNamespace(returncode=1, stdout="", stderr="not linked")
- self.links.remove(edge)
- return SimpleNamespace(returncode=0, stdout="", stderr="")
- raise AssertionError(f"unexpected command: {command}")
- class ModUiPipeWireTests(unittest.TestCase):
- def resolve(self, port):
- return {
- "/graph/rigforge_nam/input": "effect_0:input",
- "/graph/rigforge_ir/out": "effect_1:out",
- }[port]
- def test_maps_only_rigforge_boundary_edges_to_pipewire_ports(self):
- captures = {
- "capture_1": "rigforge.mod-host-input.capture:capture_AUX0",
- "capture_2": "rigforge.mod-host-input.capture:capture_AUX1",
- "capture_3": "rigforge.mod-host-input.capture:capture_AUX2",
- "capture_4": "rigforge.mod-host-input.capture:capture_AUX3",
- }
- playbacks = {
- "playback_1": "rigforge.mod-host-output.playback:playback_AUX0",
- "playback_2": "rigforge.mod-host-output.playback:playback_AUX1",
- "playback_3": "rigforge.mod-host-output.playback:playback_AUX2",
- "playback_4": "rigforge.mod-host-output.playback:playback_AUX3",
- }
- self.assertEqual(
- boundary_connection(
- "/graph/capture_1",
- "/graph/rigforge_nam/input",
- captures,
- playbacks,
- self.resolve,
- ),
- (captures["capture_1"], "effect_0:input"),
- )
- self.assertEqual(
- boundary_connection(
- "capture_1",
- "/graph/rigforge_nam/input",
- captures,
- playbacks,
- self.resolve,
- ),
- (captures["capture_1"], "effect_0:input"),
- )
- self.assertEqual(
- boundary_connection(
- "/graph/rigforge_ir/out",
- "/graph/playback_4",
- captures,
- playbacks,
- self.resolve,
- ),
- ("effect_1:out", playbacks["playback_4"]),
- )
- self.assertIsNone(
- boundary_connection(
- "/graph/rigforge_nam/output",
- "/graph/rigforge_ir/in",
- captures,
- playbacks,
- self.resolve,
- )
- )
- self.assertIsNone(
- boundary_connection(
- "/graph/capture_5",
- "/graph/rigforge_ir/in",
- captures,
- playbacks,
- self.resolve,
- )
- )
- def test_pipewire_connect_is_idempotent_and_disconnect_tracks_actual_link(self):
- edge = (
- "rigforge.mod-host-input.capture:capture_AUX0",
- "effect_0:input",
- )
- runner = FakePwLink()
- self.assertTrue(connect_link(*edge, runner=runner))
- self.assertIn(edge, runner.links)
- self.assertTrue(connect_link(*edge, runner=runner))
- self.assertTrue(disconnect_link(*edge, runner=runner))
- self.assertNotIn(edge, runner.links)
- self.assertTrue(disconnect_link(*edge, runner=runner))
- def test_unexpected_pw_link_failure_is_not_reported_as_success(self):
- def fail_connect(command, **_kwargs):
- return SimpleNamespace(returncode=1, stdout="", stderr="connection refused")
- self.assertFalse(
- connect_link(
- "rigforge.mod-host-input.capture:capture_AUX0",
- "effect_0:input",
- runner=fail_connect,
- retries=0,
- )
- )
- def test_resolves_microphone_effect_ports_to_mod_host_effect_aliases(self):
- resolver = lambda port: port
- for graph_port, expected in (
- ("/graph/rigforge_voice_high_pass/in", "effect_2:in"),
- ("/graph/rigforge_voice_compressor/out", "effect_3:out"),
- ("/graph/rigforge_voice_de_esser/in", "effect_4:in"),
- ("/graph/rigforge_voice_reverb/input_l", "effect_5:lv2_audio_in_1"),
- (
- "/graph/rigforge_voice_reverb/output_l",
- "effect_5:lv2_audio_out_1",
- ),
- ):
- self.assertEqual(resolve_pedalboard_port(graph_port, resolver), expected)
- self.assertEqual(
- resolve_pedalboard_port("/graph/rigforge_ir/out", resolver),
- "effect_1:out",
- )
- self.assertEqual(
- resolve_pedalboard_port("capture_1", resolver),
- "/graph/capture_1",
- )
- def test_resolves_plugin_edges_and_voice_playback_boundary(self):
- captures = {"capture_3": "rigforge.mod-host-input.capture:capture_AUX2"}
- playbacks = {
- "playback_3": "rigforge.mod-host-output.playback:playback_AUX2"
- }
- resolver = lambda port: port
- self.assertEqual(
- pedalboard_connection(
- "/graph/rigforge_voice_de_esser/out",
- "/graph/rigforge_voice_reverb/input_l",
- captures,
- playbacks,
- resolver,
- ),
- ("jack", ("effect_4:out", "effect_5:lv2_audio_in_1")),
- )
- self.assertEqual(
- pedalboard_connection(
- "/graph/rigforge_voice_reverb/output_l",
- "/graph/playback_3",
- captures,
- playbacks,
- resolver,
- ),
- (
- "pipewire",
- (
- "effect_5:lv2_audio_out_1",
- "rigforge.mod-host-output.playback:playback_AUX2",
- ),
- ),
- )
- def test_resolves_guitar_nam_and_ir_ports_without_jack_alias_guessing(self):
- def fail_if_called(_port):
- raise AssertionError("known Rigforge plugin port should resolve explicitly")
- self.assertEqual(
- resolve_pedalboard_port("/graph/rigforge_nam/input", fail_if_called),
- "effect_0:input",
- )
- self.assertEqual(
- resolve_pedalboard_port("/graph/rigforge_nam/output", fail_if_called),
- "effect_0:output",
- )
- self.assertEqual(
- resolve_pedalboard_port("/graph/rigforge_ir/in", fail_if_called),
- "effect_1:in",
- )
- self.assertEqual(
- resolve_pedalboard_port("/graph/rigforge_ir/out", fail_if_called),
- "effect_1:out",
- )
- self.assertEqual(
- pedalboard_connection(
- "/graph/capture_1",
- "/graph/rigforge_nam/input",
- {"capture_1": "rigforge.mod-host-input.capture:capture_AUX0"},
- {},
- fail_if_called,
- ),
- (
- "pipewire",
- ("rigforge.mod-host-input.capture:capture_AUX0", "effect_0:input"),
- ),
- )
- # mod-ui's HTTP connect handler strips "/graph/" from capture_1,
- # so restoration can call the host bridge with this bare symbol.
- self.assertEqual(
- pedalboard_connection(
- "capture_1",
- "/graph/rigforge_nam/input",
- {"capture_1": "rigforge.mod-host-input.capture:capture_AUX0"},
- {},
- fail_if_called,
- ),
- (
- "pipewire",
- ("rigforge.mod-host-input.capture:capture_AUX0", "effect_0:input"),
- ),
- )
- self.assertEqual(
- pedalboard_connection(
- "/graph/rigforge_ir/out",
- "/graph/playback_1",
- {},
- {"playback_1": "rigforge.mod-host-output.playback:playback_AUX0"},
- fail_if_called,
- ),
- (
- "pipewire",
- ("effect_1:out", "rigforge.mod-host-output.playback:playback_AUX0"),
- ),
- )
- self.assertEqual(
- pedalboard_connection(
- "/graph/rigforge_ir/out",
- "playback_1",
- {},
- {"playback_1": "rigforge.mod-host-output.playback:playback_AUX0"},
- fail_if_called,
- ),
- (
- "pipewire",
- ("effect_1:out", "rigforge.mod-host-output.playback:playback_AUX0"),
- ),
- )
- def test_connect_retries_transient_startup_port_registration_failure(self):
- edge = (
- "rigforge.mod-host-input.capture:capture_AUX0",
- "effect_0:input",
- )
- runner = FakePwLink(transient_failures=3)
- self.assertTrue(connect_link(*edge, runner=runner, retries=4, retry_interval=0))
- self.assertIn(edge, runner.links)
- self.assertEqual(runner.transient_failures, 0)
- def test_tracked_artifact_helper_matches_its_provenance(self):
- artifact = ROOT / "artifacts/mod-ui/arm64/current"
- helper_source = (ROOT / "build/arm64/mod_ui_pipewire.py").read_bytes()
- helper_artifact = artifact / "app/mod/rigforge_pipewire.py"
- provenance = json.loads((artifact / "provenance.json").read_text(encoding="utf-8"))
- self.assertEqual(helper_artifact.read_bytes(), helper_source)
- self.assertEqual(
- provenance["rigforge_pipewire_helper_sha256"],
- hashlib.sha256(helper_source).hexdigest(),
- )
- patch_source = (ROOT / "build/arm64/patch_mod_ui_rigforge.py").read_bytes()
- self.assertEqual(
- provenance["rigforge_integration_patch_sha256"],
- hashlib.sha256(patch_source).hexdigest(),
- )
- build_script = (ROOT / "build/arm64/build-mod-ui.sh").read_text(encoding="utf-8")
- self.assertIn(
- 'sha256sum "$repository_root/build/arm64/patch_mod_ui_rigforge.py"',
- build_script,
- )
- self.assertIn(
- 'sha256sum "$repository_root/build/arm64/mod_ui_pipewire.py"',
- build_script,
- )
- def test_repatches_artifact_connection_paths_idempotently(self):
- artifact = ROOT / "artifacts/mod-ui/arm64/current/app"
- with tempfile.TemporaryDirectory() as temporary:
- app = Path(temporary) / "app"
- for relative in (
- "mod/host.py",
- "mod/session.py",
- "mod/webserver.py",
- "server.py",
- "html/js/pedalboard.js",
- ):
- destination = app / relative
- destination.parent.mkdir(parents=True, exist_ok=True)
- shutil.copy2(artifact / relative, destination)
- host = app / "mod/host.py"
- contents = host.read_text(encoding="utf-8")
- # Exercise the upstream variant with an extra UI callback in the
- # connection-restoration block.
- old_callback = ' self.msg_callback("connect %s %s" % (port_from, port_to))'
- self.assertIn(old_callback, contents)
- contents = contents.replace(
- old_callback,
- old_callback + '\n self.msg_callback("upstream restore notice")',
- 1,
- )
- host.write_text(contents, encoding="utf-8")
- patch_mod_ui_rigforge.apply(app)
- once = host.read_bytes()
- patched_text = once.decode("utf-8")
- self.assertIn("def _rigforge_restore_pipewire_boundary(", patched_text)
- fix_start = patched_text.index(" def _fix_host_connection_port(")
- fix_end = patched_text.index("\n def ", fix_start + 8)
- resolver_method = patched_text[fix_start:fix_end]
- self.assertIn(
- 'if len(data) == 2 and data[0] == ""',
- resolver_method,
- )
- self.assertNotIn(
- "Rigforge bare boundary port alias",
- patched_text[:fix_start],
- )
- restore_start = patched_text.index(
- " def _rigforge_restore_pipewire_boundary("
- )
- restore_end = patched_text.index(
- "\n def load_pb_connections(", restore_start
- )
- restore_method = patched_text[restore_start:restore_end]
- self.assertIn("if connect_link(*pipewire_link):", restore_method)
- self.assertNotIn(
- "pipewire_link = self._rigforge_pipewire_boundary(port_from, port_to)",
- patched_text,
- )
- for method_name, next_method in (
- ("connect", "disconnect"),
- ("disconnect", "load"),
- ):
- start = patched_text.index(f" def {method_name}(")
- end = patched_text.index(f"\n def {next_method}(", start)
- self.assertIn("resolved = pedalboard_connection(", patched_text[start:end])
- load_start = patched_text.index(" def load_pb_connections(")
- load_end = patched_text.find("\n def ", load_start + 1)
- self.assertIn(
- "resolved = pedalboard_connection(",
- patched_text[load_start:load_end],
- )
- ast.parse(patched_text)
- patch_mod_ui_rigforge.apply(app)
- self.assertEqual(host.read_bytes(), once)
- if __name__ == "__main__":
- unittest.main()
|