| 1 | // room-recovery.test.mjs — a connection that stops working is mended |
| 2 | // without anyone asking (spec §15.28, 2026-10-21), gated without a browser. |
| 3 | // |
| 4 | // node --test test/browser/room-recovery.test.mjs |
| 5 | // |
| 6 | // recoveryStep and its helpers, clientNotice and the gate's own candidate |
| 7 | // block, lifted out of the shipped room.js by name with the constants they |
| 8 | // read. The apps' ConnectionRecoveryTests are the twin of these. |
| 9 | import { test } from 'node:test'; |
| 10 | import assert from 'node:assert/strict'; |
| 11 | import { readFileSync } from 'node:fs'; |
| 12 | import { fileURLToPath } from 'node:url'; |
| 13 | |
| 14 | const src = readFileSync(fileURLToPath(new URL('../../internal/calls/static/room.js', import.meta.url)), 'utf8'); |
| 15 | |
| 16 | function lift(name) { |
| 17 | const start = src.indexOf('function ' + name + '('); |
| 18 | assert.notEqual(start, -1, name + ' is not in room.js any more — this test is stale, not passing'); |
| 19 | let depth = 0; |
| 20 | for (let i = src.indexOf('{', start); i < src.length; i++) { |
| 21 | if (src[i] === '{') depth += 1; |
| 22 | else if (src[i] === '}') { |
| 23 | depth -= 1; |
| 24 | if (depth === 0) return src.slice(start, i + 1); |
| 25 | } |
| 26 | } |
| 27 | assert.fail('unbalanced braces reading ' + name); |
| 28 | } |
| 29 | function liftConst(name) { |
| 30 | const m = src.match(new RegExp('const ' + name + ' = ([^;]+);')); |
| 31 | assert.ok(m, name + ' is not a const in room.js any more'); |
| 32 | return 'const ' + name + ' = ' + m[1] + ';'; |
| 33 | } |
| 34 | const CONSTS = ['RECOVER_DISCONNECTED_AFTER', 'RECOVER_CONNECT_AFTER', 'RECOVER_BACKOFF', 'RECOVER_EVERY', |
| 35 | 'RECOVER_FOLLOW', 'RECOVER_NOTICE_AFTER', 'RECOVER_FLAP', 'MIN_NATIVE_VERSION'].map(liftConst).join('\n'); |
| 36 | const NAMES = ['recoveryRecord', 'recoveryGap', 'recoveryKind', 'recoveryStep', 'recoveryTrying', 'recoveryFailing', |
| 37 | 'parseClient', 'compareVersions', 'clientNotice', 'blockCandidateLine']; |
| 38 | // eslint-disable-next-line no-new-func |
| 39 | const r = new Function(CONSTS + '\n' + NAMES.map(lift).join('\n') + '; return { ' + NAMES.join(', ') + ' };')(); |
| 40 | |
| 41 | // Five looks a second, in whole ticks, from `from` to `to` seconds; each |
| 42 | // action with the second it was asked for. |
| 43 | function run(rec, from, to, look) { |
| 44 | const out = []; |
| 45 | for (let tick = Math.round(from * 5); tick <= Math.round(to * 5); tick++) { |
| 46 | const t = tick / 5; |
| 47 | const a = r.recoveryStep(rec, t * 1000, look(t)); |
| 48 | if (a) out.push([t, a]); |
| 49 | } |
| 50 | return out; |
| 51 | } |
| 52 | const look = (ice, o = {}) => Object.assign({ ice, builtAt: -100000, leader: true, signalling: true, remoteRestartAt: 0, errored: false }, o); |
| 53 | |
| 54 | test('a failed connection is restarted twice, then rebuilt, on the backoff', () => { |
| 55 | const rec = r.recoveryRecord(); |
| 56 | const seen = run(rec, 0, 160, () => look('failed')); |
| 57 | assert.deepEqual(seen.map((a) => a[0]), [0, 2, 7, 17, 37, 67, 97, 127, 157]); |
| 58 | assert.deepEqual(seen.map((a) => a[1]), |
| 59 | ['restart', 'restart', 'rebuild', 'restart', 'restart', 'rebuild', 'restart', 'restart', 'rebuild']); |
| 60 | assert.equal(rec.restarts, 6); |
| 61 | assert.equal(rec.rebuilds, 3); |
| 62 | }); |
| 63 | |
| 64 | test('disconnected is trouble after five seconds, and a blip is nothing', () => { |
| 65 | const rec = r.recoveryRecord(); |
| 66 | assert.deepEqual(run(rec, 0, 4.8, () => look('disconnected')), []); |
| 67 | assert.equal(r.recoveryStep(rec, 5000, look('disconnected')), 'restart'); |
| 68 | const blips = r.recoveryRecord(); |
| 69 | assert.deepEqual(run(blips, 0, 20, (t) => look(Math.floor(t) % 6 < 4 ? 'disconnected' : 'connected')), []); |
| 70 | }); |
| 71 | |
| 72 | test('a connection that never comes up is trouble fifteen seconds after it was made', () => { |
| 73 | for (const ice of ['new', 'checking']) { |
| 74 | const rec = r.recoveryRecord(); |
| 75 | assert.deepEqual(run(rec, 0, 14.8, () => look(ice, { builtAt: 0 })), []); |
| 76 | assert.equal(r.recoveryStep(rec, 15000, look(ice, { builtAt: 0 })), 'restart'); |
| 77 | } |
| 78 | }); |
| 79 | |
| 80 | test('the failure is said after a minute, and goes with the trouble', () => { |
| 81 | const rec = r.recoveryRecord(); |
| 82 | run(rec, 0, 59.8, () => look('failed')); |
| 83 | assert.equal(r.recoveryTrying(rec), true); |
| 84 | assert.equal(r.recoveryFailing(rec, 59800), false); |
| 85 | assert.equal(r.recoveryFailing(rec, 60000), true); |
| 86 | assert.equal(r.recoveryStep(rec, 61000, look('connected')), ''); |
| 87 | assert.equal(r.recoveryTrying(rec), false); |
| 88 | assert.equal(r.recoveryFailing(rec, 61000), false); |
| 89 | }); |
| 90 | |
| 91 | test('trouble soon after a recovery carries on where the schedule was', () => { |
| 92 | const rec = r.recoveryRecord(); |
| 93 | run(rec, 0, 8, () => look('failed')); |
| 94 | assert.equal(rec.attempts, 3); |
| 95 | r.recoveryStep(rec, 9000, look('connected')); |
| 96 | const again = run(rec, 20, 60, () => look('failed')); |
| 97 | assert.deepEqual(again[0], [30, 'restart']); |
| 98 | const later = r.recoveryRecord(); |
| 99 | run(later, 0, 8, () => look('failed')); |
| 100 | r.recoveryStep(later, 9000, look('connected')); |
| 101 | assert.equal(r.recoveryStep(later, 80000, look('failed')), 'restart'); |
| 102 | }); |
| 103 | |
| 104 | test('nothing is attempted while the signalling stream is down', () => { |
| 105 | const rec = r.recoveryRecord(); |
| 106 | assert.deepEqual(run(rec, 0, 30, () => look('failed', { signalling: false })), []); |
| 107 | assert.equal(r.recoveryTrying(rec), true); |
| 108 | assert.equal(r.recoveryStep(rec, 30200, look('failed')), 'restart'); |
| 109 | }); |
| 110 | |
| 111 | test('a description that would not apply hastens trouble only while not connected', () => { |
| 112 | const rec = r.recoveryRecord(); |
| 113 | assert.equal(r.recoveryStep(rec, 1000, look('checking', { builtAt: 0, errored: true })), 'restart'); |
| 114 | const up = r.recoveryRecord(); |
| 115 | assert.equal(r.recoveryStep(up, 1000, look('connected', { builtAt: 0, errored: true })), ''); |
| 116 | assert.equal(r.recoveryTrying(up), false); |
| 117 | }); |
| 118 | |
| 119 | test('a closed connection is left alone', () => { |
| 120 | const rec = r.recoveryRecord(); |
| 121 | assert.deepEqual(run(rec, 0, 30, () => look('closed')), []); |
| 122 | }); |
| 123 | |
| 124 | test('the polite side follows three seconds behind, and only when the leader does not act', () => { |
| 125 | const alone = r.recoveryRecord(); |
| 126 | const seen = run(alone, 0, 21, () => look('failed', { leader: false })); |
| 127 | assert.deepEqual(seen, [[3, 'restart'], [5, 'restart'], [10, 'rebuild'], [20, 'restart']]); |
| 128 | |
| 129 | const follower = r.recoveryRecord(); |
| 130 | let built = -100000; |
| 131 | const none = run(follower, 0, 40, (t) => { |
| 132 | const restart = t >= 17.1 ? 17100 : t >= 2.1 ? 2100 : t >= 0.1 ? 100 : 0; |
| 133 | if (t >= 7.1) built = 7100; |
| 134 | return look('failed', { leader: false, builtAt: built, remoteRestartAt: restart }); |
| 135 | }); |
| 136 | assert.deepEqual(none, []); |
| 137 | }); |
| 138 | |
| 139 | test('a follower a beat behind the leader does not rebuild a second time', () => { |
| 140 | const follower = r.recoveryRecord(); |
| 141 | const seen = run(follower, 0, 12, (t) => look('failed', { |
| 142 | leader: false, builtAt: t >= 6.1 ? 6100 : -100000, remoteRestartAt: t >= 1.1 ? 1100 : 0 })); |
| 143 | assert.equal(seen.some((a) => a[1] === 'rebuild'), false, JSON.stringify(seen)); |
| 144 | }); |
| 145 | |
| 146 | test('the version notice: old apps always, apps that do not say only when failing, browsers never', () => { |
| 147 | assert.equal(r.clientNotice('Farshad', 'mac 0.4.1', false), 'Farshad is on an old version of Calls — ask them to update'); |
| 148 | assert.equal(r.clientNotice('Farshad', 'iphone 0.3.9', true), 'Farshad is on an old version of Calls — ask them to update'); |
| 149 | assert.equal(r.clientNotice('Farshad', 'mac 0.4.2', true), ''); |
| 150 | assert.equal(r.clientNotice('Farshad', 'mac 0.4.12', true), ''); |
| 151 | assert.equal(r.clientNotice('Farshad', 'native', false), ''); |
| 152 | assert.equal(r.clientNotice('Farshad', 'native', true), 'Farshad may be on an old version of Calls — ask them to update'); |
| 153 | assert.equal(r.clientNotice('Farshad', '', true), ''); |
| 154 | assert.equal(r.clientNotice('Farshad', 'web 882eab8', true), ''); |
| 155 | assert.equal(r.clientNotice('Farshad', 'mac latest', true), ''); |
| 156 | }); |
| 157 | |
| 158 | test('versions compare numerically, part by part', () => { |
| 159 | assert.equal(r.compareVersions('0.4.10', '0.4.2'), 1); |
| 160 | assert.equal(r.compareVersions('0.4.1', '0.4.2'), -1); |
| 161 | assert.equal(r.compareVersions('0.4', '0.4.0'), 0); |
| 162 | assert.equal(r.compareVersions('0.10', '0.9.9'), 1); |
| 163 | }); |
| 164 | |
| 165 | test('the minimum is the server\'s', () => { |
| 166 | const go = readFileSync(fileURLToPath(new URL('../../protocol/client.go', import.meta.url)), 'utf8'); |
| 167 | const m = src.match(/const MIN_NATIVE_VERSION = '([0-9.]+)';/); |
| 168 | assert.ok(m); |
| 169 | assert.ok(go.includes('const MinNativeVersion = "' + m[1] + '"'), 'MIN_NATIVE_VERSION and protocol.MinNativeVersion disagree'); |
| 170 | }); |
| 171 | |
| 172 | test('the gate\'s block points a candidate at the discard port and leaves everything else', () => { |
| 173 | assert.equal(r.blockCandidateLine('candidate:1 1 udp 2122260223 192.168.1.5 54321 typ host generation 0'), |
| 174 | 'candidate:1 1 udp 2122260223 192.168.1.5 9 typ host generation 0'); |
| 175 | assert.equal(r.blockCandidateLine('a=candidate:1 1 tcp 1518280447 ::1 9999 typ host tcptype passive'), |
| 176 | 'a=candidate:1 1 tcp 1518280447 ::1 9 typ host tcptype passive'); |
| 177 | assert.equal(r.blockCandidateLine('a=ice-ufrag:abcd'), 'a=ice-ufrag:abcd'); |
| 178 | assert.equal(r.blockCandidateLine(''), ''); |
| 179 | }); |
| 180 | |