bridge.reconnect.test.mjs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * Unit tests for the reconnect scheduling and version resolution guards.
  3. *
  4. * Regression tests for the reconnect-wedge trap: startSocket() awaits
  5. * network I/O (fetchLatestBaileysVersion has no AbortSignal) before it
  6. * creates a socket, and the close handler used to re-enter it via a bare
  7. * `setTimeout(startSocket, ...)`. A rejection was unhandled and a stalled
  8. * fetch left the bridge permanently disconnected while its HTTP server
  9. * kept answering 503 — observed in the field as a bridge that logged
  10. * "Reconnecting in 3s..." once and then went silent for 27+ hours.
  11. *
  12. * These tests avoid importing bridge.js because that file starts an HTTP
  13. * server and Baileys socket at module load. Keep the helper module pure.
  14. */
  15. import { strict as assert } from 'node:assert';
  16. import {
  17. createReconnectScheduler,
  18. createVersionResolver,
  19. } from './bridge_helpers.js';
  20. const tick = () => new Promise(resolve => setImmediate(resolve));
  21. // -- createReconnectScheduler ---------------------------------------------
  22. // A rejecting start function is caught and rescheduled at the retry delay;
  23. // a subsequent success stops the retry chain.
  24. {
  25. const timers = [];
  26. const logs = [];
  27. let attempts = 0;
  28. const startFn = async () => {
  29. attempts += 1;
  30. if (attempts === 1) throw new Error('boom');
  31. };
  32. const schedule = createReconnectScheduler(startFn, {
  33. retryDelayMs: 5000,
  34. log: line => logs.push(line),
  35. setTimeoutFn: (fn, ms) => timers.push({ fn, ms }),
  36. });
  37. schedule(3000);
  38. assert.equal(timers.length, 1);
  39. assert.equal(timers[0].ms, 3000);
  40. timers[0].fn();
  41. await tick();
  42. await tick();
  43. assert.equal(attempts, 1);
  44. assert.equal(logs.length, 1);
  45. assert.match(logs[0], /boom/);
  46. assert.equal(timers.length, 2, 'rejection must schedule a retry');
  47. assert.equal(timers[1].ms, 5000);
  48. timers[1].fn();
  49. await tick();
  50. await tick();
  51. assert.equal(attempts, 2);
  52. assert.equal(timers.length, 2, 'success must not schedule another attempt');
  53. assert.equal(logs.length, 1);
  54. }
  55. // A synchronous throw from the start function is contained the same way as
  56. // an async rejection.
  57. {
  58. const timers = [];
  59. const logs = [];
  60. const schedule = createReconnectScheduler(
  61. () => { throw new Error('sync boom'); },
  62. {
  63. retryDelayMs: 1000,
  64. log: line => logs.push(line),
  65. setTimeoutFn: (fn, ms) => timers.push({ fn, ms }),
  66. },
  67. );
  68. schedule(0);
  69. timers[0].fn();
  70. await tick();
  71. await tick();
  72. assert.equal(logs.length, 1);
  73. assert.match(logs[0], /sync boom/);
  74. assert.equal(timers.length, 2);
  75. }
  76. // -- createVersionResolver ------------------------------------------------
  77. // A successful fetch returns and caches the version.
  78. {
  79. const resolveVersion = createVersionResolver(
  80. async () => ({ version: [2, 3000, 99] }),
  81. { log: () => {} },
  82. );
  83. assert.deepEqual(await resolveVersion(), [2, 3000, 99]);
  84. }
  85. // A fetch that never settles resolves within the timeout bound instead of
  86. // pending forever; before any success there is no cache, so the resolver
  87. // yields null (callers fall back to the Baileys default).
  88. {
  89. const logs = [];
  90. const resolveVersion = createVersionResolver(
  91. () => new Promise(() => {}),
  92. { timeoutMs: 20, log: line => logs.push(line) },
  93. );
  94. assert.equal(await resolveVersion(), null);
  95. assert.equal(logs.length, 1);
  96. }
  97. // After one success, later failures fall back to the cached version.
  98. {
  99. const logs = [];
  100. let calls = 0;
  101. const resolveVersion = createVersionResolver(
  102. async () => {
  103. calls += 1;
  104. if (calls === 1) return { version: [2, 3000, 42] };
  105. throw new Error('network down');
  106. },
  107. { timeoutMs: 20, log: line => logs.push(line) },
  108. );
  109. assert.deepEqual(await resolveVersion(), [2, 3000, 42]);
  110. assert.deepEqual(await resolveVersion(), [2, 3000, 42]);
  111. assert.equal(logs.length, 1);
  112. assert.match(logs[0], /network down/);
  113. }
  114. console.log('bridge.reconnect.test.mjs: all assertions passed');