import hashlib import json import sys import unittest from pathlib import Path from types import SimpleNamespace ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "build" / "arm64")) from mod_ui_pipewire import boundary_connection, connect_link, disconnect_link class FakePwLink: def __init__(self, links=()): self.links = set(links) self.commands = [] def __call__(self, command, **_kwargs): self.commands.append(command) if command == ["pw-link", "-l"]: output = "\n".join( f"{source}\n |-> {destination}" for source, destination in sorted(self.links) ) return SimpleNamespace(returncode=0, stdout=output, stderr="") if len(command) == 3 and command[0] == "pw-link" and command[1] != "-d": edge = (command[1], command[2]) if edge in self.links: return SimpleNamespace(returncode=1, stdout="", stderr="already linked") self.links.add(edge) return SimpleNamespace(returncode=0, stdout="", stderr="") if len(command) == 4 and command[:2] == ["pw-link", "-d"]: edge = (command[2], command[3]) if edge not in self.links: return SimpleNamespace(returncode=1, stdout="", stderr="not linked") self.links.remove(edge) return SimpleNamespace(returncode=0, stdout="", stderr="") raise AssertionError(f"unexpected command: {command}") class ModUiPipeWireTests(unittest.TestCase): def resolve(self, port): return { "/graph/rigforge_nam/input": "effect_0:input", "/graph/rigforge_ir/out": "effect_1:out", }[port] def test_maps_only_rigforge_boundary_edges_to_pipewire_ports(self): captures = { "capture_1": "rigforge.mod-host-input.capture:capture_AUX0", "capture_2": "rigforge.mod-host-input.capture:capture_AUX1", "capture_3": "rigforge.mod-host-input.capture:capture_AUX2", "capture_4": "rigforge.mod-host-input.capture:capture_AUX3", } playbacks = { "playback_1": "rigforge.mod-host-output.playback:playback_AUX0", "playback_2": "rigforge.mod-host-output.playback:playback_AUX1", "playback_3": "rigforge.mod-host-output.playback:playback_AUX2", "playback_4": "rigforge.mod-host-output.playback:playback_AUX3", } self.assertEqual( boundary_connection( "/graph/capture_1", "/graph/rigforge_nam/input", captures, playbacks, self.resolve, ), (captures["capture_1"], "effect_0:input"), ) self.assertEqual( boundary_connection( "/graph/rigforge_ir/out", "/graph/playback_4", captures, playbacks, self.resolve, ), ("effect_1:out", playbacks["playback_4"]), ) self.assertIsNone( boundary_connection( "/graph/rigforge_nam/output", "/graph/rigforge_ir/in", captures, playbacks, self.resolve, ) ) self.assertIsNone( boundary_connection( "/graph/capture_5", "/graph/rigforge_ir/in", captures, playbacks, self.resolve, ) ) def test_pipewire_connect_is_idempotent_and_disconnect_tracks_actual_link(self): edge = ( "rigforge.mod-host-input.capture:capture_AUX0", "effect_0:input", ) runner = FakePwLink() self.assertTrue(connect_link(*edge, runner=runner)) self.assertIn(edge, runner.links) self.assertTrue(connect_link(*edge, runner=runner)) self.assertTrue(disconnect_link(*edge, runner=runner)) self.assertNotIn(edge, runner.links) self.assertTrue(disconnect_link(*edge, runner=runner)) def test_unexpected_pw_link_failure_is_not_reported_as_success(self): def fail_connect(command, **_kwargs): return SimpleNamespace(returncode=1, stdout="", stderr="connection refused") self.assertFalse( connect_link( "rigforge.mod-host-input.capture:capture_AUX0", "effect_0:input", runner=fail_connect, ) ) def test_tracked_mod_ui_artifact_matches_its_rebuild_patch_and_provenance(self): artifact = ROOT / "artifacts/mod-ui/arm64/current" helper_source = (ROOT / "build/arm64/mod_ui_pipewire.py").read_bytes() helper_artifact = artifact / "app/mod/rigforge_pipewire.py" host_source = (artifact / "app/mod/host.py").read_text(encoding="utf-8") provenance = json.loads((artifact / "provenance.json").read_text(encoding="utf-8")) self.assertEqual(helper_artifact.read_bytes(), helper_source) self.assertIn("from mod.rigforge_pipewire import", host_source) self.assertIn("connect_link(*pipewire_link)", host_source) self.assertIn("disconnect_link(*pipewire_link)", host_source) self.assertEqual( provenance["rigforge_integration_patch_sha256"], hashlib.sha256((ROOT / "build/arm64/patch_mod_ui_rigforge.py").read_bytes()).hexdigest(), ) self.assertEqual( provenance["rigforge_pipewire_helper_sha256"], hashlib.sha256(helper_source).hexdigest(), ) if __name__ == "__main__": unittest.main()