helper.test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * Tests for the injected browser client (helper.js).
  3. *
  4. * helper.js runs in the browser, so its DOM behaviour is exercised live; here we
  5. * unit-test the pure reconnect-backoff function it exports and assert that the
  6. * reconnect / status / tombstone wiring is present.
  7. */
  8. const assert = require('assert');
  9. const fs = require('fs');
  10. const path = require('path');
  11. const HELPER = path.join(__dirname, '../../skills/brainstorming/scripts/helper.js');
  12. const src = fs.readFileSync(HELPER, 'utf-8');
  13. // helper.js is browser code, and the repo is an ES module package, so a plain
  14. // require() won't surface its exports. Evaluate the source in a CommonJS sandbox
  15. // with no `window`, so only the exported pure helpers run (not the browser code).
  16. const moduleShim = { exports: {} };
  17. new Function('module', src)(moduleShim);
  18. const { nextReconnectDelay, MIN_RECONNECT_MS, MAX_RECONNECT_MS, TOMBSTONE_AFTER_MS } = moduleShim.exports;
  19. let passed = 0, failed = 0;
  20. function test(name, fn) {
  21. try { fn(); console.log(` PASS: ${name}`); passed++; }
  22. catch (e) { console.log(` FAIL: ${name}`); console.log(` ${e.message}`); failed++; }
  23. }
  24. console.log('\n--- Backoff (pure) ---');
  25. test('doubles the delay each call', () => {
  26. assert.strictEqual(nextReconnectDelay(500, 30000), 1000);
  27. assert.strictEqual(nextReconnectDelay(1000, 30000), 2000);
  28. assert.strictEqual(nextReconnectDelay(2000, 30000), 4000);
  29. });
  30. test('caps at the maximum', () => {
  31. assert.strictEqual(nextReconnectDelay(20000, 30000), 30000);
  32. assert.strictEqual(nextReconnectDelay(30000, 30000), 30000);
  33. });
  34. test('full progression from MIN caps at MAX and never exceeds it', () => {
  35. const seq = [MIN_RECONNECT_MS];
  36. let d = MIN_RECONNECT_MS;
  37. for (let i = 0; i < 10; i++) { d = nextReconnectDelay(d, MAX_RECONNECT_MS); seq.push(d); }
  38. assert.strictEqual(seq[0], 500);
  39. assert.deepStrictEqual(seq.slice(0, 7), [500, 1000, 2000, 4000, 8000, 16000, 30000]);
  40. assert(seq.every(v => v <= MAX_RECONNECT_MS), 'never exceeds max');
  41. assert.strictEqual(seq[seq.length - 1], 30000, 'settles at the cap');
  42. });
  43. test('exposes sane constants', () => {
  44. assert.strictEqual(MIN_RECONNECT_MS, 500);
  45. assert.strictEqual(MAX_RECONNECT_MS, 30000);
  46. assert(TOMBSTONE_AFTER_MS >= 5000, 'tombstone grace is at least a few seconds');
  47. });
  48. console.log('\n--- Wiring (source) ---');
  49. test('reflects all three connection states', () => {
  50. assert(/Connected/.test(src) && /Reconnecting/.test(src) && /Disconnected/.test(src),
  51. 'should set Connected / Reconnecting / Disconnected status');
  52. assert(src.includes("setProperty('--status-color'"), 'drives the status dot via --status-color');
  53. });
  54. test('renders a tombstone overlay when paused', () => {
  55. assert(src.includes('bs-tombstone'), 'creates the tombstone element');
  56. assert(/Companion paused/.test(src), 'tombstone explains the companion paused');
  57. });
  58. test('hardens reconnection (onerror, null socket, clears pending timer)', () => {
  59. assert(src.includes('onerror'), 'handles onerror');
  60. assert(/ws = null/.test(src), 'nulls the socket on close so sendEvent queues');
  61. assert(src.includes('clearTimeout'), 'clears a pending reconnect before scheduling another');
  62. assert(src.includes('nextReconnectDelay'), 'uses exponential backoff for reconnects');
  63. });
  64. test('reloads on recovery and on reload messages', () => {
  65. assert(/location\.reload\(\)/.test(src), 'reloads to pick up restarted/updated content');
  66. });
  67. console.log(`\n--- Results: ${passed} passed, ${failed} failed ---`);
  68. if (failed > 0) process.exit(1);