| 1 | // room-portrait.test.mjs — portrait video (2026-10-21), gated without a |
| 2 | // browser: room.js's videoShape (the classifier, with hysteresis) and |
| 3 | // videoFits (fill or fit), lifted out of the shipped file by name with |
| 4 | // the constants they read. The native apps' VideoShapeTests hold |
| 5 | // VideoShape, VideoShapeReading and VideoPlacement to the same table. |
| 6 | // |
| 7 | // node --test test/browser/room-portrait.test.mjs |
| 8 | import { test } from 'node:test'; |
| 9 | import assert from 'node:assert/strict'; |
| 10 | import { readFileSync } from 'node:fs'; |
| 11 | import { fileURLToPath } from 'node:url'; |
| 12 | |
| 13 | const roomJS = fileURLToPath(new URL('../../internal/calls/static/room.js', import.meta.url)); |
| 14 | const src = readFileSync(roomJS, 'utf8'); |
| 15 | |
| 16 | function lift(name) { |
| 17 | const start = src.indexOf('function ' + name + '('); |
| 18 | assert.notEqual(start, -1, name + ' is not in room.js any more — this test is stale, not passing'); |
| 19 | let depth = 0; |
| 20 | for (let i = src.indexOf('{', start); i < src.length; i++) { |
| 21 | if (src[i] === '{') depth += 1; |
| 22 | else if (src[i] === '}') { |
| 23 | depth -= 1; |
| 24 | if (depth === 0) return src.slice(start, i + 1); |
| 25 | } |
| 26 | } |
| 27 | assert.fail('unbalanced braces reading ' + name + ' out of room.js'); |
| 28 | } |
| 29 | function liftConst(name) { |
| 30 | const m = src.match(new RegExp('const ' + name + ' = ([0-9.]+);')); |
| 31 | assert.ok(m, name + ' is not a numeric const in room.js any more'); |
| 32 | return 'const ' + name + ' = ' + m[1] + ';'; |
| 33 | } |
| 34 | const CONSTS = ['SHAPE_ENTER', 'SHAPE_HOLD', 'FIT_TOLERANCE'].map(liftConst).join('\n'); |
| 35 | // eslint-disable-next-line no-new-func |
| 36 | const { videoShape, videoFits } = new Function( |
| 37 | CONSTS + '\n' + lift('videoShape') + '\n' + lift('videoFits') + '; return { videoShape, videoFits };')(); |
| 38 | |
| 39 | test('the thresholds: h > w × 1.1 is portrait, w > h × 1.1 landscape, square-ish between', () => { |
| 40 | assert.equal(videoShape(720, 1280, ''), 'portrait'); |
| 41 | assert.equal(videoShape(1280, 720, ''), 'landscape'); |
| 42 | assert.equal(videoShape(640, 480, ''), 'landscape'); |
| 43 | assert.equal(videoShape(500, 500, ''), 'square'); |
| 44 | assert.equal(videoShape(1000, 1100, ''), 'square', 'exactly 1.1 is not yet portrait'); |
| 45 | assert.equal(videoShape(1000, 1101, ''), 'portrait'); |
| 46 | assert.equal(videoShape(1101, 1000, ''), 'landscape'); |
| 47 | assert.equal(videoShape(0, 0, ''), 'landscape', 'nothing known is landscape, as 0.4.10'); |
| 48 | assert.equal(videoShape(0, 0, 'portrait'), 'portrait', 'and nothing new keeps what was'); |
| 49 | }); |
| 50 | |
| 51 | test('hysteresis holds a shape through the band, and lets go past it', () => { |
| 52 | let s = videoShape(720, 1280, ''); |
| 53 | assert.equal(s, 'portrait'); |
| 54 | s = videoShape(1000, 1050, s); |
| 55 | assert.equal(s, 'portrait', 'still taller than wide'); |
| 56 | s = videoShape(1000, 1000, s); |
| 57 | assert.equal(s, 'square', 'no longer taller than wide'); |
| 58 | s = videoShape(1000, 1080, s); |
| 59 | assert.equal(s, 'square', 'back into the band is not portrait again'); |
| 60 | s = videoShape(1000, 1200, s); |
| 61 | assert.equal(s, 'portrait'); |
| 62 | }); |
| 63 | |
| 64 | test('a picture jittering about the threshold changes once, not every frame', () => { |
| 65 | let s = videoShape(1000, 1000, ''); |
| 66 | let changes = 0; |
| 67 | for (let i = 0; i < 40; i++) { |
| 68 | const next = videoShape(1000, i % 2 ? 1105 : 1095, s); |
| 69 | if (next !== s) changes += 1; |
| 70 | s = next; |
| 71 | } |
| 72 | assert.equal(changes, 1); |
| 73 | }); |
| 74 | |
| 75 | test('a phone turned mid-call switches once, through resolution steps', () => { |
| 76 | // videoWidth and videoHeight have the sender's rotation applied: an |
| 77 | // upright iPhone arrives as 720 × 1280 whatever its buffer is. |
| 78 | const sizes = [[1280, 720], [960, 540], [1280, 720], [720, 1280], [540, 960], [720, 1280]]; |
| 79 | let s = ''; |
| 80 | const seen = []; |
| 81 | for (const [w, h] of sizes) { |
| 82 | s = videoShape(w, h, s); |
| 83 | if (seen[seen.length - 1] !== s) seen.push(s); |
| 84 | } |
| 85 | assert.deepEqual(seen, ['landscape', 'portrait']); |
| 86 | }); |
| 87 | |
| 88 | test('fill or fit: the decision table', () => { |
| 89 | const fits = (vw, vh, bw, bh, screen = false) => videoFits(vw, vh, videoShape(vw, vh, ''), bw, bh, screen); |
| 90 | // A screen is always fitted. |
| 91 | assert.equal(fits(1920, 1200, 1440, 900, true), true); |
| 92 | // The same shape fills, however much it crops. |
| 93 | assert.equal(fits(1280, 720, 1440, 900), false); |
| 94 | assert.equal(fits(720, 1280, 393, 852), false); |
| 95 | assert.equal(fits(2560, 1080, 320, 180), false, '21:9 in 16:9 crops 25% and fills'); |
| 96 | assert.equal(fits(640, 480, 320, 180), false, '4:3 in a 16:9 tile fills'); |
| 97 | // Another shape with a crop of 15% or less still fills. |
| 98 | assert.equal(fits(1000, 1000, 1100, 1000), false); |
| 99 | // Another shape with more is fitted. |
| 100 | assert.equal(fits(720, 1280, 1440, 900), true, 'an upright phone on a landscape window'); |
| 101 | assert.equal(fits(720, 1280, 320, 180), true, 'and in a grid or filmstrip tile'); |
| 102 | assert.equal(fits(1280, 720, 393, 852), true, 'a laptop on an upright phone'); |
| 103 | assert.equal(fits(720, 720, 1440, 900), true, 'a square picture in a 16:10 window'); |
| 104 | assert.equal(fits(480, 640, 1080, 1000), true, 'portrait in a square-ish place, 31% cut'); |
| 105 | // Nothing known yet: fill. |
| 106 | assert.equal(videoFits(0, 0, 'portrait', 1440, 900, false), false); |
| 107 | assert.equal(videoFits(720, 1280, 'portrait', 0, 0, false), false); |
| 108 | }); |
| 109 | |
| 110 | test('the shape held by hysteresis is the one that decides', () => { |
| 111 | // A picture that was portrait and is now 1000 × 1050 is still portrait, |
| 112 | // so in a 16:9 tile it stays fitted rather than flipping to fill. |
| 113 | assert.equal(videoFits(1000, 1050, 'portrait', 320, 180, false), true); |
| 114 | assert.equal(videoFits(1000, 1050, 'square', 320, 180, false), true, 'square is another shape too'); |
| 115 | }); |
| 116 | |