// room-self.test.mjs — the self view is played, watched and, when the // browser will not play it, explained (2026-10-21: an iPhone in Safari // showed its own picture black for a whole call while sending it). // // node --test test/browser/room-self.test.mjs // // judgeSelf, playSelf and comeBack lifted out of the shipped room.js by // name, with the constants they read, as room-reconnect.test.mjs does. // The browser half is call.mjs's `selfview` and `selfviewwebkit`. 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] + ';'; } // eslint-disable-next-line no-new-func const judgeSelf = new Function(liftConst('SELF_STUCK_MS') + '\n' + lift('judgeSelf') + '; return judgeSelf;')(); const STUCK = Number(liftConst('SELF_STUCK_MS').match(/= ([0-9.]+)/)[1]); const fresh = () => ({ time: -1, since: -1, tries: 0, said: false }); const live = (now, time, paused = false) => ({ live: true, visible: true, paused, time, now }); // Once a second from `from` for `n` polls, the element's clock and paused // flag from `at(now)`; the verdicts that were not empty, with their times. function poll(w, from, n, at) { const out = []; for (let i = 0; i < n; i++) { const now = from + i * 1000; const v = judgeSelf(w, { live: true, visible: true, now, ...at(now) }); if (v) out.push([now, v]); } return out; } test('a self view that is playing is left alone', () => { const w = fresh(); assert.deepEqual(poll(w, 0, 10, (now) => ({ paused: false, time: now / 1000 })), []); }); test('a paused self view is replayed once, then said, two seconds apart', () => { const w = fresh(); const got = poll(w, 0, 8, () => ({ paused: true, time: 3 })); assert.deepEqual(got, [[STUCK, 'replay'], [2 * STUCK, 'say']]); assert.equal(w.said, true); }); test('a self view that is not paused but whose clock stands still counts as stuck', () => { const w = fresh(); const got = poll(w, 0, 6, () => ({ paused: false, time: 1.5 })); assert.deepEqual(got.map(([, v]) => v), ['replay', 'say']); }); test('the words come down as soon as the picture moves again, and the watch starts over', () => { const w = fresh(); poll(w, 0, 6, () => ({ paused: true, time: 3 })); assert.equal(w.said, true); assert.equal(judgeSelf(w, live(6000, 3.2)), 'clear'); assert.equal(w.said, false); assert.equal(w.tries, 0); // A second stall gets its own replay before anything is said. const again = poll(w, 7000, 6, () => ({ paused: true, time: 3.2 })); assert.deepEqual(again.map(([, v]) => v), ['replay', 'say']); }); test('a replay that works is never said', () => { const w = fresh(); let t = 2; const got = poll(w, 0, 10, (now) => (now <= STUCK ? { paused: true, time: t } : { paused: false, time: (t += 1) })); assert.deepEqual(got.map(([, v]) => v), ['replay']); assert.equal(w.said, false); }); test('a camera that is off, muted or ended, or a hidden tab, is not the watch’s business', () => { for (const s of [{ live: false, visible: true }, { live: true, visible: false }]) { const w = fresh(); for (let now = 0; now < 10000; now += 1000) { assert.equal(judgeSelf(w, { ...s, paused: true, time: 0, now }), ''); } assert.equal(w.tries, 0); } // And one that goes off while the words are up takes them down: the // camera-off cover says what there is to say. const w = fresh(); poll(w, 0, 6, () => ({ paused: true, time: 0 })); assert.equal(w.said, true); assert.equal(judgeSelf(w, { live: false, visible: true, paused: true, time: 0, now: 7000 }), 'clear'); }); test('a clock that went backwards (a new binding) is a start, not a stall cleared', () => { const w = fresh(); judgeSelf(w, live(0, 9)); // Rebinding resets the element's clock: not moving, so the watch starts // its two seconds rather than calling it playing. assert.equal(judgeSelf(w, live(1000, 0)), ''); assert.equal(judgeSelf(w, live(2000, 0.6)), ''); assert.equal(w.since, -1); }); // playSelf over a fake element: what a refusal and a success do to the // tile, and that neither is ever §15.20's sound refusal. function playHarness(result) { const drawn = []; const t = { video: { play: () => result }, selfWatch: { time: -1, since: -1, tries: 0, said: false }, }; const tiles = new Map([['self', t]]); // eslint-disable-next-line no-new-func const playSelf = new Function('tiles', 'drawSelfNote', 'soundRefused', 'startPlaying', lift('playSelf') + '; return playSelf;')(tiles, (x) => drawn.push(x.selfWatch.said), () => assert.fail('a self view is never a sound refusal'), () => assert.fail('a self view never goes through startPlaying')); return { t, drawn, playSelf }; } test('a refused preview is said at once, on the tile', async () => { const h = playHarness(Promise.reject(new DOMException('no', 'NotAllowedError'))); h.playSelf(h.t); await new Promise((r) => setTimeout(r, 0)); assert.equal(h.t.selfWatch.said, true); assert.deepEqual(h.drawn, [true]); }); test('a play that was only interrupted (AbortError) says nothing', async () => { const h = playHarness(Promise.reject(new DOMException('new load', 'AbortError'))); h.playSelf(h.t); await new Promise((r) => setTimeout(r, 0)); assert.equal(h.t.selfWatch.said, false); assert.deepEqual(h.drawn, []); }); test('a play that works takes the words down', async () => { const h = playHarness(Promise.resolve()); h.t.selfWatch.said = true; h.playSelf(h.t); await new Promise((r) => setTimeout(r, 0)); assert.equal(h.t.selfWatch.said, false); assert.deepEqual(h.drawn, [false]); }); // comeBack over fakes: the self tile is asked to play again along with // the remote ones — the line whose absence left it black. test('the tab coming back plays a paused self view, through playSelf', () => { const played = []; const started = []; const selfT = { video: { paused: true }, stream: {} }; const remote = { video: { paused: true }, stream: {}, play: { hidden: true } }; const tiles = new Map([['self', selfT], ['peer1', remote]]); const local = { getAudioTracks: () => [], getVideoTracks: () => [] }; // eslint-disable-next-line no-new-func const comeBack = new Function('state', 'local', 'lostKinds', 'prefs', 'switchDevice', 'tiles', 'shouldPlay', 'startPlaying', 'playSelf', lift('comeBack') + '; return comeBack;')( { stopped: false, leaveSent: false }, local, () => [], {}, () => Promise.resolve(), tiles, (t, isSelf) => !isSelf && !!t.stream, (key) => started.push(key), (t) => played.push(t)); comeBack(); assert.deepEqual(played, [selfT]); assert.deepEqual(started, ['peer1']); }); // The binding: tile() hands ours to playSelf whenever it binds a stream, // rather than trusting `autoplay`. tile() builds DOM and is not lifted; // this holds the one line in place. test('tile() asks the self view to play on every binding', () => { const body = lift('tile'); assert.match(body, /if \(isSelf\) \{\s*if \(stream\) playSelf\(t\);/); assert.match(lift('pollFrames'), /watchSelf\(\);/); });