test_configure_mod_host.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import sys
  2. import unittest
  3. from pathlib import Path
  4. from types import SimpleNamespace
  5. import yaml
  6. ROOT = Path(__file__).resolve().parents[1]
  7. sys.path.insert(0, str(ROOT / "scripts"))
  8. from configure_mod_host import configure, wait_for_mod_host
  9. class ModHostConfigurationTests(unittest.TestCase):
  10. @classmethod
  11. def setUpClass(cls):
  12. with (ROOT / "config" / "desired-state.yaml").open(encoding="utf-8") as stream:
  13. cls.state = yaml.safe_load(stream)
  14. def test_loads_nam_and_ir_then_replaces_only_declared_dry_link(self):
  15. routing = self.state["routing"]
  16. chain = routing["mod_host_chains"]["guitar"]
  17. bypass = (chain["bypass_link"]["from"], chain["bypass_link"]["to"])
  18. links = {bypass}
  19. control_commands = []
  20. bundle_added = False
  21. def control(port, command):
  22. nonlocal bundle_added
  23. control_commands.append(command)
  24. if command == "cpu_load":
  25. return "resp 0 12.5"
  26. if command.startswith("add http://lsp.plug.in/"):
  27. return "resp -101"
  28. if command.startswith("add http://lsp-plug.in/") and not bundle_added:
  29. return "resp -101"
  30. if command.startswith("bundle_add "):
  31. bundle_added = True
  32. return "resp 0"
  33. if command.startswith("add "):
  34. return f"resp {command.rsplit(' ', 1)[1]}"
  35. return "resp 0"
  36. def render_links():
  37. return "".join(
  38. f"{source}\n |-> {destination}\n"
  39. for source, destination in sorted(links)
  40. )
  41. outputs = {
  42. chain["source_port"],
  43. *(processor["output_port"] for processor in chain["processors"]),
  44. }
  45. inputs = {
  46. chain["mix_input"],
  47. *(processor["input_port"] for processor in chain["processors"]),
  48. }
  49. def runner(command):
  50. if command == ["pw-link", "-o"]:
  51. return SimpleNamespace(stdout="\n".join(sorted(outputs)) + "\n")
  52. if command == ["pw-link", "-i"]:
  53. return SimpleNamespace(stdout="\n".join(sorted(inputs)) + "\n")
  54. if command == ["pw-link", "-l"]:
  55. return SimpleNamespace(stdout=render_links())
  56. if command[:2] == ["pw-link", "-d"]:
  57. links.discard((command[2], command[3]))
  58. return SimpleNamespace(stdout="")
  59. if command[0] == "pw-link" and len(command) == 3:
  60. links.add((command[1], command[2]))
  61. return SimpleNamespace(stdout="")
  62. self.fail(f"unexpected command: {command}")
  63. result = configure(
  64. self.state,
  65. ROOT,
  66. runner=runner,
  67. sleeper=lambda _duration: None,
  68. command=control,
  69. )
  70. expected = {
  71. (chain["source_port"], chain["processors"][0]["input_port"]),
  72. (chain["processors"][0]["output_port"], chain["processors"][1]["input_port"]),
  73. (chain["processors"][1]["output_port"], chain["mix_input"]),
  74. }
  75. self.assertEqual(links, expected)
  76. self.assertNotIn(bypass, links)
  77. self.assertEqual(result["instances"], [0, 1])
  78. self.assertTrue(result["bypass_disconnected"])
  79. self.assertIn(
  80. "patch_set 0 http://github.com/mikeoliphant/neural-amp-modeler-lv2#model "
  81. + str(ROOT / "amp_emul/amphead/VOX_AC15/Vox_AC15CH_Crunch_TB.nam"),
  82. control_commands,
  83. )
  84. self.assertIn(
  85. "patch_set 1 http://lsp-plug.in/plugins/lv2/impulse_responses_mono/ports#ifn "
  86. + str(ROOT / "amp_emul/cabinet/VOX_AC30_1964/VOX_AC30_1964_celestion.wav"),
  87. control_commands,
  88. )
  89. self.assertIn("param_set 0 output_level -12.0", control_commands)
  90. self.assertTrue(any(command.startswith("bundle_add ") for command in control_commands))
  91. def test_waits_for_the_mod_host_control_socket(self):
  92. calls = []
  93. def command(_port, name):
  94. calls.append(name)
  95. if len(calls) == 1:
  96. raise ConnectionRefusedError("not ready")
  97. return "resp 0 8.0"
  98. wait_for_mod_host(5555, timeout=1, command=command, sleeper=lambda _duration: None)
  99. self.assertEqual(calls, ["cpu_load", "cpu_load"])
  100. if __name__ == "__main__":
  101. unittest.main()