test_mod_ui_pipewire.py 7.3 KB

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