test_desktop_app.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/env python3
  2. # SPDX-FileCopyrightText: 2012-2023 MOD Audio UG
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. """The MOD Desktop seam, Python side.
  5. Two things are pinned here:
  6. 1. mod.settings.TONE3000_CLIENT_ID precedence. A frozen MOD Desktop build has
  7. no shell environment when launched from Finder/Explorer, so cx_Freeze bakes
  8. the id in as BUILD_CONSTANTS.MOD_TONE3000_CLIENT_ID (fed from the
  9. environment at freeze time, never committed). The environment still wins
  10. when it is set, and a device -- no BUILD_CONSTANTS at all -- is unchanged.
  11. 2. html/index.html rendered with desktop_app off carries nothing of the seam:
  12. no js/desktop-app.js, no css/desktop-app.css, no DesktopApp call, no
  13. desktop_app_templates cache key. Rendered with it on, it carries all four.
  14. This renders the template directly with tornado, the way TemplateHandler
  15. does; it needs tornado importable and is skipped otherwise.
  16. Run with: python3 -m unittest discover -s test -p 'test_*.py'
  17. """
  18. import importlib
  19. import os
  20. import re
  21. import sys
  22. import types
  23. import unittest
  24. REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  25. HTML_DIR = os.path.join(REPO_ROOT, 'html')
  26. if REPO_ROOT not in sys.path:
  27. sys.path.insert(0, REPO_ROOT)
  28. class TestTone3000ClientIdPrecedence(unittest.TestCase):
  29. def setUp(self):
  30. self._environ = dict(os.environ)
  31. self._modules = {k: v for k, v in sys.modules.items()
  32. if k in ('BUILD_CONSTANTS', 'mod.settings')}
  33. # Keep the import side effects (key paths, data dirs) inside a sandbox
  34. # whatever this machine's environment says.
  35. os.environ['MOD_DATA_DIR'] = os.path.join(REPO_ROOT, 'test', '.tmp-data')
  36. def tearDown(self):
  37. os.environ.clear()
  38. os.environ.update(self._environ)
  39. for name in ('BUILD_CONSTANTS', 'mod.settings'):
  40. sys.modules.pop(name, None)
  41. sys.modules.update(self._modules)
  42. def _settings(self, env, frozen):
  43. """Import mod.settings afresh with MOD_TONE3000_CLIENT_ID = `env` (None
  44. = unset) and BUILD_CONSTANTS.MOD_TONE3000_CLIENT_ID = `frozen` (None =
  45. no BUILD_CONSTANTS module, i.e. a device or a source checkout)."""
  46. os.environ.pop('MOD_TONE3000_CLIENT_ID', None)
  47. if env is not None:
  48. os.environ['MOD_TONE3000_CLIENT_ID'] = env
  49. sys.modules.pop('BUILD_CONSTANTS', None)
  50. if frozen is not None:
  51. constants = types.ModuleType('BUILD_CONSTANTS')
  52. constants.MOD_TONE3000_CLIENT_ID = frozen
  53. sys.modules['BUILD_CONSTANTS'] = constants
  54. sys.modules.pop('mod.settings', None)
  55. return importlib.import_module('mod.settings')
  56. def test_device_without_build_constants_reads_the_environment(self):
  57. self.assertEqual(self._settings('t3k_pub_env', None).TONE3000_CLIENT_ID, 't3k_pub_env')
  58. def test_device_without_anything_has_no_id(self):
  59. self.assertIsNone(self._settings(None, None).TONE3000_CLIENT_ID)
  60. def test_frozen_build_falls_back_to_the_build_constant(self):
  61. self.assertEqual(self._settings(None, 't3k_pub_frozen').TONE3000_CLIENT_ID, 't3k_pub_frozen')
  62. def test_environment_wins_over_the_build_constant(self):
  63. self.assertEqual(self._settings('t3k_pub_env', 't3k_pub_frozen').TONE3000_CLIENT_ID, 't3k_pub_env')
  64. def test_an_empty_environment_value_does_not_shadow_the_build_constant(self):
  65. self.assertEqual(self._settings('', 't3k_pub_frozen').TONE3000_CLIENT_ID, 't3k_pub_frozen')
  66. def test_the_environment_variable_is_consumed(self):
  67. # As before: popped, so no child process inherits it.
  68. self._settings('t3k_pub_env', None)
  69. self.assertNotIn('MOD_TONE3000_CLIENT_ID', os.environ)
  70. try:
  71. import tornado.template
  72. except ImportError: # pragma: no cover - depends on the host
  73. tornado = None
  74. @unittest.skipIf(tornado is None, 'tornado is not importable here')
  75. class TestIndexTemplateKeepsTheSeamOffDevices(unittest.TestCase):
  76. SEAM = ('js/desktop-app.js', 'css/desktop-app.css', 'DesktopApp', 'desktop_app_templates=1')
  77. def _render(self, desktop):
  78. with open(os.path.join(HTML_DIR, 'index.html'), 'r') as fh:
  79. source = fh.read()
  80. # Every {{name}} the page substitutes, as TemplateHandler.index() would
  81. # hand them over; the values do not matter here, only the branches.
  82. context = dict.fromkeys(re.findall(r'\{\{([a-zA-Z_][a-zA-Z0-9_]*)\}\}', source), '')
  83. context.update({
  84. 'version': '1',
  85. 'using_desktop': 'true' if desktop else 'false',
  86. 'using_mod': 'false' if desktop else 'true',
  87. 'desktop_app': desktop,
  88. 'factory_pedalboards': False,
  89. 'codec_truebypass': 'false',
  90. 'preferences': '{}',
  91. 'favorites': '[]',
  92. 'addressing_pages': '[]',
  93. })
  94. template = tornado.template.Template(source, name='index.html', autoescape=None)
  95. return template.generate(**context).decode('utf-8')
  96. def test_device_page_carries_nothing_of_the_seam(self):
  97. page = self._render(desktop=False)
  98. for needle in self.SEAM:
  99. self.assertNotIn(needle, page)
  100. self.assertIn('js/templates.js?v=1"', page)
  101. self.assertIn("enable_dev_mode(true)", page)
  102. def test_desktop_page_carries_all_of_it(self):
  103. page = self._render(desktop=True)
  104. for needle in self.SEAM:
  105. self.assertIn(needle, page)
  106. self.assertIn('DesktopApp.setup(desktop)', page)
  107. self.assertIn('js/templates.js?v=1&desktop_app_templates=1"', page)
  108. self.assertNotIn("enable_dev_mode(true)", page)
  109. if __name__ == '__main__':
  110. unittest.main()