// play-through.mjs: what waits at each press, the control's words, and // the stage driven through a slide's presses and back to the finished // slide (on a stub DOM: which elements carry data-waiting, and when). import test from 'node:test' import assert from 'node:assert/strict' import { waitingAt, pressesWords, mountPlayThrough } from './play-through.mjs' import { el, withDocument } from './dom-stub.mjs' const para = t => [{ t }] // Three presses: a text on 1, a list item by item on 2 and 3 (two // items), and a heading with the slide. const slide = { id: 's1', blocks: [ { id: 'h', type: 'heading', text: [para('Title')] }, { id: 'a', type: 'text', step: 1, text: [para('First')] }, { id: 'l', type: 'list', step: 2, items: 'each', text: [para('one'), para('two')] } ] } const plain = { id: 's2', blocks: [{ id: 'h2', type: 'heading', text: [para('Plain')] }] } const texts = { one: '1 press', many: '{n} presses', exit: 'Press {n} of {total}. Esc shows the finished slide.' } test('waitingAt is everything whose press comes after the one given', () => { const key = ms => ms.map(m => `${m.block}:${m.item}`).join(' ') assert.equal(key(waitingAt(slide, 0)), 'a:-1 l:0 l:1') assert.equal(key(waitingAt(slide, 1)), 'l:0 l:1') assert.equal(key(waitingAt(slide, 2)), 'l:1') assert.equal(key(waitingAt(slide, 3)), '') assert.equal(key(waitingAt(plain, 0)), '') assert.equal(key(waitingAt(null, 1)), '') }) test('pressesWords: the count at rest, the press while playing', () => { assert.equal(pressesWords(texts, 1, 0), '1 press') assert.equal(pressesWords(texts, 3, 0), '3 presses') assert.equal(pressesWords(texts, 3, 2), 'Press 2 of 3. Esc shows the finished slide.') assert.equal(pressesWords(texts, 0, 0), '') }) // A stage holding slide's boxes, and the pane's control beside it. function stageFor () { const li = [el('li'), el('li')] const boxes = { h: el('h1', { 'slides-block': '', 'data-id': 'h' }), a: el('div', { 'slides-block': '', 'data-id': 'a' }), l: el('ul', { 'slides-block': '', 'data-id': 'l' }, li) } const section = el('section', { 'slides-slide': '' }, Object.values(boxes)) const stageEl = el('div', { 'slides-stage': '' }, [section]) const button = el('button', { 'slides-presses': '' }) button.hidden = true const pane = el('section', { 'slides-stage-pane': '' }, [stageEl, button]) return { boxes, li, stageEl, button, pane } } const waiting = s => [...Object.entries(s.boxes).filter(([, b]) => b.hasAttribute('data-waiting')).map(([id]) => id), ...s.li.map((l, i) => l.hasAttribute('data-waiting') ? 'l' + i : null).filter(Boolean)].join(' ') test('a click plays press 1, each click the next, and one after the last shows the finished slide', () => { const { restore } = withDocument() try { const s = stageFor() let current = slide let exits = 0 const play = mountPlayThrough({ stageEl: s.stageEl, getSlide: () => current, onExit: () => exits++, button: s.button, texts }) play.refresh() assert.equal(s.button.hidden, false) assert.equal(s.button.textContent, '3 presses') assert.equal(waiting(s), '') s.button.dispatchEvent({ type: 'click' }) assert.equal(s.button.textContent, 'Press 1 of 3. Esc shows the finished slide.') assert.equal(waiting(s), 'l0 l1') s.button.dispatchEvent({ type: 'keydown', key: 'ArrowRight' }) assert.equal(waiting(s), 'l1') s.button.dispatchEvent({ type: 'click' }) assert.equal(waiting(s), '') assert.equal(s.button.textContent, 'Press 3 of 3. Esc shows the finished slide.') s.button.dispatchEvent({ type: 'click' }) assert.equal(waiting(s), '') assert.equal(s.button.textContent, '3 presses') assert.equal(exits, 1) // Esc, and refresh() (a change of slide, any edit), end it too. s.button.dispatchEvent({ type: 'click' }) assert.equal(waiting(s), 'l0 l1') document.dispatchEvent({ type: 'keydown', key: 'Escape' }) assert.equal(waiting(s), '') assert.equal(s.button.textContent, '3 presses') s.button.dispatchEvent({ type: 'click' }) assert.notEqual(waiting(s), '') play.refresh() assert.equal(waiting(s), '') assert.equal(s.button.textContent, '3 presses') // A slide without presses: no control. current = plain play.refresh() assert.equal(s.button.hidden, true) play.stop() play.destroy() } finally { restore() } }) test('Esc while not playing is left alone', () => { const { restore } = withDocument() try { const s = stageFor() const play = mountPlayThrough({ stageEl: s.stageEl, getSlide: () => slide, button: s.button, texts }) play.refresh() const ev = { type: 'keydown', key: 'Escape' } document.dispatchEvent(ev) assert.equal(ev.defaultPrevented, undefined) play.destroy() } finally { restore() } }) test('a looping or once clip with a step waits for it; a clip on a press keeps its poster', () => { const { restore } = withDocument() try { const clips = { id: 's3', blocks: [ { id: 'p', type: 'clip', props: { asset: 'v', play: 'press' } }, { id: 'o', type: 'clip', step: 2, props: { asset: 'v', play: 'loop' } }, { id: 'n', type: 'clip', step: 3, props: { asset: 'v', play: 'once' } } ] } const boxes = { p: el('figure', { 'slides-block': '', 'data-id': 'p', 'data-block': 'clip', 'data-clip': 'press' }), o: el('figure', { 'slides-block': '', 'data-id': 'o', 'data-block': 'clip', 'data-clip': 'loop' }), n: el('figure', { 'slides-block': '', 'data-id': 'n', 'data-block': 'clip', 'data-clip': 'once' }) } const stageEl = el('div', { 'slides-stage': '' }, [el('section', { 'slides-slide': '' }, Object.values(boxes))]) const button = el('button', { 'slides-presses': '' }) el('section', { 'slides-stage-pane': '' }, [stageEl, button]) const play = mountPlayThrough({ stageEl, getSlide: () => clips, button, texts }) play.refresh() assert.equal(button.textContent, '3 presses') const waits = () => Object.entries(boxes).filter(([, b]) => b.hasAttribute('data-waiting')).map(([id]) => id).join(' ') button.dispatchEvent({ type: 'click' }) assert.equal(waits(), 'o n', 'press 1: the press clip shows its poster; the others wait') button.dispatchEvent({ type: 'click' }) assert.equal(waits(), 'n') button.dispatchEvent({ type: 'click' }) assert.equal(waits(), '') play.destroy() } finally { restore() } })