| 1 | // room-self.test.mjs — the self view is played, watched and, when the |
| 2 | // browser will not play it, explained (2026-10-21: an iPhone in Safari |
| 3 | // showed its own picture black for a whole call while sending it). |
| 4 | // |
| 5 | // node --test test/browser/room-self.test.mjs |
| 6 | // |
| 7 | // judgeSelf, playSelf and comeBack lifted out of the shipped room.js by |
| 8 | // name, with the constants they read, as room-reconnect.test.mjs does. |
| 9 | // The browser half is call.mjs's `selfview` and `selfviewwebkit`. |
| 10 | import { test } from 'node:test'; |
| 11 | import assert from 'node:assert/strict'; |
| 12 | import { readFileSync } from 'node:fs'; |
| 13 | import { fileURLToPath } from 'node:url'; |
| 14 | |
| 15 | const src = readFileSync(fileURLToPath(new URL('../../internal/calls/static/room.js', import.meta.url)), 'utf8'); |
| 16 | |
| 17 | function lift(name) { |
| 18 | const start = src.indexOf('function ' + name + '('); |
| 19 | assert.notEqual(start, -1, name + ' is not in room.js any more — this test is stale, not passing'); |
| 20 | let depth = 0; |
| 21 | for (let i = src.indexOf('{', start); i < src.length; i++) { |
| 22 | if (src[i] === '{') depth += 1; |
| 23 | else if (src[i] === '}') { |
| 24 | depth -= 1; |
| 25 | if (depth === 0) return src.slice(start, i + 1); |
| 26 | } |
| 27 | } |
| 28 | assert.fail('unbalanced braces reading ' + name); |
| 29 | } |
| 30 | function liftConst(name) { |
| 31 | const m = src.match(new RegExp('const ' + name + ' = ([0-9.]+);')); |
| 32 | assert.ok(m, name + ' is not a numeric const in room.js any more'); |
| 33 | return 'const ' + name + ' = ' + m[1] + ';'; |
| 34 | } |
| 35 | |
| 36 | // eslint-disable-next-line no-new-func |
| 37 | const judgeSelf = new Function(liftConst('SELF_STUCK_MS') + '\n' + lift('judgeSelf') + '; return judgeSelf;')(); |
| 38 | const STUCK = Number(liftConst('SELF_STUCK_MS').match(/= ([0-9.]+)/)[1]); |
| 39 | |
| 40 | const fresh = () => ({ time: -1, since: -1, tries: 0, said: false }); |
| 41 | const live = (now, time, paused = false) => ({ live: true, visible: true, paused, time, now }); |
| 42 | |
| 43 | // Once a second from `from` for `n` polls, the element's clock and paused |
| 44 | // flag from `at(now)`; the verdicts that were not empty, with their times. |
| 45 | function poll(w, from, n, at) { |
| 46 | const out = []; |
| 47 | for (let i = 0; i < n; i++) { |
| 48 | const now = from + i * 1000; |
| 49 | const v = judgeSelf(w, { live: true, visible: true, now, ...at(now) }); |
| 50 | if (v) out.push([now, v]); |
| 51 | } |
| 52 | return out; |
| 53 | } |
| 54 | |
| 55 | test('a self view that is playing is left alone', () => { |
| 56 | const w = fresh(); |
| 57 | assert.deepEqual(poll(w, 0, 10, (now) => ({ paused: false, time: now / 1000 })), []); |
| 58 | }); |
| 59 | |
| 60 | test('a paused self view is replayed once, then said, two seconds apart', () => { |
| 61 | const w = fresh(); |
| 62 | const got = poll(w, 0, 8, () => ({ paused: true, time: 3 })); |
| 63 | assert.deepEqual(got, [[STUCK, 'replay'], [2 * STUCK, 'say']]); |
| 64 | assert.equal(w.said, true); |
| 65 | }); |
| 66 | |
| 67 | test('a self view that is not paused but whose clock stands still counts as stuck', () => { |
| 68 | const w = fresh(); |
| 69 | const got = poll(w, 0, 6, () => ({ paused: false, time: 1.5 })); |
| 70 | assert.deepEqual(got.map(([, v]) => v), ['replay', 'say']); |
| 71 | }); |
| 72 | |
| 73 | test('the words come down as soon as the picture moves again, and the watch starts over', () => { |
| 74 | const w = fresh(); |
| 75 | poll(w, 0, 6, () => ({ paused: true, time: 3 })); |
| 76 | assert.equal(w.said, true); |
| 77 | assert.equal(judgeSelf(w, live(6000, 3.2)), 'clear'); |
| 78 | assert.equal(w.said, false); |
| 79 | assert.equal(w.tries, 0); |
| 80 | // A second stall gets its own replay before anything is said. |
| 81 | const again = poll(w, 7000, 6, () => ({ paused: true, time: 3.2 })); |
| 82 | assert.deepEqual(again.map(([, v]) => v), ['replay', 'say']); |
| 83 | }); |
| 84 | |
| 85 | test('a replay that works is never said', () => { |
| 86 | const w = fresh(); |
| 87 | let t = 2; |
| 88 | const got = poll(w, 0, 10, (now) => (now <= STUCK ? { paused: true, time: t } : { paused: false, time: (t += 1) })); |
| 89 | assert.deepEqual(got.map(([, v]) => v), ['replay']); |
| 90 | assert.equal(w.said, false); |
| 91 | }); |
| 92 | |
| 93 | test('a camera that is off, muted or ended, or a hidden tab, is not the watch’s business', () => { |
| 94 | for (const s of [{ live: false, visible: true }, { live: true, visible: false }]) { |
| 95 | const w = fresh(); |
| 96 | for (let now = 0; now < 10000; now += 1000) { |
| 97 | assert.equal(judgeSelf(w, { ...s, paused: true, time: 0, now }), ''); |
| 98 | } |
| 99 | assert.equal(w.tries, 0); |
| 100 | } |
| 101 | // And one that goes off while the words are up takes them down: the |
| 102 | // camera-off cover says what there is to say. |
| 103 | const w = fresh(); |
| 104 | poll(w, 0, 6, () => ({ paused: true, time: 0 })); |
| 105 | assert.equal(w.said, true); |
| 106 | assert.equal(judgeSelf(w, { live: false, visible: true, paused: true, time: 0, now: 7000 }), 'clear'); |
| 107 | }); |
| 108 | |
| 109 | test('a clock that went backwards (a new binding) is a start, not a stall cleared', () => { |
| 110 | const w = fresh(); |
| 111 | judgeSelf(w, live(0, 9)); |
| 112 | // Rebinding resets the element's clock: not moving, so the watch starts |
| 113 | // its two seconds rather than calling it playing. |
| 114 | assert.equal(judgeSelf(w, live(1000, 0)), ''); |
| 115 | assert.equal(judgeSelf(w, live(2000, 0.6)), ''); |
| 116 | assert.equal(w.since, -1); |
| 117 | }); |
| 118 | |
| 119 | // playSelf over a fake element: what a refusal and a success do to the |
| 120 | // tile, and that neither is ever §15.20's sound refusal. |
| 121 | function playHarness(result) { |
| 122 | const drawn = []; |
| 123 | const t = { |
| 124 | video: { play: () => result }, |
| 125 | selfWatch: { time: -1, since: -1, tries: 0, said: false }, |
| 126 | }; |
| 127 | const tiles = new Map([['self', t]]); |
| 128 | // eslint-disable-next-line no-new-func |
| 129 | const playSelf = new Function('tiles', 'drawSelfNote', 'soundRefused', 'startPlaying', |
| 130 | lift('playSelf') + '; return playSelf;')(tiles, (x) => drawn.push(x.selfWatch.said), |
| 131 | () => assert.fail('a self view is never a sound refusal'), |
| 132 | () => assert.fail('a self view never goes through startPlaying')); |
| 133 | return { t, drawn, playSelf }; |
| 134 | } |
| 135 | |
| 136 | test('a refused preview is said at once, on the tile', async () => { |
| 137 | const h = playHarness(Promise.reject(new DOMException('no', 'NotAllowedError'))); |
| 138 | h.playSelf(h.t); |
| 139 | await new Promise((r) => setTimeout(r, 0)); |
| 140 | assert.equal(h.t.selfWatch.said, true); |
| 141 | assert.deepEqual(h.drawn, [true]); |
| 142 | }); |
| 143 | |
| 144 | test('a play that was only interrupted (AbortError) says nothing', async () => { |
| 145 | const h = playHarness(Promise.reject(new DOMException('new load', 'AbortError'))); |
| 146 | h.playSelf(h.t); |
| 147 | await new Promise((r) => setTimeout(r, 0)); |
| 148 | assert.equal(h.t.selfWatch.said, false); |
| 149 | assert.deepEqual(h.drawn, []); |
| 150 | }); |
| 151 | |
| 152 | test('a play that works takes the words down', async () => { |
| 153 | const h = playHarness(Promise.resolve()); |
| 154 | h.t.selfWatch.said = true; |
| 155 | h.playSelf(h.t); |
| 156 | await new Promise((r) => setTimeout(r, 0)); |
| 157 | assert.equal(h.t.selfWatch.said, false); |
| 158 | assert.deepEqual(h.drawn, [false]); |
| 159 | }); |
| 160 | |
| 161 | // comeBack over fakes: the self tile is asked to play again along with |
| 162 | // the remote ones — the line whose absence left it black. |
| 163 | test('the tab coming back plays a paused self view, through playSelf', () => { |
| 164 | const played = []; |
| 165 | const started = []; |
| 166 | const selfT = { video: { paused: true }, stream: {} }; |
| 167 | const remote = { video: { paused: true }, stream: {}, play: { hidden: true } }; |
| 168 | const tiles = new Map([['self', selfT], ['peer1', remote]]); |
| 169 | const local = { getAudioTracks: () => [], getVideoTracks: () => [] }; |
| 170 | // eslint-disable-next-line no-new-func |
| 171 | const comeBack = new Function('state', 'local', 'lostKinds', 'prefs', 'switchDevice', 'tiles', 'shouldPlay', |
| 172 | 'startPlaying', 'playSelf', lift('comeBack') + '; return comeBack;')( |
| 173 | { stopped: false, leaveSent: false }, local, () => [], {}, () => Promise.resolve(), tiles, |
| 174 | (t, isSelf) => !isSelf && !!t.stream, (key) => started.push(key), (t) => played.push(t)); |
| 175 | comeBack(); |
| 176 | assert.deepEqual(played, [selfT]); |
| 177 | assert.deepEqual(started, ['peer1']); |
| 178 | }); |
| 179 | |
| 180 | // The binding: tile() hands ours to playSelf whenever it binds a stream, |
| 181 | // rather than trusting `autoplay`. tile() builds DOM and is not lifted; |
| 182 | // this holds the one line in place. |
| 183 | test('tile() asks the self view to play on every binding', () => { |
| 184 | const body = lift('tile'); |
| 185 | assert.match(body, /if \(isSelf\) \{\s*if \(stream\) playSelf\(t\);/); |
| 186 | assert.match(lift('pollFrames'), /watchSelf\(\);/); |
| 187 | }); |
| 188 | |