import copy import shutil import sys import tempfile import unittest from pathlib import Path import yaml ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "scripts")) from midi_action_bridge import ( binding_index, handle_midi_message, load_configuration, select_nam_model, validate_bindings, ) class MidiActionBridgeTests(unittest.TestCase): @classmethod def setUpClass(cls): cls.state, cls.bindings, cls.repo_root = load_configuration( ROOT / "config" / "desired-state.yaml", repo_root=ROOT, ) def stage_ui_models(self, root, state, repo_root, model_ids): nam = state["routing"]["mod_host_chains"]["guitar"]["processors"][0] user_files_root = root / "mod-ui-user-files" destination = user_files_root / "NAM Models" / "Rigforge" destination.mkdir(parents=True) for model_id in model_ids: source = repo_root / nam["available_models"][model_id] shutil.copyfile(source, destination / source.name) return user_files_root def test_current_bindings_map_notes_six_through_nine_to_guitar_tb_models(self): self.assertEqual(validate_bindings(self.bindings, self.state), []) events = { entry["event"]["note"]: entry["action"]["model"] for entry in self.bindings["bindings"] } self.assertEqual( events, { 6: "crystal_clean_tb", 7: "edge_of_breakup_tb", 8: "crunch_tb", 9: "overdriven_tb", }, ) self.assertTrue( all( entry["action"]["fallback_model"] == "crystal_clean_tb" for entry in self.bindings["bindings"] ) ) def test_note_on_sends_model_patch_set_through_mod_ui(self): commands = [] actions = binding_index(self.bindings) with tempfile.TemporaryDirectory() as directory: user_files_root = self.stage_ui_models( Path(directory), self.state, ROOT, ["edge_of_breakup_tb"] ) result = handle_midi_message( [0x90, 7, 127], actions, self.state, ROOT, command=lambda instance, uri, value: commands.append( ("127.0.0.1", 8888, f"rigforge_patch_set {instance} {uri} p {value}") ) or True, user_files_root=user_files_root, ) model = user_files_root / "NAM Models" / "Rigforge" / "Vox_AC15CH_Edge_of_Breakup_TB.nam" self.assertEqual(result["model"], "edge_of_breakup_tb") self.assertEqual(result["note"], 7) self.assertEqual( commands, [( "127.0.0.1", 8888, "rigforge_patch_set /graph/rigforge_nam " "http://github.com/mikeoliphant/neural-amp-modeler-lv2#model p " + str(model), )], ) def test_note_off_zero_velocity_and_unbound_channel_do_nothing(self): commands = [] command = lambda *_args: commands.append(_args) or True actions = binding_index(self.bindings) self.assertIsNone(handle_midi_message([0x80, 7, 0], actions, self.state, ROOT, command)) self.assertIsNone(handle_midi_message([0x90, 7, 0], actions, self.state, ROOT, command)) self.assertIsNone(handle_midi_message([0x91, 7, 127], actions, self.state, ROOT, command)) self.assertEqual(commands, []) def test_missing_requested_model_uses_declared_clean_fallback(self): state = copy.deepcopy(self.state) models = state["routing"]["mod_host_chains"]["guitar"]["processors"][0][ "available_models" ] models["edge_of_breakup_tb"] = "models/missing.nam" models["crystal_clean_tb"] = "models/clean.nam" action = copy.deepcopy(self.bindings["bindings"][1]["action"]) with tempfile.TemporaryDirectory() as directory: root = Path(directory) clean_model = root / "models" / "clean.nam" clean_model.parent.mkdir() clean_model.write_text("test fixture", encoding="utf-8") user_files_root = root / "user-files" ui_model = user_files_root / "NAM Models" / "Rigforge" / clean_model.name ui_model.parent.mkdir(parents=True) shutil.copyfile(clean_model, ui_model) commands = [] def command(instance, uri, value): commands.append((instance, uri, value)) return True selected_id, selected_path = select_nam_model( action, state, root, command=command, user_files_root=user_files_root, ) self.assertEqual(selected_id, "crystal_clean_tb") self.assertEqual(selected_path, ui_model) self.assertIn("NAM Models/Rigforge/clean.nam", commands[0][2]) def test_missing_requested_and_fallback_model_sends_no_ui_command(self): state = copy.deepcopy(self.state) models = state["routing"]["mod_host_chains"]["guitar"]["processors"][0][ "available_models" ] models["edge_of_breakup_tb"] = "models/missing-edge.nam" models["crystal_clean_tb"] = "models/missing-clean.nam" commands = [] action = copy.deepcopy(self.bindings["bindings"][1]["action"]) with tempfile.TemporaryDirectory() as directory: with self.assertRaises(FileNotFoundError): select_nam_model( action, state, Path(directory), command=lambda *args: commands.append(args) or True, ) self.assertEqual(commands, []) def test_invalid_or_duplicate_binding_is_rejected(self): bindings = copy.deepcopy(self.bindings) bindings["bindings"][1]["event"]["note"] = 6 errors = validate_bindings(bindings, self.state) self.assertTrue(any("duplicate MIDI note binding" in error for error in errors)) bindings = copy.deepcopy(self.bindings) bindings["bindings"][0]["action"]["target"]["processor"] = "not-a-processor" errors = validate_bindings(bindings, self.state) self.assertTrue(any("must identify exactly one processor" in error for error in errors)) if __name__ == "__main__": unittest.main()