round from from Claude Opus 4.8
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
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()
|
||||
@@ -0,0 +1,100 @@
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
import config
|
||||
from board import Board
|
||||
|
||||
|
||||
class TestBoard(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.dir = tempfile.mkdtemp()
|
||||
self.path = os.path.join(self.dir, "config.json")
|
||||
|
||||
def _board(self):
|
||||
return Board(self.path)
|
||||
|
||||
def test_add_button_defaults_label_to_basename(self):
|
||||
b = self._board()
|
||||
btn = b.add_button("/sounds/airhorn.mp3")
|
||||
self.assertEqual(btn["label"], "airhorn")
|
||||
self.assertEqual(btn["id"], "btn_001")
|
||||
self.assertEqual(len(b.buttons), 1)
|
||||
|
||||
def test_add_button_custom_label(self):
|
||||
b = self._board()
|
||||
btn = b.add_button("/sounds/x.wav", label="Custom")
|
||||
self.assertEqual(btn["label"], "Custom")
|
||||
|
||||
def test_ids_increment_and_persist(self):
|
||||
b = self._board()
|
||||
b.add_button("/a.wav")
|
||||
b.add_button("/b.wav")
|
||||
self.assertEqual([x["id"] for x in b.buttons], ["btn_001", "btn_002"])
|
||||
# Reload: new ids continue after the highest existing one.
|
||||
b2 = self._board()
|
||||
btn = b2.add_button("/c.wav")
|
||||
self.assertEqual(btn["id"], "btn_003")
|
||||
|
||||
def test_remove_button(self):
|
||||
b = self._board()
|
||||
b.add_button("/a.wav")
|
||||
btn = b.add_button("/b.wav")
|
||||
b.remove_button("btn_001")
|
||||
self.assertEqual([x["id"] for x in b.buttons], ["btn_002"])
|
||||
# persisted
|
||||
self.assertEqual(len(self._board().buttons), 1)
|
||||
|
||||
def test_remove_unknown_raises(self):
|
||||
b = self._board()
|
||||
with self.assertRaises(KeyError):
|
||||
b.remove_button("nope")
|
||||
|
||||
def test_rename_button(self):
|
||||
b = self._board()
|
||||
b.add_button("/a.wav")
|
||||
b.rename_button("btn_001", " Renamed ")
|
||||
self.assertEqual(b.get_button("btn_001")["label"], "Renamed")
|
||||
|
||||
def test_rename_empty_raises(self):
|
||||
b = self._board()
|
||||
b.add_button("/a.wav")
|
||||
with self.assertRaises(ValueError):
|
||||
b.rename_button("btn_001", " ")
|
||||
|
||||
def test_settings_persist(self):
|
||||
b = self._board()
|
||||
b.set_output_device("CABLE Input")
|
||||
b.set_volume(42)
|
||||
reloaded = self._board()
|
||||
self.assertEqual(reloaded.output_device, "CABLE Input")
|
||||
self.assertEqual(reloaded.volume, 42)
|
||||
|
||||
def test_volume_clamped(self):
|
||||
b = self._board()
|
||||
b.set_volume(150)
|
||||
self.assertEqual(b.volume, 100)
|
||||
b.set_volume(-10)
|
||||
self.assertEqual(b.volume, 0)
|
||||
|
||||
def test_on_change_fires(self):
|
||||
b = self._board()
|
||||
calls = []
|
||||
b.on_change = lambda: calls.append(1)
|
||||
b.add_button("/a.wav")
|
||||
b.rename_button("btn_001", "z")
|
||||
b.remove_button("btn_001")
|
||||
self.assertEqual(len(calls), 3)
|
||||
|
||||
def test_to_config_matches_schema(self):
|
||||
b = self._board()
|
||||
b.add_button("/a.wav", "A")
|
||||
cfg = b.to_config()
|
||||
self.assertEqual(set(cfg.keys()), set(config.default_config().keys()))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,66 @@
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
import config
|
||||
|
||||
|
||||
class TestConfig(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.dir = tempfile.mkdtemp()
|
||||
self.path = os.path.join(self.dir, "config.json")
|
||||
|
||||
def test_missing_file_returns_default(self):
|
||||
cfg = config.load_config(self.path)
|
||||
self.assertEqual(cfg, config.default_config())
|
||||
self.assertEqual(cfg["buttons"], [])
|
||||
self.assertIsNone(cfg["output_device"])
|
||||
|
||||
def test_corrupt_file_returns_default(self):
|
||||
with open(self.path, "w") as fh:
|
||||
fh.write("{not valid json")
|
||||
self.assertEqual(config.load_config(self.path), config.default_config())
|
||||
|
||||
def test_round_trip(self):
|
||||
cfg = config.default_config()
|
||||
cfg["output_device"] = "CABLE Input"
|
||||
cfg["volume"] = 55
|
||||
cfg["buttons"] = [{"id": "btn_001", "label": "Air Horn", "file_path": "/x/a.mp3"}]
|
||||
config.save_config(self.path, cfg)
|
||||
loaded = config.load_config(self.path)
|
||||
self.assertEqual(loaded, cfg)
|
||||
|
||||
def test_save_is_atomic_no_tmp_left(self):
|
||||
config.save_config(self.path, config.default_config())
|
||||
self.assertFalse(os.path.exists(self.path + ".tmp"))
|
||||
self.assertTrue(os.path.exists(self.path))
|
||||
|
||||
def test_volume_clamped(self):
|
||||
with open(self.path, "w") as fh:
|
||||
json.dump({"volume": 999}, fh)
|
||||
self.assertEqual(config.load_config(self.path)["volume"], 100)
|
||||
|
||||
def test_malformed_buttons_dropped(self):
|
||||
with open(self.path, "w") as fh:
|
||||
json.dump(
|
||||
{
|
||||
"buttons": [
|
||||
{"id": "btn_001", "label": "ok", "file_path": "/a"},
|
||||
{"id": "btn_002", "label": "missing path"},
|
||||
"not a dict",
|
||||
{"id": 5, "label": "bad id type", "file_path": "/b"},
|
||||
]
|
||||
},
|
||||
fh,
|
||||
)
|
||||
buttons = config.load_config(self.path)["buttons"]
|
||||
self.assertEqual(len(buttons), 1)
|
||||
self.assertEqual(buttons[0]["id"], "btn_001")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user