test_mod_ui_pipewire.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import ast
  2. import hashlib
  3. import json
  4. import shutil
  5. import sys
  6. import tempfile
  7. import unittest
  8. from pathlib import Path
  9. from types import SimpleNamespace
  10. ROOT = Path(__file__).resolve().parents[1]
  11. sys.path.insert(0, str(ROOT / "build" / "arm64"))
  12. import patch_mod_ui_rigforge
  13. from mod_ui_pipewire import (
  14. boundary_connection,
  15. connect_link,
  16. disconnect_link,
  17. pedalboard_connection,
  18. resolve_pedalboard_port,
  19. )
  20. class FakePwLink:
  21. def __init__(self, links=(), transient_failures=0):
  22. self.links = set(links)
  23. self.commands = []
  24. self.transient_failures = transient_failures
  25. def __call__(self, command, **_kwargs):
  26. self.commands.append(command)
  27. if command == ["pw-link", "-l"]:
  28. output = "\n".join(
  29. f"{source}\n |-> {destination}"
  30. for source, destination in sorted(self.links)
  31. )
  32. return SimpleNamespace(returncode=0, stdout=output, stderr="")
  33. if len(command) == 3 and command[0] == "pw-link" and command[1] != "-d":
  34. if self.transient_failures:
  35. self.transient_failures -= 1
  36. return SimpleNamespace(
  37. returncode=1, stdout="", stderr="port not registered yet"
  38. )
  39. edge = (command[1], command[2])
  40. if edge in self.links:
  41. return SimpleNamespace(returncode=1, stdout="", stderr="already linked")
  42. self.links.add(edge)
  43. return SimpleNamespace(returncode=0, stdout="", stderr="")
  44. if len(command) == 4 and command[:2] == ["pw-link", "-d"]:
  45. edge = (command[2], command[3])
  46. if edge not in self.links:
  47. return SimpleNamespace(returncode=1, stdout="", stderr="not linked")
  48. self.links.remove(edge)
  49. return SimpleNamespace(returncode=0, stdout="", stderr="")
  50. raise AssertionError(f"unexpected command: {command}")
  51. class ModUiPipeWireTests(unittest.TestCase):
  52. def resolve(self, port):
  53. return {
  54. "/graph/rigforge_nam/input": "effect_0:input",
  55. "/graph/rigforge_ir/out": "effect_1:out",
  56. }[port]
  57. def test_maps_only_rigforge_boundary_edges_to_pipewire_ports(self):
  58. captures = {
  59. "capture_1": "rigforge.mod-host-input.capture:capture_AUX0",
  60. "capture_2": "rigforge.mod-host-input.capture:capture_AUX1",
  61. "capture_3": "rigforge.mod-host-input.capture:capture_AUX2",
  62. "capture_4": "rigforge.mod-host-input.capture:capture_AUX3",
  63. }
  64. playbacks = {
  65. "playback_1": "rigforge.mod-host-output.playback:playback_AUX0",
  66. "playback_2": "rigforge.mod-host-output.playback:playback_AUX1",
  67. "playback_3": "rigforge.mod-host-output.playback:playback_AUX2",
  68. "playback_4": "rigforge.mod-host-output.playback:playback_AUX3",
  69. }
  70. self.assertEqual(
  71. boundary_connection(
  72. "/graph/capture_1",
  73. "/graph/rigforge_nam/input",
  74. captures,
  75. playbacks,
  76. self.resolve,
  77. ),
  78. (captures["capture_1"], "effect_0:input"),
  79. )
  80. self.assertEqual(
  81. boundary_connection(
  82. "/graph/rigforge_ir/out",
  83. "/graph/playback_4",
  84. captures,
  85. playbacks,
  86. self.resolve,
  87. ),
  88. ("effect_1:out", playbacks["playback_4"]),
  89. )
  90. self.assertIsNone(
  91. boundary_connection(
  92. "/graph/rigforge_nam/output",
  93. "/graph/rigforge_ir/in",
  94. captures,
  95. playbacks,
  96. self.resolve,
  97. )
  98. )
  99. self.assertIsNone(
  100. boundary_connection(
  101. "/graph/capture_5",
  102. "/graph/rigforge_ir/in",
  103. captures,
  104. playbacks,
  105. self.resolve,
  106. )
  107. )
  108. def test_pipewire_connect_is_idempotent_and_disconnect_tracks_actual_link(self):
  109. edge = (
  110. "rigforge.mod-host-input.capture:capture_AUX0",
  111. "effect_0:input",
  112. )
  113. runner = FakePwLink()
  114. self.assertTrue(connect_link(*edge, runner=runner))
  115. self.assertIn(edge, runner.links)
  116. self.assertTrue(connect_link(*edge, runner=runner))
  117. self.assertTrue(disconnect_link(*edge, runner=runner))
  118. self.assertNotIn(edge, runner.links)
  119. self.assertTrue(disconnect_link(*edge, runner=runner))
  120. def test_unexpected_pw_link_failure_is_not_reported_as_success(self):
  121. def fail_connect(command, **_kwargs):
  122. return SimpleNamespace(returncode=1, stdout="", stderr="connection refused")
  123. self.assertFalse(
  124. connect_link(
  125. "rigforge.mod-host-input.capture:capture_AUX0",
  126. "effect_0:input",
  127. runner=fail_connect,
  128. retries=0,
  129. )
  130. )
  131. def test_resolves_microphone_effect_ports_to_mod_host_effect_aliases(self):
  132. resolver = lambda port: port
  133. for graph_port, expected in (
  134. ("/graph/rigforge_voice_high_pass/in", "effect_2:in"),
  135. ("/graph/rigforge_voice_compressor/out", "effect_3:out"),
  136. ("/graph/rigforge_voice_de_esser/in", "effect_4:in"),
  137. ("/graph/rigforge_voice_reverb/input_l", "effect_5:lv2_audio_in_1"),
  138. (
  139. "/graph/rigforge_voice_reverb/output_l",
  140. "effect_5:lv2_audio_out_1",
  141. ),
  142. ):
  143. self.assertEqual(resolve_pedalboard_port(graph_port, resolver), expected)
  144. self.assertEqual(
  145. resolve_pedalboard_port("/graph/rigforge_ir/out", resolver),
  146. "/graph/rigforge_ir/out",
  147. )
  148. def test_resolves_plugin_edges_and_voice_playback_boundary(self):
  149. captures = {"capture_3": "rigforge.mod-host-input.capture:capture_AUX2"}
  150. playbacks = {
  151. "playback_3": "rigforge.mod-host-output.playback:playback_AUX2"
  152. }
  153. resolver = lambda port: port
  154. self.assertEqual(
  155. pedalboard_connection(
  156. "/graph/rigforge_voice_de_esser/out",
  157. "/graph/rigforge_voice_reverb/input_l",
  158. captures,
  159. playbacks,
  160. resolver,
  161. ),
  162. ("jack", ("effect_4:out", "effect_5:lv2_audio_in_1")),
  163. )
  164. self.assertEqual(
  165. pedalboard_connection(
  166. "/graph/rigforge_voice_reverb/output_l",
  167. "/graph/playback_3",
  168. captures,
  169. playbacks,
  170. resolver,
  171. ),
  172. (
  173. "pipewire",
  174. (
  175. "effect_5:lv2_audio_out_1",
  176. "rigforge.mod-host-output.playback:playback_AUX2",
  177. ),
  178. ),
  179. )
  180. def test_connect_retries_transient_startup_port_registration_failure(self):
  181. edge = (
  182. "rigforge.mod-host-input.capture:capture_AUX0",
  183. "effect_0:input",
  184. )
  185. runner = FakePwLink(transient_failures=3)
  186. self.assertTrue(connect_link(*edge, runner=runner, retries=4, retry_interval=0))
  187. self.assertIn(edge, runner.links)
  188. self.assertEqual(runner.transient_failures, 0)
  189. def test_tracked_artifact_helper_matches_its_provenance(self):
  190. artifact = ROOT / "artifacts/mod-ui/arm64/current"
  191. helper_source = (ROOT / "build/arm64/mod_ui_pipewire.py").read_bytes()
  192. helper_artifact = artifact / "app/mod/rigforge_pipewire.py"
  193. provenance = json.loads((artifact / "provenance.json").read_text(encoding="utf-8"))
  194. self.assertEqual(helper_artifact.read_bytes(), helper_source)
  195. self.assertEqual(
  196. provenance["rigforge_pipewire_helper_sha256"],
  197. hashlib.sha256(helper_source).hexdigest(),
  198. )
  199. patch_source = (ROOT / "build/arm64/patch_mod_ui_rigforge.py").read_bytes()
  200. self.assertEqual(
  201. provenance["rigforge_integration_patch_sha256"],
  202. hashlib.sha256(patch_source).hexdigest(),
  203. )
  204. build_script = (ROOT / "build/arm64/build-mod-ui.sh").read_text(encoding="utf-8")
  205. self.assertIn(
  206. 'sha256sum "$repository_root/build/arm64/patch_mod_ui_rigforge.py"',
  207. build_script,
  208. )
  209. self.assertIn(
  210. 'sha256sum "$repository_root/build/arm64/mod_ui_pipewire.py"',
  211. build_script,
  212. )
  213. def test_repatches_artifact_connection_paths_idempotently(self):
  214. artifact = ROOT / "artifacts/mod-ui/arm64/current/app"
  215. with tempfile.TemporaryDirectory() as temporary:
  216. app = Path(temporary) / "app"
  217. for relative in (
  218. "mod/host.py",
  219. "mod/session.py",
  220. "mod/webserver.py",
  221. "server.py",
  222. "html/js/pedalboard.js",
  223. ):
  224. destination = app / relative
  225. destination.parent.mkdir(parents=True, exist_ok=True)
  226. shutil.copy2(artifact / relative, destination)
  227. host = app / "mod/host.py"
  228. contents = host.read_text(encoding="utf-8")
  229. # Exercise the upstream variant with an extra UI callback in the
  230. # connection-restoration block.
  231. old_callback = ' self.msg_callback("connect %s %s" % (port_from, port_to))'
  232. self.assertIn(old_callback, contents)
  233. contents = contents.replace(
  234. old_callback,
  235. old_callback + '\n self.msg_callback("upstream restore notice")',
  236. 1,
  237. )
  238. host.write_text(contents, encoding="utf-8")
  239. patch_mod_ui_rigforge.apply(app)
  240. once = host.read_bytes()
  241. patched_text = once.decode("utf-8")
  242. self.assertIn("def _rigforge_restore_pipewire_boundary(", patched_text)
  243. self.assertNotIn(
  244. "pipewire_link = self._rigforge_pipewire_boundary(port_from, port_to)",
  245. patched_text,
  246. )
  247. for method_name, next_method in (
  248. ("connect", "disconnect"),
  249. ("disconnect", "load"),
  250. ):
  251. start = patched_text.index(f" def {method_name}(")
  252. end = patched_text.index(f"\n def {next_method}(", start)
  253. self.assertIn("resolved = pedalboard_connection(", patched_text[start:end])
  254. load_start = patched_text.index(" def load_pb_connections(")
  255. load_end = patched_text.find("\n def ", load_start + 1)
  256. self.assertIn(
  257. "resolved = pedalboard_connection(",
  258. patched_text[load_start:load_end],
  259. )
  260. ast.parse(patched_text)
  261. patch_mod_ui_rigforge.apply(app)
  262. self.assertEqual(host.read_bytes(), once)
  263. if __name__ == "__main__":
  264. unittest.main()