test_desired_state.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import copy
  2. import sys
  3. import unittest
  4. from pathlib import Path
  5. import yaml
  6. ROOT = Path(__file__).resolve().parents[1]
  7. sys.path.insert(0, str(ROOT / "scripts"))
  8. from validate_desired_state import validate_state
  9. class DesiredStateValidationTests(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_current_desired_state_is_valid(self):
  15. self.assertEqual(validate_state(self.state), [])
  16. def test_requires_four_sources_and_unique_physical_inputs(self):
  17. state = copy.deepcopy(self.state)
  18. state["sources"][0]["physical_input"] = 2
  19. errors = validate_state(state)
  20. self.assertTrue(any("duplicate physical input" in error for error in errors))
  21. self.assertTrue(any("physical inputs must cover 1 through 4" in error for error in errors))
  22. state = copy.deepcopy(self.state)
  23. state["sources"].pop()
  24. errors = validate_state(state)
  25. self.assertTrue(any("exactly four sources" in error for error in errors))
  26. def test_requires_one_four_channel_loop(self):
  27. state = copy.deepcopy(self.state)
  28. state["system"]["components"][1]["channels_per_loop"] = 2
  29. self.assertIn("looper.channels_per_loop must be 4", validate_state(state))
  30. state = copy.deepcopy(self.state)
  31. state["routing"]["looper_inputs"][3]["channel"] = 3
  32. errors = validate_state(state)
  33. self.assertTrue(any("duplicate looper input channel" in error for error in errors))
  34. self.assertTrue(any("cover channels 1 through 4" in error for error in errors))
  35. def test_requires_declared_mix_output(self):
  36. state = copy.deepcopy(self.state)
  37. state["routing"]["mix"]["output_port"] = ""
  38. self.assertIn("routing.mix.output_port must be a non-empty string", validate_state(state))
  39. def test_guitar_mod_host_chain_declares_nam_then_cabinet_without_dry_bypass(self):
  40. chain = self.state["routing"]["mod_host_chains"]["guitar"]
  41. processors = chain["processors"]
  42. self.assertEqual([item["id"] for item in processors], ["amp-head", "cabinet-ir"])
  43. self.assertEqual([item["instance"] for item in processors], [0, 1])
  44. self.assertNotIn(
  45. (chain["bypass_link"]["from"], chain["bypass_link"]["to"]),
  46. {
  47. (link["from"], link["to"])
  48. for link in self.state["routing"]["managed_links"]
  49. },
  50. )
  51. state = copy.deepcopy(self.state)
  52. state["routing"]["mod_host_chains"]["guitar"]["processors"][0]["asset_path"] = "../../outside.nam"
  53. self.assertTrue(any("asset_path must be a safe relative path" in error for error in validate_state(state)))
  54. state = copy.deepcopy(self.state)
  55. state["routing"]["mod_host_chains"]["guitar"]["processors"][1]["instance"] = 0
  56. self.assertTrue(any("processors[1].instance must be 1" in error for error in validate_state(state)))
  57. state = copy.deepcopy(self.state)
  58. state["routing"]["managed_links"].append(
  59. {
  60. "from": state["routing"]["mod_host_chains"]["guitar"]["bypass_link"]["from"],
  61. "to": state["routing"]["mod_host_chains"]["guitar"]["bypass_link"]["to"],
  62. "status": "planned",
  63. }
  64. )
  65. self.assertIn("the guitar capture-to-mix bypass must not be a managed link", validate_state(state))
  66. state = copy.deepcopy(self.state)
  67. state["routing"]["managed_links"].pop(0)
  68. self.assertTrue(any("mod-host guitar chain link must be declared" in error for error in validate_state(state)))
  69. state = copy.deepcopy(self.state)
  70. state["routing"]["mod_host_chains"]["guitar"]["processors"][0]["parameters"]["output_level"] = 30
  71. self.assertIn("NAM output_level must be between -20 and 20 dB", validate_state(state))
  72. def test_deployment_targets_are_declared_and_valid(self):
  73. self.assertEqual(validate_state(self.state), [])
  74. state = copy.deepcopy(self.state)
  75. state["deployment"]["namlv2"]["archive_url"] = "https://example.invalid/plugin.tgz"
  76. self.assertTrue(any("archive_url must match" in error for error in validate_state(state)))
  77. state = copy.deepcopy(self.state)
  78. state["deployment"]["namlv2"]["tag"] = "../latest"
  79. self.assertTrue(any("tag must be a simple version tag" in error for error in validate_state(state)))
  80. state = copy.deepcopy(self.state)
  81. state["deployment"]["lsp"]["tag"] = "../latest"
  82. self.assertTrue(any("deployment.lsp.tag must be a simple version tag" in error for error in validate_state(state)))
  83. state = copy.deepcopy(self.state)
  84. state["deployment"]["lsp"]["archive_url"] = "https://example.invalid/plugin.7z"
  85. self.assertTrue(any("deployment.lsp.archive_url must match" in error for error in validate_state(state)))
  86. state = copy.deepcopy(self.state)
  87. state["deployment"]["dragonfly_reverb"]["archive_format"] = "tar.gz"
  88. self.assertTrue(any("deployment.dragonfly_reverb.archive_format" in error for error in validate_state(state)))
  89. state = copy.deepcopy(self.state)
  90. del state["deployment"]["dragonfly_reverb"]
  91. self.assertIn("deployment.dragonfly_reverb must be a mapping", validate_state(state))
  92. state = copy.deepcopy(self.state)
  93. state["deployment"]["pipewire"]["fragment_destination"] = ".config/other.conf"
  94. self.assertTrue(any("fragment_destination" in error for error in validate_state(state)))
  95. state = copy.deepcopy(self.state)
  96. state["deployment"]["sooperlooper"]["source_url"] = "https://example.invalid/source"
  97. self.assertTrue(any("source_url must match" in error for error in validate_state(state)))
  98. state = copy.deepcopy(self.state)
  99. state["deployment"]["sooperlooper"]["tag"] = "../latest"
  100. self.assertTrue(any("sooperlooper.tag must be a simple version tag" in error for error in validate_state(state)))
  101. state = copy.deepcopy(self.state)
  102. del state["deployment"]["sooperlooper"]["tag"]
  103. self.assertTrue(any("sooperlooper.tag must be a simple version tag" in error for error in validate_state(state)))
  104. state = copy.deepcopy(self.state)
  105. state["deployment"]["mod_host"]["source_url"] = "https://example.invalid/source"
  106. self.assertTrue(any("mod_host.source_url must match" in error for error in validate_state(state)))
  107. state = copy.deepcopy(self.state)
  108. state["deployment"]["mod_host"]["tag"] = "../latest"
  109. self.assertTrue(any("mod_host.tag must be a simple version tag" in error for error in validate_state(state)))
  110. state = copy.deepcopy(self.state)
  111. del state["deployment"]["mod_host"]
  112. self.assertIn("deployment.mod_host must be a mapping", validate_state(state))
  113. state = copy.deepcopy(self.state)
  114. state["deployment"]["mod_host"]["socket_port"] = 70000
  115. self.assertIn("deployment.mod_host.socket_port must be a valid TCP port", validate_state(state))
  116. def test_return_exclusion_marker_is_not_an_active_connection(self):
  117. self.assertEqual(validate_state(self.state), [])
  118. state = copy.deepcopy(self.state)
  119. forbidden_connection = next(
  120. connection
  121. for connection in state["connections"]
  122. if connection.get("status") == "explicitly-forbidden"
  123. )
  124. forbidden_connection["status"] = "planned"
  125. self.assertTrue(
  126. any("routes looper returns into looper inputs" in error for error in validate_state(state))
  127. )
  128. state = copy.deepcopy(self.state)
  129. state["routing"]["looper_returns"]["feed_looper_inputs"] = True
  130. self.assertIn("routing.looper_returns.feed_looper_inputs must be false", validate_state(state))
  131. if __name__ == "__main__":
  132. unittest.main()