import os import sys import unittest sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) try: import numpy as np import audio_engine HAVE_NUMPY = True except Exception: # numpy not installed — skip the array-shape tests HAVE_NUMPY = False @unittest.skipUnless(HAVE_NUMPY, "numpy required") class TestMatchChannels(unittest.TestCase): def test_mono_to_stereo(self): mono = np.zeros((100, 1), dtype=np.float32) out = audio_engine.match_channels(mono, 2) self.assertEqual(out.shape, (100, 2)) def test_1d_treated_as_mono(self): out = audio_engine.match_channels(np.zeros(50, dtype=np.float32), 2) self.assertEqual(out.shape, (50, 2)) def test_stereo_unchanged(self): st = np.zeros((10, 2), dtype=np.float32) out = audio_engine.match_channels(st, 2) self.assertEqual(out.shape, (10, 2)) def test_downmix_truncates(self): five = np.zeros((10, 5), dtype=np.float32) out = audio_engine.match_channels(five, 2) self.assertEqual(out.shape, (10, 2)) def test_mono_values_duplicated(self): mono = np.arange(4, dtype=np.float32).reshape(4, 1) out = audio_engine.match_channels(mono, 2) self.assertTrue(np.array_equal(out[:, 0], out[:, 1])) @unittest.skipUnless(HAVE_NUMPY, "numpy required") class TestAutoSelectDevice(unittest.TestCase): def test_prefers_cable_input(self): devices = [ {"index": 0, "name": "Built-in Output", "channels": 2}, {"index": 1, "name": "CABLE Input (VB-Audio Virtual Cable)", "channels": 2}, ] self.assertEqual(audio_engine.auto_select_device(devices)["index"], 1) def test_linux_null_sink(self): devices = [ {"index": 0, "name": "HDA Intel PCH", "channels": 2}, {"index": 1, "name": "Null Output", "channels": 2}, ] self.assertEqual(audio_engine.auto_select_device(devices)["index"], 1) def test_none_when_no_virtual(self): devices = [{"index": 0, "name": "Speakers", "channels": 2}] self.assertIsNone(audio_engine.auto_select_device(devices)) def test_priority_order(self): # "cable input" outranks "virtual" even when virtual appears first devices = [ {"index": 0, "name": "Some Virtual Thing", "channels": 2}, {"index": 1, "name": "CABLE Input", "channels": 2}, ] self.assertEqual(audio_engine.auto_select_device(devices)["index"], 1) if __name__ == "__main__": unittest.main()