harness.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // SPDX-FileCopyrightText: 2012-2023 MOD Audio UG
  2. // SPDX-License-Identifier: AGPL-3.0-or-later
  3. /*
  4. * Test rig for the browser-global frontend.
  5. *
  6. * html/js/*.js is not modular -- it defines globals and assumes jQuery, Mustache and a few
  7. * page-scoped variables already exist. So it cannot be require()'d. Instead we stand up a
  8. * jsdom window, load the real vendored libraries into it, then load the real source file
  9. * under test with window.eval, and drive its now-global functions.
  10. *
  11. * What this reaches, it tests for real. What jsdom lacks, it cannot: there is no layout
  12. * (getBoundingClientRect is all zeros), no windowing (window.open is a stub), no network,
  13. * no CSS. Popup *placement*, cross-window behaviour and styling are out of scope here and
  14. * stay manual -- see README.md.
  15. */
  16. const fs = require('fs')
  17. const path = require('path')
  18. const { JSDOM } = require('jsdom')
  19. const HTML = path.resolve(__dirname, '../../html')
  20. // Vendored libraries the frontend takes for granted, in load order.
  21. const LIBS = [
  22. 'js/lib/jquery-1.9.1.min.js',
  23. 'js/lib/mustache.js',
  24. 'js/lib/sprintf-0.6.js',
  25. ]
  26. /*
  27. * Build a fresh window with jQuery + Mustache loaded and the globals the frontend expects
  28. * to find on the page. Returns { window, $, load } where load(relPath) evals a real source
  29. * file (path relative to html/) into the window.
  30. *
  31. * opts.url -- document origin, e.g. 'http://localhost:8888/' (default about:blank)
  32. * opts.body -- initial <body> innerHTML
  33. * opts.jqueryUI -- stub draggable/droppable/resizable (icons call these); default true
  34. */
  35. function makeWindow(opts) {
  36. opts = opts || {}
  37. const dom = new JSDOM('<!doctype html><html><body>' + (opts.body || '') + '</body></html>', {
  38. runScripts: 'outside-only',
  39. url: opts.url || 'about:blank',
  40. })
  41. const window = dom.window
  42. const load = rel => window.eval(fs.readFileSync(path.join(HTML, rel), 'utf8'))
  43. LIBS.forEach(load)
  44. const $ = window.jQuery
  45. // Page-scoped globals index.html defines before any js/ file runs. modgui.js reads
  46. // VERSION/baseUrl at GUI-construction time; several files reference desktop/isSDK.
  47. window.eval('var desktop = null; var isSDK = false; var VERSION = 1; var baseUrl = "";')
  48. // jsdom has no TextEncoder on the window; PKCE code paths want it.
  49. if (!window.TextEncoder) {
  50. window.TextEncoder = require('util').TextEncoder
  51. }
  52. if (opts.jqueryUI !== false) {
  53. ['draggable', 'droppable', 'resizable'].forEach(m => { $.fn[m] = function () { return this } })
  54. }
  55. return { window, $, load }
  56. }
  57. /*
  58. * Capture what a JqueryClass box hands to self.window(options), without pulling in the real
  59. * window.js overlay machinery. Call before loading a file that defines a *Box widget; the
  60. * returned getter yields the latest options object the box registered.
  61. */
  62. function captureWindowOptions(ctx) {
  63. let captured = null
  64. ctx.window.JqueryClass = function (name, methods) {
  65. ctx.$.fn[name] = function (o) { return methods.init.apply(this, [o]) }
  66. }
  67. ctx.$.fn.window = function (options) { captured = options; return this }
  68. return () => captured
  69. }
  70. /*
  71. * An $.ajax stub that answers by URL substring. routes is { substring: responseOrFn };
  72. * a function receives the ajax options and returns the response passed to success.
  73. * Unmatched URLs invoke opts.error if present. Returns a thenable-ish object like $.ajax.
  74. */
  75. function stubAjax(ctx, routes) {
  76. ctx.$.ajax = opts => {
  77. const url = opts.url || ''
  78. for (const key in routes) {
  79. if (url.indexOf(key) >= 0) {
  80. const r = routes[key]
  81. opts.success(typeof r === 'function' ? r(opts) : r)
  82. return { done: () => {} }
  83. }
  84. }
  85. if (opts.error) opts.error()
  86. return { done: () => {} }
  87. }
  88. }
  89. module.exports = { makeWindow, captureWindowOptions, stubAjax, HTML }