test_mod_ui_pipewire.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import hashlib
  2. import json
  3. import sys
  4. import unittest
  5. from pathlib import Path
  6. from types import SimpleNamespace
  7. ROOT = Path(__file__).resolve().parents[1]
  8. sys.path.insert(0, str(ROOT / "build" / "arm64"))
  9. from mod_ui_pipewire import (
  10. boundary_connection,
  11. connect_link,
  12. disconnect_link,
  13. pedalboard_connection,
  14. resolve_pedalboard_port,
  15. )
  16. class FakePwLink:
  17. def __init__(self, links=(), transient_failures=0):
  18. self.links = set(links)
  19. self.commands = []
  20. self.transient_failures = transient_failures
  21. def __call__(self, command, **_kwargs):
  22. self.commands.append(command)
  23. if command == ["pw-link", "-l"]:
  24. output = "\n".join(
  25. f"{source}\n |-> {destination}"
  26. for source, destination in sorted(self.links)
  27. )
  28. return SimpleNamespace(returncode=0, stdout=output, stderr="")
  29. if len(command) == 3 and command[0] == "pw-link" and command[1] != "-d":
  30. if self.transient_failures:
  31. self.transient_failures -= 1
  32. return SimpleNamespace(
  33. returncode=1, stdout="", stderr="port not registered yet"
  34. )
  35. edge = (command[1], command[2])
  36. if edge in self.links:
  37. return SimpleNamespace(returncode=1, stdout="", stderr="already linked")
  38. self.links.add(edge)
  39. return SimpleNamespace(returncode=0, stdout="", stderr="")
  40. if len(command) == 4 and command[:2] == ["pw-link", "-d"]:
  41. edge = (command[2], command[3])
  42. if edge not in self.links:
  43. return SimpleNamespace(returncode=1, stdout="", stderr="not linked")
  44. self.links.remove(edge)
  45. return SimpleNamespace(returncode=0, stdout="", stderr="")
  46. raise AssertionError(f"unexpected command: {command}")
  47. class ModUiPipeWireTests(unittest.TestCase):
  48. def resolve(self, port):
  49. return {
  50. "/graph/rigforge_nam/input": "effect_0:input",
  51. "/graph/rigforge_ir/out": "effect_1:out",
  52. }[port]
  53. def test_maps_only_rigforge_boundary_edges_to_pipewire_ports(self):
  54. captures = {
  55. "capture_1": "rigforge.mod-host-input.capture:capture_AUX0",
  56. "capture_2": "rigforge.mod-host-input.capture:capture_AUX1",
  57. "capture_3": "rigforge.mod-host-input.capture:capture_AUX2",
  58. "capture_4": "rigforge.mod-host-input.capture:capture_AUX3",
  59. }
  60. playbacks = {
  61. "playback_1": "rigforge.mod-host-output.playback:playback_AUX0",
  62. "playback_2": "rigforge.mod-host-output.playback:playback_AUX1",
  63. "playback_3": "rigforge.mod-host-output.playback:playback_AUX2",
  64. "playback_4": "rigforge.mod-host-output.playback:playback_AUX3",
  65. }
  66. self.assertEqual(
  67. boundary_connection(
  68. "/graph/capture_1",
  69. "/graph/rigforge_nam/input",
  70. captures,
  71. playbacks,
  72. self.resolve,
  73. ),
  74. (captures["capture_1"], "effect_0:input"),
  75. )
  76. self.assertEqual(
  77. boundary_connection(
  78. "/graph/rigforge_ir/out",
  79. "/graph/playback_4",
  80. captures,
  81. playbacks,
  82. self.resolve,
  83. ),
  84. ("effect_1:out", playbacks["playback_4"]),
  85. )
  86. self.assertIsNone(
  87. boundary_connection(
  88. "/graph/rigforge_nam/output",
  89. "/graph/rigforge_ir/in",
  90. captures,
  91. playbacks,
  92. self.resolve,
  93. )
  94. )
  95. self.assertIsNone(
  96. boundary_connection(
  97. "/graph/capture_5",
  98. "/graph/rigforge_ir/in",
  99. captures,
  100. playbacks,
  101. self.resolve,
  102. )
  103. )
  104. def test_pipewire_connect_is_idempotent_and_disconnect_tracks_actual_link(self):
  105. edge = (
  106. "rigforge.mod-host-input.capture:capture_AUX0",
  107. "effect_0:input",
  108. )
  109. runner = FakePwLink()
  110. self.assertTrue(connect_link(*edge, runner=runner))
  111. self.assertIn(edge, runner.links)
  112. self.assertTrue(connect_link(*edge, runner=runner))
  113. self.assertTrue(disconnect_link(*edge, runner=runner))
  114. self.assertNotIn(edge, runner.links)
  115. self.assertTrue(disconnect_link(*edge, runner=runner))
  116. def test_unexpected_pw_link_failure_is_not_reported_as_success(self):
  117. def fail_connect(command, **_kwargs):
  118. return SimpleNamespace(returncode=1, stdout="", stderr="connection refused")
  119. self.assertFalse(
  120. connect_link(
  121. "rigforge.mod-host-input.capture:capture_AUX0",
  122. "effect_0:input",
  123. runner=fail_connect,
  124. retries=0,
  125. )
  126. )
  127. def test_resolves_microphone_effect_ports_to_mod_host_effect_aliases(self):
  128. resolver = lambda port: port
  129. for graph_port, expected in (
  130. ("/graph/rigforge_voice_high_pass/in", "effect_2:in"),
  131. ("/graph/rigforge_voice_compressor/out", "effect_3:out"),
  132. ("/graph/rigforge_voice_de_esser/in", "effect_4:in"),
  133. ("/graph/rigforge_voice_reverb/input_l", "effect_5:lv2_audio_in_1"),
  134. (
  135. "/graph/rigforge_voice_reverb/output_l",
  136. "effect_5:lv2_audio_out_1",
  137. ),
  138. ):
  139. self.assertEqual(resolve_pedalboard_port(graph_port, resolver), expected)
  140. self.assertEqual(
  141. resolve_pedalboard_port("/graph/rigforge_ir/out", resolver),
  142. "/graph/rigforge_ir/out",
  143. )
  144. def test_resolves_plugin_edges_and_voice_playback_boundary(self):
  145. captures = {"capture_3": "rigforge.mod-host-input.capture:capture_AUX2"}
  146. playbacks = {
  147. "playback_3": "rigforge.mod-host-output.playback:playback_AUX2"
  148. }
  149. resolver = lambda port: port
  150. self.assertEqual(
  151. pedalboard_connection(
  152. "/graph/rigforge_voice_de_esser/out",
  153. "/graph/rigforge_voice_reverb/input_l",
  154. captures,
  155. playbacks,
  156. resolver,
  157. ),
  158. ("jack", ("effect_4:out", "effect_5:lv2_audio_in_1")),
  159. )
  160. self.assertEqual(
  161. pedalboard_connection(
  162. "/graph/rigforge_voice_reverb/output_l",
  163. "/graph/playback_3",
  164. captures,
  165. playbacks,
  166. resolver,
  167. ),
  168. (
  169. "pipewire",
  170. (
  171. "effect_5:lv2_audio_out_1",
  172. "rigforge.mod-host-output.playback:playback_AUX2",
  173. ),
  174. ),
  175. )
  176. def test_connect_retries_transient_startup_port_registration_failure(self):
  177. edge = (
  178. "rigforge.mod-host-input.capture:capture_AUX0",
  179. "effect_0:input",
  180. )
  181. runner = FakePwLink(transient_failures=3)
  182. self.assertTrue(connect_link(*edge, runner=runner, retries=4, retry_interval=0))
  183. self.assertIn(edge, runner.links)
  184. self.assertEqual(runner.transient_failures, 0)
  185. def test_tracked_mod_ui_artifact_matches_its_rebuild_patch_and_provenance(self):
  186. artifact = ROOT / "artifacts/mod-ui/arm64/current"
  187. helper_source = (ROOT / "build/arm64/mod_ui_pipewire.py").read_bytes()
  188. helper_artifact = artifact / "app/mod/rigforge_pipewire.py"
  189. host_source = (artifact / "app/mod/host.py").read_text(encoding="utf-8")
  190. provenance = json.loads((artifact / "provenance.json").read_text(encoding="utf-8"))
  191. self.assertEqual(helper_artifact.read_bytes(), helper_source)
  192. self.assertIn("from mod.rigforge_pipewire import", host_source)
  193. self.assertIn("def _rigforge_restore_pipewire_boundary(", host_source)
  194. self.assertIn("IOLoop.instance().call_later(", host_source)
  195. self.assertIn("attempts_left=60", host_source)
  196. self.assertNotIn("retries=60, retry_interval=0.1", host_source)
  197. self.assertEqual(host_source.count("PipeWire backend failed to restore ports"), 1)
  198. self.assertIn("disconnect_link(*pipewire_link)", host_source)
  199. self.assertEqual(
  200. provenance["rigforge_integration_patch_sha256"],
  201. hashlib.sha256((ROOT / "build/arm64/patch_mod_ui_rigforge.py").read_bytes()).hexdigest(),
  202. )
  203. self.assertEqual(
  204. provenance["rigforge_pipewire_helper_sha256"],
  205. hashlib.sha256(helper_source).hexdigest(),
  206. )
  207. if __name__ == "__main__":
  208. unittest.main()