// room-reconnect.test.mjs — "Reconnecting…" only for a problem that // lasts (spec §15.23, 2026-09-30), gated without a browser. // // node --test test/browser/room-reconnect.test.mjs // // reconnectStep and reconnectRecord lifted out of the shipped room.js by // name, with the constants they read, as room-screen.test.mjs does. import { test } from 'node:test'; import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import { fileURLToPath } from 'node:url'; const src = readFileSync(fileURLToPath(new URL('../../internal/calls/static/room.js', import.meta.url)), 'utf8'); function lift(name) { const start = src.indexOf('function ' + name + '('); assert.notEqual(start, -1, name + ' is not in room.js any more — this test is stale, not passing'); let depth = 0; for (let i = src.indexOf('{', start); i < src.length; i++) { if (src[i] === '{') depth += 1; else if (src[i] === '}') { depth -= 1; if (depth === 0) return src.slice(start, i + 1); } } assert.fail('unbalanced braces reading ' + name); } function liftConst(name) { const m = src.match(new RegExp('const ' + name + ' = ([0-9.]+);')); assert.ok(m, name + ' is not a numeric const in room.js any more'); return 'const ' + name + ' = ' + m[1] + ';'; } const CONSTS = ['RECONNECT_STREAM_AFTER', 'RECONNECT_CONNECTION_AFTER', 'RECONNECT_AUDIO_AFTER', 'RECONNECT_HOLD'] .map(liftConst).join('\n'); // eslint-disable-next-line no-new-func const { reconnectRecord, reconnectStep } = new Function(CONSTS + '\n' + lift('reconnectRecord') + '\n' + lift('reconnectStep') + '; return { reconnectRecord, reconnectStep };')(); const fine = { connectionBad: false, audioMissing: false }; // Five looks a second from `from` to `to` seconds; the times the whole // call's words showed, and the tiles' at each time. function run(rec, from, to, stream, people) { const call = []; const peers = new Map(); for (let t = from * 10; t < to * 10; t += 2) { const s = t / 10; reconnectStep(rec, s * 1000, stream(s), people(s)); if (rec.call) call.push(s); if (rec.peers.size) peers.set(s, Array.from(rec.peers).sort().join()); } return { call, peers }; } test('a routine stream renewal, and a slow one, are never said', () => { const rec = reconnectRecord(); const seen = run(rec, 0, 60, (t) => (t >= 10 && t < 10.6) || (t >= 30 && t < 33.5), () => new Map([['sam', fine]])); assert.deepEqual(seen.call, []); assert.equal(seen.peers.size, 0); }); test('a stream down four seconds is said, and held a second past recovery', () => { const rec = reconnectRecord(); const seen = run(rec, 0, 20, (t) => t >= 5 && t < 12, () => new Map([['sam', fine]])); assert.equal(seen.call[0], 9); assert.equal(seen.call[seen.call.length - 1], 12.8); }); test('short blips on one person are never said', () => { const rec = reconnectRecord(); const seen = run(rec, 0, 30, () => false, (t) => new Map([['sam', { connectionBad: t >= 5 && t < 7.5, audioMissing: t >= 15 && t < 16.5, }]])); assert.deepEqual(seen.call, []); assert.equal(seen.peers.size, 0); }); test('one person\'s lasting trouble is said on their tile alone', () => { const rec = reconnectRecord(); const seen = run(rec, 0, 30, () => false, (t) => new Map([['sam', { connectionBad: t >= 5 && t < 15, audioMissing: false }], ['ana', fine]])); assert.deepEqual(seen.call, []); const times = Array.from(seen.peers.keys()); assert.equal(times[0], 8); assert.equal(times[times.length - 1], 15.8); assert.ok(Array.from(seen.peers.values()).every((v) => v === 'sam')); }); test('audio that stops from someone unmuted is said after two seconds', () => { const rec = reconnectRecord(); const seen = run(rec, 0, 20, () => false, (t) => new Map([['sam', { connectionBad: false, audioMissing: t >= 5 && t < 10 }], ['ana', fine]])); assert.equal(Array.from(seen.peers.keys())[0], 7); }); test('everyone\'s trouble is the whole call, not a tile each', () => { const rec = reconnectRecord(); const seen = run(rec, 0, 20, () => false, (t) => { const bad = { connectionBad: t >= 5 && t < 12, audioMissing: false }; return new Map([['sam', bad], ['ana', bad]]); }); assert.equal(seen.call[0], 8); assert.equal(seen.peers.size, 0); });