|
@@ -2,11 +2,18 @@ import copy
|
|
|
import sys
|
|
import sys
|
|
|
import unittest
|
|
import unittest
|
|
|
from pathlib import Path
|
|
from pathlib import Path
|
|
|
|
|
+from types import SimpleNamespace
|
|
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
sys.path.insert(0, str(ROOT / "scripts"))
|
|
sys.path.insert(0, str(ROOT / "scripts"))
|
|
|
|
|
|
|
|
-from observe_pipewire import load_state, observe, parse_pw_link_output, validate_managed_links
|
|
|
|
|
|
|
+from observe_pipewire import (
|
|
|
|
|
+ load_state,
|
|
|
|
|
+ observe,
|
|
|
|
|
+ parse_pw_link_output,
|
|
|
|
|
+ reconcile_once,
|
|
|
|
|
+ validate_managed_links,
|
|
|
|
|
+)
|
|
|
|
|
|
|
|
CAPTURE_0 = "alsa_input.hw_U192k_0:capture_AUX0"
|
|
CAPTURE_0 = "alsa_input.hw_U192k_0:capture_AUX0"
|
|
|
CAPTURE_2 = "alsa_input.hw_U192k_0:capture_AUX2"
|
|
CAPTURE_2 = "alsa_input.hw_U192k_0:capture_AUX2"
|
|
@@ -121,6 +128,135 @@ class PipeWireObserverTests(unittest.TestCase):
|
|
|
],
|
|
],
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+ def test_reconcile_once_is_noop_when_all_managed_links_exist(self):
|
|
|
|
|
+ calls = []
|
|
|
|
|
+
|
|
|
|
|
+ def runner(command, **kwargs):
|
|
|
|
|
+ calls.append(command)
|
|
|
|
|
+ return SimpleNamespace(stdout=(
|
|
|
|
|
+ f"{CAPTURE_0}\n |-> {MIX_INPUT_0}\n"
|
|
|
|
|
+ f"{CAPTURE_2}\n |-> {MIX_INPUT_2}\n"
|
|
|
|
|
+ f"{MIX_OUTPUT}\n |-> {PLAYBACK_0}\n"
|
|
|
|
|
+ ))
|
|
|
|
|
+
|
|
|
|
|
+ result = reconcile_once(self.state, runner)
|
|
|
|
|
+
|
|
|
|
|
+ self.assertEqual(result["actions"], [])
|
|
|
|
|
+ self.assertEqual(result["differences"], [])
|
|
|
|
|
+ self.assertEqual(calls, [["pw-link", "-l"], ["pw-link", "-l"]])
|
|
|
|
|
+
|
|
|
|
|
+ def test_reconcile_once_links_only_declared_missing_edges_and_verifies(self):
|
|
|
|
|
+ state = copy.deepcopy(self.state)
|
|
|
|
|
+ links = {
|
|
|
|
|
+ (CAPTURE_0, MIX_INPUT_0),
|
|
|
|
|
+ (MIX_OUTPUT, PLAYBACK_0),
|
|
|
|
|
+ }
|
|
|
|
|
+ calls = []
|
|
|
|
|
+
|
|
|
|
|
+ def render_links():
|
|
|
|
|
+ return "".join(f"{source}\n |-> {destination}\n" for source, destination in sorted(links))
|
|
|
|
|
+
|
|
|
|
|
+ def runner(command, **kwargs):
|
|
|
|
|
+ calls.append(command)
|
|
|
|
|
+ if command == ["pw-link", "-l"]:
|
|
|
|
|
+ return SimpleNamespace(stdout=render_links())
|
|
|
|
|
+ if command == ["pw-link", "-o"]:
|
|
|
|
|
+ return SimpleNamespace(stdout=f"{CAPTURE_0}\n{CAPTURE_2}\n{MIX_OUTPUT}\n")
|
|
|
|
|
+ if command == ["pw-link", "-i"]:
|
|
|
|
|
+ return SimpleNamespace(stdout=f"{MIX_INPUT_0}\n{MIX_INPUT_2}\n{PLAYBACK_0}\n")
|
|
|
|
|
+ if command == ["pw-link", CAPTURE_2, MIX_INPUT_2]:
|
|
|
|
|
+ links.add((CAPTURE_2, MIX_INPUT_2))
|
|
|
|
|
+ return SimpleNamespace(stdout="")
|
|
|
|
|
+ self.fail(f"unexpected command: {command}")
|
|
|
|
|
+
|
|
|
|
|
+ result = reconcile_once(state, runner)
|
|
|
|
|
+
|
|
|
|
|
+ self.assertEqual(
|
|
|
|
|
+ result["actions"],
|
|
|
|
|
+ [{"kind": "link", "from": CAPTURE_2, "to": MIX_INPUT_2, "result": "requested"}],
|
|
|
|
|
+ )
|
|
|
|
|
+ self.assertEqual(result["differences"], [])
|
|
|
|
|
+ self.assertIn(["pw-link", CAPTURE_2, MIX_INPUT_2], calls)
|
|
|
|
|
+
|
|
|
|
|
+ def test_reconcile_once_does_not_link_when_endpoint_is_unavailable(self):
|
|
|
|
|
+ state = copy.deepcopy(self.state)
|
|
|
|
|
+ links = {(CAPTURE_0, MIX_INPUT_0), (MIX_OUTPUT, PLAYBACK_0)}
|
|
|
|
|
+ calls = []
|
|
|
|
|
+
|
|
|
|
|
+ def render_links():
|
|
|
|
|
+ return "".join(f"{source}\n |-> {destination}\n" for source, destination in sorted(links))
|
|
|
|
|
+
|
|
|
|
|
+ def runner(command, **kwargs):
|
|
|
|
|
+ calls.append(command)
|
|
|
|
|
+ if command == ["pw-link", "-l"]:
|
|
|
|
|
+ return SimpleNamespace(stdout=render_links())
|
|
|
|
|
+ if command == ["pw-link", "-o"]:
|
|
|
|
|
+ return SimpleNamespace(stdout=f"{CAPTURE_0}\n{MIX_OUTPUT}\n")
|
|
|
|
|
+ if command == ["pw-link", "-i"]:
|
|
|
|
|
+ return SimpleNamespace(stdout=f"{MIX_INPUT_0}\n{MIX_INPUT_2}\n{PLAYBACK_0}\n")
|
|
|
|
|
+ self.fail(f"unexpected command: {command}")
|
|
|
|
|
+
|
|
|
|
|
+ result = reconcile_once(state, runner)
|
|
|
|
|
+
|
|
|
|
|
+ self.assertEqual(
|
|
|
|
|
+ result["actions"],
|
|
|
|
|
+ [{
|
|
|
|
|
+ "kind": "unavailable-port",
|
|
|
|
|
+ "from": CAPTURE_2,
|
|
|
|
|
+ "to": MIX_INPUT_2,
|
|
|
|
|
+ "result": "not-linked",
|
|
|
|
|
+ }],
|
|
|
|
|
+ )
|
|
|
|
|
+ self.assertTrue(result["differences"])
|
|
|
|
|
+ self.assertFalse(any(command[:2] == ["pw-link", CAPTURE_2] for command in calls))
|
|
|
|
|
+
|
|
|
|
|
+ def test_reconcile_once_preserves_unexpected_managed_links(self):
|
|
|
|
|
+ state = copy.deepcopy(self.state)
|
|
|
|
|
+ state["routing"]["managed_ports"].append(CAPTURE_3)
|
|
|
|
|
+ links = {
|
|
|
|
|
+ (CAPTURE_0, MIX_INPUT_0),
|
|
|
|
|
+ (CAPTURE_2, MIX_INPUT_2),
|
|
|
|
|
+ (MIX_OUTPUT, PLAYBACK_0),
|
|
|
|
|
+ (CAPTURE_3, MIX_INPUT_2),
|
|
|
|
|
+ }
|
|
|
|
|
+ calls = []
|
|
|
|
|
+
|
|
|
|
|
+ def runner(command, **kwargs):
|
|
|
|
|
+ calls.append(command)
|
|
|
|
|
+ return SimpleNamespace(
|
|
|
|
|
+ stdout="".join(f"{source}\n |-> {destination}\n" for source, destination in sorted(links))
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ result = reconcile_once(state, runner)
|
|
|
|
|
+
|
|
|
|
|
+ self.assertEqual(result["actions"], [])
|
|
|
|
|
+ self.assertEqual(len(result["differences"]), 1)
|
|
|
|
|
+ self.assertEqual(result["differences"][0]["kind"], "unexpected")
|
|
|
|
|
+ self.assertEqual(calls, [["pw-link", "-l"], ["pw-link", "-l"]])
|
|
|
|
|
+
|
|
|
|
|
+ def test_reconcile_once_reports_link_still_missing_after_request(self):
|
|
|
|
|
+ state = copy.deepcopy(self.state)
|
|
|
|
|
+ links = {(CAPTURE_0, MIX_INPUT_0), (MIX_OUTPUT, PLAYBACK_0)}
|
|
|
|
|
+
|
|
|
|
|
+ def render_links():
|
|
|
|
|
+ return "".join(f"{source}\n |-> {destination}\n" for source, destination in sorted(links))
|
|
|
|
|
+
|
|
|
|
|
+ def runner(command, **kwargs):
|
|
|
|
|
+ if command == ["pw-link", "-l"]:
|
|
|
|
|
+ return SimpleNamespace(stdout=render_links())
|
|
|
|
|
+ if command == ["pw-link", "-o"]:
|
|
|
|
|
+ return SimpleNamespace(stdout=f"{CAPTURE_0}\n{CAPTURE_2}\n{MIX_OUTPUT}\n")
|
|
|
|
|
+ if command == ["pw-link", "-i"]:
|
|
|
|
|
+ return SimpleNamespace(stdout=f"{MIX_INPUT_0}\n{MIX_INPUT_2}\n{PLAYBACK_0}\n")
|
|
|
|
|
+ if command == ["pw-link", CAPTURE_2, MIX_INPUT_2]:
|
|
|
|
|
+ return SimpleNamespace(stdout="")
|
|
|
|
|
+ self.fail(f"unexpected command: {command}")
|
|
|
|
|
+
|
|
|
|
|
+ result = reconcile_once(state, runner)
|
|
|
|
|
+
|
|
|
|
|
+ self.assertEqual(len(result["differences"]), 1)
|
|
|
|
|
+ self.assertEqual(result["differences"][0]["kind"], "missing")
|
|
|
|
|
+
|
|
|
def test_managed_links_must_use_declared_ports(self):
|
|
def test_managed_links_must_use_declared_ports(self):
|
|
|
state = copy.deepcopy(self.state)
|
|
state = copy.deepcopy(self.state)
|
|
|
state["routing"]["managed_ports"].remove(PLAYBACK_0)
|
|
state["routing"]["managed_ports"].remove(PLAYBACK_0)
|