import tempfile import textwrap import sys import unittest from pathlib import Path ROOT = Path(__file__).resolve().parents[1] SRC = ROOT / "src" if str(SRC) not in sys.path: sys.path.insert(0, str(SRC)) from pipelines_runtime import fingerprint, load_bindings class PipelinesRuntimeTests(unittest.TestCase): def test_missing_file_uses_default_binding(self): with tempfile.TemporaryDirectory() as td: path = Path(td) / "pipelines.py" called = {"count": 0} def factory(): called["count"] += 1 def handler(_audio, _lib): return "ok" return handler bindings = load_bindings( path=path, default_hotkey="Cmd+m", default_handler_factory=factory, ) self.assertEqual(list(bindings.keys()), ["Cmd+m"]) self.assertEqual(called["count"], 1) def test_loads_module_bindings_and_options(self): with tempfile.TemporaryDirectory() as td: path = Path(td) / "pipelines.py" path.write_text( textwrap.dedent( """ def p1(audio, lib): return "one" def p2(audio, lib): return "two" HOTKEY_PIPELINES = { "Cmd+m": p1, "Ctrl+space": p2, } PIPELINE_OPTIONS = { "Ctrl+space": {"failure_policy": "strict"}, } """ ), encoding="utf-8", ) bindings = load_bindings( path=path, default_hotkey="Cmd+m", default_handler_factory=lambda: (lambda _audio, _lib: "default"), ) self.assertEqual(sorted(bindings.keys()), ["Cmd+m", "Ctrl+space"]) self.assertEqual(bindings["Cmd+m"].options.failure_policy, "best_effort") self.assertEqual(bindings["Ctrl+space"].options.failure_policy, "strict") def test_invalid_options_fail(self): with tempfile.TemporaryDirectory() as td: path = Path(td) / "pipelines.py" path.write_text( textwrap.dedent( """ def p(audio, lib): return "ok" HOTKEY_PIPELINES = {"Cmd+m": p} PIPELINE_OPTIONS = {"Cmd+m": {"failure_policy": "invalid"}} """ ), encoding="utf-8", ) with self.assertRaisesRegex(ValueError, "failure_policy"): load_bindings( path=path, default_hotkey="Cmd+m", default_handler_factory=lambda: (lambda _audio, _lib: "default"), ) def test_fingerprint_changes_with_content(self): with tempfile.TemporaryDirectory() as td: path = Path(td) / "pipelines.py" self.assertIsNone(fingerprint(path)) path.write_text("HOTKEY_PIPELINES = {}", encoding="utf-8") first = fingerprint(path) path.write_text("HOTKEY_PIPELINES = {'Cmd+m': lambda a, l: ''}", encoding="utf-8") second = fingerprint(path) self.assertIsNotNone(first) self.assertIsNotNone(second) self.assertNotEqual(first, second) if __name__ == "__main__": unittest.main()