// room-portrait.test.mjs — portrait video (2026-10-21), gated without a // browser: room.js's videoShape (the classifier, with hysteresis) and // videoFits (fill or fit), lifted out of the shipped file by name with // the constants they read. The native apps' VideoShapeTests hold // VideoShape, VideoShapeReading and VideoPlacement to the same table. // // node --test test/browser/room-portrait.test.mjs import { test } from 'node:test'; import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import { fileURLToPath } from 'node:url'; const roomJS = fileURLToPath(new URL('../../internal/calls/static/room.js', import.meta.url)); const src = readFileSync(roomJS, '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 + ' out of room.js'); } 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] + ';'; } const CONSTS = ['SHAPE_ENTER', 'SHAPE_HOLD', 'FIT_TOLERANCE'].map(liftConst).join('\n'); // eslint-disable-next-line no-new-func const { videoShape, videoFits } = new Function( CONSTS + '\n' + lift('videoShape') + '\n' + lift('videoFits') + '; return { videoShape, videoFits };')(); test('the thresholds: h > w × 1.1 is portrait, w > h × 1.1 landscape, square-ish between', () => { assert.equal(videoShape(720, 1280, ''), 'portrait'); assert.equal(videoShape(1280, 720, ''), 'landscape'); assert.equal(videoShape(640, 480, ''), 'landscape'); assert.equal(videoShape(500, 500, ''), 'square'); assert.equal(videoShape(1000, 1100, ''), 'square', 'exactly 1.1 is not yet portrait'); assert.equal(videoShape(1000, 1101, ''), 'portrait'); assert.equal(videoShape(1101, 1000, ''), 'landscape'); assert.equal(videoShape(0, 0, ''), 'landscape', 'nothing known is landscape, as 0.4.10'); assert.equal(videoShape(0, 0, 'portrait'), 'portrait', 'and nothing new keeps what was'); }); test('hysteresis holds a shape through the band, and lets go past it', () => { let s = videoShape(720, 1280, ''); assert.equal(s, 'portrait'); s = videoShape(1000, 1050, s); assert.equal(s, 'portrait', 'still taller than wide'); s = videoShape(1000, 1000, s); assert.equal(s, 'square', 'no longer taller than wide'); s = videoShape(1000, 1080, s); assert.equal(s, 'square', 'back into the band is not portrait again'); s = videoShape(1000, 1200, s); assert.equal(s, 'portrait'); }); test('a picture jittering about the threshold changes once, not every frame', () => { let s = videoShape(1000, 1000, ''); let changes = 0; for (let i = 0; i < 40; i++) { const next = videoShape(1000, i % 2 ? 1105 : 1095, s); if (next !== s) changes += 1; s = next; } assert.equal(changes, 1); }); test('a phone turned mid-call switches once, through resolution steps', () => { // videoWidth and videoHeight have the sender's rotation applied: an // upright iPhone arrives as 720 × 1280 whatever its buffer is. const sizes = [[1280, 720], [960, 540], [1280, 720], [720, 1280], [540, 960], [720, 1280]]; let s = ''; const seen = []; for (const [w, h] of sizes) { s = videoShape(w, h, s); if (seen[seen.length - 1] !== s) seen.push(s); } assert.deepEqual(seen, ['landscape', 'portrait']); }); test('fill or fit: the decision table', () => { const fits = (vw, vh, bw, bh, screen = false) => videoFits(vw, vh, videoShape(vw, vh, ''), bw, bh, screen); // A screen is always fitted. assert.equal(fits(1920, 1200, 1440, 900, true), true); // The same shape fills, however much it crops. assert.equal(fits(1280, 720, 1440, 900), false); assert.equal(fits(720, 1280, 393, 852), false); assert.equal(fits(2560, 1080, 320, 180), false, '21:9 in 16:9 crops 25% and fills'); assert.equal(fits(640, 480, 320, 180), false, '4:3 in a 16:9 tile fills'); // Another shape with a crop of 15% or less still fills. assert.equal(fits(1000, 1000, 1100, 1000), false); // Another shape with more is fitted. assert.equal(fits(720, 1280, 1440, 900), true, 'an upright phone on a landscape window'); assert.equal(fits(720, 1280, 320, 180), true, 'and in a grid or filmstrip tile'); assert.equal(fits(1280, 720, 393, 852), true, 'a laptop on an upright phone'); assert.equal(fits(720, 720, 1440, 900), true, 'a square picture in a 16:10 window'); assert.equal(fits(480, 640, 1080, 1000), true, 'portrait in a square-ish place, 31% cut'); // Nothing known yet: fill. assert.equal(videoFits(0, 0, 'portrait', 1440, 900, false), false); assert.equal(videoFits(720, 1280, 'portrait', 0, 0, false), false); }); test('the shape held by hysteresis is the one that decides', () => { // A picture that was portrait and is now 1000 × 1050 is still portrait, // so in a 16:9 tile it stays fitted rather than flipping to fill. assert.equal(videoFits(1000, 1050, 'portrait', 320, 180, false), true); assert.equal(videoFits(1000, 1050, 'square', 320, 180, false), true, 'square is another shape too'); });