#!/usr/bin/env python3 """Fixture tests for the audio layer's parsing and selection. python3 mic-follow/test_selection.py The commands that CHANGE state cannot be tested without a real PipeWire session; what can be tested is everything that decides which device those commands are pointed at, and that is where the damaging mistakes live — picking a monitor source (transmitting what the desktop is playing instead of what the person is saying), or picking a different microphone after a reboot because two matched and the order changed. The fixtures are hand-written in pactl's documented JSON shape. VERIFY THEM against real output (`pactl -f json list sources`) the first time this runs on the desktop — they are the assumption this whole component rests on. """ import sys import unittest from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) import audio_sources as audio SOURCES = [ { "index": 44, "name": "alsa_output.pci-0000_0c_00.4.analog-stereo.monitor", "description": "Monitor of Family 17h HD Audio", "properties": {"device.class": "monitor"}, }, { "index": 46, "name": "alsa_input.usb-Shure_Inc_MV6-00.mono-fallback", "description": "MV6 Mono", "properties": {"device.class": "sound"}, }, { "index": 51, "name": "bluez_input.AC:12:2F:9B:00:01.headset-head-unit", "description": "Arctis Nova 7 (headset mic)", "properties": {"device.class": "sound"}, }, { "index": 58, "name": "alsa_input.usb-DJI_MIC_MINI-00.mono-fallback", "description": "DJI MIC MINI Mono", "properties": {"device.class": "sound"}, }, ] STREAMS = [ {"index": 12, "source": 46, "properties": {"application.name": "Discord"}}, {"index": 13, "source": 46, "properties": {"application.name": "obs"}}, {"index": 14, "source": 58, "properties": {"application.process.binary": "Discord"}}, ] class ParsingTests(unittest.TestCase): def test_monitors_are_dropped(self): names = [s.name for s in audio.parse_sources(SOURCES)] self.assertNotIn("alsa_output.pci-0000_0c_00.4.analog-stereo.monitor", names) self.assertEqual(len(names), 3) def test_monitor_dropped_by_property_even_without_the_suffix(self): odd = [{"index": 1, "name": "weird_monitor_name", "description": "x", "properties": {"device.class": "Monitor"}}] self.assertEqual(audio.parse_sources(odd), []) def test_description_falls_back_to_the_name(self): bare = [{"index": 2, "name": "some.source", "properties": {}}] self.assertEqual(audio.parse_sources(bare)[0].description, "some.source") def test_capture_streams_read_either_application_property(self): streams = audio.parse_capture_streams(STREAMS) self.assertEqual([s.application for s in streams], ["Discord", "obs", "Discord"]) def test_garbage_does_not_raise(self): self.assertEqual(audio.parse_sources(None), []) self.assertEqual(audio.parse_sources(["not a dict"]), []) self.assertEqual(audio.parse_capture_streams(None), []) class SelectionTests(unittest.TestCase): def setUp(self): self.sources = audio.parse_sources(SOURCES) def test_substring_of_the_description(self): self.assertEqual(audio.select_source(self.sources, "MV6").index, 46) def test_substring_of_the_node_name(self): self.assertEqual(audio.select_source(self.sources, "DJI_MIC").index, 58) def test_case_insensitive(self): self.assertEqual(audio.select_source(self.sources, "dji mic mini").index, 58) def test_exact_name_wins_over_a_substring_of_another(self): sources = self.sources + [audio.Source(70, "mv6", "Something else entirely")] self.assertEqual(audio.select_source(sources, "mv6").index, 70) def test_ambiguous_match_is_deterministic(self): # Two headsets, one pattern: the answer must be the same after a reboot, not # whichever pactl happened to list first. sources = self.sources + [audio.Source(9, "bluez_input.OTHER.headset", "Other headset mic")] self.assertEqual(audio.select_source(sources, "headset").index, 9) def test_no_match_is_none_not_a_guess(self): self.assertIsNone(audio.select_source(self.sources, "rode wireless")) def test_empty_pattern_is_none(self): self.assertIsNone(audio.select_source(self.sources, " ")) def test_a_monitor_can_never_be_selected(self): # The pattern below matches the monitor's description and nothing else; the # right answer is None, never "transmit the desktop's audio". self.assertIsNone(audio.select_source(self.sources, "Monitor of Family")) class StreamMoveTests(unittest.TestCase): def setUp(self): self.streams = audio.parse_capture_streams(STREAMS) self.dji = audio.Source(58, "alsa_input.usb-DJI_MIC_MINI-00.mono-fallback", "DJI") self.mv6 = audio.Source(46, "alsa_input.usb-Shure_Inc_MV6-00.mono-fallback", "MV6") def test_only_configured_applications_move(self): moved = audio.streams_to_move(self.streams, ["Discord"], self.dji) self.assertEqual([s.index for s in moved], [12]) def test_streams_already_on_the_target_are_left_alone(self): moved = audio.streams_to_move(self.streams, ["Discord"], self.mv6) self.assertEqual([s.index for s in moved], [14]) def test_no_applications_configured_moves_nothing(self): self.assertEqual(audio.streams_to_move(self.streams, [], self.dji), []) def test_matching_is_case_insensitive_and_partial(self): moved = audio.streams_to_move(self.streams, ["discord"], self.dji) self.assertEqual([s.index for s in moved], [12]) if __name__ == "__main__": unittest.main(verbosity=2)