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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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++) {
  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_RANDOM_DELAY_MIN_ALLOWED_SECONDS = 0;',
  40. 'const AUTO_STEP_RANDOM_DELAY_MAX_ALLOWED_SECONDS = 600;',
  41. 'const AUTO_STEP_RANDOM_DELAY_DEFAULT_MIN_SECONDS = 12;',
  42. 'const AUTO_STEP_RANDOM_DELAY_DEFAULT_MAX_SECONDS = 18;',
  43. 'const PERSISTED_SETTING_DEFAULTS = { autoStepRandomDelayMinSeconds: 12, autoStepRandomDelayMaxSeconds: 18 };',
  44. extractFunction('normalizeAutoStepRandomDelaySeconds'),
  45. extractFunction('normalizeAutoStepRandomDelayRange'),
  46. extractFunction('getAutoStepRandomDelayMs'),
  47. ].join('\n');
  48. const api = new Function(`${bundle}; return { normalizeAutoStepRandomDelaySeconds, normalizeAutoStepRandomDelayRange, getAutoStepRandomDelayMs };`)();
  49. const defaultRange = api.normalizeAutoStepRandomDelayRange({});
  50. assert.deepStrictEqual(defaultRange, { minSeconds: 12, maxSeconds: 18 }, 'missing settings should fall back to the default 12-18 second range');
  51. for (let i = 0; i < 200; i += 1) {
  52. const delay = api.getAutoStepRandomDelayMs(defaultRange.minSeconds * 1000, defaultRange.maxSeconds * 1000);
  53. assert.ok(delay >= 12000, `delay ${delay} should respect the lower bound`);
  54. assert.ok(delay <= 18000, `delay ${delay} should respect the upper bound`);
  55. assert.ok(Number.isInteger(delay), `delay ${delay} should be an integer`);
  56. }
  57. const samples = new Set(Array.from({ length: 50 }, () => api.getAutoStepRandomDelayMs(defaultRange.minSeconds * 1000, defaultRange.maxSeconds * 1000)));
  58. assert.ok(samples.size > 1, 'delay helper should produce randomized values');
  59. const customRange = api.normalizeAutoStepRandomDelayRange({
  60. autoStepRandomDelayMinSeconds: 5,
  61. autoStepRandomDelayMaxSeconds: 7,
  62. });
  63. assert.deepStrictEqual(customRange, { minSeconds: 5, maxSeconds: 7 }, 'configured ranges should be preserved when already valid');
  64. const collapsedRange = api.normalizeAutoStepRandomDelayRange({
  65. autoStepRandomDelayMinSeconds: 20,
  66. autoStepRandomDelayMaxSeconds: 10,
  67. });
  68. assert.deepStrictEqual(collapsedRange, { minSeconds: 20, maxSeconds: 20 }, 'max delay should collapse up to min delay when user input is inverted');
  69. assert.strictEqual(
  70. api.getAutoStepRandomDelayMs(15000, 15000),
  71. 15000,
  72. 'equal bounds should produce a deterministic delay'
  73. );
  74. const originalRandom = Math.random;
  75. try {
  76. Math.random = () => 0.6;
  77. assert.strictEqual(
  78. api.normalizeAutoStepRandomDelaySeconds(-50, 12),
  79. 0,
  80. 'negative configured delay should clamp to zero seconds'
  81. );
  82. assert.strictEqual(
  83. api.getAutoStepRandomDelayMs(-50, 10),
  84. 6,
  85. 'lower bound should be clamped to zero and still allow random output'
  86. );
  87. } finally {
  88. Math.random = originalRandom;
  89. }
  90. console.log('auto step random delay tests passed');