// fixtures/scene-layer.test.mjs — placing scene frames over a slide's // scene blocks and carrying them on (scene-layer.mjs; scenes and clips // spec §3, §8.7): which state a block is at, what counts as the same // box, which frames are kept, made and dropped, which moves are one // press, and which of the deck's faces a scene is sent. import { test } from 'node:test' import assert from 'node:assert/strict' import { el } from './dom-stub.mjs' import { scenePositions, planFrames, talkMove, sceneFonts, stillsOnly } from './scene-layer.mjs' import { pageMove } from './scene-pages.mjs' function sceneBlock (id, scene, states, style) { const b = el('div', { 'slides-block': '', 'data-block': 'scene', 'data-id': id, 'data-scene': scene }, states.map(([state, waiting]) => el('img', waiting ? { 'data-scene-state': state, 'data-waiting': '' } : { 'data-scene-state': state }))) b.style = style || {} return b } test('a block is at the newest state that has arrived; the box is its key', () => { const canvas = el('section', { 'slides-slide': '', 'data-layout': 'canvas' }, [ sceneBlock('b1', 'worldaaaaaaa', [['field'], ['town'], ['city', true]], { left: '10%', top: '10%', width: '50%', height: '50%' }), el('div', { 'slides-block': '', 'data-block': 'text', 'data-id': 't' }) ]) const p = scenePositions(canvas) assert.equal(p.length, 1) assert.equal(p[0].block, 'b1') assert.equal(p[0].scene, 'worldaaaaaaa') assert.equal(p[0].state, 'town') assert.equal(p[0].key, 'box:10%,10%,50%,50%') const flow = el('section', { 'slides-slide': '', 'data-layout': 'flow' }, [ el('div', { 'slides-frame': '' }, [el('div', { 'slides-region': '', 'data-region': 'aside' }, [ sceneBlock('b2', 'worldaaaaaaa', [['field']]) ])]) ]) assert.equal(scenePositions(flow)[0].key, 'region:aside') // A block with no state to draw (the panel of a removed scene) runs nothing. const gone = el('section', { 'slides-slide': '', 'data-layout': 'flow' }, [sceneBlock('b3', 'goneaaaaaaaa', [])]) assert.deepEqual(scenePositions(gone), []) }) test('a frame is kept for the same scene in the same box, made fresh otherwise', () => { const frames = new Map([ ['a 0', { key: 'box:1', sig: 's' }], ['b 0', { key: 'box:1', sig: 's' }], ['c 0', { key: 'region:body', sig: 's' }], ['d 0', { key: 'box:1', sig: 's' }] ]) const plan = planFrames(frames, [ { scene: 'a', slot: 'a 0', key: 'box:1', sig: 's' }, // the same: kept { scene: 'b', slot: 'b 0', key: 'box:2', sig: 's' }, // another box: fresh { scene: 'c', slot: 'c 0', key: 'region:body', sig: 't' }, // its code, words or data changed: fresh { scene: 'e', slot: 'e 0', key: 'box:1', sig: 's' } // new // d: not on this slide: dropped ]) assert.deepEqual(plan.keep.map(p => p.scene), ['a']) assert.deepEqual(plan.make.map(p => p.scene), ['b', 'c', 'e']) assert.deepEqual(plan.drop.sort(), ['b 0', 'c 0', 'd 0']) }) test('each placement of a scene on a slide is its own, paired with the next slide\'s by order', () => { const slide = el('section', { 'slides-slide': '', 'data-layout': 'canvas' }, [ sceneBlock('l', 'w', [['one']], { left: '1%' }), sceneBlock('o', 'x', [['one']], { left: '2%' }), sceneBlock('r', 'w', [['one']], { left: '3%' }) ]) assert.deepEqual(scenePositions(slide).map(p => p.slot), ['w 0', 'x 0', 'w 1']) // The first placement of w carries on into the next slide's first, // in the same box; its second, in another box, is made afresh; and the // first's old frame is not dropped for the second's sake. const frames = new Map([['w 0', { key: 'box:l', sig: 's' }], ['w 1', { key: 'box:r', sig: 's' }]]) const plan = planFrames(frames, [ { scene: 'w', slot: 'w 0', key: 'box:l', sig: 's' }, { scene: 'w', slot: 'w 1', key: 'box:low', sig: 's' } ]) assert.deepEqual(plan.keep.map(p => p.slot), ['w 0']) assert.deepEqual(plan.make.map(p => p.slot), ['w 1']) assert.deepEqual(plan.drop, ['w 1']) }) test('a placement keeps its number while an earlier block of its scene waits for its press', () => { const first = sceneBlock('l', 'w', [['one']], { left: '1%' }) first.setAttribute('data-waiting', '') const slide = el('section', { 'slides-slide': '', 'data-layout': 'canvas' }, [first, sceneBlock('r', 'w', [['one']], { left: '3%' })]) assert.deepEqual(scenePositions(slide).map(p => [p.block, p.slot]), [['r', 'w 1']]) first.removeAttribute('data-waiting') assert.deepEqual(scenePositions(slide).map(p => [p.block, p.slot]), [['l', 'w 0'], ['r', 'w 1']]) }) test('one press apart is a move that animates; anything else is a jump', () => { const slides = [{ id: 's1', presses: 2 }, { id: 's2', presses: 1 }, { id: 'p', presses: 0, full: true }, { id: 's3', presses: 0 }] const store = { view: { slides: slides.map(s => ({ id: s.id, talk: s.full ? 'full' : '' })) }, slide: id => slides.find(s => s.id === id) || null } const at = (slide, step, mode = 'core') => ({ slide, step, mode }) assert.equal(talkMove(store, at('s1', 0), at('s1', 1)), true) assert.equal(talkMove(store, at('s1', 2), at('s1', 1)), true) assert.equal(talkMove(store, at('s1', 0), at('s1', 2)), false) assert.equal(talkMove(store, at('s1', 2), at('s2', 0)), true, 'next from the last step') assert.equal(talkMove(store, at('s2', 0), at('s1', 2)), true, 'back to the last step') assert.equal(talkMove(store, at('s1', 1), at('s2', 0)), false, 'a slide early is a jump') assert.equal(talkMove(store, at('s2', 1), at('s3', 0)), true, 'the full-only slide is not in the core talk') assert.equal(talkMove(store, at('s2', 1, 'full'), at('s3', 0, 'full')), false) assert.equal(talkMove(store, null, at('s1', 0)), false) assert.equal(talkMove(store, at('s1', 0), at('s3', 0)), false) }) test('a scene is sent the deck faces its data names, and only those', () => { const manifest = { families: { 'liberation-sans': { css: 'Liberation Sans', faces: [{ weight: 400, style: 'normal', url: '/static/fonts/ls.woff2' }] } }, uploaded: { 'u:Brand': { css: 'Brand', faces: [{ weight: '100 900', style: 'normal', url: '/d/x/fonts/a' }, { weight: 400, style: 'italic', url: '/d/x/fonts/b' }] }, 'u:Other': { css: 'Other', faces: [{ weight: 400, style: 'normal', url: '/d/x/fonts/c' }] } } } assert.deepEqual(sceneFonts({ fonts: ['Brand', 'Liberation Sans', 'Nowhere', 7] }, manifest), [ { family: 'Brand', url: '/d/x/fonts/a', weight: '100 900', style: 'normal' }, { family: 'Brand', url: '/d/x/fonts/b', weight: '400', style: 'italic' }, { family: 'Liberation Sans', url: '/static/fonts/ls.woff2', weight: '400', style: 'normal' } ]) assert.deepEqual(sceneFonts({}, manifest), []) assert.deepEqual(sceneFonts({ fonts: ['Brand'] }, null), []) assert.deepEqual(sceneFonts(null, manifest), []) }) test('a phone view shows stills', () => { const block = sceneBlock('b1', 'worldaaaaaaa', [['field']]) const desktop = el('section', { 'slides-slide': '', 'data-layout': 'flow' }, [block]) desktop.offsetWidth = 1280 assert.equal(stillsOnly(block), false) desktop.offsetWidth = 390 assert.equal(stillsOnly(block), true, 'a slide the phone layer lays out') const phone = sceneBlock('b2', 'worldaaaaaaa', [['field']]) const section = el('section', { 'slides-slide': '', 'data-layout': 'flow', 'data-vp': 'phone' }, [phone]) section.offsetWidth = 1280 assert.equal(stillsOnly(phone), true, 'rendered for a phone') }) test('a scene inside a box that has not arrived runs nothing yet', () => { const section = el('section', { 'slides-slide': '', 'data-layout': 'flow' }, [ el('div', { 'slides-frame': '' }, [el('div', { 'slides-region': '', 'data-region': 'body' }, [ el('div', { 'slides-block': '', 'data-block': 'columns', 'data-id': 'c', 'data-press': '1', 'data-waiting': '' }, [ sceneBlock('b1', 'worldaaaaaaa', [['field']]) ]) ])]) ]) assert.deepEqual(scenePositions(section), []) section.querySelector('[data-id="c"]').removeAttribute('data-waiting') assert.equal(scenePositions(section).length, 1) }) test('a scene with a box over it on a canvas runs inside its block, not over the slide', () => { const box = (id, z, left, top, width, height, type = 'shape') => { const b = el('div', { 'slides-block': '', 'data-block': type, 'data-id': id, 'data-z': String(z) }) b.style = { left, top, width, height } return b } const scene = sceneBlock('s', 'worldaaaaaaa', [['field']], { left: '10%', top: '10%', width: '60%', height: '60%' }) scene.setAttribute('data-z', '1') const slide = (...others) => el('section', { 'slides-slide': '', 'data-layout': 'canvas' }, [scene, ...others]) // Below it, beside it, or above it elsewhere: unobstructed. slide(box('under', 0, '20%', '20%', '10%', '10%'), box('beside', 2, '75%', '10%', '20%', '20%')) assert.equal(scenePositions(scene.parentNode)[0].inside, false) // Above it and over it: obstructed, and its key is its own element. slide(box('caption', 2, '30%', '60%', '30%', '15%', 'text')) const p = scenePositions(scene.parentNode)[0] assert.equal(p.inside, true) assert.notEqual(p.key, 'box:10%,10%,60%,60%') // The same z, later in the slide, paints over it too. slide(box('later', 1, '15%', '15%', '5%', '5%')) assert.equal(scenePositions(scene.parentNode)[0].inside, true) }) test('image-full puts its heading over the media: a scene there runs inside its block', () => { const scene = sceneBlock('s', 'worldaaaaaaa', [['field']]) el('section', { 'slides-slide': '', 'data-layout': 'flow', 'data-template': 'image-full' }, [ el('div', { 'slides-frame': '' }, [ el('div', { 'slides-region': '', 'data-region': 'heading' }, [el('h1', { 'slides-block': '', 'data-block': 'heading', 'data-id': 'h' })]), el('div', { 'slides-region': '', 'data-region': 'media' }, [scene]) ]) ]) assert.equal(scenePositions(scene.closest('[slides-slide]'))[0].inside, true) }) test('on a page that steps, one press apart animates and anything else is a jump', () => { const at = (index, step, presses) => ({ id: 's' + index, index, step, presses }) assert.equal(pageMove(at(0, 0, 1), at(0, 1, 1)), true) assert.equal(pageMove(at(0, 1, 1), at(1, 0, 1)), true, 'next from the last step') assert.equal(pageMove(at(0, 0, 1), at(1, 0, 1)), false, 'the next slide early is a jump') assert.equal(pageMove(at(1, 0, 1), at(0, 1, 1)), true, 'back onto the last step') assert.equal(pageMove(at(1, 0, 1), at(0, 0, 1)), false) assert.equal(pageMove(at(0, 0, 0), at(2, 0, 0)), false) assert.equal(pageMove(null, at(0, 0, 0)), false) // A page with no steps (the viewer) shows finished slides: the next or previous one is one move. const fin = index => ({ id: 's' + index, index, step: null, presses: 3 }) assert.equal(pageMove(fin(0), fin(1)), true) assert.equal(pageMove(fin(2), fin(1)), true) assert.equal(pageMove(fin(0), fin(2)), false) })