test_pipewire_observer.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. import copy
  2. import sys
  3. import unittest
  4. from pathlib import Path
  5. from types import SimpleNamespace
  6. ROOT = Path(__file__).resolve().parents[1]
  7. sys.path.insert(0, str(ROOT / "scripts"))
  8. from observe_pipewire import (
  9. load_state,
  10. observe,
  11. parse_pw_link_output,
  12. reconcile_once,
  13. validate_managed_links,
  14. )
  15. CAPTURE_0 = "alsa_input.hw_U192k_0:capture_AUX0"
  16. CAPTURE_2 = "alsa_input.hw_U192k_0:capture_AUX2"
  17. CAPTURE_3 = "alsa_input.hw_U192k_0:capture_AUX3"
  18. AMP_INPUT = "effect_0:input"
  19. AMP_OUTPUT = "effect_0:output"
  20. CAB_INPUT = "effect_1:in"
  21. CAB_OUTPUT = "effect_1:out"
  22. MIX_INPUT_0 = "rigforge.direct-mix.input:playback_AUX0"
  23. MIX_INPUT_2 = "rigforge.direct-mix.input:playback_AUX2"
  24. MIX_OUTPUT = "rigforge.direct-mix.output:capture_FL"
  25. PLAYBACK_0 = "alsa_output.hw_U192k_0:playback_AUX0"
  26. class PipeWireObserverTests(unittest.TestCase):
  27. @classmethod
  28. def setUpClass(cls):
  29. cls.state = load_state(ROOT / "config" / "desired-state.yaml")
  30. def test_empty_managed_set_ignores_wireplumber_hardware_links(self):
  31. state = copy.deepcopy(self.state)
  32. state["routing"]["managed_ports"] = []
  33. state["routing"]["managed_links"] = []
  34. observed = parse_pw_link_output(
  35. "alsa_output.usb-umc:Line1.split:output_AUX0\n"
  36. f" |-> {PLAYBACK_0}\n"
  37. f"{CAPTURE_0}\n"
  38. " |-> alsa_input.usb-umc:Mic1.split:input_AUX0\n"
  39. )
  40. self.assertEqual(observe(state, observed), [])
  41. def test_reports_missing_and_unexpected_managed_links_without_mutating(self):
  42. state = copy.deepcopy(self.state)
  43. state["routing"]["managed_ports"] = [CAPTURE_0, CAPTURE_2, CAPTURE_3, PLAYBACK_0]
  44. state["routing"]["managed_links"] = [
  45. {"from": CAPTURE_0, "to": PLAYBACK_0, "status": "planned"},
  46. {"from": CAPTURE_2, "to": PLAYBACK_0, "status": "observed"},
  47. ]
  48. observed = {
  49. (CAPTURE_2, PLAYBACK_0),
  50. (CAPTURE_3, PLAYBACK_0),
  51. }
  52. reports = observe(state, observed)
  53. self.assertEqual(
  54. reports,
  55. [
  56. {"kind": "missing", "from": CAPTURE_0, "to": PLAYBACK_0, "action": "would-link"},
  57. {"kind": "unexpected", "from": CAPTURE_3, "to": PLAYBACK_0, "action": "would-review"},
  58. ],
  59. )
  60. self.assertEqual(state["routing"]["managed_links"][0]["status"], "planned")
  61. def test_parser_handles_incoming_and_outgoing_link_views(self):
  62. links = parse_pw_link_output(
  63. f"{CAPTURE_0}\n"
  64. f" |-> {PLAYBACK_0}\n"
  65. f"{PLAYBACK_0}\n"
  66. f" |<- {CAPTURE_0}\n"
  67. )
  68. self.assertEqual(links, {(CAPTURE_0, PLAYBACK_0)})
  69. def test_rejects_logical_mix_endpoint(self):
  70. state = copy.deepcopy(self.state)
  71. state["routing"]["managed_ports"] = [PLAYBACK_0]
  72. state["routing"]["managed_links"] = [
  73. {"from": "direct_mix", "to": PLAYBACK_0, "status": "planned"}
  74. ]
  75. self.assertTrue(any("approved observed Rigforge or UMC port" in error for error in validate_managed_links(state)))
  76. def test_declared_guitar_effect_chain_and_voice_mix_links_match_observed_graph(self):
  77. links = {
  78. (CAPTURE_0, AMP_INPUT),
  79. (AMP_OUTPUT, CAB_INPUT),
  80. (CAB_OUTPUT, MIX_INPUT_0),
  81. (CAPTURE_2, MIX_INPUT_2),
  82. (MIX_OUTPUT, PLAYBACK_0),
  83. }
  84. self.assertEqual(validate_managed_links(self.state), [])
  85. self.assertEqual(observe(self.state, links), [])
  86. def test_reports_missing_voice_mix_link_without_expectation_of_dry_guitar(self):
  87. links = {
  88. (CAPTURE_0, AMP_INPUT),
  89. (AMP_OUTPUT, CAB_INPUT),
  90. (CAB_OUTPUT, MIX_INPUT_0),
  91. (MIX_OUTPUT, PLAYBACK_0),
  92. }
  93. reports = observe(self.state, links)
  94. self.assertEqual(
  95. reports,
  96. [
  97. {
  98. "kind": "missing",
  99. "from": CAPTURE_2,
  100. "to": MIX_INPUT_2,
  101. "action": "would-link",
  102. }
  103. ],
  104. )
  105. def test_reports_unexpected_link_between_managed_filter_ports(self):
  106. state = copy.deepcopy(self.state)
  107. state["routing"]["managed_ports"].append(CAPTURE_3)
  108. links = {
  109. (CAPTURE_0, AMP_INPUT),
  110. (AMP_OUTPUT, CAB_INPUT),
  111. (CAB_OUTPUT, MIX_INPUT_0),
  112. (CAPTURE_2, MIX_INPUT_2),
  113. (MIX_OUTPUT, PLAYBACK_0),
  114. (CAPTURE_3, MIX_INPUT_2),
  115. }
  116. reports = observe(state, links)
  117. self.assertEqual(
  118. reports,
  119. [
  120. {
  121. "kind": "unexpected",
  122. "from": CAPTURE_3,
  123. "to": MIX_INPUT_2,
  124. "action": "would-review",
  125. }
  126. ],
  127. )
  128. def test_reconcile_once_is_noop_when_all_managed_links_exist(self):
  129. calls = []
  130. def runner(command, **kwargs):
  131. calls.append(command)
  132. return SimpleNamespace(stdout=(
  133. f"{CAPTURE_0}\n |-> {AMP_INPUT}\n"
  134. f"{AMP_OUTPUT}\n |-> {CAB_INPUT}\n"
  135. f"{CAB_OUTPUT}\n |-> {MIX_INPUT_0}\n"
  136. f"{CAPTURE_2}\n |-> {MIX_INPUT_2}\n"
  137. f"{MIX_OUTPUT}\n |-> {PLAYBACK_0}\n"
  138. ))
  139. result = reconcile_once(self.state, runner)
  140. self.assertEqual(result["actions"], [])
  141. self.assertEqual(result["differences"], [])
  142. self.assertEqual(calls, [["pw-link", "-l"], ["pw-link", "-l"]])
  143. def test_reconcile_once_links_only_declared_missing_edges_and_verifies(self):
  144. state = copy.deepcopy(self.state)
  145. links = {
  146. (CAPTURE_0, AMP_INPUT),
  147. (AMP_OUTPUT, CAB_INPUT),
  148. (CAB_OUTPUT, MIX_INPUT_0),
  149. (MIX_OUTPUT, PLAYBACK_0),
  150. }
  151. calls = []
  152. def render_links():
  153. return "".join(f"{source}\n |-> {destination}\n" for source, destination in sorted(links))
  154. def runner(command, **kwargs):
  155. calls.append(command)
  156. if command == ["pw-link", "-l"]:
  157. return SimpleNamespace(stdout=render_links())
  158. if command == ["pw-link", "-o"]:
  159. return SimpleNamespace(stdout=(
  160. f"{AMP_OUTPUT}\n{CAB_OUTPUT}\n{CAPTURE_0}\n{CAPTURE_2}\n{MIX_OUTPUT}\n"
  161. ))
  162. if command == ["pw-link", "-i"]:
  163. return SimpleNamespace(stdout=(
  164. f"{AMP_INPUT}\n{CAB_INPUT}\n{MIX_INPUT_0}\n{MIX_INPUT_2}\n{PLAYBACK_0}\n"
  165. ))
  166. if command == ["pw-link", CAPTURE_2, MIX_INPUT_2]:
  167. links.add((CAPTURE_2, MIX_INPUT_2))
  168. return SimpleNamespace(stdout="")
  169. self.fail(f"unexpected command: {command}")
  170. result = reconcile_once(state, runner)
  171. self.assertEqual(
  172. result["actions"],
  173. [{"kind": "link", "from": CAPTURE_2, "to": MIX_INPUT_2, "result": "requested"}],
  174. )
  175. self.assertEqual(result["differences"], [])
  176. self.assertIn(["pw-link", CAPTURE_2, MIX_INPUT_2], calls)
  177. def test_reconcile_once_restores_all_managed_links_after_service_restart(self):
  178. links = set()
  179. calls = []
  180. def render_links():
  181. return "".join(f"{source}\n |-> {destination}\n" for source, destination in sorted(links))
  182. def runner(command, **kwargs):
  183. calls.append(command)
  184. if command == ["pw-link", "-l"]:
  185. return SimpleNamespace(stdout=render_links())
  186. if command == ["pw-link", "-o"]:
  187. return SimpleNamespace(stdout=(
  188. f"{AMP_OUTPUT}\n{CAB_OUTPUT}\n{CAPTURE_0}\n{CAPTURE_2}\n{MIX_OUTPUT}\n"
  189. ))
  190. if command == ["pw-link", "-i"]:
  191. return SimpleNamespace(stdout=(
  192. f"{AMP_INPUT}\n{CAB_INPUT}\n{MIX_INPUT_0}\n{MIX_INPUT_2}\n{PLAYBACK_0}\n"
  193. ))
  194. if tuple(command) in {
  195. ("pw-link", AMP_OUTPUT, CAB_INPUT),
  196. ("pw-link", CAB_OUTPUT, MIX_INPUT_0),
  197. ("pw-link", CAPTURE_0, AMP_INPUT),
  198. ("pw-link", CAPTURE_2, MIX_INPUT_2),
  199. ("pw-link", MIX_OUTPUT, PLAYBACK_0),
  200. }:
  201. links.add((command[1], command[2]))
  202. return SimpleNamespace(stdout="")
  203. self.fail(f"unexpected command: {command}")
  204. result = reconcile_once(self.state, runner)
  205. self.assertEqual(
  206. result["actions"],
  207. [
  208. {"kind": "link", "from": CAPTURE_0, "to": AMP_INPUT, "result": "requested"},
  209. {"kind": "link", "from": CAPTURE_2, "to": MIX_INPUT_2, "result": "requested"},
  210. {"kind": "link", "from": AMP_OUTPUT, "to": CAB_INPUT, "result": "requested"},
  211. {"kind": "link", "from": CAB_OUTPUT, "to": MIX_INPUT_0, "result": "requested"},
  212. {"kind": "link", "from": MIX_OUTPUT, "to": PLAYBACK_0, "result": "requested"},
  213. ],
  214. )
  215. self.assertEqual(result["differences"], [])
  216. self.assertEqual(
  217. [command for command in calls if len(command) == 3 and command[0] == "pw-link"],
  218. [
  219. ["pw-link", CAPTURE_0, AMP_INPUT],
  220. ["pw-link", CAPTURE_2, MIX_INPUT_2],
  221. ["pw-link", AMP_OUTPUT, CAB_INPUT],
  222. ["pw-link", CAB_OUTPUT, MIX_INPUT_0],
  223. ["pw-link", MIX_OUTPUT, PLAYBACK_0],
  224. ],
  225. )
  226. def test_reconcile_once_does_not_link_when_endpoint_is_unavailable(self):
  227. state = copy.deepcopy(self.state)
  228. links = {
  229. (CAPTURE_0, AMP_INPUT),
  230. (AMP_OUTPUT, CAB_INPUT),
  231. (CAB_OUTPUT, MIX_INPUT_0),
  232. (CAPTURE_2, MIX_INPUT_2),
  233. (MIX_OUTPUT, PLAYBACK_0),
  234. }
  235. links.remove((CAPTURE_2, MIX_INPUT_2))
  236. calls = []
  237. def render_links():
  238. return "".join(f"{source}\n |-> {destination}\n" for source, destination in sorted(links))
  239. def runner(command, **kwargs):
  240. calls.append(command)
  241. if command == ["pw-link", "-l"]:
  242. return SimpleNamespace(stdout=render_links())
  243. if command == ["pw-link", "-o"]:
  244. return SimpleNamespace(stdout=f"{AMP_OUTPUT}\n{CAB_OUTPUT}\n{CAPTURE_0}\n{MIX_OUTPUT}\n")
  245. if command == ["pw-link", "-i"]:
  246. return SimpleNamespace(stdout=(
  247. f"{AMP_INPUT}\n{CAB_INPUT}\n{MIX_INPUT_0}\n{MIX_INPUT_2}\n{PLAYBACK_0}\n"
  248. ))
  249. self.fail(f"unexpected command: {command}")
  250. result = reconcile_once(state, runner)
  251. self.assertEqual(
  252. result["actions"],
  253. [{
  254. "kind": "unavailable-port",
  255. "from": CAPTURE_2,
  256. "to": MIX_INPUT_2,
  257. "result": "not-linked",
  258. }],
  259. )
  260. self.assertTrue(result["differences"])
  261. self.assertFalse(any(command[:2] == ["pw-link", CAPTURE_2] for command in calls))
  262. def test_reconcile_once_preserves_unexpected_managed_links(self):
  263. state = copy.deepcopy(self.state)
  264. state["routing"]["managed_ports"].append(CAPTURE_3)
  265. links = {
  266. (CAPTURE_0, AMP_INPUT),
  267. (AMP_OUTPUT, CAB_INPUT),
  268. (CAB_OUTPUT, MIX_INPUT_0),
  269. (CAPTURE_2, MIX_INPUT_2),
  270. (MIX_OUTPUT, PLAYBACK_0),
  271. (CAPTURE_3, MIX_INPUT_2),
  272. }
  273. calls = []
  274. def runner(command, **kwargs):
  275. calls.append(command)
  276. return SimpleNamespace(
  277. stdout="".join(f"{source}\n |-> {destination}\n" for source, destination in sorted(links))
  278. )
  279. result = reconcile_once(state, runner)
  280. self.assertEqual(result["actions"], [])
  281. self.assertEqual(len(result["differences"]), 1)
  282. self.assertEqual(result["differences"][0]["kind"], "unexpected")
  283. self.assertEqual(calls, [["pw-link", "-l"], ["pw-link", "-l"]])
  284. def test_reconcile_once_reports_link_still_missing_after_request(self):
  285. state = copy.deepcopy(self.state)
  286. links = {
  287. (CAPTURE_0, AMP_INPUT),
  288. (AMP_OUTPUT, CAB_INPUT),
  289. (CAB_OUTPUT, MIX_INPUT_0),
  290. (MIX_OUTPUT, PLAYBACK_0),
  291. }
  292. def render_links():
  293. return "".join(f"{source}\n |-> {destination}\n" for source, destination in sorted(links))
  294. def runner(command, **kwargs):
  295. if command == ["pw-link", "-l"]:
  296. return SimpleNamespace(stdout=render_links())
  297. if command == ["pw-link", "-o"]:
  298. return SimpleNamespace(stdout=(
  299. f"{AMP_OUTPUT}\n{CAB_OUTPUT}\n{CAPTURE_0}\n{CAPTURE_2}\n{MIX_OUTPUT}\n"
  300. ))
  301. if command == ["pw-link", "-i"]:
  302. return SimpleNamespace(stdout=(
  303. f"{AMP_INPUT}\n{CAB_INPUT}\n{MIX_INPUT_0}\n{MIX_INPUT_2}\n{PLAYBACK_0}\n"
  304. ))
  305. if command == ["pw-link", CAPTURE_2, MIX_INPUT_2]:
  306. return SimpleNamespace(stdout="")
  307. self.fail(f"unexpected command: {command}")
  308. result = reconcile_once(state, runner)
  309. self.assertEqual(len(result["differences"]), 1)
  310. self.assertEqual(result["differences"][0]["kind"], "missing")
  311. def test_managed_links_must_use_declared_ports(self):
  312. state = copy.deepcopy(self.state)
  313. state["routing"]["managed_ports"].remove(PLAYBACK_0)
  314. state["routing"]["managed_links"] = [
  315. {"from": CAPTURE_0, "to": PLAYBACK_0, "status": "planned"}
  316. ]
  317. self.assertTrue(any("endpoints must be listed" in error for error in validate_managed_links(state)))
  318. if __name__ == "__main__":
  319. unittest.main()