// native-ice.mjs — spec §15.28 between the Mac app and a browser: a // connection that cannot come up for a while (ICETestBlock, the app's // every candidate either way pointed at nothing) is mended once the path // is back, with nobody pressing anything. Driven by TestNativeMacIceBrowser. // // node native-ice.mjs // // case "both": both sides are this release. "app-old": the app neither // mends nor says its version (an app from before §15.28), so the browser // mends it alone, and once a minute has passed says the app may be old. // "web-old": the browser's recovery is off, so the app mends it alone. // The browser's camera is off: its own camera watch (§15.16) resets a pair // whose camera stops encoding, which mends a blocked pair by accident and // would hide whether the recovery did. import { chromium } from 'playwright'; import { spawn } from 'node:child_process'; import { createInterface } from 'node:readline'; const [origin, room, binary, kase] = process.argv.slice(2); const blockMs = Number(process.env.ICE_BLOCK_MS || 25000); const within = Number(process.env.ICE_RECOVER_WITHIN || 90000); let failed = false; const check = (ok, what) => { console.log((ok ? 'PASS ' : 'FAIL ') + what); if (!ok) failed = true; return ok; }; const browser = await chromium.launch({ headless: true, args: [ '--use-fake-device-for-media-stream', '--use-fake-ui-for-media-stream', '--autoplay-policy=no-user-gesture-required', '--disable-features=WebRtcHideLocalIpsWithMdns', '--mute-audio', ] }); const context = await browser.newContext({ permissions: ['camera', 'microphone'] }); await context.addCookies([{ name: process.env.CALLS_COOKIE_NAME, value: process.env.CALLS_COOKIE_A, url: origin }]); const page = await context.newPage(); let native; try { await page.goto(`${origin}/m/${room}`); await page.waitForFunction(() => !!window.meet); await page.waitForFunction(() => window.meet.state().admitted, null, { timeout: 20000 }); await page.click('#camera'); if (kase === 'web-old') await page.evaluate(() => window.meet.recoveryForTest(false)); const env = { ...process.env, CALLS_TEST_ICE_BLOCK_MS: String(blockMs) }; if (kase === 'app-old') Object.assign(env, { CALLS_AUTO_RECOVERY: '0', CALLS_TEST_NO_CLIENT: '1' }); const blockedAt = Date.now(); native = spawn(binary, [], { stdio: ['pipe', 'pipe', 'pipe'], env }); native.stdin.write(process.env.CALLS_NATIVE_INPUT + '\n'); let sample = null; let exited = false; native.on('exit', () => { exited = true; }); // Only the gate's own observations: never library output, which can // carry SDP and candidates. createInterface({ input: native.stdout }).on('line', (line) => { try { const v = JSON.parse(line); if (v.stats) sample = v; } catch { /* not ours */ } }); native.stderr.resume(); const until = async (pred, ms) => { const end = Date.now() + ms; while (Date.now() < end) { if (exited) return false; const v = await pred(); if (v) return v; await new Promise((r) => setTimeout(r, 250)); } return false; }; const web = (id) => page.evaluate((id) => { const s = window.meet.state(); return { connection: Object.fromEntries(s.connection)[id] || '', hearing: s.hearingList.includes(id), recovery: s.recovery[id] || null, autoRestarts: s.autoRestarts, autoRebuilds: s.autoRebuilds, note: s.notes[id] || '', client: s.clients[id], reconnecting: s.reconnect.peers.includes(id), connecting: !!(s.connecting[id] && s.connecting[id].shown) }; }, id); const inCall = await until(() => sample && sample.phase === 'admitted' && sample.peer ? sample : false, 30000); if (!check(inCall, 'the app is in the call')) throw new Error('no app'); const app = inCall.peer; // The block: nothing comes up, and nothing worse than "connecting" or // "Reconnecting to …" is said before the minute. let said = false; let early = ''; let failure = false; let maybeOld = false; while (Date.now() - blockedAt < blockMs) { const w = await web(app); if (w.connection === 'connected') { check(false, 'the browser connected to the app through the block'); break; } if (w.reconnecting || w.connecting) said = true; if (/^Could not connect/.test(w.note)) { if (Date.now() - blockedAt < 50000) early = w.note; failure = true; } if (/may be on an old version of Calls/.test(w.note)) maybeOld = true; await new Promise((r) => setTimeout(r, 250)); } console.log('block over: browser ' + JSON.stringify(await web(app)) + '; app ' + JSON.stringify({ iceConnected: sample.stats.iceConnectedPeers, recoveryTrying: sample.stats.recoveryTrying, autoRestarts: sample.stats.autoRestarts, autoRebuilds: sample.stats.autoRebuilds, mangled: sample.stats.iceBlockMangled })); check(said, 'the browser’s tile for the app said it was connecting, or Reconnecting to …'); check(!early, 'no failure sentence before its minute (' + early + ')'); if (blockMs >= 75000) { check(failure, 'the failure sentence came once the minute had passed'); check(maybeOld === (kase === 'app-old'), 'the tile says the app may be old exactly when it does not say its version (' + maybeOld + ')'); } check((await web(app)).client === (kase === 'app-old' ? 'native' : 'mac 9.9.9'), 'the roster names the app’s client (' + (await web(app)).client + ')'); // Nobody does anything. const audio = sample.stats.audioBytesReceived; const mended = await until(async () => { const w = await web(app); return w.connection === 'connected' && w.hearing && sample.stats.iceConnectedPeers === 1 && sample.stats.audioBytesReceived > audio + 10000 ? w : false; }, within); const w = await web(app); console.log('browser: ' + JSON.stringify(w)); console.log('app: ' + JSON.stringify({ status: sample.status, notices: sample.notices, autoRestarts: sample.stats.autoRestarts, autoRebuilds: sample.stats.autoRebuilds })); if (check(mended, 'the browser and the app hear each other again within ' + within / 1000 + ' s of the block lifting, with nobody acting')) { console.log('recovered ' + Math.round((Date.now() - blockedAt - blockMs) / 1000) + ' s after the block lifted'); } if (kase === 'app-old') check(sample.stats.autoRestarts === 0 && w.autoRestarts > 0, 'the browser mended it alone'); if (kase === 'web-old') check(w.autoRestarts === 0 && sample.stats.autoRestarts > 0, 'the app mended it alone'); const quiet = await until(async () => { const v = await web(app); return !v.note && !v.reconnecting && !sample.status.includes('could not connect') ? v : false; }, 5000); check(quiet, 'the words go with the trouble, both sides'); } catch (err) { check(false, String(err && err.message || err)); } finally { if (native) { try { native.stdin.write('leave\n'); } catch { /* gone */ } await new Promise((r) => setTimeout(r, 1500)); native.kill(); } await browser.close(); } process.exit(failed ? 1 : 0);