test_mod_ui_pipewire.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. capture = "rigforge.mod-host-input.capture:capture_AUX0"
  43. playback = "rigforge.mod-host-output.playback:playback_AUX0"
  44. self.assertEqual(
  45. boundary_connection(
  46. "/graph/capture_1",
  47. "/graph/rigforge_nam/input",
  48. capture,
  49. playback,
  50. self.resolve,
  51. ),
  52. (capture, "effect_0:input"),
  53. )
  54. self.assertEqual(
  55. boundary_connection(
  56. "/graph/rigforge_ir/out",
  57. "/graph/playback_1",
  58. capture,
  59. playback,
  60. self.resolve,
  61. ),
  62. ("effect_1:out", playback),
  63. )
  64. self.assertIsNone(
  65. boundary_connection(
  66. "/graph/rigforge_nam/output",
  67. "/graph/rigforge_ir/in",
  68. capture,
  69. playback,
  70. self.resolve,
  71. )
  72. )
  73. def test_pipewire_connect_is_idempotent_and_disconnect_tracks_actual_link(self):
  74. edge = (
  75. "rigforge.mod-host-input.capture:capture_AUX0",
  76. "effect_0:input",
  77. )
  78. runner = FakePwLink()
  79. self.assertTrue(connect_link(*edge, runner=runner))
  80. self.assertIn(edge, runner.links)
  81. self.assertTrue(connect_link(*edge, runner=runner))
  82. self.assertTrue(disconnect_link(*edge, runner=runner))
  83. self.assertNotIn(edge, runner.links)
  84. self.assertTrue(disconnect_link(*edge, runner=runner))
  85. def test_unexpected_pw_link_failure_is_not_reported_as_success(self):
  86. def fail_connect(command, **_kwargs):
  87. return SimpleNamespace(returncode=1, stdout="", stderr="connection refused")
  88. self.assertFalse(
  89. connect_link(
  90. "rigforge.mod-host-input.capture:capture_AUX0",
  91. "effect_0:input",
  92. runner=fail_connect,
  93. )
  94. )
  95. def test_tracked_mod_ui_artifact_matches_its_rebuild_patch_and_provenance(self):
  96. artifact = ROOT / "artifacts/mod-ui/arm64/current"
  97. helper_source = (ROOT / "build/arm64/mod_ui_pipewire.py").read_bytes()
  98. helper_artifact = artifact / "app/mod/rigforge_pipewire.py"
  99. host_source = (artifact / "app/mod/host.py").read_text(encoding="utf-8")
  100. provenance = json.loads((artifact / "provenance.json").read_text(encoding="utf-8"))
  101. self.assertEqual(helper_artifact.read_bytes(), helper_source)
  102. self.assertIn("from mod.rigforge_pipewire import", host_source)
  103. self.assertIn("connect_link(*pipewire_link)", host_source)
  104. self.assertIn("disconnect_link(*pipewire_link)", host_source)
  105. self.assertEqual(
  106. provenance["rigforge_integration_patch_sha256"],
  107. hashlib.sha256((ROOT / "build/arm64/patch_mod_ui_rigforge.py").read_bytes()).hexdigest(),
  108. )
  109. self.assertEqual(
  110. provenance["rigforge_pipewire_helper_sha256"],
  111. hashlib.sha256(helper_source).hexdigest(),
  112. )
  113. if __name__ == "__main__":
  114. unittest.main()