test_pipewire_observer.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. import io
  2. import copy
  3. import sys
  4. import unittest
  5. from contextlib import redirect_stdout
  6. from pathlib import Path
  7. from types import SimpleNamespace
  8. from unittest.mock import patch
  9. ROOT = Path(__file__).resolve().parents[1]
  10. sys.path.insert(0, str(ROOT / "scripts"))
  11. from observe_pipewire import (
  12. load_state,
  13. observe,
  14. parse_pw_link_output,
  15. reconcile_once,
  16. main,
  17. validate_managed_links,
  18. )
  19. CAPTURE_0 = "alsa_input.hw_U192k_0:capture_AUX0"
  20. CAPTURE_1 = "alsa_input.hw_U192k_0:capture_AUX1"
  21. CAPTURE_2 = "alsa_input.hw_U192k_0:capture_AUX2"
  22. CAPTURE_3 = "alsa_input.hw_U192k_0:capture_AUX3"
  23. AMP_INPUT = "rigforge.mod-host-input:playback_AUX0"
  24. AMP_INPUT_1 = "rigforge.mod-host-input:playback_AUX1"
  25. AMP_INPUT_2 = "rigforge.mod-host-input:playback_AUX2"
  26. AMP_INPUT_3 = "rigforge.mod-host-input:playback_AUX3"
  27. AMP_OUTPUT = "effect_0:output"
  28. CAB_INPUT = "effect_1:in"
  29. CAB_OUTPUT = "rigforge.mod-host-output:capture_AUX0"
  30. CAB_OUTPUT_1 = "rigforge.mod-host-output:capture_AUX1"
  31. CAB_OUTPUT_2 = "rigforge.mod-host-output:capture_AUX2"
  32. CAB_OUTPUT_3 = "rigforge.mod-host-output:capture_AUX3"
  33. MIX_INPUT_0 = "rigforge.direct-mix.input:playback_AUX0"
  34. MIX_INPUT_2 = "rigforge.direct-mix.input:playback_AUX2"
  35. MIX_INPUT_4 = "rigforge.direct-mix.input:playback_AUX4"
  36. MIX_INPUT_5 = "rigforge.direct-mix.input:playback_AUX5"
  37. MIX_INPUT_6 = "rigforge.direct-mix.input:playback_AUX6"
  38. MIX_INPUT_7 = "rigforge.direct-mix.input:playback_AUX7"
  39. MIX_INPUT_8 = "rigforge.direct-mix.input:playback_AUX8"
  40. LOOPER_INPUT_1 = "sooperlooper:loop0_in_1"
  41. LOOPER_INPUT_2 = "sooperlooper:loop0_in_2"
  42. LOOPER_INPUT_3 = "sooperlooper:loop0_in_3"
  43. LOOPER_INPUT_4 = "sooperlooper:loop0_in_4"
  44. LOOPER_OUTPUT_1 = "sooperlooper:loop0_out_1"
  45. LOOPER_OUTPUT_2 = "sooperlooper:loop0_out_2"
  46. LOOPER_OUTPUT_3 = "sooperlooper:loop0_out_3"
  47. LOOPER_OUTPUT_4 = "sooperlooper:loop0_out_4"
  48. LOOPER2_INPUT_1 = "sooperlooper:loop1_in_1"
  49. LOOPER2_OUTPUT_1 = "sooperlooper:loop1_out_1"
  50. MIDI_BRIDGE_UMC = "Midi-Bridge:UMC404HD 192k MIDI 1 (capture)"
  51. SOOPERLOOPER_MIDI_IN = "sooperlooper:midi_in"
  52. MIX_OUTPUT = "rigforge.direct-mix.output:capture_FL"
  53. PLAYBACK_0 = "alsa_output.hw_U192k_0:playback_AUX0"
  54. class PipeWireObserverTests(unittest.TestCase):
  55. @classmethod
  56. def setUpClass(cls):
  57. cls.desired_state = load_state(ROOT / "config" / "desired-state.yaml")
  58. cls.state = copy.deepcopy(cls.desired_state)
  59. existing_test_edges = {
  60. (CAPTURE_0, AMP_INPUT),
  61. (CAB_OUTPUT, MIX_INPUT_0),
  62. (CAPTURE_2, MIX_INPUT_2),
  63. (MIX_OUTPUT, PLAYBACK_0),
  64. }
  65. cls.state["routing"]["managed_links"] = [
  66. link
  67. for link in cls.state["routing"]["managed_links"]
  68. if (link["from"], link["to"]) in existing_test_edges
  69. ]
  70. cls.state["routing"]["managed_links"].append(
  71. {"from": CAPTURE_2, "to": MIX_INPUT_2, "status": "observed"}
  72. )
  73. cls.state["routing"]["managed_unlinks"] = [
  74. cls.desired_state["routing"]["managed_unlinks"][0]
  75. ]
  76. def test_empty_managed_set_ignores_wireplumber_hardware_links(self):
  77. state = copy.deepcopy(self.state)
  78. state["routing"]["managed_ports"] = []
  79. state["routing"]["managed_links"] = []
  80. observed = parse_pw_link_output(
  81. "alsa_output.usb-umc:Line1.split:output_AUX0\n"
  82. f" |-> {PLAYBACK_0}\n"
  83. f"{CAPTURE_0}\n"
  84. " |-> alsa_input.usb-umc:Mic1.split:input_AUX0\n"
  85. )
  86. self.assertEqual(observe(state, observed), [])
  87. def test_reports_missing_and_unexpected_managed_links_without_mutating(self):
  88. state = copy.deepcopy(self.state)
  89. state["routing"]["managed_ports"] = [CAPTURE_0, CAPTURE_2, CAPTURE_3, PLAYBACK_0]
  90. state["routing"]["managed_links"] = [
  91. {"from": CAPTURE_0, "to": PLAYBACK_0, "status": "planned"},
  92. {"from": CAPTURE_2, "to": PLAYBACK_0, "status": "observed"},
  93. ]
  94. observed = {
  95. (CAPTURE_2, PLAYBACK_0),
  96. (CAPTURE_3, PLAYBACK_0),
  97. }
  98. reports = observe(state, observed)
  99. self.assertEqual(
  100. reports,
  101. [
  102. {"kind": "missing", "from": CAPTURE_0, "to": PLAYBACK_0, "action": "would-link"},
  103. {"kind": "unexpected", "from": CAPTURE_3, "to": PLAYBACK_0, "action": "would-review"},
  104. ],
  105. )
  106. self.assertEqual(state["routing"]["managed_links"][0]["status"], "planned")
  107. def test_parser_handles_incoming_and_outgoing_link_views(self):
  108. links = parse_pw_link_output(
  109. f"{CAPTURE_0}\n"
  110. f" |-> {PLAYBACK_0}\n"
  111. f"{PLAYBACK_0}\n"
  112. f" |<- {CAPTURE_0}\n"
  113. )
  114. self.assertEqual(links, {(CAPTURE_0, PLAYBACK_0)})
  115. def test_quiet_reconcile_suppresses_output_only_when_clean(self):
  116. with (
  117. patch("observe_pipewire.load_state", return_value=self.state),
  118. patch(
  119. "observe_pipewire.reconcile_once",
  120. return_value={
  121. "actions": [],
  122. "differences": [],
  123. },
  124. ),
  125. ):
  126. output = io.StringIO()
  127. with redirect_stdout(output):
  128. result = main(["--reconcile-once", "--quiet-if-clean"])
  129. self.assertEqual(result, 0)
  130. self.assertEqual(output.getvalue(), "")
  131. def test_rejects_logical_mix_endpoint(self):
  132. state = copy.deepcopy(self.state)
  133. state["routing"]["managed_ports"] = [PLAYBACK_0]
  134. state["routing"]["managed_links"] = [
  135. {"from": "direct_mix", "to": PLAYBACK_0, "status": "planned"}
  136. ]
  137. self.assertTrue(any("approved Rigforge boundary, UMC, or MIDI port" in error for error in validate_managed_links(state)))
  138. def test_rejects_unapproved_midi_endpoint(self):
  139. state = copy.deepcopy(self.state)
  140. unexpected = "Midi-Bridge:Unexpected MIDI device (capture)"
  141. state["routing"]["managed_ports"] = [unexpected, SOOPERLOOPER_MIDI_IN]
  142. state["routing"]["managed_links"] = [
  143. {"from": unexpected, "to": SOOPERLOOPER_MIDI_IN, "status": "planned"}
  144. ]
  145. errors = validate_managed_links(state)
  146. self.assertEqual(
  147. sum("approved Rigforge boundary, UMC, or MIDI port" in error for error in errors),
  148. 2,
  149. )
  150. def test_rejects_mod_host_plugin_ports_owned_by_mod_ui(self):
  151. state = copy.deepcopy(self.state)
  152. state["routing"]["managed_ports"].append("effect_0:input")
  153. state["routing"]["managed_links"].append(
  154. {"from": CAPTURE_0, "to": "effect_0:input", "status": "planned"}
  155. )
  156. errors = validate_managed_links(state)
  157. self.assertTrue(any("approved Rigforge boundary" in error for error in errors))
  158. def test_desired_graph_declares_the_tested_pipewire_midi_route(self):
  159. links = {
  160. (link["from"], link["to"])
  161. for link in self.desired_state["routing"]["managed_links"]
  162. }
  163. self.assertIn((MIDI_BRIDGE_UMC, SOOPERLOOPER_MIDI_IN), links)
  164. self.assertIn(MIDI_BRIDGE_UMC, self.desired_state["routing"]["managed_ports"])
  165. self.assertIn(SOOPERLOOPER_MIDI_IN, self.desired_state["routing"]["managed_ports"])
  166. self.assertEqual(validate_managed_links(self.desired_state), [])
  167. def test_reconcile_once_restores_the_pipewire_midi_route(self):
  168. state = copy.deepcopy(self.desired_state)
  169. state["routing"]["managed_ports"] = [MIDI_BRIDGE_UMC, SOOPERLOOPER_MIDI_IN]
  170. state["routing"]["managed_links"] = [
  171. {"from": MIDI_BRIDGE_UMC, "to": SOOPERLOOPER_MIDI_IN, "status": "observed"}
  172. ]
  173. state["routing"]["managed_unlinks"] = []
  174. links = set()
  175. calls = []
  176. def render_links():
  177. return "".join(f"{source}\n |-> {destination}\n" for source, destination in sorted(links))
  178. def runner(command, **kwargs):
  179. calls.append(command)
  180. if command == ["pw-link", "-l"]:
  181. return SimpleNamespace(stdout=render_links())
  182. if command == ["pw-link", "-o"]:
  183. return SimpleNamespace(stdout=f"{MIDI_BRIDGE_UMC}\n")
  184. if command == ["pw-link", "-i"]:
  185. return SimpleNamespace(stdout=f"{SOOPERLOOPER_MIDI_IN}\n")
  186. if command == ["pw-link", MIDI_BRIDGE_UMC, SOOPERLOOPER_MIDI_IN]:
  187. links.add((MIDI_BRIDGE_UMC, SOOPERLOOPER_MIDI_IN))
  188. return SimpleNamespace(stdout="")
  189. self.fail(f"unexpected command: {command}")
  190. result = reconcile_once(state, runner)
  191. self.assertEqual(
  192. result["actions"],
  193. [{
  194. "kind": "link",
  195. "from": MIDI_BRIDGE_UMC,
  196. "to": SOOPERLOOPER_MIDI_IN,
  197. "result": "requested",
  198. }],
  199. )
  200. self.assertEqual(result["differences"], [])
  201. self.assertIn(["pw-link", MIDI_BRIDGE_UMC, SOOPERLOOPER_MIDI_IN], calls)
  202. def test_parser_handles_pipewire_midi_link_names_with_spaces(self):
  203. links = parse_pw_link_output(
  204. f"{MIDI_BRIDGE_UMC}\n"
  205. f" |-> {SOOPERLOOPER_MIDI_IN}\n"
  206. f"{SOOPERLOOPER_MIDI_IN}\n"
  207. f" |<- {MIDI_BRIDGE_UMC}\n"
  208. )
  209. self.assertEqual(links, {(MIDI_BRIDGE_UMC, SOOPERLOOPER_MIDI_IN)})
  210. def test_declared_guitar_effect_chain_and_voice_mix_links_match_observed_graph(self):
  211. links = {
  212. (CAPTURE_0, AMP_INPUT),
  213. (AMP_OUTPUT, CAB_INPUT),
  214. (CAB_OUTPUT, MIX_INPUT_0),
  215. (CAPTURE_2, MIX_INPUT_2),
  216. (MIX_OUTPUT, PLAYBACK_0),
  217. }
  218. self.assertEqual(validate_managed_links(self.state), [])
  219. self.assertEqual(observe(self.state, links), [])
  220. def test_desired_graph_routes_all_sources_through_their_mod_ui_channel_to_mix_and_looper(self):
  221. links = {
  222. (link["from"], link["to"])
  223. for link in self.desired_state["routing"]["managed_links"]
  224. }
  225. self.assertIn((CAB_OUTPUT, MIX_INPUT_0), links)
  226. self.assertIn((CAB_OUTPUT, LOOPER_INPUT_1), links)
  227. self.assertTrue(
  228. {
  229. (CAPTURE_0, AMP_INPUT),
  230. (CAPTURE_1, AMP_INPUT_1),
  231. (CAPTURE_2, AMP_INPUT_2),
  232. (CAPTURE_3, AMP_INPUT_3),
  233. (CAB_OUTPUT, MIX_INPUT_0),
  234. (CAB_OUTPUT_1, "rigforge.direct-mix.input:playback_AUX1"),
  235. (CAB_OUTPUT_2, MIX_INPUT_2),
  236. (CAB_OUTPUT_3, "rigforge.direct-mix.input:playback_AUX3"),
  237. (CAB_OUTPUT, LOOPER_INPUT_1),
  238. (CAB_OUTPUT_1, LOOPER_INPUT_2),
  239. (CAB_OUTPUT_2, LOOPER_INPUT_3),
  240. (CAB_OUTPUT_3, LOOPER_INPUT_4),
  241. }
  242. <= links
  243. )
  244. self.assertTrue(
  245. {
  246. (LOOPER_OUTPUT_1, MIX_INPUT_4),
  247. (LOOPER_OUTPUT_2, MIX_INPUT_5),
  248. (LOOPER_OUTPUT_3, MIX_INPUT_6),
  249. (LOOPER_OUTPUT_4, MIX_INPUT_7),
  250. }
  251. <= links
  252. )
  253. self.assertIn((LOOPER2_OUTPUT_1, MIX_INPUT_8), links)
  254. self.assertFalse(
  255. any(destination.startswith("sooperlooper:loop1_in_") for _, destination in links)
  256. )
  257. self.assertFalse(
  258. any(
  259. source.startswith("sooperlooper:loop1_out_")
  260. and source != LOOPER2_OUTPUT_1
  261. for source, _ in links
  262. )
  263. )
  264. self.assertEqual(validate_managed_links(self.desired_state), [])
  265. self.assertEqual(observe(self.desired_state, links), [])
  266. def test_observer_flags_unexpected_connections_to_second_loop_inputs(self):
  267. reports = observe(self.desired_state, {(CAB_OUTPUT, LOOPER2_INPUT_1)})
  268. self.assertIn(
  269. {
  270. "kind": "unexpected",
  271. "from": CAB_OUTPUT,
  272. "to": LOOPER2_INPUT_1,
  273. "action": "would-review",
  274. },
  275. reports,
  276. )
  277. def test_reports_missing_voice_mix_link_without_expectation_of_dry_guitar(self):
  278. links = {
  279. (CAPTURE_0, AMP_INPUT),
  280. (AMP_OUTPUT, CAB_INPUT),
  281. (CAB_OUTPUT, MIX_INPUT_0),
  282. (MIX_OUTPUT, PLAYBACK_0),
  283. }
  284. reports = observe(self.state, links)
  285. self.assertEqual(
  286. reports,
  287. [
  288. {
  289. "kind": "missing",
  290. "from": CAPTURE_2,
  291. "to": MIX_INPUT_2,
  292. "action": "would-link",
  293. }
  294. ],
  295. )
  296. def test_reports_unexpected_link_between_managed_filter_ports(self):
  297. state = copy.deepcopy(self.state)
  298. state["routing"]["managed_ports"].append(CAPTURE_3)
  299. links = {
  300. (CAPTURE_0, AMP_INPUT),
  301. (AMP_OUTPUT, CAB_INPUT),
  302. (CAB_OUTPUT, MIX_INPUT_0),
  303. (CAPTURE_2, MIX_INPUT_2),
  304. (MIX_OUTPUT, PLAYBACK_0),
  305. (CAPTURE_3, MIX_INPUT_2),
  306. }
  307. reports = observe(state, links)
  308. self.assertEqual(
  309. reports,
  310. [
  311. {
  312. "kind": "unexpected",
  313. "from": CAPTURE_3,
  314. "to": MIX_INPUT_2,
  315. "action": "would-review",
  316. }
  317. ],
  318. )
  319. def test_reports_declared_stale_link_as_would_unlink(self):
  320. links = {
  321. (CAPTURE_0, AMP_INPUT),
  322. (AMP_OUTPUT, CAB_INPUT),
  323. (CAB_OUTPUT, MIX_INPUT_0),
  324. (CAPTURE_2, MIX_INPUT_2),
  325. (MIX_OUTPUT, PLAYBACK_0),
  326. (CAPTURE_0, MIX_INPUT_0),
  327. }
  328. self.assertEqual(
  329. observe(self.state, links),
  330. [
  331. {
  332. "kind": "unexpected",
  333. "from": CAPTURE_0,
  334. "to": MIX_INPUT_0,
  335. "action": "would-unlink",
  336. }
  337. ],
  338. )
  339. def test_reconcile_once_is_noop_when_all_managed_links_exist(self):
  340. calls = []
  341. def runner(command, **kwargs):
  342. calls.append(command)
  343. return SimpleNamespace(stdout=(
  344. f"{CAPTURE_0}\n |-> {AMP_INPUT}\n"
  345. f"{AMP_OUTPUT}\n |-> {CAB_INPUT}\n"
  346. f"{CAB_OUTPUT}\n |-> {MIX_INPUT_0}\n"
  347. f"{CAPTURE_2}\n |-> {MIX_INPUT_2}\n"
  348. f"{MIX_OUTPUT}\n |-> {PLAYBACK_0}\n"
  349. ))
  350. result = reconcile_once(self.state, runner)
  351. self.assertEqual(result["actions"], [])
  352. self.assertEqual(result["differences"], [])
  353. self.assertEqual(calls, [["pw-link", "-l"], ["pw-link", "-l"]])
  354. def test_reconcile_once_links_only_declared_missing_edges_and_verifies(self):
  355. state = copy.deepcopy(self.state)
  356. links = {
  357. (CAPTURE_0, AMP_INPUT),
  358. (AMP_OUTPUT, CAB_INPUT),
  359. (CAB_OUTPUT, MIX_INPUT_0),
  360. (MIX_OUTPUT, PLAYBACK_0),
  361. }
  362. calls = []
  363. def render_links():
  364. return "".join(f"{source}\n |-> {destination}\n" for source, destination in sorted(links))
  365. def runner(command, **kwargs):
  366. calls.append(command)
  367. if command == ["pw-link", "-l"]:
  368. return SimpleNamespace(stdout=render_links())
  369. if command == ["pw-link", "-o"]:
  370. return SimpleNamespace(stdout=(
  371. f"{AMP_OUTPUT}\n{CAB_OUTPUT}\n{CAPTURE_0}\n{CAPTURE_2}\n{MIX_OUTPUT}\n"
  372. ))
  373. if command == ["pw-link", "-i"]:
  374. return SimpleNamespace(stdout=(
  375. f"{AMP_INPUT}\n{CAB_INPUT}\n{MIX_INPUT_0}\n{MIX_INPUT_2}\n{PLAYBACK_0}\n"
  376. ))
  377. if command == ["pw-link", CAPTURE_2, MIX_INPUT_2]:
  378. links.add((CAPTURE_2, MIX_INPUT_2))
  379. return SimpleNamespace(stdout="")
  380. self.fail(f"unexpected command: {command}")
  381. result = reconcile_once(state, runner)
  382. self.assertEqual(
  383. result["actions"],
  384. [{"kind": "link", "from": CAPTURE_2, "to": MIX_INPUT_2, "result": "requested"}],
  385. )
  386. self.assertEqual(result["differences"], [])
  387. self.assertIn(["pw-link", CAPTURE_2, MIX_INPUT_2], calls)
  388. def test_reconcile_once_unlinks_only_declared_stale_edge_after_replacement_exists(self):
  389. state = copy.deepcopy(self.state)
  390. state["routing"]["managed_ports"].append(CAPTURE_3)
  391. stale_link = (CAPTURE_0, MIX_INPUT_0)
  392. unrelated_link = (CAPTURE_3, MIX_INPUT_2)
  393. links = {
  394. stale_link,
  395. unrelated_link,
  396. (CAPTURE_2, MIX_INPUT_2),
  397. (MIX_OUTPUT, PLAYBACK_0),
  398. }
  399. calls = []
  400. def render_links():
  401. return "".join(
  402. f"{source}\n |-> {destination}\n"
  403. for source, destination in sorted(links)
  404. )
  405. def runner(command, **kwargs):
  406. calls.append(command)
  407. if command == ["pw-link", "-l"]:
  408. return SimpleNamespace(stdout=render_links())
  409. if command == ["pw-link", "-o"]:
  410. return SimpleNamespace(stdout=(
  411. f"{AMP_OUTPUT}\n{CAB_OUTPUT}\n{CAPTURE_0}\n{CAPTURE_2}\n{MIX_OUTPUT}\n"
  412. ))
  413. if command == ["pw-link", "-i"]:
  414. return SimpleNamespace(stdout=(
  415. f"{AMP_INPUT}\n{CAB_INPUT}\n{MIX_INPUT_0}\n{MIX_INPUT_2}\n{PLAYBACK_0}\n"
  416. ))
  417. if len(command) == 3 and command[0] == "pw-link":
  418. edge = (command[1], command[2])
  419. if edge in {
  420. (CAPTURE_0, AMP_INPUT),
  421. (CAB_OUTPUT, MIX_INPUT_0),
  422. }:
  423. links.add(edge)
  424. return SimpleNamespace(stdout="")
  425. if len(command) == 4 and command[:2] == ["pw-link", "-d"]:
  426. links.discard((command[2], command[3]))
  427. return SimpleNamespace(stdout="")
  428. self.fail(f"unexpected command: {command}")
  429. result = reconcile_once(state, runner)
  430. self.assertEqual(
  431. result["actions"],
  432. [
  433. {"kind": "link", "from": CAPTURE_0, "to": AMP_INPUT, "result": "requested"},
  434. {"kind": "link", "from": CAB_OUTPUT, "to": MIX_INPUT_0, "result": "requested"},
  435. {"kind": "unlink", "from": CAPTURE_0, "to": MIX_INPUT_0, "result": "requested"},
  436. ],
  437. )
  438. self.assertNotIn(stale_link, links)
  439. self.assertIn(unrelated_link, links)
  440. self.assertEqual(
  441. result["differences"],
  442. [
  443. {
  444. "kind": "unexpected",
  445. "from": CAPTURE_3,
  446. "to": MIX_INPUT_2,
  447. "action": "would-review",
  448. }
  449. ],
  450. )
  451. self.assertIn(["pw-link", "-d", CAPTURE_0, MIX_INPUT_0], calls)
  452. self.assertNotIn(["pw-link", "-d", CAPTURE_3, MIX_INPUT_2], calls)
  453. def test_reconcile_once_defers_unlink_until_all_desired_links_exist(self):
  454. links = {
  455. (CAPTURE_0, MIX_INPUT_0),
  456. (CAPTURE_2, MIX_INPUT_2),
  457. (MIX_OUTPUT, PLAYBACK_0),
  458. }
  459. calls = []
  460. def render_links():
  461. return "".join(f"{source}\n |-> {destination}\n" for source, destination in sorted(links))
  462. def runner(command, **kwargs):
  463. calls.append(command)
  464. if command == ["pw-link", "-l"]:
  465. return SimpleNamespace(stdout=render_links())
  466. if command == ["pw-link", "-o"]:
  467. return SimpleNamespace(stdout=(
  468. f"{AMP_OUTPUT}\n{CAB_OUTPUT}\n{CAPTURE_0}\n{CAPTURE_2}\n{MIX_OUTPUT}\n"
  469. ))
  470. if command == ["pw-link", "-i"]:
  471. return SimpleNamespace(stdout=(
  472. f"{CAB_INPUT}\n{MIX_INPUT_0}\n{MIX_INPUT_2}\n{PLAYBACK_0}\n"
  473. ))
  474. if command == ["pw-link", CAB_OUTPUT, MIX_INPUT_0]:
  475. links.add((CAB_OUTPUT, MIX_INPUT_0))
  476. return SimpleNamespace(stdout="")
  477. self.fail(f"unexpected command: {command}")
  478. result = reconcile_once(self.state, runner)
  479. self.assertEqual(
  480. result["actions"],
  481. [
  482. {
  483. "kind": "unavailable-port",
  484. "from": CAPTURE_0,
  485. "to": AMP_INPUT,
  486. "missing_ports": [{"port": AMP_INPUT, "direction": "input"}],
  487. "result": "not-linked",
  488. "reason": "required PipeWire port(s) are not present in the current graph",
  489. "hint": (
  490. "Rigforge mod-host boundary ports must be running; mod-ui provides the "
  491. "internal NAM/IR connections"
  492. ),
  493. },
  494. {"kind": "link", "from": CAB_OUTPUT, "to": MIX_INPUT_0, "result": "requested"},
  495. {
  496. "kind": "unlink-deferred",
  497. "from": CAPTURE_0,
  498. "to": MIX_INPUT_0,
  499. "result": "desired managed links are still missing",
  500. },
  501. ],
  502. )
  503. self.assertIn((CAPTURE_0, MIX_INPUT_0), links)
  504. self.assertFalse(any(command[:2] == ["pw-link", "-d"] for command in calls))
  505. def test_reconcile_once_restores_all_managed_links_after_service_restart(self):
  506. links = set()
  507. calls = []
  508. def render_links():
  509. return "".join(f"{source}\n |-> {destination}\n" for source, destination in sorted(links))
  510. def runner(command, **kwargs):
  511. calls.append(command)
  512. if command == ["pw-link", "-l"]:
  513. return SimpleNamespace(stdout=render_links())
  514. if command == ["pw-link", "-o"]:
  515. return SimpleNamespace(stdout=(
  516. f"{AMP_OUTPUT}\n{CAB_OUTPUT}\n{CAPTURE_0}\n{CAPTURE_2}\n{MIX_OUTPUT}\n"
  517. ))
  518. if command == ["pw-link", "-i"]:
  519. return SimpleNamespace(stdout=(
  520. f"{AMP_INPUT}\n{CAB_INPUT}\n{MIX_INPUT_0}\n{MIX_INPUT_2}\n{PLAYBACK_0}\n"
  521. ))
  522. if tuple(command) in {
  523. ("pw-link", CAB_OUTPUT, MIX_INPUT_0),
  524. ("pw-link", CAPTURE_0, AMP_INPUT),
  525. ("pw-link", CAPTURE_2, MIX_INPUT_2),
  526. ("pw-link", MIX_OUTPUT, PLAYBACK_0),
  527. }:
  528. links.add((command[1], command[2]))
  529. return SimpleNamespace(stdout="")
  530. self.fail(f"unexpected command: {command}")
  531. result = reconcile_once(self.state, runner)
  532. self.assertEqual(
  533. result["actions"],
  534. [
  535. {"kind": "link", "from": CAPTURE_0, "to": AMP_INPUT, "result": "requested"},
  536. {"kind": "link", "from": CAPTURE_2, "to": MIX_INPUT_2, "result": "requested"},
  537. {"kind": "link", "from": MIX_OUTPUT, "to": PLAYBACK_0, "result": "requested"},
  538. {"kind": "link", "from": CAB_OUTPUT, "to": MIX_INPUT_0, "result": "requested"},
  539. ],
  540. )
  541. self.assertEqual(result["differences"], [])
  542. self.assertEqual(
  543. [command for command in calls if len(command) == 3 and command[0] == "pw-link"],
  544. [
  545. ["pw-link", CAPTURE_0, AMP_INPUT],
  546. ["pw-link", CAPTURE_2, MIX_INPUT_2],
  547. ["pw-link", MIX_OUTPUT, PLAYBACK_0],
  548. ["pw-link", CAB_OUTPUT, MIX_INPUT_0],
  549. ],
  550. )
  551. def test_reconcile_once_does_not_link_when_endpoint_is_unavailable(self):
  552. state = copy.deepcopy(self.state)
  553. links = {
  554. (CAPTURE_0, AMP_INPUT),
  555. (AMP_OUTPUT, CAB_INPUT),
  556. (CAB_OUTPUT, MIX_INPUT_0),
  557. (CAPTURE_2, MIX_INPUT_2),
  558. (MIX_OUTPUT, PLAYBACK_0),
  559. }
  560. links.remove((CAPTURE_2, MIX_INPUT_2))
  561. calls = []
  562. def render_links():
  563. return "".join(f"{source}\n |-> {destination}\n" for source, destination in sorted(links))
  564. def runner(command, **kwargs):
  565. calls.append(command)
  566. if command == ["pw-link", "-l"]:
  567. return SimpleNamespace(stdout=render_links())
  568. if command == ["pw-link", "-o"]:
  569. return SimpleNamespace(stdout=f"{AMP_OUTPUT}\n{CAB_OUTPUT}\n{CAPTURE_0}\n{MIX_OUTPUT}\n")
  570. if command == ["pw-link", "-i"]:
  571. return SimpleNamespace(stdout=(
  572. f"{AMP_INPUT}\n{CAB_INPUT}\n{MIX_INPUT_0}\n{MIX_INPUT_2}\n{PLAYBACK_0}\n"
  573. ))
  574. self.fail(f"unexpected command: {command}")
  575. result = reconcile_once(state, runner)
  576. self.assertEqual(
  577. result["actions"],
  578. [{
  579. "kind": "unavailable-port",
  580. "from": CAPTURE_2,
  581. "to": MIX_INPUT_2,
  582. "missing_ports": [{"port": CAPTURE_2, "direction": "output"}],
  583. "result": "not-linked",
  584. "reason": "required PipeWire port(s) are not present in the current graph",
  585. }],
  586. )
  587. self.assertTrue(result["differences"])
  588. self.assertFalse(any(command[:2] == ["pw-link", CAPTURE_2] for command in calls))
  589. def test_reconcile_once_preserves_unexpected_managed_links(self):
  590. state = copy.deepcopy(self.state)
  591. state["routing"]["managed_ports"].append(CAPTURE_3)
  592. links = {
  593. (CAPTURE_0, AMP_INPUT),
  594. (AMP_OUTPUT, CAB_INPUT),
  595. (CAB_OUTPUT, MIX_INPUT_0),
  596. (CAPTURE_2, MIX_INPUT_2),
  597. (MIX_OUTPUT, PLAYBACK_0),
  598. (CAPTURE_3, MIX_INPUT_2),
  599. }
  600. calls = []
  601. def runner(command, **kwargs):
  602. calls.append(command)
  603. return SimpleNamespace(
  604. stdout="".join(f"{source}\n |-> {destination}\n" for source, destination in sorted(links))
  605. )
  606. result = reconcile_once(state, runner)
  607. self.assertEqual(result["actions"], [])
  608. self.assertEqual(len(result["differences"]), 1)
  609. self.assertEqual(result["differences"][0]["kind"], "unexpected")
  610. self.assertEqual(calls, [["pw-link", "-l"], ["pw-link", "-l"]])
  611. def test_reconcile_once_reports_link_still_missing_after_request(self):
  612. state = copy.deepcopy(self.state)
  613. links = {
  614. (CAPTURE_0, AMP_INPUT),
  615. (AMP_OUTPUT, CAB_INPUT),
  616. (CAB_OUTPUT, MIX_INPUT_0),
  617. (MIX_OUTPUT, PLAYBACK_0),
  618. }
  619. def render_links():
  620. return "".join(f"{source}\n |-> {destination}\n" for source, destination in sorted(links))
  621. def runner(command, **kwargs):
  622. if command == ["pw-link", "-l"]:
  623. return SimpleNamespace(stdout=render_links())
  624. if command == ["pw-link", "-o"]:
  625. return SimpleNamespace(stdout=(
  626. f"{AMP_OUTPUT}\n{CAB_OUTPUT}\n{CAPTURE_0}\n{CAPTURE_2}\n{MIX_OUTPUT}\n"
  627. ))
  628. if command == ["pw-link", "-i"]:
  629. return SimpleNamespace(stdout=(
  630. f"{AMP_INPUT}\n{CAB_INPUT}\n{MIX_INPUT_0}\n{MIX_INPUT_2}\n{PLAYBACK_0}\n"
  631. ))
  632. if command == ["pw-link", CAPTURE_2, MIX_INPUT_2]:
  633. return SimpleNamespace(stdout="")
  634. self.fail(f"unexpected command: {command}")
  635. result = reconcile_once(state, runner)
  636. self.assertEqual(len(result["differences"]), 1)
  637. self.assertEqual(result["differences"][0]["kind"], "missing")
  638. def test_managed_links_must_use_declared_ports(self):
  639. state = copy.deepcopy(self.state)
  640. state["routing"]["managed_ports"].remove(PLAYBACK_0)
  641. state["routing"]["managed_links"] = [
  642. {"from": CAPTURE_0, "to": PLAYBACK_0, "status": "planned"}
  643. ]
  644. self.assertTrue(any("endpoints must be listed" in error for error in validate_managed_links(state)))
  645. def test_managed_link_cannot_also_be_scheduled_for_removal(self):
  646. state = copy.deepcopy(self.state)
  647. state["routing"]["managed_links"].append(
  648. {"from": CAPTURE_0, "to": MIX_INPUT_0, "status": "planned"}
  649. )
  650. self.assertTrue(
  651. any(
  652. "link cannot be both managed and scheduled for removal" in error
  653. for error in validate_managed_links(state)
  654. )
  655. )
  656. if __name__ == "__main__":
  657. unittest.main()