auto-step-random-delay.test.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. const assert = require('assert');
  2. const fs = require('fs');
  3. const source = fs.readFileSync('background.js', 'utf8');
  4. function extractFunction(name) {
  5. const start = source.indexOf(`function ${name}(`);
  6. if (start < 0) {
  7. throw new Error(`missing function ${name}`);
  8. }
  9. const paramsStart = source.indexOf('(', start);
  10. let paramsDepth = 0;
  11. let paramsEnd = paramsStart;
  12. for (; paramsEnd < source.length; paramsEnd += 1) {
  13. const ch = source[paramsEnd];
  14. if (ch === '(') paramsDepth += 1;
  15. if (ch === ')') {
  16. paramsDepth -= 1;
  17. if (paramsDepth === 0) {
  18. break;
  19. }
  20. }
  21. }
  22. const braceStart = source.indexOf('{', paramsEnd);
  23. let depth = 0;
  24. let end = braceStart;
  25. for (; end < source.length; end += 1) {
  26. const ch = source[end];
  27. if (ch === '{') depth += 1;
  28. if (ch === '}') {
  29. depth -= 1;
  30. if (depth === 0) {
  31. end += 1;
  32. break;
  33. }
  34. }
  35. }
  36. return source.slice(start, end);
  37. }
  38. const bundle = [
  39. 'const AUTO_STEP_DELAY_MIN_ALLOWED_SECONDS = 0;',
  40. 'const AUTO_STEP_DELAY_MAX_ALLOWED_SECONDS = 600;',
  41. 'const PERSISTED_SETTING_DEFAULTS = { autoStepDelaySeconds: null };',
  42. extractFunction('normalizeAutoStepDelaySeconds'),
  43. extractFunction('resolveLegacyAutoStepDelaySeconds'),
  44. ].join('\n');
  45. const api = new Function(`${bundle}; return { normalizeAutoStepDelaySeconds, resolveLegacyAutoStepDelaySeconds };`)();
  46. assert.strictEqual(
  47. api.normalizeAutoStepDelaySeconds(''),
  48. null,
  49. 'empty input should remain empty instead of being forced to a default delay'
  50. );
  51. assert.strictEqual(
  52. api.normalizeAutoStepDelaySeconds(null, null),
  53. null,
  54. 'null input should stay null'
  55. );
  56. assert.strictEqual(
  57. api.normalizeAutoStepDelaySeconds('0'),
  58. 0,
  59. 'zero seconds should be kept so the UI can explicitly show no extra delay'
  60. );
  61. assert.strictEqual(
  62. api.normalizeAutoStepDelaySeconds('12.9'),
  63. 12,
  64. 'delay seconds should be floored to an integer'
  65. );
  66. assert.strictEqual(
  67. api.normalizeAutoStepDelaySeconds('-50'),
  68. 0,
  69. 'negative delay should clamp to zero seconds'
  70. );
  71. assert.strictEqual(
  72. api.normalizeAutoStepDelaySeconds('999'),
  73. 600,
  74. 'delay should clamp to the configured upper bound'
  75. );
  76. assert.strictEqual(
  77. api.resolveLegacyAutoStepDelaySeconds({}),
  78. undefined,
  79. 'missing legacy fields should not synthesize a migrated delay'
  80. );
  81. assert.strictEqual(
  82. api.resolveLegacyAutoStepDelaySeconds({ autoStepRandomDelayMinSeconds: 12 }),
  83. 12,
  84. 'legacy min-only settings should migrate to that same delay'
  85. );
  86. assert.strictEqual(
  87. api.resolveLegacyAutoStepDelaySeconds({ autoStepRandomDelayMaxSeconds: 18 }),
  88. 18,
  89. 'legacy max-only settings should migrate to that same delay'
  90. );
  91. assert.strictEqual(
  92. api.resolveLegacyAutoStepDelaySeconds({
  93. autoStepRandomDelayMinSeconds: 12,
  94. autoStepRandomDelayMaxSeconds: 18,
  95. }),
  96. 15,
  97. 'legacy min/max ranges should migrate to their rounded midpoint'
  98. );
  99. assert.strictEqual(
  100. api.resolveLegacyAutoStepDelaySeconds({
  101. autoStepRandomDelayMinSeconds: '',
  102. autoStepRandomDelayMaxSeconds: '',
  103. }),
  104. null,
  105. 'empty legacy settings should migrate to no delay'
  106. );
  107. console.log('auto step delay tests passed');