// room-adapt.test.mjs — adaptive video (spec §15.30, 2026-10-28), gated // without a browser. // // node --test test/browser/room-adapt.test.mjs // // The receiver's watch (adaptStep and its helpers: down fast, up slow, a // probe up that fails doubling the wait), what a sender sends at each level // (the camera giving way before a shared screen, a screen never paused), // and the report's `video` member, lifted out of the shipped room.js by // name with the constants they read, on a fake clock. The apps' // VideoAdaptTests are the twin of these, number for number. 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 = ['ADAPT_LEVELS', 'ADAPT_WINDOW', 'ADAPT_SEVERE_AFTER', 'ADAPT_DOWN_FREEZES', 'ADAPT_DOWN_LOSS', 'ADAPT_DOWN_RTT', 'ADAPT_DOWN_JITTER_BUFFER', 'ADAPT_DOWN_DROPPED', 'ADAPT_DOWN_DROPPED_MIN', 'ADAPT_DOWN_FREEZE_SECONDS', 'ADAPT_DOWN_KEYFRAME_ASKS', 'ADAPT_DOWN_LOAD_SECONDS', 'ADAPT_SEVERE_FREEZES', 'ADAPT_SEVERE_FREEZE_SECONDS', 'ADAPT_SEVERE_LOSS', 'ADAPT_CLEAN_LOSS', 'ADAPT_CLEAN_RTT', 'ADAPT_CLEAN_JITTER_BUFFER', 'ADAPT_UP_AFTER', 'ADAPT_PROBE_WATCH', 'ADAPT_PROBE_JUDGE', 'ADAPT_UP_MAX', 'ADAPT_CAMERA', 'ADAPT_SCREEN', 'ADAPT_MIN_NATIVE_VERSION', 'QUALITY_MAX_SECONDS'].map(liftConst).join('\n'); const NAMES = ['adaptRecord', 'adaptSample', 'loadSample', 'adaptWhy', 'adaptWindow', 'adaptTrouble', 'adaptClean', 'adaptMove', 'adaptStep', 'videoFloor', 'adaptAsks', 'askedOf', 'cameraCap', 'cameraAtLevel', 'sharePlan', 'screenAtLevel', 'parseClient', 'compareVersions', 'honoursAsks', 'adaptNoticeWords', 'withVideoTime', 'mediaFrom', 'mediaKey', 'mediaPayload']; // eslint-disable-next-line no-new-func const r = new Function(CONSTS + '\n' + NAMES.map(lift).join('\n') + '; return { ' + NAMES.join(', ') + ', ADAPT_UP_AFTER, ADAPT_UP_MAX };')(); const P1 = 'a'.repeat(32); const P2 = 'b'.repeat(32); // Trouble that is not severe: a freeze every four seconds, two or three in // any ten. const freezing = (t) => clean({ freezes: t % 4 === 0 ? 1 : 0 }); // One second's sample: clean unless told otherwise. const clean = (o = {}) => Object.assign({ freezes: 0, freezeSeconds: 0, packets: 100, lost: 0, rtt: 80, jitterBufferMs: 60, decoded: 30, dropped: 0 }, o); // Once a second from `from` to `to` seconds (inclusive), each second's // sample from `at(t)`; the level after every look, and the seconds it // changed at. function run(rec, from, to, at) { const changes = []; for (let t = from; t <= to; t++) { const before = rec.level; r.adaptStep(rec, t * 1000, at(t)); if (rec.level !== before) changes.push([t, rec.level]); } return changes; } test('the numbers are the approved ones', () => { assert.equal(r.ADAPT_UP_AFTER, 45000); assert.equal(r.ADAPT_UP_MAX, 300000); }); test('trouble that lasts steps down one level every ten seconds, to audio only, and no further', () => { const rec = r.adaptRecord(); const seen = run(rec, 0, 60, freezing); assert.deepEqual(seen, [[10, 1], [20, 2], [30, 3]]); assert.equal(rec.level, 3); assert.equal(rec.downs, 3); }); test('severe trouble steps two levels at once, five seconds in', () => { const rec = r.adaptRecord(); assert.deepEqual(run(rec, 0, 12, () => clean({ freezes: 2 })), [[5, 2], [10, 3]]); const lossy = r.adaptRecord(); assert.deepEqual(run(lossy, 0, 6, () => clean({ packets: 80, lost: 20 })), [[5, 2]]); }); test('a freeze now and then, a little loss, or a slow link alone is not trouble', () => { const rare = r.adaptRecord(); assert.deepEqual(run(rare, 0, 60, (t) => clean({ freezes: t % 11 === 0 ? 1 : 0 })), []); const lossy = r.adaptRecord(); assert.deepEqual(run(lossy, 0, 60, () => clean({ packets: 97, lost: 3 })), []); const slow = r.adaptRecord(); assert.deepEqual(run(slow, 0, 60, () => clean({ rtt: 900 })), []); }); test('loss over five percent, a slow link with freezes, a deep jitter buffer and dropped frames each step down', () => { for (const [what, sample] of [ ['loss', () => clean({ packets: 94, lost: 6 })], ['one freeze on a 700 ms round trip', (t) => clean({ rtt: 700, freezes: t === 5 ? 1 : 0 })], ['the jitter buffer', () => clean({ jitterBufferMs: 650 })], ['frames dropped', () => clean({ decoded: 20, dropped: 10 })], ['one long freeze', (t) => clean({ freezes: t === 5 ? 1 : 0, freezeSeconds: t === 5 ? 2.5 : 0 })], ['a keyframe asked for every three seconds', (t) => clean({ keyframeAsks: t % 3 === 0 ? 1 : 0 })], ]) { const rec = r.adaptRecord(); assert.deepEqual(run(rec, 0, 10, sample), [[10, 1]], what); } }); test('the round trip is the window median: one spike is not a slow link', () => { const rec = r.adaptRecord(); assert.deepEqual(run(rec, 0, 30, (t) => clean({ rtt: t % 10 === 3 ? 2000 : 100, freezes: t % 10 === 3 ? 1 : 0 })), []); }); test('a second with nothing measured is no news either way', () => { const rec = r.adaptRecord(); run(rec, 0, 9, freezing); assert.deepEqual(run(rec, 10, 40, () => null), []); assert.equal(rec.level, 0, 'a connection that is down says nothing about the link'); assert.equal(r.adaptSample({}, true), null); assert.equal(r.adaptSample({ in: { audio: { packets: 0 } } }, true), null); }); test('freezes count only while the camera is watched', () => { const q = { rtt: 50, in: { audio: { packets: 50, lost: 1, jitterBufferMs: 70 }, video: { packets: 90, freezes: 2, freezeSeconds: 1.5, decoded: 30, dropped: 1 }, screen: { packets: 10, lost: 1, decoded: 1 } } }; assert.deepEqual(r.adaptSample(q, true, 2), { freezes: 2, freezeSeconds: 1.5, keyframeAsks: 2, packets: 150, lost: 2, rtt: 50, jitterBufferMs: 70, decoded: 31, dropped: 1 }); const off = r.adaptSample(q, false, 2); assert.equal(off.freezes, 0); assert.equal(off.keyframeAsks, 0); assert.equal(off.freezeSeconds, 0); assert.equal(off.packets, 150); }); test('clean seconds step up one level after forty-five, and again after another forty-five', () => { const rec = r.adaptRecord(); run(rec, 0, 20, freezing); assert.equal(rec.level, 2); // Clean from 21 s: up at 21 + 45; the probe holds, and the next clean run // starts the second after the step, so up again at 67 + 45. const seen = run(rec, 21, 200, () => clean()); assert.deepEqual(seen, [[66, 1], [112, 0]]); assert.equal(rec.upWait, r.ADAPT_UP_AFTER, 'a probe that holds puts the wait back'); }); test('a keyframe asked for is not a clean second', () => { const rec = r.adaptRecord(); run(rec, 0, 10, freezing); const seen = run(rec, 11, 120, (t) => clean({ keyframeAsks: t === 30 ? 1 : 0 })); assert.deepEqual(seen, [[76, 0]]); }); test('one bad second resets the clean run', () => { const rec = r.adaptRecord(); run(rec, 0, 10, freezing); assert.equal(rec.level, 1); const seen = run(rec, 11, 120, (t) => clean({ lost: t === 40 ? 10 : 0, packets: 100 })); assert.deepEqual(seen, [[86, 0]], 'clean from 41 s, so up at 86 s'); }); test('a probe that fails within twenty seconds steps straight back and doubles the wait, to five minutes at most', () => { const rec = r.adaptRecord(); run(rec, 0, 10, freezing); assert.equal(rec.level, 1); // The link carries Reduced and not Normal: clean at 1, freezing at 0. const at = () => (rec.level === 0 ? clean({ freezes: 1 }) : clean()); const seen = run(rec, 11, 1200, at); const ups = seen.filter(([, l]) => l === 0).map(([t]) => t); const backs = seen.filter(([, l]) => l === 1).map(([t]) => t); // Up after 45 s clean, back 5 s later; then 90, 180, 300, 300 … of // clean seconds counted from the second after each step back. assert.deepEqual(ups, [56, 152, 338, 644, 950]); assert.deepEqual(backs, [61, 157, 343, 649, 955]); assert.equal(rec.upWait, r.ADAPT_UP_MAX); }); test('trouble after the probe watch is an ordinary step down, and the wait is back to its base', () => { const rec = r.adaptRecord(); run(rec, 0, 10, freezing); run(rec, 11, 56, () => clean()); assert.equal(rec.level, 0); const seen = run(rec, 57, 100, (t) => (t >= 80 ? freezing(t) : clean())); assert.deepEqual(seen, [[84, 1], [94, 2]], 'freezes at 80 and 84 s: the window is trouble at 84, and ten seconds on'); assert.equal(rec.upWait, r.ADAPT_UP_AFTER); }); test('the person’s own setting is a floor under the watch, and only levels below normal are asked', () => { const levels = new Map([[P1, 0], [P2, 1]]); assert.deepEqual(r.adaptAsks(levels, 'auto'), { [P2]: 'reduced' }); assert.deepEqual(r.adaptAsks(levels, 'low'), { [P1]: 'minimal', [P2]: 'minimal' }); assert.deepEqual(r.adaptAsks(new Map([[P1, 3]]), 'low'), { [P1]: 'audio' }); assert.deepEqual(r.adaptAsks(levels, 'audio'), { [P1]: 'audio', [P2]: 'audio' }); assert.deepEqual(r.adaptAsks(new Map([[P1, 0]]), 'auto'), {}); const many = new Map(Array.from({ length: 20 }, (_, i) => [i.toString(16).padStart(32, '0'), 2])); assert.equal(Object.keys(r.adaptAsks(many, 'auto')).length, 15); }); test('a sender reads only its own id, and only the wire’s levels', () => { assert.equal(r.askedOf({ video: { [P1]: 'minimal' } }, P1), 2); assert.equal(r.askedOf({ video: { [P1]: 'minimal' } }, P2), 0); assert.equal(r.askedOf({ video: { [P1]: 'tiny' } }, P1), 0); assert.equal(r.askedOf(null, P1), 0); assert.equal(r.askedOf({ muted: false }, P1), 0); }); test('the camera at each level, fitted by its long and short sides', () => { assert.deepEqual(r.cameraAtLevel(0, false, 640, 480), { maxFramerate: undefined, active: true, maxBitrate: undefined, scaleResolutionDownBy: 1 }); assert.deepEqual(r.cameraAtLevel(1, false, 640, 480), { maxBitrate: 350000, maxFramerate: 15, scaleResolutionDownBy: 480 / 360, active: true }); assert.deepEqual(r.cameraAtLevel(1, false, 1280, 720), { maxBitrate: 350000, maxFramerate: 15, scaleResolutionDownBy: 2, active: true }); // A portrait camera is boxed the right way round: 360 × 640 at most. assert.equal(r.cameraAtLevel(1, false, 720, 1280).scaleResolutionDownBy, 2); assert.deepEqual(r.cameraAtLevel(2, false, 640, 480), { maxBitrate: 120000, maxFramerate: 8, scaleResolutionDownBy: 480 / 180, active: true }); assert.equal(r.cameraAtLevel(3, false, 640, 480).active, false); // Unknown size: no scaling, the rest as the level says. assert.equal(r.cameraAtLevel(2, false, 0, 0).scaleResolutionDownBy, 1); }); test('while a screen is shared the camera gives way first', () => { // Normal: §5.4's cap beside the screen. assert.deepEqual(r.cameraAtLevel(0, true, 640, 720), Object.assign({ maxFramerate: undefined, active: true }, r.cameraCap(true, 720))); // Reduced asks the camera for minimal, and minimal for none. assert.equal(r.cameraAtLevel(1, true, 640, 480).maxBitrate, 120000); assert.equal(r.cameraAtLevel(1, true, 640, 480).active, true); assert.equal(r.cameraAtLevel(2, true, 640, 480).active, false); assert.equal(r.cameraAtLevel(3, true, 640, 480).active, false); }); test('a screen keeps its size and gives up frame rate first, is made smaller only at minimal, and is never paused', () => { const plan = r.sharePlan('balanced', 1920, 1080); assert.deepEqual(r.screenAtLevel(plan, 0, 1920, 1080), plan); const reduced = r.screenAtLevel(plan, 1, 1920, 1080); assert.equal(reduced.encoding.maxFramerate, 10); assert.equal(reduced.encoding.maxBitrate, 1200000); assert.equal(reduced.encoding.scaleResolutionDownBy, plan.encoding.scaleResolutionDownBy, 'the size is kept'); assert.equal(reduced.degradationPreference, 'maintain-resolution'); const minimal = r.screenAtLevel(plan, 2, 1920, 1080); assert.equal(minimal.encoding.maxFramerate, 5); assert.equal(minimal.encoding.maxBitrate, 500000); assert.equal(minimal.encoding.scaleResolutionDownBy, 1.5); const audio = r.screenAtLevel(plan, 3, 1920, 1080); assert.deepEqual(audio, minimal, 'audio only sends the screen as minimal does'); assert.notEqual(audio.encoding.active, false); // Motion's own smaller cap is kept where it is smaller still. const motion = r.sharePlan('motion', 3840, 2160); assert.equal(r.screenAtLevel(motion, 2, 3840, 2160).encoding.scaleResolutionDownBy, 3840 / 1280); // The plan it was given is not changed under it. assert.equal(plan.encoding.maxFramerate, 30); }); test('browsers and new apps act on an ask; an app that does not say, or an old one, does not', () => { assert.equal(r.honoursAsks(''), true); assert.equal(r.honoursAsks('mac 0.4.15'), true); assert.equal(r.honoursAsks('mac 0.4.14'), false); assert.equal(r.honoursAsks('iphone 0.5'), true); assert.equal(r.honoursAsks('mac 0.4.13'), false); assert.equal(r.honoursAsks('native'), false); assert.equal(r.honoursAsks('something else'), false); }); test('the sender’s notice names the people, and says audio only only when that is all it is', () => { assert.equal(r.adaptNoticeWords([]), ''); assert.equal(r.adaptNoticeWords([{ name: 'David', level: 2 }]), 'Sending reduced video to David (their connection is weak)'); assert.equal(r.adaptNoticeWords([{ name: 'David', level: 3 }]), 'Sending only audio to David (their connection is weak)'); assert.equal(r.adaptNoticeWords([{ name: 'David', level: 1 }, { name: 'Ana', level: 3 }, { name: 'Cy', level: 2 }]), 'Sending reduced video to David, Ana and Cy (their connections are weak)'); }); test('the report carries the seconds at each level, asked and sent, only when there were any', () => { assert.equal(r.withVideoTime(null, undefined), null); assert.deepEqual(r.withVideoTime(null, { asked: [0, 24.04, 0], sent: [0, 0, 0] }), { askedSeconds: [0, 24, 0] }); assert.deepEqual(r.withVideoTime({ noSound: true }, { asked: [0, 0, 0], sent: [5, 0, 1] }), { noSound: true, sentSeconds: [5, 0, 1] }); assert.equal(r.withVideoTime(null, { asked: [0, 0, 0], sent: [0, 0, 0] }), null); }); test('the media report carries the asks, and reads them back leniently', () => { const mine = { muted: false, cameraOff: false }; assert.deepEqual(r.mediaPayload(mine, false, [], false, [], [], { [P1]: 'minimal' }), { muted: false, cameraOff: false, hearing: [], video: { [P1]: 'minimal' } }); assert.deepEqual(r.mediaPayload(mine, false, [], false, [], [], {}), { muted: false, cameraOff: false, hearing: [] }); assert.deepEqual(r.mediaPayload(mine, false, [], true, [], [], { [P1]: 'minimal' }), mine, 'an old server gets the two keys'); const back = r.mediaFrom({ muted: false, cameraOff: false, video: { [P1]: 'audio', [P2]: 'tiny', short: 'minimal' } }); assert.deepEqual(back.video, { [P1]: 'audio' }); assert.equal(r.mediaFrom({ muted: false, cameraOff: false, video: [P1] }).video, undefined); // The same asks in another order are the same report; a level changed is not. const a = r.mediaKey({ muted: false, cameraOff: false, video: { [P1]: 'audio', [P2]: 'reduced' } }); const b = r.mediaKey({ muted: false, cameraOff: false, video: { [P2]: 'reduced', [P1]: 'audio' } }); const c = r.mediaKey({ muted: false, cameraOff: false, video: { [P2]: 'minimal', [P1]: 'audio' } }); assert.equal(a, b); assert.notEqual(a, c); assert.notEqual(r.mediaKey({ muted: false, cameraOff: false }), a); }); test('the sender’s own watch: this device struggling for 8 of 10 seconds lowers its camera, and calm brings it back', () => { const rec = r.adaptRecord(); // Struggling every second: a level every ten, never severe. assert.deepEqual(run(rec, 0, 35, () => r.loadSample(true)), [[10, 1], [20, 2], [30, 3]]); // Calm: up after forty-five seconds, as any watch. const calm = r.adaptRecord(); run(calm, 0, 10, () => r.loadSample(true)); assert.equal(calm.level, 1); assert.deepEqual(run(calm, 11, 60, () => r.loadSample(false)), [[56, 0]]); // Seven seconds in ten is not sustained. const brief = r.adaptRecord(); assert.deepEqual(run(brief, 0, 60, (t) => r.loadSample(t % 10 < 7)), []); // A struggling second is not a clean one. assert.equal(r.adaptSample({ in: {} }, true), null); const one = r.loadSample(true); assert.equal(one.load, true); });