test_teleport.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-FileCopyrightText: 2012-2026 MOD Audio UG
  2. // SPDX-License-Identifier: AGPL-3.0-or-later
  3. // Run with: node test/test_teleport.js
  4. const assert = require('assert')
  5. const fs = require('fs')
  6. const path = require('path')
  7. // html/js/utils/*.js are plain globals, no module system
  8. eval(fs.readFileSync(path.join(__dirname, '..', 'html', 'js', 'utils', 'teleport.js'), 'utf8'))
  9. // --- default names ---------------------------------------------------------
  10. // plugin name and port name together, because the port name alone collides at once:
  11. // two reverbs both have an "Out L"
  12. assert.equal(teleportDefaultName('Reverb', 'Out L', {}), 'Reverb Out L')
  13. // a hardware port has no plugin, so the port name stands alone
  14. assert.equal(teleportDefaultName('', 'Capture 1', {}), 'Capture 1')
  15. // two instances of one plugin: the generated name disambiguates itself, silently,
  16. // because the user did not type it
  17. assert.equal(teleportDefaultName('Reverb', 'Out L', { 'graph/a/out_l': 'Reverb Out L' }),
  18. 'Reverb Out L 2')
  19. assert.equal(teleportDefaultName('Reverb', 'Out L', { 'graph/a/out_l': 'Reverb Out L',
  20. 'graph/b/out_l': 'Reverb Out L 2' }),
  21. 'Reverb Out L 3')
  22. // --- names in use ----------------------------------------------------------
  23. const names = { 'graph/rev/out_l': 'verb L', 'graph/dly/out': 'delay' }
  24. assert.deepEqual(teleportNamesInUse(names, null), { 'verb L': true, 'delay': true })
  25. assert.deepEqual(teleportNamesInUse(names, 'graph/dly/out'), { 'verb L': true })
  26. // --- what the user may type ------------------------------------------------
  27. // a free name is fine
  28. assert.equal(teleportNameAvailable('chorus', 'graph/cho/out', names), true)
  29. // another port's name is refused
  30. assert.equal(teleportNameAvailable('verb L', 'graph/cho/out', names), false)
  31. // a port's OWN current name is not a collision with itself: renaming something to
  32. // what it is already called must be accepted, not rejected
  33. assert.equal(teleportNameAvailable('verb L', 'graph/rev/out_l', names), true)
  34. // empty is not a name; it is how you ask for the teleport to go away
  35. assert.equal(teleportNameAvailable('', 'graph/cho/out', names), false)
  36. assert.equal(teleportNameAvailable(' ', 'graph/cho/out', names), false)
  37. // surrounding space is not significant, so it cannot be used to sneak a duplicate past
  38. assert.equal(teleportNameAvailable(' verb L ', 'graph/cho/out', names), false)
  39. // --- the lifetime rule -----------------------------------------------------
  40. // THE invariant: a name exists exactly as long as at least one of its output's cables
  41. // is teleported. Everything that removes a cable relies on this and nothing else.
  42. {
  43. const n = { 'graph/rev/out_l': 'verb L', 'graph/dly/out': 'delay' }
  44. const c = [{ from: 'graph/rev/out_l', to: 'graph/cho/in' }]
  45. const out = teleportSerialise(n, c)
  46. assert.deepEqual(out.names, { 'graph/rev/out_l': 'verb L' },
  47. 'a name with no teleported cable must not be written')
  48. assert.deepEqual(out.cables, ['graph/rev/out_l -> graph/cho/in'])
  49. }
  50. // the freed name is then available to another output -- the case the rule exists for
  51. {
  52. const n = { 'graph/dly/out': 'delay' }
  53. const c = []
  54. assert.deepEqual(teleportSerialise(n, c).names, {})
  55. // and having been dropped, nothing is holding "delay" any more
  56. assert.equal(teleportNameAvailable('delay', 'graph/other/out', teleportSerialise(n, c).names),
  57. true)
  58. }
  59. // symmetric safety net: a cable whose output has no name cannot be drawn as a
  60. // teleport, so it is not written either
  61. {
  62. const out = teleportSerialise({}, [{ from: 'graph/x/out', to: 'graph/y/in' }])
  63. assert.deepEqual(out.cables, [])
  64. assert.deepEqual(out.names, {})
  65. }
  66. // --- round trip ------------------------------------------------------------
  67. {
  68. const n = { 'graph/rev/out_l': 'verb L' }
  69. const c = [{ from: 'graph/rev/out_l', to: 'graph/cho/in' }]
  70. const back = teleportDeserialise(teleportSerialise(n, c))
  71. assert.deepEqual(back.names, n)
  72. assert.deepEqual(back.cables, c)
  73. }
  74. // junk from disk must not throw; an unreadable file is the same as no teleports
  75. assert.deepEqual(teleportDeserialise(null), { names: {}, cables: [] })
  76. assert.deepEqual(teleportDeserialise({}), { names: {}, cables: [] })
  77. assert.deepEqual(teleportDeserialise({ names: 'nope', cables: 5 }), { names: {}, cables: [] })
  78. assert.deepEqual(teleportDeserialise({ names: {}, cables: ['malformed'] }).cables, [])
  79. // --- stereo pair names --------------------------------------------------------
  80. // a teleported stereo pair shows one box, named after what both halves share
  81. assert.equal(teleportPairPortName('Audio Output 1', 'Audio Output 2'), 'Audio Output')
  82. assert.equal(teleportPairPortName('Output Left', 'Output Right'), 'Output')
  83. assert.equal(teleportPairPortName('Out L', 'Out R'), 'Out')
  84. assert.equal(teleportPairPortName('OutL', 'OutR'), 'Out')
  85. assert.equal(teleportPairPortName('output_left', 'output_right'), 'output')
  86. // a number cut in half is not a name: 11/12 share "1", which is not channel 1
  87. assert.equal(teleportPairPortName('Out 11', 'Out 12'), 'Out')
  88. assert.equal(teleportPairPortName('In 9', 'In 10'), 'In')
  89. // nothing in common, or nothing at all
  90. assert.equal(teleportPairPortName('Left', 'Right'), '')
  91. assert.equal(teleportPairPortName('', 'R'), '')
  92. assert.equal(teleportPairPortName(undefined, null), '')
  93. // and it composes with the default name the way toggleTeleport uses it
  94. assert.equal(teleportDefaultName('Sitar', teleportPairPortName('Audio Output 1', 'Audio Output 2'), {}),
  95. 'Sitar Audio Output')
  96. assert.equal(teleportDefaultName('LP3 Basic', teleportPairPortName('Left', 'Right'), {}), 'LP3 Basic')
  97. console.log('teleport: all assertions passed')