test_pipewire_observer.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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 = "effect_0:input"
  24. AMP_OUTPUT = "effect_0:output"
  25. CAB_INPUT = "effect_1:in"
  26. CAB_OUTPUT = "effect_1:out"
  27. MIX_INPUT_0 = "rigforge.direct-mix.input:playback_AUX0"
  28. MIX_INPUT_2 = "rigforge.direct-mix.input:playback_AUX2"
  29. MIX_INPUT_4 = "rigforge.direct-mix.input:playback_AUX4"
  30. MIX_INPUT_5 = "rigforge.direct-mix.input:playback_AUX5"
  31. MIX_INPUT_6 = "rigforge.direct-mix.input:playback_AUX6"
  32. MIX_INPUT_7 = "rigforge.direct-mix.input:playback_AUX7"
  33. LOOPER_INPUT_1 = "sooperlooper:loop0_in_1"
  34. LOOPER_INPUT_2 = "sooperlooper:loop0_in_2"
  35. LOOPER_INPUT_3 = "sooperlooper:loop0_in_3"
  36. LOOPER_INPUT_4 = "sooperlooper:loop0_in_4"
  37. LOOPER_OUTPUT_1 = "sooperlooper:loop0_out_1"
  38. LOOPER_OUTPUT_2 = "sooperlooper:loop0_out_2"
  39. LOOPER_OUTPUT_3 = "sooperlooper:loop0_out_3"
  40. LOOPER_OUTPUT_4 = "sooperlooper:loop0_out_4"
  41. MIX_OUTPUT = "rigforge.direct-mix.output:capture_FL"
  42. PLAYBACK_0 = "alsa_output.hw_U192k_0:playback_AUX0"
  43. class PipeWireObserverTests(unittest.TestCase):
  44. @classmethod
  45. def setUpClass(cls):
  46. cls.desired_state = load_state(ROOT / "config" / "desired-state.yaml")
  47. cls.state = copy.deepcopy(cls.desired_state)
  48. existing_test_edges = {
  49. (CAPTURE_0, AMP_INPUT),
  50. (AMP_OUTPUT, CAB_INPUT),
  51. (CAB_OUTPUT, MIX_INPUT_0),
  52. (CAPTURE_2, MIX_INPUT_2),
  53. (MIX_OUTPUT, PLAYBACK_0),
  54. }
  55. cls.state["routing"]["managed_links"] = [
  56. link
  57. for link in cls.state["routing"]["managed_links"]
  58. if (link["from"], link["to"]) in existing_test_edges
  59. ]
  60. def test_empty_managed_set_ignores_wireplumber_hardware_links(self):
  61. state = copy.deepcopy(self.state)
  62. state["routing"]["managed_ports"] = []
  63. state["routing"]["managed_links"] = []
  64. observed = parse_pw_link_output(
  65. "alsa_output.usb-umc:Line1.split:output_AUX0\n"
  66. f" |-> {PLAYBACK_0}\n"
  67. f"{CAPTURE_0}\n"
  68. " |-> alsa_input.usb-umc:Mic1.split:input_AUX0\n"
  69. )
  70. self.assertEqual(observe(state, observed), [])
  71. def test_reports_missing_and_unexpected_managed_links_without_mutating(self):
  72. state = copy.deepcopy(self.state)
  73. state["routing"]["managed_ports"] = [CAPTURE_0, CAPTURE_2, CAPTURE_3, PLAYBACK_0]
  74. state["routing"]["managed_links"] = [
  75. {"from": CAPTURE_0, "to": PLAYBACK_0, "status": "planned"},
  76. {"from": CAPTURE_2, "to": PLAYBACK_0, "status": "observed"},
  77. ]
  78. observed = {
  79. (CAPTURE_2, PLAYBACK_0),
  80. (CAPTURE_3, PLAYBACK_0),
  81. }
  82. reports = observe(state, observed)
  83. self.assertEqual(
  84. reports,
  85. [
  86. {"kind": "missing", "from": CAPTURE_0, "to": PLAYBACK_0, "action": "would-link"},
  87. {"kind": "unexpected", "from": CAPTURE_3, "to": PLAYBACK_0, "action": "would-review"},
  88. ],
  89. )
  90. self.assertEqual(state["routing"]["managed_links"][0]["status"], "planned")
  91. def test_parser_handles_incoming_and_outgoing_link_views(self):
  92. links = parse_pw_link_output(
  93. f"{CAPTURE_0}\n"
  94. f" |-> {PLAYBACK_0}\n"
  95. f"{PLAYBACK_0}\n"
  96. f" |<- {CAPTURE_0}\n"
  97. )
  98. self.assertEqual(links, {(CAPTURE_0, PLAYBACK_0)})
  99. def test_quiet_reconcile_suppresses_output_only_when_clean(self):
  100. with (
  101. patch("observe_pipewire.load_state", return_value=self.state),
  102. patch(
  103. "observe_pipewire.reconcile_once",
  104. return_value={
  105. "actions": [],
  106. "differences": [],
  107. },
  108. ),
  109. ):
  110. output = io.StringIO()
  111. with redirect_stdout(output):
  112. result = main(["--reconcile-once", "--quiet-if-clean"])
  113. self.assertEqual(result, 0)
  114. self.assertEqual(output.getvalue(), "")
  115. def test_rejects_logical_mix_endpoint(self):
  116. state = copy.deepcopy(self.state)
  117. state["routing"]["managed_ports"] = [PLAYBACK_0]
  118. state["routing"]["managed_links"] = [
  119. {"from": "direct_mix", "to": PLAYBACK_0, "status": "planned"}
  120. ]
  121. self.assertTrue(any("approved observed Rigforge or UMC port" in error for error in validate_managed_links(state)))
  122. def test_declared_guitar_effect_chain_and_voice_mix_links_match_observed_graph(self):
  123. links = {
  124. (CAPTURE_0, AMP_INPUT),
  125. (AMP_OUTPUT, CAB_INPUT),
  126. (CAB_OUTPUT, MIX_INPUT_0),
  127. (CAPTURE_2, MIX_INPUT_2),
  128. (MIX_OUTPUT, PLAYBACK_0),
  129. }
  130. self.assertEqual(validate_managed_links(self.state), [])
  131. self.assertEqual(observe(self.state, links), [])
  132. def test_desired_graph_keeps_direct_guitar_and_routes_looper_channels_through_mix(self):
  133. links = {
  134. (link["from"], link["to"])
  135. for link in self.desired_state["routing"]["managed_links"]
  136. }
  137. self.assertIn((CAB_OUTPUT, MIX_INPUT_0), links)
  138. self.assertIn((CAB_OUTPUT, LOOPER_INPUT_1), links)
  139. self.assertIn((CAPTURE_1, LOOPER_INPUT_2), links)
  140. self.assertIn((CAPTURE_2, LOOPER_INPUT_3), links)
  141. self.assertIn((CAPTURE_3, LOOPER_INPUT_4), links)
  142. self.assertTrue(
  143. {
  144. (LOOPER_OUTPUT_1, MIX_INPUT_4),
  145. (LOOPER_OUTPUT_2, MIX_INPUT_5),
  146. (LOOPER_OUTPUT_3, MIX_INPUT_6),
  147. (LOOPER_OUTPUT_4, MIX_INPUT_7),
  148. }
  149. <= links
  150. )
  151. self.assertEqual(validate_managed_links(self.desired_state), [])
  152. self.assertEqual(observe(self.desired_state, links), [])
  153. def test_reports_missing_voice_mix_link_without_expectation_of_dry_guitar(self):
  154. links = {
  155. (CAPTURE_0, AMP_INPUT),
  156. (AMP_OUTPUT, CAB_INPUT),
  157. (CAB_OUTPUT, MIX_INPUT_0),
  158. (MIX_OUTPUT, PLAYBACK_0),
  159. }
  160. reports = observe(self.state, links)
  161. self.assertEqual(
  162. reports,
  163. [
  164. {
  165. "kind": "missing",
  166. "from": CAPTURE_2,
  167. "to": MIX_INPUT_2,
  168. "action": "would-link",
  169. }
  170. ],
  171. )
  172. def test_reports_unexpected_link_between_managed_filter_ports(self):
  173. state = copy.deepcopy(self.state)
  174. state["routing"]["managed_ports"].append(CAPTURE_3)
  175. links = {
  176. (CAPTURE_0, AMP_INPUT),
  177. (AMP_OUTPUT, CAB_INPUT),
  178. (CAB_OUTPUT, MIX_INPUT_0),
  179. (CAPTURE_2, MIX_INPUT_2),
  180. (MIX_OUTPUT, PLAYBACK_0),
  181. (CAPTURE_3, MIX_INPUT_2),
  182. }
  183. reports = observe(state, links)
  184. self.assertEqual(
  185. reports,
  186. [
  187. {
  188. "kind": "unexpected",
  189. "from": CAPTURE_3,
  190. "to": MIX_INPUT_2,
  191. "action": "would-review",
  192. }
  193. ],
  194. )
  195. def test_reports_declared_stale_link_as_would_unlink(self):
  196. links = {
  197. (CAPTURE_0, AMP_INPUT),
  198. (AMP_OUTPUT, CAB_INPUT),
  199. (CAB_OUTPUT, MIX_INPUT_0),
  200. (CAPTURE_2, MIX_INPUT_2),
  201. (MIX_OUTPUT, PLAYBACK_0),
  202. (CAPTURE_0, MIX_INPUT_0),
  203. }
  204. self.assertEqual(
  205. observe(self.state, links),
  206. [
  207. {
  208. "kind": "unexpected",
  209. "from": CAPTURE_0,
  210. "to": MIX_INPUT_0,
  211. "action": "would-unlink",
  212. }
  213. ],
  214. )
  215. def test_reconcile_once_is_noop_when_all_managed_links_exist(self):
  216. calls = []
  217. def runner(command, **kwargs):
  218. calls.append(command)
  219. return SimpleNamespace(stdout=(
  220. f"{CAPTURE_0}\n |-> {AMP_INPUT}\n"
  221. f"{AMP_OUTPUT}\n |-> {CAB_INPUT}\n"
  222. f"{CAB_OUTPUT}\n |-> {MIX_INPUT_0}\n"
  223. f"{CAPTURE_2}\n |-> {MIX_INPUT_2}\n"
  224. f"{MIX_OUTPUT}\n |-> {PLAYBACK_0}\n"
  225. ))
  226. result = reconcile_once(self.state, runner)
  227. self.assertEqual(result["actions"], [])
  228. self.assertEqual(result["differences"], [])
  229. self.assertEqual(calls, [["pw-link", "-l"], ["pw-link", "-l"]])
  230. def test_reconcile_once_links_only_declared_missing_edges_and_verifies(self):
  231. state = copy.deepcopy(self.state)
  232. links = {
  233. (CAPTURE_0, AMP_INPUT),
  234. (AMP_OUTPUT, CAB_INPUT),
  235. (CAB_OUTPUT, MIX_INPUT_0),
  236. (MIX_OUTPUT, PLAYBACK_0),
  237. }
  238. calls = []
  239. def render_links():
  240. return "".join(f"{source}\n |-> {destination}\n" for source, destination in sorted(links))
  241. def runner(command, **kwargs):
  242. calls.append(command)
  243. if command == ["pw-link", "-l"]:
  244. return SimpleNamespace(stdout=render_links())
  245. if command == ["pw-link", "-o"]:
  246. return SimpleNamespace(stdout=(
  247. f"{AMP_OUTPUT}\n{CAB_OUTPUT}\n{CAPTURE_0}\n{CAPTURE_2}\n{MIX_OUTPUT}\n"
  248. ))
  249. if command == ["pw-link", "-i"]:
  250. return SimpleNamespace(stdout=(
  251. f"{AMP_INPUT}\n{CAB_INPUT}\n{MIX_INPUT_0}\n{MIX_INPUT_2}\n{PLAYBACK_0}\n"
  252. ))
  253. if command == ["pw-link", CAPTURE_2, MIX_INPUT_2]:
  254. links.add((CAPTURE_2, MIX_INPUT_2))
  255. return SimpleNamespace(stdout="")
  256. self.fail(f"unexpected command: {command}")
  257. result = reconcile_once(state, runner)
  258. self.assertEqual(
  259. result["actions"],
  260. [{"kind": "link", "from": CAPTURE_2, "to": MIX_INPUT_2, "result": "requested"}],
  261. )
  262. self.assertEqual(result["differences"], [])
  263. self.assertIn(["pw-link", CAPTURE_2, MIX_INPUT_2], calls)
  264. def test_reconcile_once_unlinks_only_declared_stale_edge_after_replacement_exists(self):
  265. state = copy.deepcopy(self.state)
  266. state["routing"]["managed_ports"].append(CAPTURE_3)
  267. stale_link = (CAPTURE_0, MIX_INPUT_0)
  268. unrelated_link = (CAPTURE_3, MIX_INPUT_2)
  269. links = {
  270. stale_link,
  271. unrelated_link,
  272. (CAPTURE_2, MIX_INPUT_2),
  273. (MIX_OUTPUT, PLAYBACK_0),
  274. }
  275. calls = []
  276. def render_links():
  277. return "".join(
  278. f"{source}\n |-> {destination}\n"
  279. for source, destination in sorted(links)
  280. )
  281. def runner(command, **kwargs):
  282. calls.append(command)
  283. if command == ["pw-link", "-l"]:
  284. return SimpleNamespace(stdout=render_links())
  285. if command == ["pw-link", "-o"]:
  286. return SimpleNamespace(stdout=(
  287. f"{AMP_OUTPUT}\n{CAB_OUTPUT}\n{CAPTURE_0}\n{CAPTURE_2}\n{MIX_OUTPUT}\n"
  288. ))
  289. if command == ["pw-link", "-i"]:
  290. return SimpleNamespace(stdout=(
  291. f"{AMP_INPUT}\n{CAB_INPUT}\n{MIX_INPUT_0}\n{MIX_INPUT_2}\n{PLAYBACK_0}\n"
  292. ))
  293. if len(command) == 3 and command[0] == "pw-link":
  294. edge = (command[1], command[2])
  295. if edge in {
  296. (CAPTURE_0, AMP_INPUT),
  297. (AMP_OUTPUT, CAB_INPUT),
  298. (CAB_OUTPUT, MIX_INPUT_0),
  299. }:
  300. links.add(edge)
  301. return SimpleNamespace(stdout="")
  302. if len(command) == 4 and command[:2] == ["pw-link", "-d"]:
  303. links.discard((command[2], command[3]))
  304. return SimpleNamespace(stdout="")
  305. self.fail(f"unexpected command: {command}")
  306. result = reconcile_once(state, runner)
  307. self.assertEqual(
  308. result["actions"],
  309. [
  310. {"kind": "link", "from": CAPTURE_0, "to": AMP_INPUT, "result": "requested"},
  311. {"kind": "link", "from": AMP_OUTPUT, "to": CAB_INPUT, "result": "requested"},
  312. {"kind": "link", "from": CAB_OUTPUT, "to": MIX_INPUT_0, "result": "requested"},
  313. {"kind": "unlink", "from": CAPTURE_0, "to": MIX_INPUT_0, "result": "requested"},
  314. ],
  315. )
  316. self.assertNotIn(stale_link, links)
  317. self.assertIn(unrelated_link, links)
  318. self.assertEqual(
  319. result["differences"],
  320. [
  321. {
  322. "kind": "unexpected",
  323. "from": CAPTURE_3,
  324. "to": MIX_INPUT_2,
  325. "action": "would-review",
  326. }
  327. ],
  328. )
  329. self.assertIn(["pw-link", "-d", CAPTURE_0, MIX_INPUT_0], calls)
  330. self.assertNotIn(["pw-link", "-d", CAPTURE_3, MIX_INPUT_2], calls)
  331. def test_reconcile_once_defers_unlink_until_all_desired_links_exist(self):
  332. links = {
  333. (CAPTURE_0, MIX_INPUT_0),
  334. (CAPTURE_2, MIX_INPUT_2),
  335. (MIX_OUTPUT, PLAYBACK_0),
  336. }
  337. calls = []
  338. def render_links():
  339. return "".join(f"{source}\n |-> {destination}\n" for source, destination in sorted(links))
  340. def runner(command, **kwargs):
  341. calls.append(command)
  342. if command == ["pw-link", "-l"]:
  343. return SimpleNamespace(stdout=render_links())
  344. if command == ["pw-link", "-o"]:
  345. return SimpleNamespace(stdout=(
  346. f"{AMP_OUTPUT}\n{CAB_OUTPUT}\n{CAPTURE_0}\n{CAPTURE_2}\n{MIX_OUTPUT}\n"
  347. ))
  348. if command == ["pw-link", "-i"]:
  349. return SimpleNamespace(stdout=(
  350. f"{CAB_INPUT}\n{MIX_INPUT_0}\n{MIX_INPUT_2}\n{PLAYBACK_0}\n"
  351. ))
  352. if command == ["pw-link", AMP_OUTPUT, CAB_INPUT]:
  353. links.add((AMP_OUTPUT, CAB_INPUT))
  354. return SimpleNamespace(stdout="")
  355. if command == ["pw-link", CAB_OUTPUT, MIX_INPUT_0]:
  356. links.add((CAB_OUTPUT, MIX_INPUT_0))
  357. return SimpleNamespace(stdout="")
  358. self.fail(f"unexpected command: {command}")
  359. result = reconcile_once(self.state, runner)
  360. self.assertEqual(
  361. result["actions"],
  362. [
  363. {
  364. "kind": "unavailable-port",
  365. "from": CAPTURE_0,
  366. "to": AMP_INPUT,
  367. "missing_ports": [{"port": AMP_INPUT, "direction": "input"}],
  368. "result": "not-linked",
  369. "reason": "required PipeWire port(s) are not present in the current graph",
  370. "hint": (
  371. "mod-host must be running and load the NAM/IR plugins to expose "
  372. "effect_0/effect_1 ports"
  373. ),
  374. },
  375. {"kind": "link", "from": AMP_OUTPUT, "to": CAB_INPUT, "result": "requested"},
  376. {"kind": "link", "from": CAB_OUTPUT, "to": MIX_INPUT_0, "result": "requested"},
  377. {
  378. "kind": "unlink-deferred",
  379. "from": CAPTURE_0,
  380. "to": MIX_INPUT_0,
  381. "result": "desired managed links are still missing",
  382. },
  383. ],
  384. )
  385. self.assertIn((CAPTURE_0, MIX_INPUT_0), links)
  386. self.assertFalse(any(command[:2] == ["pw-link", "-d"] for command in calls))
  387. def test_reconcile_once_restores_all_managed_links_after_service_restart(self):
  388. links = set()
  389. calls = []
  390. def render_links():
  391. return "".join(f"{source}\n |-> {destination}\n" for source, destination in sorted(links))
  392. def runner(command, **kwargs):
  393. calls.append(command)
  394. if command == ["pw-link", "-l"]:
  395. return SimpleNamespace(stdout=render_links())
  396. if command == ["pw-link", "-o"]:
  397. return SimpleNamespace(stdout=(
  398. f"{AMP_OUTPUT}\n{CAB_OUTPUT}\n{CAPTURE_0}\n{CAPTURE_2}\n{MIX_OUTPUT}\n"
  399. ))
  400. if command == ["pw-link", "-i"]:
  401. return SimpleNamespace(stdout=(
  402. f"{AMP_INPUT}\n{CAB_INPUT}\n{MIX_INPUT_0}\n{MIX_INPUT_2}\n{PLAYBACK_0}\n"
  403. ))
  404. if tuple(command) in {
  405. ("pw-link", AMP_OUTPUT, CAB_INPUT),
  406. ("pw-link", CAB_OUTPUT, MIX_INPUT_0),
  407. ("pw-link", CAPTURE_0, AMP_INPUT),
  408. ("pw-link", CAPTURE_2, MIX_INPUT_2),
  409. ("pw-link", MIX_OUTPUT, PLAYBACK_0),
  410. }:
  411. links.add((command[1], command[2]))
  412. return SimpleNamespace(stdout="")
  413. self.fail(f"unexpected command: {command}")
  414. result = reconcile_once(self.state, runner)
  415. self.assertEqual(
  416. result["actions"],
  417. [
  418. {"kind": "link", "from": CAPTURE_0, "to": AMP_INPUT, "result": "requested"},
  419. {"kind": "link", "from": CAPTURE_2, "to": MIX_INPUT_2, "result": "requested"},
  420. {"kind": "link", "from": AMP_OUTPUT, "to": CAB_INPUT, "result": "requested"},
  421. {"kind": "link", "from": CAB_OUTPUT, "to": MIX_INPUT_0, "result": "requested"},
  422. {"kind": "link", "from": MIX_OUTPUT, "to": PLAYBACK_0, "result": "requested"},
  423. ],
  424. )
  425. self.assertEqual(result["differences"], [])
  426. self.assertEqual(
  427. [command for command in calls if len(command) == 3 and command[0] == "pw-link"],
  428. [
  429. ["pw-link", CAPTURE_0, AMP_INPUT],
  430. ["pw-link", CAPTURE_2, MIX_INPUT_2],
  431. ["pw-link", AMP_OUTPUT, CAB_INPUT],
  432. ["pw-link", CAB_OUTPUT, MIX_INPUT_0],
  433. ["pw-link", MIX_OUTPUT, PLAYBACK_0],
  434. ],
  435. )
  436. def test_reconcile_once_does_not_link_when_endpoint_is_unavailable(self):
  437. state = copy.deepcopy(self.state)
  438. links = {
  439. (CAPTURE_0, AMP_INPUT),
  440. (AMP_OUTPUT, CAB_INPUT),
  441. (CAB_OUTPUT, MIX_INPUT_0),
  442. (CAPTURE_2, MIX_INPUT_2),
  443. (MIX_OUTPUT, PLAYBACK_0),
  444. }
  445. links.remove((CAPTURE_2, MIX_INPUT_2))
  446. calls = []
  447. def render_links():
  448. return "".join(f"{source}\n |-> {destination}\n" for source, destination in sorted(links))
  449. def runner(command, **kwargs):
  450. calls.append(command)
  451. if command == ["pw-link", "-l"]:
  452. return SimpleNamespace(stdout=render_links())
  453. if command == ["pw-link", "-o"]:
  454. return SimpleNamespace(stdout=f"{AMP_OUTPUT}\n{CAB_OUTPUT}\n{CAPTURE_0}\n{MIX_OUTPUT}\n")
  455. if command == ["pw-link", "-i"]:
  456. return SimpleNamespace(stdout=(
  457. f"{AMP_INPUT}\n{CAB_INPUT}\n{MIX_INPUT_0}\n{MIX_INPUT_2}\n{PLAYBACK_0}\n"
  458. ))
  459. self.fail(f"unexpected command: {command}")
  460. result = reconcile_once(state, runner)
  461. self.assertEqual(
  462. result["actions"],
  463. [{
  464. "kind": "unavailable-port",
  465. "from": CAPTURE_2,
  466. "to": MIX_INPUT_2,
  467. "missing_ports": [{"port": CAPTURE_2, "direction": "output"}],
  468. "result": "not-linked",
  469. "reason": "required PipeWire port(s) are not present in the current graph",
  470. }],
  471. )
  472. self.assertTrue(result["differences"])
  473. self.assertFalse(any(command[:2] == ["pw-link", CAPTURE_2] for command in calls))
  474. def test_reconcile_once_preserves_unexpected_managed_links(self):
  475. state = copy.deepcopy(self.state)
  476. state["routing"]["managed_ports"].append(CAPTURE_3)
  477. links = {
  478. (CAPTURE_0, AMP_INPUT),
  479. (AMP_OUTPUT, CAB_INPUT),
  480. (CAB_OUTPUT, MIX_INPUT_0),
  481. (CAPTURE_2, MIX_INPUT_2),
  482. (MIX_OUTPUT, PLAYBACK_0),
  483. (CAPTURE_3, MIX_INPUT_2),
  484. }
  485. calls = []
  486. def runner(command, **kwargs):
  487. calls.append(command)
  488. return SimpleNamespace(
  489. stdout="".join(f"{source}\n |-> {destination}\n" for source, destination in sorted(links))
  490. )
  491. result = reconcile_once(state, runner)
  492. self.assertEqual(result["actions"], [])
  493. self.assertEqual(len(result["differences"]), 1)
  494. self.assertEqual(result["differences"][0]["kind"], "unexpected")
  495. self.assertEqual(calls, [["pw-link", "-l"], ["pw-link", "-l"]])
  496. def test_reconcile_once_reports_link_still_missing_after_request(self):
  497. state = copy.deepcopy(self.state)
  498. links = {
  499. (CAPTURE_0, AMP_INPUT),
  500. (AMP_OUTPUT, CAB_INPUT),
  501. (CAB_OUTPUT, MIX_INPUT_0),
  502. (MIX_OUTPUT, PLAYBACK_0),
  503. }
  504. def render_links():
  505. return "".join(f"{source}\n |-> {destination}\n" for source, destination in sorted(links))
  506. def runner(command, **kwargs):
  507. if command == ["pw-link", "-l"]:
  508. return SimpleNamespace(stdout=render_links())
  509. if command == ["pw-link", "-o"]:
  510. return SimpleNamespace(stdout=(
  511. f"{AMP_OUTPUT}\n{CAB_OUTPUT}\n{CAPTURE_0}\n{CAPTURE_2}\n{MIX_OUTPUT}\n"
  512. ))
  513. if command == ["pw-link", "-i"]:
  514. return SimpleNamespace(stdout=(
  515. f"{AMP_INPUT}\n{CAB_INPUT}\n{MIX_INPUT_0}\n{MIX_INPUT_2}\n{PLAYBACK_0}\n"
  516. ))
  517. if command == ["pw-link", CAPTURE_2, MIX_INPUT_2]:
  518. return SimpleNamespace(stdout="")
  519. self.fail(f"unexpected command: {command}")
  520. result = reconcile_once(state, runner)
  521. self.assertEqual(len(result["differences"]), 1)
  522. self.assertEqual(result["differences"][0]["kind"], "missing")
  523. def test_managed_links_must_use_declared_ports(self):
  524. state = copy.deepcopy(self.state)
  525. state["routing"]["managed_ports"].remove(PLAYBACK_0)
  526. state["routing"]["managed_links"] = [
  527. {"from": CAPTURE_0, "to": PLAYBACK_0, "status": "planned"}
  528. ]
  529. self.assertTrue(any("endpoints must be listed" in error for error in validate_managed_links(state)))
  530. def test_managed_link_cannot_also_be_scheduled_for_removal(self):
  531. state = copy.deepcopy(self.state)
  532. state["routing"]["managed_links"].append(
  533. {"from": CAPTURE_0, "to": MIX_INPUT_0, "status": "planned"}
  534. )
  535. self.assertTrue(
  536. any(
  537. "link cannot be both managed and scheduled for removal" in error
  538. for error in validate_managed_links(state)
  539. )
  540. )
  541. if __name__ == "__main__":
  542. unittest.main()