test_mod_ui_pipewire.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. "capture_1",
  83. "/graph/rigforge_nam/input",
  84. captures,
  85. playbacks,
  86. self.resolve,
  87. ),
  88. (captures["capture_1"], "effect_0:input"),
  89. )
  90. self.assertEqual(
  91. boundary_connection(
  92. "/graph/rigforge_ir/out",
  93. "/graph/playback_4",
  94. captures,
  95. playbacks,
  96. self.resolve,
  97. ),
  98. ("effect_1:out", playbacks["playback_4"]),
  99. )
  100. self.assertIsNone(
  101. boundary_connection(
  102. "/graph/rigforge_nam/output",
  103. "/graph/rigforge_ir/in",
  104. captures,
  105. playbacks,
  106. self.resolve,
  107. )
  108. )
  109. self.assertIsNone(
  110. boundary_connection(
  111. "/graph/capture_5",
  112. "/graph/rigforge_ir/in",
  113. captures,
  114. playbacks,
  115. self.resolve,
  116. )
  117. )
  118. def test_pipewire_connect_is_idempotent_and_disconnect_tracks_actual_link(self):
  119. edge = (
  120. "rigforge.mod-host-input.capture:capture_AUX0",
  121. "effect_0:input",
  122. )
  123. runner = FakePwLink()
  124. self.assertTrue(connect_link(*edge, runner=runner))
  125. self.assertIn(edge, runner.links)
  126. self.assertTrue(connect_link(*edge, runner=runner))
  127. self.assertTrue(disconnect_link(*edge, runner=runner))
  128. self.assertNotIn(edge, runner.links)
  129. self.assertTrue(disconnect_link(*edge, runner=runner))
  130. def test_unexpected_pw_link_failure_is_not_reported_as_success(self):
  131. def fail_connect(command, **_kwargs):
  132. return SimpleNamespace(returncode=1, stdout="", stderr="connection refused")
  133. self.assertFalse(
  134. connect_link(
  135. "rigforge.mod-host-input.capture:capture_AUX0",
  136. "effect_0:input",
  137. runner=fail_connect,
  138. retries=0,
  139. )
  140. )
  141. def test_resolves_microphone_effect_ports_to_mod_host_effect_aliases(self):
  142. resolver = lambda port: port
  143. for graph_port, expected in (
  144. ("/graph/rigforge_voice_high_pass/in", "effect_2:in"),
  145. ("/graph/rigforge_voice_compressor/out", "effect_3:out"),
  146. ("/graph/rigforge_voice_de_esser/in", "effect_4:in"),
  147. ("/graph/rigforge_voice_reverb/input_l", "effect_5:lv2_audio_in_1"),
  148. (
  149. "/graph/rigforge_voice_reverb/output_l",
  150. "effect_5:lv2_audio_out_1",
  151. ),
  152. ):
  153. self.assertEqual(resolve_pedalboard_port(graph_port, resolver), expected)
  154. self.assertEqual(
  155. resolve_pedalboard_port("/graph/rigforge_ir/out", resolver),
  156. "effect_1:out",
  157. )
  158. self.assertEqual(
  159. resolve_pedalboard_port("capture_1", resolver),
  160. "/graph/capture_1",
  161. )
  162. def test_resolves_plugin_edges_and_voice_playback_boundary(self):
  163. captures = {"capture_3": "rigforge.mod-host-input.capture:capture_AUX2"}
  164. playbacks = {
  165. "playback_3": "rigforge.mod-host-output.playback:playback_AUX2"
  166. }
  167. resolver = lambda port: port
  168. self.assertEqual(
  169. pedalboard_connection(
  170. "/graph/rigforge_voice_de_esser/out",
  171. "/graph/rigforge_voice_reverb/input_l",
  172. captures,
  173. playbacks,
  174. resolver,
  175. ),
  176. ("jack", ("effect_4:out", "effect_5:lv2_audio_in_1")),
  177. )
  178. self.assertEqual(
  179. pedalboard_connection(
  180. "/graph/rigforge_voice_reverb/output_l",
  181. "/graph/playback_3",
  182. captures,
  183. playbacks,
  184. resolver,
  185. ),
  186. (
  187. "pipewire",
  188. (
  189. "effect_5:lv2_audio_out_1",
  190. "rigforge.mod-host-output.playback:playback_AUX2",
  191. ),
  192. ),
  193. )
  194. def test_boundary_failures_never_fall_back_to_guessed_jack_ports(self):
  195. def resolve(port):
  196. return f"system:{port.rsplit('/', 1)[-1]}"
  197. self.assertIsNone(
  198. pedalboard_connection(
  199. "/graph/capture_3",
  200. "/graph/rigforge_voice_high_pass/in",
  201. {"capture_3": "rigforge.mod-host-input.capture:capture_AUX2"},
  202. {},
  203. resolve,
  204. )
  205. )
  206. self.assertIsNone(
  207. pedalboard_connection(
  208. "/graph/rigforge_voice_de_esser/out",
  209. "/graph/rigforge_voice_reverb/input_l",
  210. {},
  211. {},
  212. resolve,
  213. )
  214. )
  215. self.assertIsNone(
  216. pedalboard_connection(
  217. "/capture_3",
  218. "/graph/rigforge_voice_high_pass/in",
  219. {"capture_3": "rigforge.mod-host-input.capture:capture_AUX2"},
  220. {},
  221. resolve,
  222. )
  223. )
  224. def test_voice_plugin_connections_resolve_to_the_loaded_plugin_ids(self):
  225. self.assertEqual(
  226. pedalboard_connection(
  227. "/graph/rigforge_voice_de_esser/out",
  228. "/graph/rigforge_voice_reverb/input_l",
  229. {},
  230. {},
  231. lambda _port: self.fail("Rigforge voice ports should be explicit"),
  232. ),
  233. ("jack", ("effect_4:out", "effect_5:lv2_audio_in_1")),
  234. )
  235. def test_resolver_preserves_capture_symbols_outside_first_two_channels(self):
  236. resolver = lambda port: port
  237. self.assertEqual(
  238. resolve_pedalboard_port("/graph/capture_3", resolver),
  239. "/graph/capture_3",
  240. )
  241. self.assertEqual(
  242. resolve_pedalboard_port("/graph/playback_4", resolver),
  243. "/graph/playback_4",
  244. )
  245. def test_resolves_guitar_nam_and_ir_ports_without_jack_alias_guessing(self):
  246. def fail_if_called(_port):
  247. raise AssertionError("known Rigforge plugin port should resolve explicitly")
  248. self.assertEqual(
  249. resolve_pedalboard_port("/graph/rigforge_nam/input", fail_if_called),
  250. "effect_0:input",
  251. )
  252. self.assertEqual(
  253. resolve_pedalboard_port("/graph/rigforge_nam/output", fail_if_called),
  254. "effect_0:output",
  255. )
  256. self.assertEqual(
  257. resolve_pedalboard_port("/graph/rigforge_ir/in", fail_if_called),
  258. "effect_1:in",
  259. )
  260. self.assertEqual(
  261. resolve_pedalboard_port("/graph/rigforge_ir/out", fail_if_called),
  262. "effect_1:out",
  263. )
  264. self.assertEqual(
  265. pedalboard_connection(
  266. "/graph/capture_1",
  267. "/graph/rigforge_nam/input",
  268. {"capture_1": "rigforge.mod-host-input.capture:capture_AUX0"},
  269. {},
  270. fail_if_called,
  271. ),
  272. (
  273. "pipewire",
  274. ("rigforge.mod-host-input.capture:capture_AUX0", "effect_0:input"),
  275. ),
  276. )
  277. # mod-ui's HTTP connect handler strips "/graph/" from capture_1,
  278. # so restoration can call the host bridge with this bare symbol.
  279. self.assertEqual(
  280. pedalboard_connection(
  281. "capture_1",
  282. "/graph/rigforge_nam/input",
  283. {"capture_1": "rigforge.mod-host-input.capture:capture_AUX0"},
  284. {},
  285. fail_if_called,
  286. ),
  287. (
  288. "pipewire",
  289. ("rigforge.mod-host-input.capture:capture_AUX0", "effect_0:input"),
  290. ),
  291. )
  292. self.assertEqual(
  293. pedalboard_connection(
  294. "/graph/rigforge_ir/out",
  295. "/graph/playback_1",
  296. {},
  297. {"playback_1": "rigforge.mod-host-output.playback:playback_AUX0"},
  298. fail_if_called,
  299. ),
  300. (
  301. "pipewire",
  302. ("effect_1:out", "rigforge.mod-host-output.playback:playback_AUX0"),
  303. ),
  304. )
  305. self.assertEqual(
  306. pedalboard_connection(
  307. "/graph/rigforge_ir/out",
  308. "playback_1",
  309. {},
  310. {"playback_1": "rigforge.mod-host-output.playback:playback_AUX0"},
  311. fail_if_called,
  312. ),
  313. (
  314. "pipewire",
  315. ("effect_1:out", "rigforge.mod-host-output.playback:playback_AUX0"),
  316. ),
  317. )
  318. def test_connect_retries_transient_startup_port_registration_failure(self):
  319. edge = (
  320. "rigforge.mod-host-input.capture:capture_AUX0",
  321. "effect_0:input",
  322. )
  323. runner = FakePwLink(transient_failures=3)
  324. self.assertTrue(connect_link(*edge, runner=runner, retries=4, retry_interval=0))
  325. self.assertIn(edge, runner.links)
  326. self.assertEqual(runner.transient_failures, 0)
  327. def test_tracked_artifact_helper_matches_its_provenance(self):
  328. artifact = ROOT / "artifacts/mod-ui/arm64/current"
  329. helper_source = (ROOT / "build/arm64/mod_ui_pipewire.py").read_bytes()
  330. helper_artifact = artifact / "app/mod/rigforge_pipewire.py"
  331. provenance = json.loads((artifact / "provenance.json").read_text(encoding="utf-8"))
  332. self.assertEqual(helper_artifact.read_bytes(), helper_source)
  333. self.assertEqual(
  334. provenance["rigforge_pipewire_helper_sha256"],
  335. hashlib.sha256(helper_source).hexdigest(),
  336. )
  337. patch_source = (ROOT / "build/arm64/patch_mod_ui_rigforge.py").read_bytes()
  338. self.assertEqual(
  339. provenance["rigforge_integration_patch_sha256"],
  340. hashlib.sha256(patch_source).hexdigest(),
  341. )
  342. build_script = (ROOT / "build/arm64/build-mod-ui.sh").read_text(encoding="utf-8")
  343. self.assertIn(
  344. 'sha256sum "$repository_root/build/arm64/patch_mod_ui_rigforge.py"',
  345. build_script,
  346. )
  347. self.assertIn(
  348. 'sha256sum "$repository_root/build/arm64/mod_ui_pipewire.py"',
  349. build_script,
  350. )
  351. def test_repatches_artifact_connection_paths_idempotently(self):
  352. artifact = ROOT / "artifacts/mod-ui/arm64/current/app"
  353. with tempfile.TemporaryDirectory() as temporary:
  354. app = Path(temporary) / "app"
  355. for relative in (
  356. "mod/host.py",
  357. "mod/session.py",
  358. "mod/webserver.py",
  359. "server.py",
  360. "html/js/pedalboard.js",
  361. ):
  362. destination = app / relative
  363. destination.parent.mkdir(parents=True, exist_ok=True)
  364. shutil.copy2(artifact / relative, destination)
  365. host = app / "mod/host.py"
  366. contents = host.read_text(encoding="utf-8")
  367. # Exercise the upstream variant with an extra UI callback in the
  368. # connection-restoration block.
  369. old_callback = ' self.msg_callback("connect %s %s" % (port_from, port_to))'
  370. self.assertIn(old_callback, contents)
  371. contents = contents.replace(
  372. old_callback,
  373. old_callback + '\n self.msg_callback("upstream restore notice")',
  374. 1,
  375. )
  376. host.write_text(contents, encoding="utf-8")
  377. patch_mod_ui_rigforge.apply(app)
  378. once = host.read_bytes()
  379. patched_text = once.decode("utf-8")
  380. self.assertIn("def _rigforge_restore_pipewire_boundary(", patched_text)
  381. self.assertIn(
  382. '"capture_%d" % index in self.audioportsIn',
  383. patched_text,
  384. )
  385. self.assertIn(
  386. '"playback_%d" % index in self.audioportsOut',
  387. patched_text,
  388. )
  389. fix_start = patched_text.index(" def _fix_host_connection_port(")
  390. fix_end = patched_text.index("\n def ", fix_start + 8)
  391. resolver_method = patched_text[fix_start:fix_end]
  392. self.assertIn(
  393. "if port in self.rigforge_capture_ports:",
  394. resolver_method,
  395. )
  396. self.assertIn(
  397. "def is_rigforge_managed_boundary_port(port):",
  398. (ROOT / "build/arm64/mod_ui_pipewire.py").read_text(encoding="utf-8"),
  399. )
  400. self.assertIn(
  401. 'if len(data) == 2 and data[0] == ""',
  402. resolver_method,
  403. )
  404. self.assertNotIn(
  405. "Rigforge bare boundary port alias",
  406. patched_text[:fix_start],
  407. )
  408. restore_start = patched_text.index(
  409. " def _rigforge_restore_pipewire_boundary("
  410. )
  411. restore_end = patched_text.index(
  412. "\n def load_pb_connections(", restore_start
  413. )
  414. restore_method = patched_text[restore_start:restore_end]
  415. self.assertIn("if ready and connect_link(*pipewire_link):", restore_method)
  416. self.assertIn('["pw-link", "-o"]', restore_method)
  417. self.assertIn("edge = (port_from, port_to)", restore_method)
  418. self.assertIn("if edge in self.connections:", restore_method)
  419. self.assertNotIn("connections_now", restore_method)
  420. self.assertIn(
  421. "self._rigforge_restore_pipewire_boundary,\n"
  422. " pipewire_link,\n"
  423. " port_from,\n"
  424. " port_to,\n"
  425. " attempts_left - 1",
  426. restore_method,
  427. )
  428. self.assertNotIn(
  429. " connections_now,\n attempts_left - 1",
  430. restore_method,
  431. )
  432. self.assertIn("if edge not in self.connections:", restore_method)
  433. self.assertNotIn("self.connections is not connection_list", restore_method)
  434. self.assertNotIn("len(self.audioportsIn) ==", restore_method)
  435. self.assertNotIn("len(self.audioportsOut) ==", restore_method)
  436. self.assertIn(
  437. " attempts_left - 1\n )",
  438. patched_text[restore_start:restore_end],
  439. )
  440. self.assertNotIn(
  441. " connections_now,\n attempts_left - 1",
  442. patched_text[restore_start:restore_end],
  443. )
  444. self.assertNotIn(
  445. "pipewire_link = self._rigforge_pipewire_boundary(port_from, port_to)",
  446. patched_text,
  447. )
  448. for method_name, next_method in (
  449. ("connect", "disconnect"),
  450. ("disconnect", "load"),
  451. ):
  452. start = patched_text.index(f" def {method_name}(")
  453. end = patched_text.index(f"\n def {next_method}(", start)
  454. self.assertIn("resolved = pedalboard_connection(", patched_text[start:end])
  455. load_start = patched_text.index(" def load_pb_connections(")
  456. load_end = patched_text.find("\n def ", load_start + 1)
  457. self.assertIn(
  458. "resolved = pedalboard_connection(",
  459. patched_text[load_start:load_end],
  460. )
  461. ast.parse(patched_text)
  462. helper_artifact = app / "mod/rigforge_pipewire.py"
  463. helper_text = helper_artifact.read_text(encoding="utf-8")
  464. self.assertIn(
  465. "def is_rigforge_managed_boundary_port(port):",
  466. helper_text,
  467. )
  468. self.assertIn("if boundary_endpoint:", helper_text)
  469. patch_mod_ui_rigforge.apply(app)
  470. self.assertEqual(host.read_bytes(), once)
  471. if __name__ == "__main__":
  472. unittest.main()