| 1 | // fixtures/scene-layer.test.mjs — placing scene frames over a slide's |
| 2 | // scene blocks and carrying them on (scene-layer.mjs; scenes and clips |
| 3 | // spec §3, §8.7): which state a block is at, what counts as the same |
| 4 | // box, which frames are kept, made and dropped, which moves are one |
| 5 | // press, and which of the deck's faces a scene is sent. |
| 6 | import { test } from 'node:test' |
| 7 | import assert from 'node:assert/strict' |
| 8 | import { el } from './dom-stub.mjs' |
| 9 | import { scenePositions, planFrames, talkMove, sceneFonts, stillsOnly } from './scene-layer.mjs' |
| 10 | import { pageMove } from './scene-pages.mjs' |
| 11 | |
| 12 | function sceneBlock (id, scene, states, style) { |
| 13 | const b = el('div', { 'slides-block': '', 'data-block': 'scene', 'data-id': id, 'data-scene': scene }, |
| 14 | states.map(([state, waiting]) => el('img', waiting ? { 'data-scene-state': state, 'data-waiting': '' } : { 'data-scene-state': state }))) |
| 15 | b.style = style || {} |
| 16 | return b |
| 17 | } |
| 18 | |
| 19 | test('a block is at the newest state that has arrived; the box is its key', () => { |
| 20 | const canvas = el('section', { 'slides-slide': '', 'data-layout': 'canvas' }, [ |
| 21 | sceneBlock('b1', 'worldaaaaaaa', [['field'], ['town'], ['city', true]], { left: '10%', top: '10%', width: '50%', height: '50%' }), |
| 22 | el('div', { 'slides-block': '', 'data-block': 'text', 'data-id': 't' }) |
| 23 | ]) |
| 24 | const p = scenePositions(canvas) |
| 25 | assert.equal(p.length, 1) |
| 26 | assert.equal(p[0].block, 'b1') |
| 27 | assert.equal(p[0].scene, 'worldaaaaaaa') |
| 28 | assert.equal(p[0].state, 'town') |
| 29 | assert.equal(p[0].key, 'box:10%,10%,50%,50%') |
| 30 | |
| 31 | const flow = el('section', { 'slides-slide': '', 'data-layout': 'flow' }, [ |
| 32 | el('div', { 'slides-frame': '' }, [el('div', { 'slides-region': '', 'data-region': 'aside' }, [ |
| 33 | sceneBlock('b2', 'worldaaaaaaa', [['field']]) |
| 34 | ])]) |
| 35 | ]) |
| 36 | assert.equal(scenePositions(flow)[0].key, 'region:aside') |
| 37 | // A block with no state to draw (the panel of a removed scene) runs nothing. |
| 38 | const gone = el('section', { 'slides-slide': '', 'data-layout': 'flow' }, [sceneBlock('b3', 'goneaaaaaaaa', [])]) |
| 39 | assert.deepEqual(scenePositions(gone), []) |
| 40 | }) |
| 41 | |
| 42 | test('a frame is kept for the same scene in the same box, made fresh otherwise', () => { |
| 43 | const frames = new Map([ |
| 44 | ['a 0', { key: 'box:1', sig: 's' }], |
| 45 | ['b 0', { key: 'box:1', sig: 's' }], |
| 46 | ['c 0', { key: 'region:body', sig: 's' }], |
| 47 | ['d 0', { key: 'box:1', sig: 's' }] |
| 48 | ]) |
| 49 | const plan = planFrames(frames, [ |
| 50 | { scene: 'a', slot: 'a 0', key: 'box:1', sig: 's' }, // the same: kept |
| 51 | { scene: 'b', slot: 'b 0', key: 'box:2', sig: 's' }, // another box: fresh |
| 52 | { scene: 'c', slot: 'c 0', key: 'region:body', sig: 't' }, // its code, words or data changed: fresh |
| 53 | { scene: 'e', slot: 'e 0', key: 'box:1', sig: 's' } // new |
| 54 | // d: not on this slide: dropped |
| 55 | ]) |
| 56 | assert.deepEqual(plan.keep.map(p => p.scene), ['a']) |
| 57 | assert.deepEqual(plan.make.map(p => p.scene), ['b', 'c', 'e']) |
| 58 | assert.deepEqual(plan.drop.sort(), ['b 0', 'c 0', 'd 0']) |
| 59 | }) |
| 60 | |
| 61 | test('each placement of a scene on a slide is its own, paired with the next slide\'s by order', () => { |
| 62 | const slide = el('section', { 'slides-slide': '', 'data-layout': 'canvas' }, [ |
| 63 | sceneBlock('l', 'w', [['one']], { left: '1%' }), |
| 64 | sceneBlock('o', 'x', [['one']], { left: '2%' }), |
| 65 | sceneBlock('r', 'w', [['one']], { left: '3%' }) |
| 66 | ]) |
| 67 | assert.deepEqual(scenePositions(slide).map(p => p.slot), ['w 0', 'x 0', 'w 1']) |
| 68 | // The first placement of w carries on into the next slide's first, |
| 69 | // in the same box; its second, in another box, is made afresh; and the |
| 70 | // first's old frame is not dropped for the second's sake. |
| 71 | const frames = new Map([['w 0', { key: 'box:l', sig: 's' }], ['w 1', { key: 'box:r', sig: 's' }]]) |
| 72 | const plan = planFrames(frames, [ |
| 73 | { scene: 'w', slot: 'w 0', key: 'box:l', sig: 's' }, |
| 74 | { scene: 'w', slot: 'w 1', key: 'box:low', sig: 's' } |
| 75 | ]) |
| 76 | assert.deepEqual(plan.keep.map(p => p.slot), ['w 0']) |
| 77 | assert.deepEqual(plan.make.map(p => p.slot), ['w 1']) |
| 78 | assert.deepEqual(plan.drop, ['w 1']) |
| 79 | }) |
| 80 | |
| 81 | test('a placement keeps its number while an earlier block of its scene waits for its press', () => { |
| 82 | const first = sceneBlock('l', 'w', [['one']], { left: '1%' }) |
| 83 | first.setAttribute('data-waiting', '') |
| 84 | const slide = el('section', { 'slides-slide': '', 'data-layout': 'canvas' }, [first, sceneBlock('r', 'w', [['one']], { left: '3%' })]) |
| 85 | assert.deepEqual(scenePositions(slide).map(p => [p.block, p.slot]), [['r', 'w 1']]) |
| 86 | first.removeAttribute('data-waiting') |
| 87 | assert.deepEqual(scenePositions(slide).map(p => [p.block, p.slot]), [['l', 'w 0'], ['r', 'w 1']]) |
| 88 | }) |
| 89 | |
| 90 | test('one press apart is a move that animates; anything else is a jump', () => { |
| 91 | const slides = [{ id: 's1', presses: 2 }, { id: 's2', presses: 1 }, { id: 'p', presses: 0, full: true }, { id: 's3', presses: 0 }] |
| 92 | const store = { |
| 93 | view: { slides: slides.map(s => ({ id: s.id, talk: s.full ? 'full' : '' })) }, |
| 94 | slide: id => slides.find(s => s.id === id) || null |
| 95 | } |
| 96 | const at = (slide, step, mode = 'core') => ({ slide, step, mode }) |
| 97 | assert.equal(talkMove(store, at('s1', 0), at('s1', 1)), true) |
| 98 | assert.equal(talkMove(store, at('s1', 2), at('s1', 1)), true) |
| 99 | assert.equal(talkMove(store, at('s1', 0), at('s1', 2)), false) |
| 100 | assert.equal(talkMove(store, at('s1', 2), at('s2', 0)), true, 'next from the last step') |
| 101 | assert.equal(talkMove(store, at('s2', 0), at('s1', 2)), true, 'back to the last step') |
| 102 | assert.equal(talkMove(store, at('s1', 1), at('s2', 0)), false, 'a slide early is a jump') |
| 103 | assert.equal(talkMove(store, at('s2', 1), at('s3', 0)), true, 'the full-only slide is not in the core talk') |
| 104 | assert.equal(talkMove(store, at('s2', 1, 'full'), at('s3', 0, 'full')), false) |
| 105 | assert.equal(talkMove(store, null, at('s1', 0)), false) |
| 106 | assert.equal(talkMove(store, at('s1', 0), at('s3', 0)), false) |
| 107 | }) |
| 108 | |
| 109 | test('a scene is sent the deck faces its data names, and only those', () => { |
| 110 | const manifest = { |
| 111 | families: { |
| 112 | 'liberation-sans': { css: 'Liberation Sans', faces: [{ weight: 400, style: 'normal', url: '/static/fonts/ls.woff2' }] } |
| 113 | }, |
| 114 | uploaded: { |
| 115 | 'u:Brand': { css: 'Brand', faces: [{ weight: '100 900', style: 'normal', url: '/d/x/fonts/a' }, { weight: 400, style: 'italic', url: '/d/x/fonts/b' }] }, |
| 116 | 'u:Other': { css: 'Other', faces: [{ weight: 400, style: 'normal', url: '/d/x/fonts/c' }] } |
| 117 | } |
| 118 | } |
| 119 | assert.deepEqual(sceneFonts({ fonts: ['Brand', 'Liberation Sans', 'Nowhere', 7] }, manifest), [ |
| 120 | { family: 'Brand', url: '/d/x/fonts/a', weight: '100 900', style: 'normal' }, |
| 121 | { family: 'Brand', url: '/d/x/fonts/b', weight: '400', style: 'italic' }, |
| 122 | { family: 'Liberation Sans', url: '/static/fonts/ls.woff2', weight: '400', style: 'normal' } |
| 123 | ]) |
| 124 | assert.deepEqual(sceneFonts({}, manifest), []) |
| 125 | assert.deepEqual(sceneFonts({ fonts: ['Brand'] }, null), []) |
| 126 | assert.deepEqual(sceneFonts(null, manifest), []) |
| 127 | }) |
| 128 | |
| 129 | test('a phone view shows stills', () => { |
| 130 | const block = sceneBlock('b1', 'worldaaaaaaa', [['field']]) |
| 131 | const desktop = el('section', { 'slides-slide': '', 'data-layout': 'flow' }, [block]) |
| 132 | desktop.offsetWidth = 1280 |
| 133 | assert.equal(stillsOnly(block), false) |
| 134 | desktop.offsetWidth = 390 |
| 135 | assert.equal(stillsOnly(block), true, 'a slide the phone layer lays out') |
| 136 | const phone = sceneBlock('b2', 'worldaaaaaaa', [['field']]) |
| 137 | const section = el('section', { 'slides-slide': '', 'data-layout': 'flow', 'data-vp': 'phone' }, [phone]) |
| 138 | section.offsetWidth = 1280 |
| 139 | assert.equal(stillsOnly(phone), true, 'rendered for a phone') |
| 140 | }) |
| 141 | |
| 142 | test('a scene inside a box that has not arrived runs nothing yet', () => { |
| 143 | const section = el('section', { 'slides-slide': '', 'data-layout': 'flow' }, [ |
| 144 | el('div', { 'slides-frame': '' }, [el('div', { 'slides-region': '', 'data-region': 'body' }, [ |
| 145 | el('div', { 'slides-block': '', 'data-block': 'columns', 'data-id': 'c', 'data-press': '1', 'data-waiting': '' }, [ |
| 146 | sceneBlock('b1', 'worldaaaaaaa', [['field']]) |
| 147 | ]) |
| 148 | ])]) |
| 149 | ]) |
| 150 | assert.deepEqual(scenePositions(section), []) |
| 151 | section.querySelector('[data-id="c"]').removeAttribute('data-waiting') |
| 152 | assert.equal(scenePositions(section).length, 1) |
| 153 | }) |
| 154 | |
| 155 | test('a scene with a box over it on a canvas runs inside its block, not over the slide', () => { |
| 156 | const box = (id, z, left, top, width, height, type = 'shape') => { |
| 157 | const b = el('div', { 'slides-block': '', 'data-block': type, 'data-id': id, 'data-z': String(z) }) |
| 158 | b.style = { left, top, width, height } |
| 159 | return b |
| 160 | } |
| 161 | const scene = sceneBlock('s', 'worldaaaaaaa', [['field']], { left: '10%', top: '10%', width: '60%', height: '60%' }) |
| 162 | scene.setAttribute('data-z', '1') |
| 163 | const slide = (...others) => el('section', { 'slides-slide': '', 'data-layout': 'canvas' }, [scene, ...others]) |
| 164 | // Below it, beside it, or above it elsewhere: unobstructed. |
| 165 | slide(box('under', 0, '20%', '20%', '10%', '10%'), box('beside', 2, '75%', '10%', '20%', '20%')) |
| 166 | assert.equal(scenePositions(scene.parentNode)[0].inside, false) |
| 167 | // Above it and over it: obstructed, and its key is its own element. |
| 168 | slide(box('caption', 2, '30%', '60%', '30%', '15%', 'text')) |
| 169 | const p = scenePositions(scene.parentNode)[0] |
| 170 | assert.equal(p.inside, true) |
| 171 | assert.notEqual(p.key, 'box:10%,10%,60%,60%') |
| 172 | // The same z, later in the slide, paints over it too. |
| 173 | slide(box('later', 1, '15%', '15%', '5%', '5%')) |
| 174 | assert.equal(scenePositions(scene.parentNode)[0].inside, true) |
| 175 | }) |
| 176 | |
| 177 | test('image-full puts its heading over the media: a scene there runs inside its block', () => { |
| 178 | const scene = sceneBlock('s', 'worldaaaaaaa', [['field']]) |
| 179 | el('section', { 'slides-slide': '', 'data-layout': 'flow', 'data-template': 'image-full' }, [ |
| 180 | el('div', { 'slides-frame': '' }, [ |
| 181 | el('div', { 'slides-region': '', 'data-region': 'heading' }, [el('h1', { 'slides-block': '', 'data-block': 'heading', 'data-id': 'h' })]), |
| 182 | el('div', { 'slides-region': '', 'data-region': 'media' }, [scene]) |
| 183 | ]) |
| 184 | ]) |
| 185 | assert.equal(scenePositions(scene.closest('[slides-slide]'))[0].inside, true) |
| 186 | }) |
| 187 | |
| 188 | test('on a page that steps, one press apart animates and anything else is a jump', () => { |
| 189 | const at = (index, step, presses) => ({ id: 's' + index, index, step, presses }) |
| 190 | assert.equal(pageMove(at(0, 0, 1), at(0, 1, 1)), true) |
| 191 | assert.equal(pageMove(at(0, 1, 1), at(1, 0, 1)), true, 'next from the last step') |
| 192 | assert.equal(pageMove(at(0, 0, 1), at(1, 0, 1)), false, 'the next slide early is a jump') |
| 193 | assert.equal(pageMove(at(1, 0, 1), at(0, 1, 1)), true, 'back onto the last step') |
| 194 | assert.equal(pageMove(at(1, 0, 1), at(0, 0, 1)), false) |
| 195 | assert.equal(pageMove(at(0, 0, 0), at(2, 0, 0)), false) |
| 196 | assert.equal(pageMove(null, at(0, 0, 0)), false) |
| 197 | // A page with no steps (the viewer) shows finished slides: the next or previous one is one move. |
| 198 | const fin = index => ({ id: 's' + index, index, step: null, presses: 3 }) |
| 199 | assert.equal(pageMove(fin(0), fin(1)), true) |
| 200 | assert.equal(pageMove(fin(2), fin(1)), true) |
| 201 | assert.equal(pageMove(fin(0), fin(2)), false) |
| 202 | }) |
| 203 | |