desktop-app.test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // SPDX-FileCopyrightText: 2012-2023 MOD Audio UG
  2. // SPDX-License-Identifier: AGPL-3.0-or-later
  3. /*
  4. * DesktopApp -- the MOD Desktop ("desktop-app") seam.
  5. *
  6. * Driven against the REAL html/js/desktop-app.js, the REAL mustache partials under
  7. * html/include/desktop-app/, and the REAL markup in html/index.html and
  8. * html/include/addressing.html. jsdom has no CSS, so what a greyed icon or the overlay
  9. * *looks* like is out of scope; what is pinned here is the wiring:
  10. *
  11. * - Plugin Store, Banks and File Manager keep their icon, lose their window, and open
  12. * the one shared overlay with their own copy.
  13. * - Hardware addressing keeps the in-window card; the Control Chain icon is hidden; Share is greyed.
  14. * - Settings, status, RAM and the MIDI-port toggle hide; the Constructor and the
  15. * Pedalboard Library are untouched.
  16. * - setup() is what flips the switch, and nothing else does. That a MOD device never
  17. * meets any of this is pinned by desktop-app.scan.test.js, which needs no jsdom.
  18. */
  19. const { test, beforeEach } = require('node:test')
  20. const assert = require('node:assert')
  21. const fs = require('fs')
  22. const path = require('path')
  23. const { makeWindow, HTML } = require('./harness')
  24. // Mirrors BulkTemplateLoader: html/include/desktop-app/<name>.html becomes
  25. // TEMPLATES['desktop_app_<name>'].
  26. function loadDesktopAppTemplates(window) {
  27. const dir = path.join(HTML, 'include', 'desktop-app')
  28. const templates = {}
  29. fs.readdirSync(dir).forEach(f => {
  30. if (!/^[a-z_]+\.html$/.test(f)) return
  31. templates['desktop_app_' + f.slice(0, -5)] = fs.readFileSync(path.join(dir, f), 'utf8')
  32. })
  33. window.TEMPLATES = templates
  34. }
  35. /*
  36. * html/index.html is a tornado template, so {{ }} and {% %} survive as text -- harmless
  37. * here, since we only assert on structure.
  38. */
  39. function loadIndexBody(ctx) {
  40. const html = fs.readFileSync(path.join(HTML, 'index.html'), 'utf8')
  41. const body = html.slice(html.indexOf('<body'), html.lastIndexOf('</body>'))
  42. ctx.window.document.body.innerHTML = body.slice(body.indexOf('>') + 1)
  43. }
  44. let ctx, $, DesktopApp
  45. beforeEach(() => {
  46. ctx = makeWindow()
  47. $ = ctx.$
  48. loadDesktopAppTemplates(ctx.window)
  49. ctx.load('js/desktop-app.js')
  50. DesktopApp = ctx.window.DesktopApp
  51. DesktopApp.active = false
  52. })
  53. // The card ----------------------------------------------------------------
  54. test('the card renders the feature name, the sentence and the store link', () => {
  55. const panel = $(DesktopApp.panel('Banks', 'Step through pedalboards hands-free.'))
  56. assert.strictEqual(panel.find('h2').text(), 'Banks')
  57. assert.match(panel.find('.desktop-app-exclusive-eyebrow').text(), /Exclusive to MOD Audio devices/)
  58. assert.match(panel.find('p').text(), /Step through pedalboards hands-free\./)
  59. assert.strictEqual(panel.find('a').attr('href'), DesktopApp.STORE_URL)
  60. assert.strictEqual(panel.find('a').attr('target'), '_blank')
  61. assert.strictEqual(panel.find('img').attr('src'), DesktopApp.IMAGE_URL)
  62. })
  63. /* One card, two layouts: dark full-window, and light-on-white in a form. Only the
  64. * variant class and the photo differ, so a template change cannot leave one behind. */
  65. test('the form variant is the same card with the light photo and a variant class', () => {
  66. const dark = $(DesktopApp.panel('Banks', 'x'))
  67. const light = $(DesktopApp.formPanel('Hardware addressing', 'x'))
  68. ;[dark, light].forEach(card => {
  69. assert.ok(card.hasClass('desktop-app-exclusive-panel'), 'shares the base class')
  70. assert.strictEqual(card.find('a').attr('href'), DesktopApp.STORE_URL)
  71. })
  72. assert.ok(!dark.hasClass('desktop-app-exclusive-form'))
  73. assert.strictEqual(dark.find('img').attr('src'), DesktopApp.IMAGE_URL)
  74. assert.ok(light.hasClass('desktop-app-exclusive-form'))
  75. assert.strictEqual(light.find('img').attr('src'), DesktopApp.IMAGE_URL_LIGHT)
  76. })
  77. /* The photos are bundled, not hotlinked: MOD Desktop runs offline, and a broken
  78. * image is the whole point of these panels failing. */
  79. test('the panel images are bundled files', () => {
  80. ;[DesktopApp.IMAGE_URL, DesktopApp.IMAGE_URL_LIGHT].forEach(url => {
  81. assert.ok(!/^https?:/.test(url), url + ' must be bundled')
  82. assert.ok(fs.existsSync(path.join(HTML, url)), url + ' is missing from html/')
  83. })
  84. })
  85. // The main menu and the overlay --------------------------------------------
  86. test('setup() finds every element it reaches for in index.html', () => {
  87. loadIndexBody(ctx)
  88. // Each of these is a selector crossing from desktop-app.js into index.html.
  89. const targets = ['#mod-cloud-plugins', '#mod-bank', '#mod-file-manager',
  90. '#mod-settings', '#mod-status', '#mod-ram', '#mod-show-midi-port',
  91. '#pedalboards-library', '#mod-devices', '#mod-devices-window',
  92. '#pedalboard-info .js-cloud',
  93. '#pedal-presets-window']
  94. targets.forEach(sel => {
  95. assert.strictEqual($(sel).length, 1, sel + ' is missing from index.html')
  96. })
  97. Object.keys(DesktopApp.EXCLUSIVE).forEach(id => {
  98. assert.strictEqual($('#main-menu #' + id).length, 1, '#' + id + ' is not a main-menu icon')
  99. })
  100. })
  101. test('Plugin Store, Banks and File Manager keep a greyed icon and open the overlay, not their window', () => {
  102. loadIndexBody(ctx)
  103. // index.html hides the store in its non-device branch, before setup() runs.
  104. $('#mod-cloud-plugins').hide()
  105. // What window.js binds on each icon when `desktop` constructs the boxes.
  106. const opened = []
  107. ;['mod-cloud-plugins', 'mod-bank', 'mod-file-manager'].forEach(id => {
  108. $('#' + id).click(() => opened.push(id))
  109. })
  110. DesktopApp.setup(null)
  111. assert.ok($('body').hasClass('desktop-app'))
  112. ;['mod-cloud-plugins', 'mod-bank', 'mod-file-manager'].forEach(id => {
  113. const icon = $('#' + id)
  114. assert.notStrictEqual(icon.css('display'), 'none', '#' + id + ' must stay visible')
  115. assert.ok(icon.hasClass('desktop-app-greyed'), '#' + id + ' must be greyed')
  116. icon.click()
  117. const overlay = $('#desktop-app-exclusive-overlay')
  118. assert.strictEqual(overlay.length, 1, 'one overlay, appended to the body')
  119. assert.strictEqual(overlay.css('display'), 'block', 'the overlay opens')
  120. assert.strictEqual(overlay.find('h2').text(), DesktopApp.EXCLUSIVE[id].feature)
  121. assert.strictEqual(overlay.find('p').text(), DesktopApp.EXCLUSIVE[id].message)
  122. assert.strictEqual(overlay.find('a').attr('href'), DesktopApp.STORE_URL)
  123. DesktopApp.hideUpsell()
  124. })
  125. assert.deepStrictEqual(opened, [], "the feature windows' own toggles no longer run")
  126. assert.strictEqual($('.desktop-app-exclusive-overlay').length, 1, 'the overlay is reused, not stacked')
  127. })
  128. test('the overlay closes on its button and on Escape', () => {
  129. loadIndexBody(ctx)
  130. DesktopApp.setup(null)
  131. $('#mod-bank').click()
  132. const overlay = $('#desktop-app-exclusive-overlay')
  133. assert.strictEqual(overlay.css('display'), 'block')
  134. overlay.find('.js-close').click()
  135. assert.strictEqual(overlay.css('display'), 'none')
  136. $('#mod-file-manager').click()
  137. assert.strictEqual(overlay.css('display'), 'block')
  138. $(ctx.window.document).trigger($.Event('keydown', { keyCode: 27 }))
  139. assert.strictEqual(overlay.css('display'), 'none')
  140. // Anywhere outside the card is backdrop; the card itself is not.
  141. $('#mod-cloud-plugins').click()
  142. assert.strictEqual(overlay.css('display'), 'block')
  143. overlay.find('.desktop-app-exclusive-card h2').click()
  144. assert.strictEqual(overlay.css('display'), 'block', 'a click inside the card keeps it open')
  145. overlay.find('.desktop-app-exclusive-panel').click()
  146. assert.strictEqual(overlay.css('display'), 'none', 'a click beside the card closes it')
  147. })
  148. test('setup() hides the device-only readouts, Control Chain included, and keeps the Constructor and the Library', () => {
  149. loadIndexBody(ctx)
  150. DesktopApp.setup(null)
  151. // Absent for unrelated reasons: hidden, no card.
  152. ;['#mod-settings', '#mod-status', '#mod-ram', '#mod-show-midi-port'].forEach(sel => {
  153. assert.strictEqual($(sel).css('display'), 'none', sel + ' must hide')
  154. })
  155. assert.strictEqual($('#pedal-presets-window .js-assign-all').css('display'), 'none')
  156. // What MOD Desktop is for.
  157. ;['#mod-plugins', '#mod-pedalboard'].forEach(sel => {
  158. assert.notStrictEqual($(sel).css('display'), 'none', sel + ' must stay')
  159. assert.ok(!$(sel).hasClass('desktop-app-greyed'), sel + ' must not be greyed')
  160. })
  161. // Control Chain is not advertised: icon and window gone, no card anywhere.
  162. assert.strictEqual($('#mod-devices').css('display'), 'none', 'the Control Chain icon must not show')
  163. assert.strictEqual($('#mod-devices-window').css('display'), 'none')
  164. assert.strictEqual($('#mod-devices-window .desktop-app-exclusive-panel').length, 0, 'no card in the window')
  165. $('#mod-devices').click()
  166. assert.strictEqual($('#mod-devices-window').css('display'), 'none', 'clicking the hidden icon opens nothing')
  167. })
  168. test('setup() greys the Share button and routes it to the overlay instead of the share window', () => {
  169. loadIndexBody(ctx)
  170. let shared = 0
  171. const button = $('#pedalboard-info .js-cloud')
  172. button.click(() => { shared++ }) // what desktop.js binds: the share window
  173. DesktopApp.setup(null)
  174. assert.notStrictEqual(button.css('display'), 'none', 'the button stays visible')
  175. assert.ok(button.hasClass('desktop-app-greyed'), 'and greyed like the other device-only entries')
  176. button.click()
  177. assert.strictEqual(shared, 0, 'the share window handler must be gone')
  178. const overlay = $('#desktop-app-exclusive-overlay')
  179. assert.strictEqual(overlay.length, 1, 'the shared overlay opened')
  180. assert.notStrictEqual(overlay.css('display'), 'none')
  181. assert.strictEqual(overlay.find('h2').text(), 'Share')
  182. assert.ok(overlay.find('a.desktop-app-exclusive-cta').attr('href').startsWith('https://mod.audio/'), 'button to the website')
  183. })
  184. test('setup() tells the shared controller it is the app and switches analytics off', () => {
  185. loadIndexBody(ctx)
  186. let tracked = 0
  187. const desktop = { isApp: false, setupMatomo: () => { tracked++ } }
  188. DesktopApp.setup(desktop)
  189. assert.strictEqual(desktop.isApp, true, 'hardware.js and installMissingPlugins ask desktop.isApp')
  190. desktop.setupMatomo()
  191. assert.strictEqual(tracked, 0, 'the Matomo loader must never run on MOD Desktop')
  192. })
  193. test('isActive stays false until setup runs', () => {
  194. loadIndexBody(ctx)
  195. assert.strictEqual(DesktopApp.isActive(), false)
  196. DesktopApp.setup(null)
  197. assert.strictEqual(DesktopApp.isActive(), true)
  198. })
  199. // Hardware addressing ------------------------------------------------------
  200. /*
  201. * Selecting a hardware-only tab swaps the addressing options for the card, and
  202. * takes Save and Advanced with them -- there is nothing to save or configure.
  203. * Driven against the real html/include/addressing.html so a markup rename fails
  204. * here rather than silently at runtime.
  205. */
  206. test('the addressing upsell replaces the options, and every tab switch undoes it', () => {
  207. ctx.window.document.body.innerHTML =
  208. fs.readFileSync(path.join(HTML, 'include', 'addressing.html'), 'utf8')
  209. DesktopApp.active = true
  210. const form = $('form')
  211. const dynamic = form.find('.dynamic-field').show()
  212. DesktopApp.showAddressingUpsell(form)
  213. const card = form.find('.main-form-container').find('.desktop-app-exclusive-form')
  214. assert.strictEqual(card.length, 1, 'the card lands inside the options area')
  215. assert.strictEqual(card.css('display'), 'block')
  216. dynamic.each((_, el) => assert.strictEqual($(el).css('display'), 'none'))
  217. assert.strictEqual(form.find('.js-save').css('display'), 'none')
  218. assert.strictEqual(form.find('.advanced-toggle').css('display'), 'none')
  219. DesktopApp.hideAddressingUpsell(form)
  220. assert.strictEqual(card.css('display'), 'none')
  221. assert.notStrictEqual(form.find('.js-save').css('display'), 'none')
  222. assert.notStrictEqual(form.find('.advanced-toggle').css('display'), 'none')
  223. // Re-selecting reuses the one card rather than stacking a second.
  224. DesktopApp.showAddressingUpsell(form)
  225. assert.strictEqual(form.find('.desktop-app-exclusive').length, 1)
  226. })
  227. test('the addressing upsell is inert when not active', () => {
  228. ctx.window.document.body.innerHTML =
  229. fs.readFileSync(path.join(HTML, 'include', 'addressing.html'), 'utf8')
  230. const form = $('form')
  231. DesktopApp.hideAddressingUpsell(form)
  232. assert.strictEqual(form.find('.desktop-app-exclusive').length, 0)
  233. assert.strictEqual(form.find('.js-save').attr('style'), undefined, 'Save is left alone')
  234. })