| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726 |
- import io
- import copy
- import sys
- import unittest
- from contextlib import redirect_stdout
- from pathlib import Path
- from types import SimpleNamespace
- from unittest.mock import patch
- ROOT = Path(__file__).resolve().parents[1]
- sys.path.insert(0, str(ROOT / "scripts"))
- from observe_pipewire import (
- load_state,
- observe,
- parse_pw_link_output,
- reconcile_once,
- main,
- validate_managed_links,
- )
- CAPTURE_0 = "alsa_input.hw_U192k_0:capture_AUX0"
- CAPTURE_1 = "alsa_input.hw_U192k_0:capture_AUX1"
- CAPTURE_2 = "alsa_input.hw_U192k_0:capture_AUX2"
- CAPTURE_3 = "alsa_input.hw_U192k_0:capture_AUX3"
- AMP_INPUT = "rigforge.mod-host-input:playback_AUX0"
- AMP_INPUT_1 = "rigforge.mod-host-input:playback_AUX1"
- AMP_INPUT_2 = "rigforge.mod-host-input:playback_AUX2"
- AMP_INPUT_3 = "rigforge.mod-host-input:playback_AUX3"
- AMP_OUTPUT = "effect_0:output"
- CAB_INPUT = "effect_1:in"
- CAB_OUTPUT = "rigforge.mod-host-output:capture_AUX0"
- CAB_OUTPUT_1 = "rigforge.mod-host-output:capture_AUX1"
- CAB_OUTPUT_2 = "rigforge.mod-host-output:capture_AUX2"
- CAB_OUTPUT_3 = "rigforge.mod-host-output:capture_AUX3"
- MIX_INPUT_0 = "rigforge.direct-mix.input:playback_AUX0"
- MIX_INPUT_2 = "rigforge.direct-mix.input:playback_AUX2"
- MIX_INPUT_4 = "rigforge.direct-mix.input:playback_AUX4"
- MIX_INPUT_5 = "rigforge.direct-mix.input:playback_AUX5"
- MIX_INPUT_6 = "rigforge.direct-mix.input:playback_AUX6"
- MIX_INPUT_7 = "rigforge.direct-mix.input:playback_AUX7"
- MIX_INPUT_8 = "rigforge.direct-mix.input:playback_AUX8"
- LOOPER_INPUT_1 = "sooperlooper:loop0_in_1"
- LOOPER_INPUT_2 = "sooperlooper:loop0_in_2"
- LOOPER_INPUT_3 = "sooperlooper:loop0_in_3"
- LOOPER_INPUT_4 = "sooperlooper:loop0_in_4"
- LOOPER_OUTPUT_1 = "sooperlooper:loop0_out_1"
- LOOPER_OUTPUT_2 = "sooperlooper:loop0_out_2"
- LOOPER_OUTPUT_3 = "sooperlooper:loop0_out_3"
- LOOPER_OUTPUT_4 = "sooperlooper:loop0_out_4"
- LOOPER2_INPUT_1 = "sooperlooper:loop1_in_1"
- LOOPER2_OUTPUT_1 = "sooperlooper:loop1_out_1"
- MIDI_BRIDGE_UMC = "Midi-Bridge:UMC404HD 192k MIDI 1 (capture)"
- SOOPERLOOPER_MIDI_IN = "sooperlooper:midi_in"
- MIX_OUTPUT = "rigforge.direct-mix.output:capture_FL"
- PLAYBACK_0 = "alsa_output.hw_U192k_0:playback_AUX0"
- class PipeWireObserverTests(unittest.TestCase):
- @classmethod
- def setUpClass(cls):
- cls.desired_state = load_state(ROOT / "config" / "desired-state.yaml")
- cls.state = copy.deepcopy(cls.desired_state)
- existing_test_edges = {
- (CAPTURE_0, AMP_INPUT),
- (CAB_OUTPUT, MIX_INPUT_0),
- (CAPTURE_2, MIX_INPUT_2),
- (MIX_OUTPUT, PLAYBACK_0),
- }
- cls.state["routing"]["managed_links"] = [
- link
- for link in cls.state["routing"]["managed_links"]
- if (link["from"], link["to"]) in existing_test_edges
- ]
- cls.state["routing"]["managed_links"].append(
- {"from": CAPTURE_2, "to": MIX_INPUT_2, "status": "observed"}
- )
- cls.state["routing"]["managed_unlinks"] = [
- cls.desired_state["routing"]["managed_unlinks"][0]
- ]
- def test_empty_managed_set_ignores_wireplumber_hardware_links(self):
- state = copy.deepcopy(self.state)
- state["routing"]["managed_ports"] = []
- state["routing"]["managed_links"] = []
- observed = parse_pw_link_output(
- "alsa_output.usb-umc:Line1.split:output_AUX0\n"
- f" |-> {PLAYBACK_0}\n"
- f"{CAPTURE_0}\n"
- " |-> alsa_input.usb-umc:Mic1.split:input_AUX0\n"
- )
- self.assertEqual(observe(state, observed), [])
- def test_reports_missing_and_unexpected_managed_links_without_mutating(self):
- state = copy.deepcopy(self.state)
- state["routing"]["managed_ports"] = [CAPTURE_0, CAPTURE_2, CAPTURE_3, PLAYBACK_0]
- state["routing"]["managed_links"] = [
- {"from": CAPTURE_0, "to": PLAYBACK_0, "status": "planned"},
- {"from": CAPTURE_2, "to": PLAYBACK_0, "status": "observed"},
- ]
- observed = {
- (CAPTURE_2, PLAYBACK_0),
- (CAPTURE_3, PLAYBACK_0),
- }
- reports = observe(state, observed)
- self.assertEqual(
- reports,
- [
- {"kind": "missing", "from": CAPTURE_0, "to": PLAYBACK_0, "action": "would-link"},
- {"kind": "unexpected", "from": CAPTURE_3, "to": PLAYBACK_0, "action": "would-review"},
- ],
- )
- self.assertEqual(state["routing"]["managed_links"][0]["status"], "planned")
- def test_parser_handles_incoming_and_outgoing_link_views(self):
- links = parse_pw_link_output(
- f"{CAPTURE_0}\n"
- f" |-> {PLAYBACK_0}\n"
- f"{PLAYBACK_0}\n"
- f" |<- {CAPTURE_0}\n"
- )
- self.assertEqual(links, {(CAPTURE_0, PLAYBACK_0)})
- def test_quiet_reconcile_suppresses_output_only_when_clean(self):
- with (
- patch("observe_pipewire.load_state", return_value=self.state),
- patch(
- "observe_pipewire.reconcile_once",
- return_value={
- "actions": [],
- "differences": [],
- },
- ),
- ):
- output = io.StringIO()
- with redirect_stdout(output):
- result = main(["--reconcile-once", "--quiet-if-clean"])
- self.assertEqual(result, 0)
- self.assertEqual(output.getvalue(), "")
- def test_rejects_logical_mix_endpoint(self):
- state = copy.deepcopy(self.state)
- state["routing"]["managed_ports"] = [PLAYBACK_0]
- state["routing"]["managed_links"] = [
- {"from": "direct_mix", "to": PLAYBACK_0, "status": "planned"}
- ]
- self.assertTrue(any("approved Rigforge boundary, UMC, or MIDI port" in error for error in validate_managed_links(state)))
- def test_rejects_unapproved_midi_endpoint(self):
- state = copy.deepcopy(self.state)
- unexpected = "Midi-Bridge:Unexpected MIDI device (capture)"
- state["routing"]["managed_ports"] = [unexpected, SOOPERLOOPER_MIDI_IN]
- state["routing"]["managed_links"] = [
- {"from": unexpected, "to": SOOPERLOOPER_MIDI_IN, "status": "planned"}
- ]
- errors = validate_managed_links(state)
- self.assertEqual(
- sum("approved Rigforge boundary, UMC, or MIDI port" in error for error in errors),
- 2,
- )
- def test_rejects_mod_host_plugin_ports_owned_by_mod_ui(self):
- state = copy.deepcopy(self.state)
- state["routing"]["managed_ports"].append("effect_0:input")
- state["routing"]["managed_links"].append(
- {"from": CAPTURE_0, "to": "effect_0:input", "status": "planned"}
- )
- errors = validate_managed_links(state)
- self.assertTrue(any("approved Rigforge boundary" in error for error in errors))
- def test_desired_graph_declares_the_tested_pipewire_midi_route(self):
- links = {
- (link["from"], link["to"])
- for link in self.desired_state["routing"]["managed_links"]
- }
- self.assertIn((MIDI_BRIDGE_UMC, SOOPERLOOPER_MIDI_IN), links)
- self.assertIn(MIDI_BRIDGE_UMC, self.desired_state["routing"]["managed_ports"])
- self.assertIn(SOOPERLOOPER_MIDI_IN, self.desired_state["routing"]["managed_ports"])
- self.assertEqual(validate_managed_links(self.desired_state), [])
- def test_reconcile_once_restores_the_pipewire_midi_route(self):
- state = copy.deepcopy(self.desired_state)
- state["routing"]["managed_ports"] = [MIDI_BRIDGE_UMC, SOOPERLOOPER_MIDI_IN]
- state["routing"]["managed_links"] = [
- {"from": MIDI_BRIDGE_UMC, "to": SOOPERLOOPER_MIDI_IN, "status": "observed"}
- ]
- state["routing"]["managed_unlinks"] = []
- links = set()
- 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"{MIDI_BRIDGE_UMC}\n")
- if command == ["pw-link", "-i"]:
- return SimpleNamespace(stdout=f"{SOOPERLOOPER_MIDI_IN}\n")
- if command == ["pw-link", MIDI_BRIDGE_UMC, SOOPERLOOPER_MIDI_IN]:
- links.add((MIDI_BRIDGE_UMC, SOOPERLOOPER_MIDI_IN))
- return SimpleNamespace(stdout="")
- self.fail(f"unexpected command: {command}")
- result = reconcile_once(state, runner)
- self.assertEqual(
- result["actions"],
- [{
- "kind": "link",
- "from": MIDI_BRIDGE_UMC,
- "to": SOOPERLOOPER_MIDI_IN,
- "result": "requested",
- }],
- )
- self.assertEqual(result["differences"], [])
- self.assertIn(["pw-link", MIDI_BRIDGE_UMC, SOOPERLOOPER_MIDI_IN], calls)
- def test_parser_handles_pipewire_midi_link_names_with_spaces(self):
- links = parse_pw_link_output(
- f"{MIDI_BRIDGE_UMC}\n"
- f" |-> {SOOPERLOOPER_MIDI_IN}\n"
- f"{SOOPERLOOPER_MIDI_IN}\n"
- f" |<- {MIDI_BRIDGE_UMC}\n"
- )
- self.assertEqual(links, {(MIDI_BRIDGE_UMC, SOOPERLOOPER_MIDI_IN)})
- def test_declared_guitar_effect_chain_and_voice_mix_links_match_observed_graph(self):
- links = {
- (CAPTURE_0, AMP_INPUT),
- (AMP_OUTPUT, CAB_INPUT),
- (CAB_OUTPUT, MIX_INPUT_0),
- (CAPTURE_2, MIX_INPUT_2),
- (MIX_OUTPUT, PLAYBACK_0),
- }
- self.assertEqual(validate_managed_links(self.state), [])
- self.assertEqual(observe(self.state, links), [])
- def test_desired_graph_routes_all_sources_through_their_mod_ui_channel_to_mix_and_looper(self):
- links = {
- (link["from"], link["to"])
- for link in self.desired_state["routing"]["managed_links"]
- }
- self.assertIn((CAB_OUTPUT, MIX_INPUT_0), links)
- self.assertIn((CAB_OUTPUT, LOOPER_INPUT_1), links)
- self.assertTrue(
- {
- (CAPTURE_0, AMP_INPUT),
- (CAPTURE_1, AMP_INPUT_1),
- (CAPTURE_2, AMP_INPUT_2),
- (CAPTURE_3, AMP_INPUT_3),
- (CAB_OUTPUT, MIX_INPUT_0),
- (CAB_OUTPUT_1, "rigforge.direct-mix.input:playback_AUX1"),
- (CAB_OUTPUT_2, MIX_INPUT_2),
- (CAB_OUTPUT_3, "rigforge.direct-mix.input:playback_AUX3"),
- (CAB_OUTPUT, LOOPER_INPUT_1),
- (CAB_OUTPUT_1, LOOPER_INPUT_2),
- (CAB_OUTPUT_2, LOOPER_INPUT_3),
- (CAB_OUTPUT_3, LOOPER_INPUT_4),
- }
- <= links
- )
- self.assertTrue(
- {
- (LOOPER_OUTPUT_1, MIX_INPUT_4),
- (LOOPER_OUTPUT_2, MIX_INPUT_5),
- (LOOPER_OUTPUT_3, MIX_INPUT_6),
- (LOOPER_OUTPUT_4, MIX_INPUT_7),
- }
- <= links
- )
- self.assertIn((LOOPER2_OUTPUT_1, MIX_INPUT_8), links)
- self.assertFalse(
- any(destination.startswith("sooperlooper:loop1_in_") for _, destination in links)
- )
- self.assertFalse(
- any(
- source.startswith("sooperlooper:loop1_out_")
- and source != LOOPER2_OUTPUT_1
- for source, _ in links
- )
- )
- self.assertEqual(validate_managed_links(self.desired_state), [])
- self.assertEqual(observe(self.desired_state, links), [])
- def test_observer_flags_unexpected_connections_to_second_loop_inputs(self):
- reports = observe(self.desired_state, {(CAB_OUTPUT, LOOPER2_INPUT_1)})
- self.assertIn(
- {
- "kind": "unexpected",
- "from": CAB_OUTPUT,
- "to": LOOPER2_INPUT_1,
- "action": "would-review",
- },
- reports,
- )
- def test_reports_missing_voice_mix_link_without_expectation_of_dry_guitar(self):
- links = {
- (CAPTURE_0, AMP_INPUT),
- (AMP_OUTPUT, CAB_INPUT),
- (CAB_OUTPUT, MIX_INPUT_0),
- (MIX_OUTPUT, PLAYBACK_0),
- }
- reports = observe(self.state, links)
- self.assertEqual(
- reports,
- [
- {
- "kind": "missing",
- "from": CAPTURE_2,
- "to": MIX_INPUT_2,
- "action": "would-link",
- }
- ],
- )
- def test_reports_unexpected_link_between_managed_filter_ports(self):
- state = copy.deepcopy(self.state)
- state["routing"]["managed_ports"].append(CAPTURE_3)
- links = {
- (CAPTURE_0, AMP_INPUT),
- (AMP_OUTPUT, CAB_INPUT),
- (CAB_OUTPUT, MIX_INPUT_0),
- (CAPTURE_2, MIX_INPUT_2),
- (MIX_OUTPUT, PLAYBACK_0),
- (CAPTURE_3, MIX_INPUT_2),
- }
- reports = observe(state, links)
- self.assertEqual(
- reports,
- [
- {
- "kind": "unexpected",
- "from": CAPTURE_3,
- "to": MIX_INPUT_2,
- "action": "would-review",
- }
- ],
- )
- def test_reports_declared_stale_link_as_would_unlink(self):
- links = {
- (CAPTURE_0, AMP_INPUT),
- (AMP_OUTPUT, CAB_INPUT),
- (CAB_OUTPUT, MIX_INPUT_0),
- (CAPTURE_2, MIX_INPUT_2),
- (MIX_OUTPUT, PLAYBACK_0),
- (CAPTURE_0, MIX_INPUT_0),
- }
- self.assertEqual(
- observe(self.state, links),
- [
- {
- "kind": "unexpected",
- "from": CAPTURE_0,
- "to": MIX_INPUT_0,
- "action": "would-unlink",
- }
- ],
- )
- 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 |-> {AMP_INPUT}\n"
- f"{AMP_OUTPUT}\n |-> {CAB_INPUT}\n"
- f"{CAB_OUTPUT}\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, AMP_INPUT),
- (AMP_OUTPUT, CAB_INPUT),
- (CAB_OUTPUT, 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"{AMP_OUTPUT}\n{CAB_OUTPUT}\n{CAPTURE_0}\n{CAPTURE_2}\n{MIX_OUTPUT}\n"
- ))
- if command == ["pw-link", "-i"]:
- return SimpleNamespace(stdout=(
- f"{AMP_INPUT}\n{CAB_INPUT}\n{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_unlinks_only_declared_stale_edge_after_replacement_exists(self):
- state = copy.deepcopy(self.state)
- state["routing"]["managed_ports"].append(CAPTURE_3)
- stale_link = (CAPTURE_0, MIX_INPUT_0)
- unrelated_link = (CAPTURE_3, MIX_INPUT_2)
- links = {
- stale_link,
- unrelated_link,
- (CAPTURE_2, MIX_INPUT_2),
- (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"{AMP_OUTPUT}\n{CAB_OUTPUT}\n{CAPTURE_0}\n{CAPTURE_2}\n{MIX_OUTPUT}\n"
- ))
- if command == ["pw-link", "-i"]:
- return SimpleNamespace(stdout=(
- f"{AMP_INPUT}\n{CAB_INPUT}\n{MIX_INPUT_0}\n{MIX_INPUT_2}\n{PLAYBACK_0}\n"
- ))
- if len(command) == 3 and command[0] == "pw-link":
- edge = (command[1], command[2])
- if edge in {
- (CAPTURE_0, AMP_INPUT),
- (CAB_OUTPUT, MIX_INPUT_0),
- }:
- links.add(edge)
- return SimpleNamespace(stdout="")
- if len(command) == 4 and command[:2] == ["pw-link", "-d"]:
- links.discard((command[2], command[3]))
- return SimpleNamespace(stdout="")
- self.fail(f"unexpected command: {command}")
- result = reconcile_once(state, runner)
- self.assertEqual(
- result["actions"],
- [
- {"kind": "link", "from": CAPTURE_0, "to": AMP_INPUT, "result": "requested"},
- {"kind": "link", "from": CAB_OUTPUT, "to": MIX_INPUT_0, "result": "requested"},
- {"kind": "unlink", "from": CAPTURE_0, "to": MIX_INPUT_0, "result": "requested"},
- ],
- )
- self.assertNotIn(stale_link, links)
- self.assertIn(unrelated_link, links)
- self.assertEqual(
- result["differences"],
- [
- {
- "kind": "unexpected",
- "from": CAPTURE_3,
- "to": MIX_INPUT_2,
- "action": "would-review",
- }
- ],
- )
- self.assertIn(["pw-link", "-d", CAPTURE_0, MIX_INPUT_0], calls)
- self.assertNotIn(["pw-link", "-d", CAPTURE_3, MIX_INPUT_2], calls)
- def test_reconcile_once_defers_unlink_until_all_desired_links_exist(self):
- links = {
- (CAPTURE_0, MIX_INPUT_0),
- (CAPTURE_2, MIX_INPUT_2),
- (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"{AMP_OUTPUT}\n{CAB_OUTPUT}\n{CAPTURE_0}\n{CAPTURE_2}\n{MIX_OUTPUT}\n"
- ))
- if command == ["pw-link", "-i"]:
- return SimpleNamespace(stdout=(
- f"{CAB_INPUT}\n{MIX_INPUT_0}\n{MIX_INPUT_2}\n{PLAYBACK_0}\n"
- ))
- if command == ["pw-link", CAB_OUTPUT, MIX_INPUT_0]:
- links.add((CAB_OUTPUT, MIX_INPUT_0))
- return SimpleNamespace(stdout="")
- self.fail(f"unexpected command: {command}")
- result = reconcile_once(self.state, runner)
- self.assertEqual(
- result["actions"],
- [
- {
- "kind": "unavailable-port",
- "from": CAPTURE_0,
- "to": AMP_INPUT,
- "missing_ports": [{"port": AMP_INPUT, "direction": "input"}],
- "result": "not-linked",
- "reason": "required PipeWire port(s) are not present in the current graph",
- "hint": (
- "Rigforge mod-host boundary ports must be running; mod-ui provides the "
- "internal NAM/IR connections"
- ),
- },
- {"kind": "link", "from": CAB_OUTPUT, "to": MIX_INPUT_0, "result": "requested"},
- {
- "kind": "unlink-deferred",
- "from": CAPTURE_0,
- "to": MIX_INPUT_0,
- "result": "desired managed links are still missing",
- },
- ],
- )
- self.assertIn((CAPTURE_0, MIX_INPUT_0), links)
- self.assertFalse(any(command[:2] == ["pw-link", "-d"] for command in calls))
- def test_reconcile_once_restores_all_managed_links_after_service_restart(self):
- links = set()
- 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"{AMP_OUTPUT}\n{CAB_OUTPUT}\n{CAPTURE_0}\n{CAPTURE_2}\n{MIX_OUTPUT}\n"
- ))
- if command == ["pw-link", "-i"]:
- return SimpleNamespace(stdout=(
- f"{AMP_INPUT}\n{CAB_INPUT}\n{MIX_INPUT_0}\n{MIX_INPUT_2}\n{PLAYBACK_0}\n"
- ))
- if tuple(command) in {
- ("pw-link", CAB_OUTPUT, MIX_INPUT_0),
- ("pw-link", CAPTURE_0, AMP_INPUT),
- ("pw-link", CAPTURE_2, MIX_INPUT_2),
- ("pw-link", MIX_OUTPUT, PLAYBACK_0),
- }:
- links.add((command[1], command[2]))
- return SimpleNamespace(stdout="")
- self.fail(f"unexpected command: {command}")
- result = reconcile_once(self.state, runner)
- self.assertEqual(
- result["actions"],
- [
- {"kind": "link", "from": CAPTURE_0, "to": AMP_INPUT, "result": "requested"},
- {"kind": "link", "from": CAPTURE_2, "to": MIX_INPUT_2, "result": "requested"},
- {"kind": "link", "from": MIX_OUTPUT, "to": PLAYBACK_0, "result": "requested"},
- {"kind": "link", "from": CAB_OUTPUT, "to": MIX_INPUT_0, "result": "requested"},
- ],
- )
- self.assertEqual(result["differences"], [])
- self.assertEqual(
- [command for command in calls if len(command) == 3 and command[0] == "pw-link"],
- [
- ["pw-link", CAPTURE_0, AMP_INPUT],
- ["pw-link", CAPTURE_2, MIX_INPUT_2],
- ["pw-link", MIX_OUTPUT, PLAYBACK_0],
- ["pw-link", CAB_OUTPUT, MIX_INPUT_0],
- ],
- )
- def test_reconcile_once_does_not_link_when_endpoint_is_unavailable(self):
- state = copy.deepcopy(self.state)
- links = {
- (CAPTURE_0, AMP_INPUT),
- (AMP_OUTPUT, CAB_INPUT),
- (CAB_OUTPUT, MIX_INPUT_0),
- (CAPTURE_2, MIX_INPUT_2),
- (MIX_OUTPUT, PLAYBACK_0),
- }
- links.remove((CAPTURE_2, MIX_INPUT_2))
- 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"{AMP_OUTPUT}\n{CAB_OUTPUT}\n{CAPTURE_0}\n{MIX_OUTPUT}\n")
- if command == ["pw-link", "-i"]:
- return SimpleNamespace(stdout=(
- f"{AMP_INPUT}\n{CAB_INPUT}\n{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,
- "missing_ports": [{"port": CAPTURE_2, "direction": "output"}],
- "result": "not-linked",
- "reason": "required PipeWire port(s) are not present in the current graph",
- }],
- )
- 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, AMP_INPUT),
- (AMP_OUTPUT, CAB_INPUT),
- (CAB_OUTPUT, 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, AMP_INPUT),
- (AMP_OUTPUT, CAB_INPUT),
- (CAB_OUTPUT, 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"{AMP_OUTPUT}\n{CAB_OUTPUT}\n{CAPTURE_0}\n{CAPTURE_2}\n{MIX_OUTPUT}\n"
- ))
- if command == ["pw-link", "-i"]:
- return SimpleNamespace(stdout=(
- f"{AMP_INPUT}\n{CAB_INPUT}\n{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):
- state = copy.deepcopy(self.state)
- state["routing"]["managed_ports"].remove(PLAYBACK_0)
- state["routing"]["managed_links"] = [
- {"from": CAPTURE_0, "to": PLAYBACK_0, "status": "planned"}
- ]
- self.assertTrue(any("endpoints must be listed" in error for error in validate_managed_links(state)))
- def test_managed_link_cannot_also_be_scheduled_for_removal(self):
- state = copy.deepcopy(self.state)
- state["routing"]["managed_links"].append(
- {"from": CAPTURE_0, "to": MIX_INPUT_0, "status": "planned"}
- )
- self.assertTrue(
- any(
- "link cannot be both managed and scheduled for removal" in error
- for error in validate_managed_links(state)
- )
- )
- if __name__ == "__main__":
- unittest.main()
|