// room-recovery.test.mjs — a connection that stops working is mended // without anyone asking (spec §15.28, 2026-10-21), gated without a browser. // // node --test test/browser/room-recovery.test.mjs // // recoveryStep and its helpers, clientNotice and the gate's own candidate // block, lifted out of the shipped room.js by name with the constants they // read. The apps' ConnectionRecoveryTests are the twin of these. 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 + ' = ([^;]+);')); assert.ok(m, name + ' is not a const in room.js any more'); return 'const ' + name + ' = ' + m[1] + ';'; } const CONSTS = ['RECOVER_DISCONNECTED_AFTER', 'RECOVER_CONNECT_AFTER', 'RECOVER_BACKOFF', 'RECOVER_EVERY', 'RECOVER_FOLLOW', 'RECOVER_NOTICE_AFTER', 'RECOVER_FLAP', 'MIN_NATIVE_VERSION'].map(liftConst).join('\n'); const NAMES = ['recoveryRecord', 'recoveryGap', 'recoveryKind', 'recoveryStep', 'recoveryTrying', 'recoveryFailing', 'parseClient', 'compareVersions', 'clientNotice', 'blockCandidateLine']; // eslint-disable-next-line no-new-func const r = new Function(CONSTS + '\n' + NAMES.map(lift).join('\n') + '; return { ' + NAMES.join(', ') + ' };')(); // Five looks a second, in whole ticks, from `from` to `to` seconds; each // action with the second it was asked for. function run(rec, from, to, look) { const out = []; for (let tick = Math.round(from * 5); tick <= Math.round(to * 5); tick++) { const t = tick / 5; const a = r.recoveryStep(rec, t * 1000, look(t)); if (a) out.push([t, a]); } return out; } const look = (ice, o = {}) => Object.assign({ ice, builtAt: -100000, leader: true, signalling: true, remoteRestartAt: 0, errored: false }, o); test('a failed connection is restarted twice, then rebuilt, on the backoff', () => { const rec = r.recoveryRecord(); const seen = run(rec, 0, 160, () => look('failed')); assert.deepEqual(seen.map((a) => a[0]), [0, 2, 7, 17, 37, 67, 97, 127, 157]); assert.deepEqual(seen.map((a) => a[1]), ['restart', 'restart', 'rebuild', 'restart', 'restart', 'rebuild', 'restart', 'restart', 'rebuild']); assert.equal(rec.restarts, 6); assert.equal(rec.rebuilds, 3); }); test('disconnected is trouble after five seconds, and a blip is nothing', () => { const rec = r.recoveryRecord(); assert.deepEqual(run(rec, 0, 4.8, () => look('disconnected')), []); assert.equal(r.recoveryStep(rec, 5000, look('disconnected')), 'restart'); const blips = r.recoveryRecord(); assert.deepEqual(run(blips, 0, 20, (t) => look(Math.floor(t) % 6 < 4 ? 'disconnected' : 'connected')), []); }); test('a connection that never comes up is trouble fifteen seconds after it was made', () => { for (const ice of ['new', 'checking']) { const rec = r.recoveryRecord(); assert.deepEqual(run(rec, 0, 14.8, () => look(ice, { builtAt: 0 })), []); assert.equal(r.recoveryStep(rec, 15000, look(ice, { builtAt: 0 })), 'restart'); } }); test('the failure is said after a minute, and goes with the trouble', () => { const rec = r.recoveryRecord(); run(rec, 0, 59.8, () => look('failed')); assert.equal(r.recoveryTrying(rec), true); assert.equal(r.recoveryFailing(rec, 59800), false); assert.equal(r.recoveryFailing(rec, 60000), true); assert.equal(r.recoveryStep(rec, 61000, look('connected')), ''); assert.equal(r.recoveryTrying(rec), false); assert.equal(r.recoveryFailing(rec, 61000), false); }); test('trouble soon after a recovery carries on where the schedule was', () => { const rec = r.recoveryRecord(); run(rec, 0, 8, () => look('failed')); assert.equal(rec.attempts, 3); r.recoveryStep(rec, 9000, look('connected')); const again = run(rec, 20, 60, () => look('failed')); assert.deepEqual(again[0], [30, 'restart']); const later = r.recoveryRecord(); run(later, 0, 8, () => look('failed')); r.recoveryStep(later, 9000, look('connected')); assert.equal(r.recoveryStep(later, 80000, look('failed')), 'restart'); }); test('nothing is attempted while the signalling stream is down', () => { const rec = r.recoveryRecord(); assert.deepEqual(run(rec, 0, 30, () => look('failed', { signalling: false })), []); assert.equal(r.recoveryTrying(rec), true); assert.equal(r.recoveryStep(rec, 30200, look('failed')), 'restart'); }); test('a description that would not apply hastens trouble only while not connected', () => { const rec = r.recoveryRecord(); assert.equal(r.recoveryStep(rec, 1000, look('checking', { builtAt: 0, errored: true })), 'restart'); const up = r.recoveryRecord(); assert.equal(r.recoveryStep(up, 1000, look('connected', { builtAt: 0, errored: true })), ''); assert.equal(r.recoveryTrying(up), false); }); test('a closed connection is left alone', () => { const rec = r.recoveryRecord(); assert.deepEqual(run(rec, 0, 30, () => look('closed')), []); }); test('the polite side follows three seconds behind, and only when the leader does not act', () => { const alone = r.recoveryRecord(); const seen = run(alone, 0, 21, () => look('failed', { leader: false })); assert.deepEqual(seen, [[3, 'restart'], [5, 'restart'], [10, 'rebuild'], [20, 'restart']]); const follower = r.recoveryRecord(); let built = -100000; const none = run(follower, 0, 40, (t) => { const restart = t >= 17.1 ? 17100 : t >= 2.1 ? 2100 : t >= 0.1 ? 100 : 0; if (t >= 7.1) built = 7100; return look('failed', { leader: false, builtAt: built, remoteRestartAt: restart }); }); assert.deepEqual(none, []); }); test('a follower a beat behind the leader does not rebuild a second time', () => { const follower = r.recoveryRecord(); const seen = run(follower, 0, 12, (t) => look('failed', { leader: false, builtAt: t >= 6.1 ? 6100 : -100000, remoteRestartAt: t >= 1.1 ? 1100 : 0 })); assert.equal(seen.some((a) => a[1] === 'rebuild'), false, JSON.stringify(seen)); }); test('the version notice: old apps always, apps that do not say only when failing, browsers never', () => { assert.equal(r.clientNotice('Farshad', 'mac 0.4.1', false), 'Farshad is on an old version of Calls — ask them to update'); assert.equal(r.clientNotice('Farshad', 'iphone 0.3.9', true), 'Farshad is on an old version of Calls — ask them to update'); assert.equal(r.clientNotice('Farshad', 'mac 0.4.2', true), ''); assert.equal(r.clientNotice('Farshad', 'mac 0.4.12', true), ''); assert.equal(r.clientNotice('Farshad', 'native', false), ''); assert.equal(r.clientNotice('Farshad', 'native', true), 'Farshad may be on an old version of Calls — ask them to update'); assert.equal(r.clientNotice('Farshad', '', true), ''); assert.equal(r.clientNotice('Farshad', 'web 882eab8', true), ''); assert.equal(r.clientNotice('Farshad', 'mac latest', true), ''); }); test('versions compare numerically, part by part', () => { assert.equal(r.compareVersions('0.4.10', '0.4.2'), 1); assert.equal(r.compareVersions('0.4.1', '0.4.2'), -1); assert.equal(r.compareVersions('0.4', '0.4.0'), 0); assert.equal(r.compareVersions('0.10', '0.9.9'), 1); }); test('the minimum is the server\'s', () => { const go = readFileSync(fileURLToPath(new URL('../../protocol/client.go', import.meta.url)), 'utf8'); const m = src.match(/const MIN_NATIVE_VERSION = '([0-9.]+)';/); assert.ok(m); assert.ok(go.includes('const MinNativeVersion = "' + m[1] + '"'), 'MIN_NATIVE_VERSION and protocol.MinNativeVersion disagree'); }); test('the gate\'s block points a candidate at the discard port and leaves everything else', () => { assert.equal(r.blockCandidateLine('candidate:1 1 udp 2122260223 192.168.1.5 54321 typ host generation 0'), 'candidate:1 1 udp 2122260223 192.168.1.5 9 typ host generation 0'); assert.equal(r.blockCandidateLine('a=candidate:1 1 tcp 1518280447 ::1 9999 typ host tcptype passive'), 'a=candidate:1 1 tcp 1518280447 ::1 9 typ host tcptype passive'); assert.equal(r.blockCandidateLine('a=ice-ufrag:abcd'), 'a=ice-ufrag:abcd'); assert.equal(r.blockCandidateLine(''), ''); });