import sys import unittest from pathlib import Path from types import SimpleNamespace import yaml ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "scripts")) from configure_mod_host import configure, wait_for_mod_host class ModHostConfigurationTests(unittest.TestCase): @classmethod def setUpClass(cls): with (ROOT / "config" / "desired-state.yaml").open(encoding="utf-8") as stream: cls.state = yaml.safe_load(stream) def test_loads_nam_and_ir_then_replaces_only_declared_dry_link(self): routing = self.state["routing"] chain = routing["mod_host_chains"]["guitar"] bypass = (chain["bypass_link"]["from"], chain["bypass_link"]["to"]) links = {bypass} control_commands = [] bundle_added = False def control(port, command): nonlocal bundle_added control_commands.append(command) if command == "cpu_load": return "resp 0 12.5" if command.startswith("add http://lsp.plug.in/"): return "resp -101" if command.startswith("add http://lsp-plug.in/") and not bundle_added: return "resp -101" if command.startswith("bundle_add "): bundle_added = True return "resp 0" if command.startswith("add "): return f"resp {command.rsplit(' ', 1)[1]}" return "resp 0" def render_links(): return "".join( f"{source}\n |-> {destination}\n" for source, destination in sorted(links) ) outputs = { chain["source_port"], *(processor["output_port"] for processor in chain["processors"]), } inputs = { chain["mix_input"], *(processor["input_port"] for processor in chain["processors"]), } def runner(command): if command == ["pw-link", "-o"]: return SimpleNamespace(stdout="\n".join(sorted(outputs)) + "\n") if command == ["pw-link", "-i"]: return SimpleNamespace(stdout="\n".join(sorted(inputs)) + "\n") if command == ["pw-link", "-l"]: return SimpleNamespace(stdout=render_links()) if command[:2] == ["pw-link", "-d"]: links.discard((command[2], command[3])) return SimpleNamespace(stdout="") if command[0] == "pw-link" and len(command) == 3: links.add((command[1], command[2])) return SimpleNamespace(stdout="") self.fail(f"unexpected command: {command}") result = configure( self.state, ROOT, runner=runner, sleeper=lambda _duration: None, command=control, ) expected = { (chain["source_port"], chain["processors"][0]["input_port"]), (chain["processors"][0]["output_port"], chain["processors"][1]["input_port"]), (chain["processors"][1]["output_port"], chain["mix_input"]), } self.assertEqual(links, expected) self.assertNotIn(bypass, links) self.assertEqual(result["instances"], [0, 1]) self.assertTrue(result["bypass_disconnected"]) self.assertIn( "patch_set 0 http://github.com/mikeoliphant/neural-amp-modeler-lv2#model " + str(ROOT / "amp_emul/amphead/VOX_AC15/Vox_AC15CH_Crunch_TB.nam"), control_commands, ) self.assertIn( "patch_set 1 http://lsp-plug.in/plugins/lv2/impulse_responses_mono/ports#ifn " + str(ROOT / "amp_emul/cabinet/VOX_AC30_1964/VOX_AC30_1964_celestion.wav"), control_commands, ) self.assertIn("param_set 0 output_level -12.0", control_commands) self.assertTrue(any(command.startswith("bundle_add ") for command in control_commands)) def test_waits_for_the_mod_host_control_socket(self): calls = [] def command(_port, name): calls.append(name) if len(calls) == 1: raise ConnectionRefusedError("not ready") return "resp 0 8.0" wait_for_mod_host(5555, timeout=1, command=command, sleeper=lambda _duration: None) self.assertEqual(calls, ["cpu_load", "cpu_load"]) if __name__ == "__main__": unittest.main()