SmartestHome/mic-follow/test_selection.py

201 lines
8.4 KiB
Python
Executable File

#!/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
import generate
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])
class RemoteSourceTests(unittest.TestCase):
"""The generated hooks for a microphone on another machine.
These are worth pinning because the STOP hook is a safety property: a room
microphone still streaming after the switch left it is a hot mic in a shared flat,
and "the generator emitted a stop command" is exactly the kind of thing that
silently stops being true.
"""
def setUp(self):
self.remote = {
"room": "loggia",
"description": "Loggia panel mic",
"remote": {"host": "touch-panel-loggia", "user": "kiosk", "latency_ms": 40},
}
self.local = {"room": "kitchen", "source": "DJI MIC MINI"}
def test_remote_source_gets_a_generated_name(self):
self.assertEqual(generate.remote_source_name(self.remote), "micfollow_loggia")
def test_an_explicit_name_wins(self):
named = dict(self.remote, source="my_own_name")
self.assertEqual(generate.remote_source_name(named), "my_own_name")
def test_remote_source_gets_both_hooks(self):
entry = generate.source_entry(self.remote, Path("/repo"))
self.assertEqual(entry["source"], "micfollow_loggia")
self.assertIn("remote-mic.sh start loggia", entry["start_command"])
self.assertIn("remote-mic.sh stop loggia", entry["stop_command"])
def test_every_remote_source_has_a_stop_hook(self):
for host in ("panel", "other-machine"):
entry = generate.source_entry(
{"room": "x", "remote": {"host": host}}, Path("/repo"))
self.assertTrue(entry["stop_command"], "a remote source without a stop hook")
def test_local_source_is_left_alone(self):
entry = generate.source_entry(self.local, Path("/repo"))
self.assertEqual(entry["source"], "DJI MIC MINI")
self.assertEqual(entry["start_command"], "")
self.assertEqual(entry["stop_command"], "")
def test_env_carries_the_transport_parameters(self):
env = generate.build_remote_env(self.remote, Path("/repo"))
self.assertIn("REMOTE_HOST=touch-panel-loggia", env)
self.assertIn("REMOTE_USER=kiosk", env)
self.assertIn("SOURCE_NAME=micfollow_loggia", env)
self.assertIn("LATENCY_MS=40", env)
# Empty means "the panel's own default input", which is what lets this work
# without knowing that machine's device names.
self.assertIn("REMOTE_MIC=", env)
def test_room_labels_are_short_and_readable(self):
self.assertEqual(generate.room_label("loggia"), "Loggia")
self.assertEqual(generate.room_label("living_room"), "Living room")
self.assertEqual(generate.room_label("desk"), "Desk")
if __name__ == "__main__":
unittest.main(verbosity=2)