test_mod_ui_pipewire.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 boundary_connection, connect_link, disconnect_link
  10. class FakePwLink:
  11. def __init__(self, links=()):
  12. self.links = set(links)
  13. self.commands = []
  14. def __call__(self, command, **_kwargs):
  15. self.commands.append(command)
  16. if command == ["pw-link", "-l"]:
  17. output = "\n".join(
  18. f"{source}\n |-> {destination}"
  19. for source, destination in sorted(self.links)
  20. )
  21. return SimpleNamespace(returncode=0, stdout=output, stderr="")
  22. if len(command) == 3 and command[0] == "pw-link" and command[1] != "-d":
  23. edge = (command[1], command[2])
  24. if edge in self.links:
  25. return SimpleNamespace(returncode=1, stdout="", stderr="already linked")
  26. self.links.add(edge)
  27. return SimpleNamespace(returncode=0, stdout="", stderr="")
  28. if len(command) == 4 and command[:2] == ["pw-link", "-d"]:
  29. edge = (command[2], command[3])
  30. if edge not in self.links:
  31. return SimpleNamespace(returncode=1, stdout="", stderr="not linked")
  32. self.links.remove(edge)
  33. return SimpleNamespace(returncode=0, stdout="", stderr="")
  34. raise AssertionError(f"unexpected command: {command}")
  35. class ModUiPipeWireTests(unittest.TestCase):
  36. def resolve(self, port):
  37. return {
  38. "/graph/rigforge_nam/input": "effect_0:input",
  39. "/graph/rigforge_ir/out": "effect_1:out",
  40. }[port]
  41. def test_maps_only_rigforge_boundary_edges_to_pipewire_ports(self):
  42. captures = {
  43. "capture_1": "rigforge.mod-host-input.capture:capture_AUX0",
  44. "capture_2": "rigforge.mod-host-input.capture:capture_AUX1",
  45. "capture_3": "rigforge.mod-host-input.capture:capture_AUX2",
  46. "capture_4": "rigforge.mod-host-input.capture:capture_AUX3",
  47. }
  48. playbacks = {
  49. "playback_1": "rigforge.mod-host-output.playback:playback_AUX0",
  50. "playback_2": "rigforge.mod-host-output.playback:playback_AUX1",
  51. "playback_3": "rigforge.mod-host-output.playback:playback_AUX2",
  52. "playback_4": "rigforge.mod-host-output.playback:playback_AUX3",
  53. }
  54. self.assertEqual(
  55. boundary_connection(
  56. "/graph/capture_1",
  57. "/graph/rigforge_nam/input",
  58. captures,
  59. playbacks,
  60. self.resolve,
  61. ),
  62. (captures["capture_1"], "effect_0:input"),
  63. )
  64. self.assertEqual(
  65. boundary_connection(
  66. "/graph/rigforge_ir/out",
  67. "/graph/playback_4",
  68. captures,
  69. playbacks,
  70. self.resolve,
  71. ),
  72. ("effect_1:out", playbacks["playback_4"]),
  73. )
  74. self.assertIsNone(
  75. boundary_connection(
  76. "/graph/rigforge_nam/output",
  77. "/graph/rigforge_ir/in",
  78. captures,
  79. playbacks,
  80. self.resolve,
  81. )
  82. )
  83. self.assertIsNone(
  84. boundary_connection(
  85. "/graph/capture_5",
  86. "/graph/rigforge_ir/in",
  87. captures,
  88. playbacks,
  89. self.resolve,
  90. )
  91. )
  92. def test_pipewire_connect_is_idempotent_and_disconnect_tracks_actual_link(self):
  93. edge = (
  94. "rigforge.mod-host-input.capture:capture_AUX0",
  95. "effect_0:input",
  96. )
  97. runner = FakePwLink()
  98. self.assertTrue(connect_link(*edge, runner=runner))
  99. self.assertIn(edge, runner.links)
  100. self.assertTrue(connect_link(*edge, runner=runner))
  101. self.assertTrue(disconnect_link(*edge, runner=runner))
  102. self.assertNotIn(edge, runner.links)
  103. self.assertTrue(disconnect_link(*edge, runner=runner))
  104. def test_unexpected_pw_link_failure_is_not_reported_as_success(self):
  105. def fail_connect(command, **_kwargs):
  106. return SimpleNamespace(returncode=1, stdout="", stderr="connection refused")
  107. self.assertFalse(
  108. connect_link(
  109. "rigforge.mod-host-input.capture:capture_AUX0",
  110. "effect_0:input",
  111. runner=fail_connect,
  112. )
  113. )
  114. def test_tracked_mod_ui_artifact_matches_its_rebuild_patch_and_provenance(self):
  115. artifact = ROOT / "artifacts/mod-ui/arm64/current"
  116. helper_source = (ROOT / "build/arm64/mod_ui_pipewire.py").read_bytes()
  117. helper_artifact = artifact / "app/mod/rigforge_pipewire.py"
  118. host_source = (artifact / "app/mod/host.py").read_text(encoding="utf-8")
  119. provenance = json.loads((artifact / "provenance.json").read_text(encoding="utf-8"))
  120. self.assertEqual(helper_artifact.read_bytes(), helper_source)
  121. self.assertIn("from mod.rigforge_pipewire import", host_source)
  122. self.assertIn("connect_link(*pipewire_link)", host_source)
  123. self.assertIn("disconnect_link(*pipewire_link)", host_source)
  124. self.assertEqual(
  125. provenance["rigforge_integration_patch_sha256"],
  126. hashlib.sha256((ROOT / "build/arm64/patch_mod_ui_rigforge.py").read_bytes()).hexdigest(),
  127. )
  128. self.assertEqual(
  129. provenance["rigforge_pipewire_helper_sha256"],
  130. hashlib.sha256(helper_source).hexdigest(),
  131. )
  132. if __name__ == "__main__":
  133. unittest.main()