// presenter-grid.mjs: the panels on their 12×12 grid, as a person // moves and resizes them, the keys that do it, and the stylesheet that // places them (the server's own, presenter_window.go's css). import test from 'node:test' import assert from 'node:assert/strict' import { clampPanels, parsePanels, formatPanels, keyChange, toolChange, canChange, whereWords, mirror, arrangeCSS, DEFAULT_PANELS } from './presenter-grid.mjs' const defaults = parsePanels(DEFAULT_PANELS) // Room to move: Now 4×4 at the top left, Next 4×4 below it, the notes // 4×12 on the right, and the middle of the grid empty. const roomy = parsePanels('0,0,4,4;0,4,4,4;8,0,4,12') function valid (panels) { for (const p of panels) { assert.ok(p.w >= 2 && p.h >= 2, 'at least 2×2: ' + JSON.stringify(p)) assert.ok(p.x >= 0 && p.y >= 0 && p.x + p.w <= 12 && p.y + p.h <= 12, 'inside the grid: ' + JSON.stringify(p)) } for (let i = 0; i < panels.length; i++) { for (let j = i + 1; j < panels.length; j++) { const a = panels[i] const b = panels[j] assert.ok(!(a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h), 'no overlap: ' + JSON.stringify([a, b])) } } } test('the cookie\'s panels read and write back the same', () => { assert.deepEqual(defaults, [{ x: 0, y: 0, w: 7, h: 8 }, { x: 0, y: 8, w: 7, h: 4 }, { x: 7, y: 0, w: 5, h: 12 }]) assert.equal(formatPanels(defaults), DEFAULT_PANELS) assert.equal(DEFAULT_PANELS, '0,0,7,8;0,8,7,4;7,0,5,12') for (const bad of ['', 'x', '0,0,7,8;0,8,7,4', '0,0,7,8;0,8,7,4;7,0,5,13', '0,0,1,8;0,8,7,4;7,0,5,12', '0,0,7,8;0,7,7,4;7,0,5,12', '0,0,7,8;0,8,7,4;7,0,5,12;1,1,2,2', '-1,0,7,8;0,8,7,4;7,0,5,12', '0,0,7.5,8;0,8,7,4;7,0,5,12']) { assert.equal(parsePanels(bad), null, bad) } }) test('a panel moves by whole cells into free room', () => { const out = clampPanels(roomy, 0, { dx: 4, dy: 1 }) assert.deepEqual(out[0], { x: 4, y: 1, w: 4, h: 4 }) assert.deepEqual(out[1], roomy[1]) assert.deepEqual(out[2], roomy[2]) valid(out) assert.deepEqual(roomy[0], { x: 0, y: 0, w: 4, h: 4 }, 'the panels given are not changed') }) test('a move stops at the edge of the grid', () => { const out = clampPanels(roomy, 0, { dx: -5, dy: -3 }) assert.deepEqual(out[0], { x: 0, y: 0, w: 4, h: 4 }) const down = clampPanels(parsePanels('0,0,4,4;4,8,4,4;8,0,4,4'), 0, { dy: 40 }) assert.deepEqual(down[0], { x: 0, y: 8, w: 4, h: 4 }) valid(down) }) test('a move goes round a panel in its way when it can', () => { // Down then across is blocked by Next; across then down is not. assert.deepEqual(clampPanels(roomy, 0, { dx: 4, dy: 2 })[0], { x: 4, y: 2, w: 4, h: 4 }) // Across then down is blocked by the notes; down then across is not. const tall = parsePanels('0,0,4,4;4,0,4,4;8,0,4,4') assert.deepEqual(clampPanels(tall, 1, { dx: 4, dy: 4 })[1], { x: 8, y: 4, w: 4, h: 4 }) }) test('a move stops at the panel in its way, never over it', () => { // Now moving right runs into the notes at column 8. const out = clampPanels(roomy, 0, { dx: 7 }) assert.deepEqual(out[0], { x: 4, y: 0, w: 4, h: 4 }) valid(out) // Next moving up runs into Now. const up = clampPanels(roomy, 1, { dy: -3 }) assert.deepEqual(up[1], roomy[1]) // The default window is full: nothing moves until something shrinks. for (let i = 0; i < 3; i++) { for (const change of [{ dx: 1 }, { dx: -1 }, { dy: 1 }, { dy: -1 }]) { const moved = clampPanels(defaults, i, change) assert.deepEqual(moved, defaults, `panel ${i} ${JSON.stringify(change)}`) } } }) test('a resize keeps at least 2×2, inside the grid, clear of the others', () => { assert.deepEqual(clampPanels(roomy, 0, { dw: -9, dh: -9 })[0], { x: 0, y: 0, w: 2, h: 2 }) assert.deepEqual(clampPanels(roomy, 2, { dw: 5 })[2], { x: 8, y: 0, w: 4, h: 12 }) // Now grows right to the notes and down to Next, no further. const grown = clampPanels(roomy, 0, { dw: 9, dh: 9 }) assert.deepEqual(grown[0], { x: 0, y: 0, w: 8, h: 4 }) valid(grown) const shrunk = clampPanels(defaults, 0, { dw: -2, dh: -3 }) assert.deepEqual(shrunk[0], { x: 0, y: 0, w: 5, h: 5 }) valid(shrunk) }) test('every change from every arrangement stays a valid window', () => { const starts = [defaults, roomy, parsePanels('5,5,2,2;0,0,2,2;10,10,2,2')] const changes = [] for (const d of [-13, -3, -1, 0, 1, 2, 13]) { changes.push({ dx: d }, { dy: d }, { dw: d }, { dh: d }, { dx: d, dy: -d }, { dw: d, dh: d }) } for (const start of starts) { for (let i = 0; i < 3; i++) { for (const change of changes) valid(clampPanels(start, i, change)) } } }) test('arrow keys move a panel a cell; with Shift they resize it', () => { assert.deepEqual(keyChange('ArrowLeft', false), { dx: -1 }) assert.deepEqual(keyChange('ArrowRight', false), { dx: 1 }) assert.deepEqual(keyChange('ArrowUp', false), { dy: -1 }) assert.deepEqual(keyChange('ArrowDown', false), { dy: 1 }) assert.deepEqual(keyChange('ArrowLeft', true), { dw: -1 }) assert.deepEqual(keyChange('ArrowRight', true), { dw: 1 }) assert.deepEqual(keyChange('ArrowUp', true), { dh: -1 }) assert.deepEqual(keyChange('ArrowDown', true), { dh: 1 }) assert.equal(keyChange('PageDown', false), null) assert.equal(keyChange('b', false), null) }) test('the stylesheet places the panels as the server does', () => { const css = arrangeCSS(roomy) assert.match(css, /^@media \(min-width: 720px\) and \(min-height: 560px\) \{/) assert.ok(css.includes('[presenter-panel="now"] { grid-area: 1 / 1 / span 4 / span 4; }')) assert.ok(css.includes('[presenter-panel="next"] { grid-area: 5 / 1 / span 4 / span 4; }')) assert.ok(css.includes('[presenter-panel="notes"] { grid-area: 1 / 9 / span 12 / span 4; }')) assert.ok(css.includes('[presenter-panel] { --p-now: min(4 / 12 * 100cqw - 32px, (4 / 12 * 100cqh - 16px) * 16 / 9); }')) // Arranged, Next fills its own panel. assert.ok(css.includes('[presenter-panel="next"] [presenter-frame] { inline-size: min(100cqw, 100cqh * 16 / 9); }')) // The default leaves Next room for half of Now, and Next is half. const def = arrangeCSS(defaults) assert.ok(def.includes('[presenter-panel] { --p-now: min(7 / 12 * 100cqw - 32px, (8 / 12 * 100cqh - 16px) * 16 / 9, (4 / 12 * 100cqh - 48px) * 2 * 16 / 9); }')) assert.ok(def.includes('[presenter-panel="next"] [presenter-frame] { inline-size: min(100cqw, 100cqh * 16 / 9, var(--p-now) / 2); }')) }) test('each of the eight buttons is one cell of one change', () => { assert.deepEqual(toolChange('left'), { dx: -1 }) assert.deepEqual(toolChange('right'), { dx: 1 }) assert.deepEqual(toolChange('up'), { dy: -1 }) assert.deepEqual(toolChange('down'), { dy: 1 }) assert.deepEqual(toolChange('narrower'), { dw: -1 }) assert.deepEqual(toolChange('wider'), { dw: 1 }) assert.deepEqual(toolChange('shorter'), { dh: -1 }) assert.deepEqual(toolChange('taller'), { dh: 1 }) assert.equal(toolChange('sideways'), null) }) test('a button is offered only where its change can be made', () => { assert.equal(canChange(defaults, 0, toolChange('narrower')), true) assert.equal(canChange(defaults, 0, toolChange('shorter')), true) for (const op of ['left', 'right', 'up', 'down', 'wider', 'taller']) { assert.equal(canChange(defaults, 0, toolChange(op)), false, op) } assert.equal(canChange(roomy, 0, toolChange('right')), true) assert.equal(canChange(roomy, 0, null), false) }) test('the live region says the panel\'s size and place, counted from one', () => { const where = '{name}: {w} by {h}, column {x}, row {y}' assert.equal(whereWords(where, 'Coming next', { x: 0, y: 5, w: 7, h: 4 }), 'Coming next: 7 by 4, column 1, row 6') assert.equal(whereWords(where, 'Speaker notes', { x: 7, y: 0, w: 5, h: 12 }), 'Speaker notes: 5 by 12, column 8, row 1') }) test('right to left, moves are mirrored and the buttons\' sizes keep their meaning', () => { assert.deepEqual(mirror(toolChange('left'), true, true), { dx: 1 }) assert.deepEqual(mirror(toolChange('right'), true, true), { dx: -1 }) assert.deepEqual(mirror(toolChange('narrower'), true, true), { dw: -1 }) assert.deepEqual(mirror(toolChange('wider'), true, true), { dw: 1 }) assert.deepEqual(mirror(toolChange('up'), true, true), { dy: -1 }) // An arrow key is a direction: Shift+ArrowLeft pulls the far edge, // which is on the left, further left. assert.deepEqual(mirror(keyChange('ArrowLeft', true), true, false), { dw: 1 }) assert.deepEqual(mirror(keyChange('ArrowLeft', false), true, false), { dx: 1 }) // Left to right, nothing changes. assert.deepEqual(mirror(toolChange('left'), false, true), { dx: -1 }) assert.deepEqual(mirror(keyChange('ArrowRight', true), false, false), { dw: 1 }) assert.equal(mirror(null, true, true), null) const c = toolChange('left') mirror(c, true, true) assert.deepEqual(c, { dx: -1 }, 'the change given is not altered') })