test_stereo.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // SPDX-FileCopyrightText: 2012-2026 MOD Audio UG
  2. // SPDX-License-Identifier: AGPL-3.0-or-later
  3. // Run with: node test/test_stereo.js
  4. //
  5. // stereoCounterpart is the only guessing part of the stereo cable feature, so it is the
  6. // only part worth a test. Everything downstream of it checks the guess against a port
  7. // that either exists or does not.
  8. const assert = require('assert')
  9. const fs = require('fs')
  10. const path = require('path')
  11. // html/js/utils/*.js are plain globals, no module system
  12. eval(fs.readFileSync(path.join(__dirname, '..', 'html', 'js', 'utils', 'stereo.js'), 'utf8'))
  13. function pairs(symbol, partner, first) {
  14. const got = stereoCounterpart(symbol)
  15. assert.notStrictEqual(got, null, `${symbol} should have a counterpart`)
  16. assert.strictEqual(got.symbol, partner, `${symbol} -> ${got.symbol}, expected ${partner}`)
  17. assert.strictEqual(got.first, first, `${symbol} first=${got.first}, expected ${first}`)
  18. }
  19. function mono(symbol) {
  20. assert.strictEqual(stereoCounterpart(symbol), null, `${symbol} should have no counterpart`)
  21. }
  22. // A symbol that merely ends in l or r still yields a guess. That is deliberate: the
  23. // caller only draws a pair when a port of that exact name, direction and type exists,
  24. // and nothing is called "signar". Refusing to guess here is what broke real plugins.
  25. function guessesNothingReal(symbol, guess) {
  26. const got = stereoCounterpart(symbol)
  27. assert.notStrictEqual(got, null, `${symbol} should still produce a guess`)
  28. assert.strictEqual(got.symbol, guess, `${symbol} -> ${got.symbol}, expected ${guess}`)
  29. }
  30. // separator-delimited sides, both directions
  31. pairs('in_l', 'in_r', true)
  32. pairs('in_r', 'in_l', false)
  33. pairs('out-l', 'out-r', true)
  34. pairs('audio_left', 'audio_right', true)
  35. pairs('audio_right', 'audio_left', false)
  36. // the whole symbol is the side
  37. pairs('l', 'r', true)
  38. pairs('R', 'L', false)
  39. pairs('Left', 'Right', true)
  40. // a case change is enough on its own, and casing is carried over
  41. pairs('outL', 'outR', true)
  42. pairs('InR', 'InL', false)
  43. pairs('OUT_L', 'OUT_R', true)
  44. // ...but so is no case change at all. Real symbols, from plugins installed here and
  45. // from https://github.com/sejerpz/mod-ui/pull/12: tap/reverb names its ports inputl
  46. // and inputr, and refusing those meant its cables never merged.
  47. pairs('inputl', 'inputr', true) // tap/reverb
  48. pairs('inputr', 'inputl', false)
  49. pairs('InputL', 'InputR', true) // tap/autopan, the same plugin pair
  50. pairs('outl', 'outr', true) // caps Plate, Scape, Wider
  51. pairs('inl', 'inr', true) // caps SpiceX2, PlateX2
  52. pairs('inputleft', 'inputright', true) // tap-echo, a full word and no separator
  53. pairs('OUT1L', 'OUT1R', true) // switchbox, a digit before the side
  54. pairs('In1L', 'In1R', true)
  55. // numbered channels, including the second pair of a four-channel plugin
  56. pairs('out1', 'out2', true)
  57. pairs('out2', 'out1', false)
  58. pairs('capture_1', 'capture_2', true)
  59. pairs('playback_2', 'playback_1', false)
  60. pairs('out_3', 'out_4', true)
  61. pairs('out_4', 'out_3', false)
  62. pairs('lv2_audio_out_01', 'lv2_audio_out_02', true) // zero padding survives
  63. // symbols that name no side at all
  64. mono('in')
  65. mono('output')
  66. mono('sidechain')
  67. mono('out_0') // channel numbering starts at 1
  68. // words that merely end in l or r: a guess is made, but it can never match a port
  69. guessesNothingReal('signal', 'signar')
  70. guessesNothingReal('SIGNAL', 'SIGNAR')
  71. guessesNothingReal('pedal', 'pedar')
  72. guessesNothingReal('mixer', 'mixel')
  73. guessesNothingReal('monitor', 'monitol')
  74. guessesNothingReal('bright', 'bleft')
  75. // a guessed partner is not a real one: these only pair because the port exists
  76. assert.strictEqual(stereoCounterpart('gain_1').symbol, 'gain_2')
  77. console.log('stereo: all assertions passed')