desktop-app.scan.test.js 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // SPDX-FileCopyrightText: 2012-2023 MOD Audio UG
  2. // SPDX-License-Identifier: AGPL-3.0-or-later
  3. /*
  4. * Nothing of the MOD Desktop seam reaches a MOD device.
  5. *
  6. * The seam (html/js/desktop-app.js, its stylesheet, the desktop_app_* partials) is
  7. * only served with MOD_DESKTOP=1. These scans pin the two shared files that still
  8. * mention it to the shapes that keep a device's page clean. They read source only
  9. * and need no jsdom, so they run anywhere node does.
  10. */
  11. const { test } = require('node:test')
  12. const assert = require('node:assert')
  13. const fs = require('fs')
  14. const path = require('path')
  15. const HTML = path.resolve(__dirname, '../../html')
  16. /*
  17. * index.html is served to devices with desktop_app false. Every line that names
  18. * DesktopApp or a desktop-app asset must sit inside the true branch of a
  19. * {% if desktop_app %} block, so the rendered device page carries none of it.
  20. */
  21. test('index.html only mentions the seam inside {% if desktop_app %}', () => {
  22. const src = fs.readFileSync(path.join(HTML, 'index.html'), 'utf8')
  23. const lines = src.split('\n')
  24. // Walk the template's block structure, tracking whether we are inside the
  25. // true branch of a desktop_app conditional. Other {% if %}s are tracked too,
  26. // so their {% else %} / {% end %} cannot be mistaken for ours.
  27. const stack = []
  28. const inDesktop = () => stack.some(b => b.desktop && b.branch === 'then')
  29. let seam = 0
  30. lines.forEach((line, i) => {
  31. const open = /\{%\s*if\s+(.*?)\s*%\}/.exec(line)
  32. if (open) stack.push({ desktop: open[1] === 'desktop_app', branch: 'then' })
  33. if (/\{%\s*else\s*%\}/.test(line)) stack[stack.length - 1].branch = 'else'
  34. const mentions = /DesktopApp|desktop-app\.(js|css)|desktop_app_templates/.test(line)
  35. if (mentions) {
  36. seam++
  37. assert.ok(inDesktop(), 'index.html:' + (i + 1) + ' reaches a device: ' + line.trim())
  38. }
  39. if (/\{%\s*end\s*%\}/.test(line)) stack.pop()
  40. })
  41. assert.strictEqual(stack.length, 0, 'unbalanced template blocks')
  42. assert.ok(seam >= 4, 'the scan found nothing -- it is broken')
  43. })
  44. /*
  45. * The seam is not served to devices, so no shared script may call DesktopApp on a
  46. * path a device can run. hardware.js is the one file that calls it, and only
  47. * behind isApp(), which desktop.js answers from desktop.isApp -- set by
  48. * DesktopApp.setup() and nothing else.
  49. */
  50. test('shared scripts only call DesktopApp behind isApp()', () => {
  51. const dir = path.join(HTML, 'js')
  52. const callers = {}
  53. fs.readdirSync(dir)
  54. .filter(f => f.endsWith('.js') && f !== 'desktop-app.js')
  55. .forEach(f => {
  56. const lines = fs.readFileSync(path.join(dir, f), 'utf8').split('\n')
  57. lines.forEach((line, i) => {
  58. if (!/DesktopApp\./.test(line)) return
  59. callers[f] = (callers[f] || 0) + 1
  60. // The guard opens within a few lines above the call.
  61. const above = lines.slice(Math.max(0, i - 8), i).join('\n')
  62. assert.ok(/if \(options\.isApp\(\)\)/.test(above),
  63. f + ':' + (i + 1) + ' calls DesktopApp outside isApp(): ' + line.trim())
  64. })
  65. })
  66. assert.deepStrictEqual(Object.keys(callers), ['hardware.js'],
  67. 'only hardware.js may call DesktopApp; found ' + JSON.stringify(callers))
  68. assert.strictEqual(callers['hardware.js'], 2, 'show + hide of the addressing upsell')
  69. })
  70. test('desktop.js no longer carries its own MOD Desktop setup', () => {
  71. const src = fs.readFileSync(path.join(HTML, 'js', 'desktop.js'), 'utf8')
  72. assert.ok(!/setupApp/.test(src), 'setupApp moved to DesktopApp.setup')
  73. assert.ok(/this\.isApp = false/.test(src), 'the controller still answers isApp for hardware.js')
  74. })
  75. test('the Desktop partials match what BulkTemplateLoader serves', () => {
  76. const dir = path.join(HTML, 'include', 'desktop-app')
  77. const names = fs.readdirSync(dir)
  78. names.forEach(f => assert.match(f, /^[a-z_]+\.html$/, f + ' would be skipped by the loader'))
  79. // What desktop-app.js renders by name.
  80. const src = fs.readFileSync(path.join(HTML, 'js', 'desktop-app.js'), 'utf8')
  81. for (const m of src.matchAll(/desktop_app_([a-z_]+)/g)) {
  82. assert.ok(names.includes(m[1] + '.html'), 'desktop-app.js renders desktop_app_' + m[1] + ', which has no partial')
  83. }
  84. })