test_pipewire_observer.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import copy
  2. import sys
  3. import unittest
  4. from pathlib import Path
  5. ROOT = Path(__file__).resolve().parents[1]
  6. sys.path.insert(0, str(ROOT / "scripts"))
  7. from observe_pipewire import load_state, observe, parse_pw_link_output, validate_managed_links
  8. CAPTURE_0 = "alsa_input.hw_U192k_0:capture_AUX0"
  9. CAPTURE_2 = "alsa_input.hw_U192k_0:capture_AUX2"
  10. CAPTURE_3 = "alsa_input.hw_U192k_0:capture_AUX3"
  11. PLAYBACK_0 = "alsa_output.hw_U192k_0:playback_AUX0"
  12. class PipeWireObserverTests(unittest.TestCase):
  13. @classmethod
  14. def setUpClass(cls):
  15. cls.state = load_state(ROOT / "config" / "desired-state.yaml")
  16. def test_empty_managed_set_ignores_wireplumber_hardware_links(self):
  17. observed = parse_pw_link_output(
  18. "alsa_output.usb-umc:Line1.split:output_AUX0\n"
  19. f" |-> {PLAYBACK_0}\n"
  20. f"{CAPTURE_0}\n"
  21. " |-> alsa_input.usb-umc:Mic1.split:input_AUX0\n"
  22. )
  23. self.assertEqual(observe(self.state, observed), [])
  24. def test_reports_missing_and_unexpected_managed_links_without_mutating(self):
  25. state = copy.deepcopy(self.state)
  26. state["routing"]["managed_ports"] = [CAPTURE_0, CAPTURE_2, CAPTURE_3, PLAYBACK_0]
  27. state["routing"]["managed_links"] = [
  28. {"from": CAPTURE_0, "to": PLAYBACK_0, "status": "planned"},
  29. {"from": CAPTURE_2, "to": PLAYBACK_0, "status": "observed"},
  30. ]
  31. observed = {
  32. (CAPTURE_2, PLAYBACK_0),
  33. (CAPTURE_3, PLAYBACK_0),
  34. }
  35. reports = observe(state, observed)
  36. self.assertEqual(
  37. reports,
  38. [
  39. {"kind": "missing", "from": CAPTURE_0, "to": PLAYBACK_0, "action": "would-link"},
  40. {"kind": "unexpected", "from": CAPTURE_3, "to": PLAYBACK_0, "action": "would-review"},
  41. ],
  42. )
  43. self.assertEqual(state["routing"]["managed_links"][0]["status"], "planned")
  44. def test_parser_handles_incoming_and_outgoing_link_views(self):
  45. links = parse_pw_link_output(
  46. f"{CAPTURE_0}\n"
  47. f" |-> {PLAYBACK_0}\n"
  48. f"{PLAYBACK_0}\n"
  49. f" |<- {CAPTURE_0}\n"
  50. )
  51. self.assertEqual(links, {(CAPTURE_0, PLAYBACK_0)})
  52. def test_rejects_logical_mix_endpoint(self):
  53. state = copy.deepcopy(self.state)
  54. state["routing"]["managed_ports"] = [PLAYBACK_0]
  55. state["routing"]["managed_links"] = [
  56. {"from": "direct_mix", "to": PLAYBACK_0, "status": "planned"}
  57. ]
  58. self.assertTrue(any("concrete UMC AUX port" in error for error in validate_managed_links(state)))
  59. def test_managed_links_must_use_declared_ports(self):
  60. state = copy.deepcopy(self.state)
  61. state["routing"]["managed_links"] = [
  62. {"from": CAPTURE_0, "to": PLAYBACK_0, "status": "planned"}
  63. ]
  64. self.assertTrue(any("endpoints must be listed" in error for error in validate_managed_links(state)))
  65. if __name__ == "__main__":
  66. unittest.main()