// fixtures/clip.test.mjs — a clip block's video over its poster // (clip.mjs; scenes and clips spec §2, plan call R3, Review Focus 5): // when it plays, that it is always muted, that back pauses and rewinds, // that reduced motion keeps the poster for what nobody pressed, and that // a video that will not play leaves the poster where it is. import { test } from 'node:test' import assert from 'node:assert/strict' import { el, withDocument } from './dom-stub.mjs' import { clipWants, mountClips } from './clip.mjs' // A clip block as render.go draws it: its poster, and for a clip played // on a press drawn at a press, the mark that press arrives on. function clip (play, { marker, waiting, src = '/d/x/assets/vid' } = {}) { const kids = [el('img', { src: '/d/x/assets/poster', alt: '' })] if (marker) kids.push(el('span', waiting ? { 'data-clip-press': '', 'data-press': '1', 'data-waiting': '' } : { 'data-clip-press': '', 'data-press': '1' })) return el('figure', { 'slides-block': '', 'data-block': 'clip', 'data-id': 'c1', 'data-clip': play, 'data-clip-src': src }, kids) } test('a clip on a press plays once its press has come; loop and once from arrival', () => { assert.equal(clipWants(clip('press', { marker: true, waiting: true })), false) assert.equal(clipWants(clip('press', { marker: true })), true) assert.equal(clipWants(clip('loop')), true) assert.equal(clipWants(clip('once')), true) // A slide drawn finished (the viewer) has had every press. assert.equal(clipWants(clip('press')), true) // A clip that has not arrived itself, or is inside what has not, waits. const late = clip('loop') late.setAttribute('data-waiting', '') assert.equal(clipWants(late), false) const inner = clip('once') const slide = el('section', { 'slides-slide': '' }, [el('div', { 'data-waiting': '' }, [inner])]) assert.equal(clipWants(inner, { within: slide }), false) }) test('reduced motion keeps the poster for loop and once; a press still plays', () => { assert.equal(clipWants(clip('loop'), { reduce: true }), false) assert.equal(clipWants(clip('once'), { reduce: true }), false) assert.equal(clipWants(clip('press'), { reduce: true }), false, 'nobody pressed a finished slide') assert.equal(clipWants(clip('press', { marker: true }), { reduce: true }), true) assert.equal(clipWants(clip('press', { marker: true, waiting: true }), { reduce: true }), false) }) // A video element enough for clip.mjs: what it was told, and its events. function stubVideos (doc) { const made = [] const create = doc.createElement doc.createElement = tag => { const v = create(tag) if (tag !== 'video') return v Object.assign(v, { paused: true, currentTime: 0, plays: 0, refuse: null, play () { this.plays++ if (this.refuse) return Promise.reject(this.refuse) this.paused = false return Promise.resolve() }, pause () { this.paused = true }, fire (type) { this.dispatchEvent({ type }) } }) made.push(v) return v } return made } function mount (root, opts = {}) { return mountClips({ root, doc: globalThis.document, win: { matchMedia: null }, ...opts }) } test('one muted, inline video per clip, over its poster, live only once it plays', async () => { const { document, restore } = withDocument() try { const made = stubVideos(document) const block = clip('loop') const root = el('div', {}, [el('section', { 'slides-slide': '' }, [block])]) const clips = mount(root) clips.sync() clips.sync() assert.equal(made.length, 1) const v = made[0] assert.equal(v.parentNode, block) assert.equal(v.muted, true) assert.equal(v.defaultMuted, true) assert.ok(v.hasAttribute('muted') && v.hasAttribute('playsinline')) assert.equal(v.getAttribute('preload'), 'auto') assert.equal(v.getAttribute('aria-hidden'), 'true') assert.equal(v.loop, true) assert.equal(v.src, '/d/x/assets/vid') assert.equal(v.plays, 1) assert.equal(block.hasAttribute('data-clip-live'), false, 'the poster shows until it plays') v.fire('playing') assert.equal(block.hasAttribute('data-clip-live'), true) // Something that has a volume control was never offered one. assert.equal(v.hasAttribute('controls'), false) clips.destroy() } finally { restore() } }) test('a press plays from the start; back pauses, rewinds and shows the poster', async () => { const { document, restore } = withDocument() try { const made = stubVideos(document) const block = clip('press', { marker: true, waiting: true }) const root = el('div', {}, [block]) const clips = mount(root) clips.sync() const v = made[0] assert.equal(v.plays, 0) assert.equal(v.loop, false) block.querySelector('[data-clip-press]').removeAttribute('data-waiting') clips.sync() assert.equal(v.plays, 1) v.fire('playing') v.currentTime = 1.2 assert.ok(block.hasAttribute('data-clip-live')) block.querySelector('[data-clip-press]').setAttribute('data-waiting', '') clips.sync() assert.equal(v.paused, true) assert.equal(v.currentTime, 0) assert.equal(block.hasAttribute('data-clip-live'), false) // Pressed again, it plays from the start again. block.querySelector('[data-clip-press]').removeAttribute('data-waiting') clips.sync() assert.equal(v.plays, 2) assert.equal(v.currentTime, 0) clips.destroy() } finally { restore() } }) test('a video that will not play leaves its poster, and is not tried again', async () => { const { document, restore } = withDocument() try { const made = stubVideos(document) const broken = clip('loop') const refused = clip('press', { marker: true }) const root = el('div', {}, [broken, refused]) // The second's play() is refused as a browser refuses a codec it lacks. const create = document.createElement let n = 0 document.createElement = tag => { const v = create(tag) if (tag === 'video' && ++n === 2) v.refuse = Object.assign(new Error('no codec'), { name: 'NotSupportedError' }) return v } const clips = mount(root) made[0].fire('error') await new Promise(resolve => setTimeout(resolve, 0)) for (const b of [broken, refused]) { assert.equal(b.hasAttribute('data-clip-live'), false) assert.deepEqual(b.children.map(c => c.tagName).filter(t => t !== 'SPAN'), ['IMG'], 'the video is gone; the poster is what shows') } clips.sync() assert.equal(made.length, 2, 'a broken clip is not made again') clips.destroy() } finally { restore() } }) test('under reduced motion a loop is never started', () => { const { document, restore } = withDocument() try { const made = stubVideos(document) const root = el('div', {}, [clip('loop')]) const clips = mount(root, { reducedMotion: true }) clips.sync() assert.equal(made.length, 1) assert.equal(made[0].plays, 0) clips.destroy() } finally { restore() } })