oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1// fixtures/clip.test.mjs — a clip block's video over its poster
2// (clip.mjs; scenes and clips spec §2, plan call R3, Review Focus 5):
3// when it plays, that it is always muted, that back pauses and rewinds,
4// that reduced motion keeps the poster for what nobody pressed, and that
5// a video that will not play leaves the poster where it is.
6import { test } from 'node:test'
7import assert from 'node:assert/strict'
8import { el, withDocument } from './dom-stub.mjs'
9import { clipWants, mountClips } from './clip.mjs'
10
11// A clip block as render.go draws it: its poster, and for a clip played
12// on a press drawn at a press, the mark that press arrives on.
13function clip (play, { marker, waiting, src = '/d/x/assets/vid' } = {}) {
14 const kids = [el('img', { src: '/d/x/assets/poster', alt: '' })]
15 if (marker) kids.push(el('span', waiting ? { 'data-clip-press': '', 'data-press': '1', 'data-waiting': '' } : { 'data-clip-press': '', 'data-press': '1' }))
16 return el('figure', { 'slides-block': '', 'data-block': 'clip', 'data-id': 'c1', 'data-clip': play, 'data-clip-src': src }, kids)
17}
18
19test('a clip on a press plays once its press has come; loop and once from arrival', () => {
20 assert.equal(clipWants(clip('press', { marker: true, waiting: true })), false)
21 assert.equal(clipWants(clip('press', { marker: true })), true)
22 assert.equal(clipWants(clip('loop')), true)
23 assert.equal(clipWants(clip('once')), true)
24 // A slide drawn finished (the viewer) has had every press.
25 assert.equal(clipWants(clip('press')), true)
26 // A clip that has not arrived itself, or is inside what has not, waits.
27 const late = clip('loop')
28 late.setAttribute('data-waiting', '')
29 assert.equal(clipWants(late), false)
30 const inner = clip('once')
31 const slide = el('section', { 'slides-slide': '' }, [el('div', { 'data-waiting': '' }, [inner])])
32 assert.equal(clipWants(inner, { within: slide }), false)
33})
34
35test('reduced motion keeps the poster for loop and once; a press still plays', () => {
36 assert.equal(clipWants(clip('loop'), { reduce: true }), false)
37 assert.equal(clipWants(clip('once'), { reduce: true }), false)
38 assert.equal(clipWants(clip('press'), { reduce: true }), false, 'nobody pressed a finished slide')
39 assert.equal(clipWants(clip('press', { marker: true }), { reduce: true }), true)
40 assert.equal(clipWants(clip('press', { marker: true, waiting: true }), { reduce: true }), false)
41})
42
43// A video element enough for clip.mjs: what it was told, and its events.
44function stubVideos (doc) {
45 const made = []
46 const create = doc.createElement
47 doc.createElement = tag => {
48 const v = create(tag)
49 if (tag !== 'video') return v
50 Object.assign(v, {
51 paused: true,
52 currentTime: 0,
53 plays: 0,
54 refuse: null,
55 play () {
56 this.plays++
57 if (this.refuse) return Promise.reject(this.refuse)
58 this.paused = false
59 return Promise.resolve()
60 },
61 pause () { this.paused = true },
62 fire (type) { this.dispatchEvent({ type }) }
63 })
64 made.push(v)
65 return v
66 }
67 return made
68}
69
70function mount (root, opts = {}) {
71 return mountClips({ root, doc: globalThis.document, win: { matchMedia: null }, ...opts })
72}
73
74test('one muted, inline video per clip, over its poster, live only once it plays', async () => {
75 const { document, restore } = withDocument()
76 try {
77 const made = stubVideos(document)
78 const block = clip('loop')
79 const root = el('div', {}, [el('section', { 'slides-slide': '' }, [block])])
80 const clips = mount(root)
81 clips.sync()
82 clips.sync()
83 assert.equal(made.length, 1)
84 const v = made[0]
85 assert.equal(v.parentNode, block)
86 assert.equal(v.muted, true)
87 assert.equal(v.defaultMuted, true)
88 assert.ok(v.hasAttribute('muted') && v.hasAttribute('playsinline'))
89 assert.equal(v.getAttribute('preload'), 'auto')
90 assert.equal(v.getAttribute('aria-hidden'), 'true')
91 assert.equal(v.loop, true)
92 assert.equal(v.src, '/d/x/assets/vid')
93 assert.equal(v.plays, 1)
94 assert.equal(block.hasAttribute('data-clip-live'), false, 'the poster shows until it plays')
95 v.fire('playing')
96 assert.equal(block.hasAttribute('data-clip-live'), true)
97 // Something that has a volume control was never offered one.
98 assert.equal(v.hasAttribute('controls'), false)
99 clips.destroy()
100 } finally {
101 restore()
102 }
103})
104
105test('a press plays from the start; back pauses, rewinds and shows the poster', async () => {
106 const { document, restore } = withDocument()
107 try {
108 const made = stubVideos(document)
109 const block = clip('press', { marker: true, waiting: true })
110 const root = el('div', {}, [block])
111 const clips = mount(root)
112 clips.sync()
113 const v = made[0]
114 assert.equal(v.plays, 0)
115 assert.equal(v.loop, false)
116 block.querySelector('[data-clip-press]').removeAttribute('data-waiting')
117 clips.sync()
118 assert.equal(v.plays, 1)
119 v.fire('playing')
120 v.currentTime = 1.2
121 assert.ok(block.hasAttribute('data-clip-live'))
122 block.querySelector('[data-clip-press]').setAttribute('data-waiting', '')
123 clips.sync()
124 assert.equal(v.paused, true)
125 assert.equal(v.currentTime, 0)
126 assert.equal(block.hasAttribute('data-clip-live'), false)
127 // Pressed again, it plays from the start again.
128 block.querySelector('[data-clip-press]').removeAttribute('data-waiting')
129 clips.sync()
130 assert.equal(v.plays, 2)
131 assert.equal(v.currentTime, 0)
132 clips.destroy()
133 } finally {
134 restore()
135 }
136})
137
138test('a video that will not play leaves its poster, and is not tried again', async () => {
139 const { document, restore } = withDocument()
140 try {
141 const made = stubVideos(document)
142 const broken = clip('loop')
143 const refused = clip('press', { marker: true })
144 const root = el('div', {}, [broken, refused])
145 // The second's play() is refused as a browser refuses a codec it lacks.
146 const create = document.createElement
147 let n = 0
148 document.createElement = tag => {
149 const v = create(tag)
150 if (tag === 'video' && ++n === 2) v.refuse = Object.assign(new Error('no codec'), { name: 'NotSupportedError' })
151 return v
152 }
153 const clips = mount(root)
154 made[0].fire('error')
155 await new Promise(resolve => setTimeout(resolve, 0))
156 for (const b of [broken, refused]) {
157 assert.equal(b.hasAttribute('data-clip-live'), false)
158 assert.deepEqual(b.children.map(c => c.tagName).filter(t => t !== 'SPAN'), ['IMG'], 'the video is gone; the poster is what shows')
159 }
160 clips.sync()
161 assert.equal(made.length, 2, 'a broken clip is not made again')
162 clips.destroy()
163 } finally {
164 restore()
165 }
166})
167
168test('under reduced motion a loop is never started', () => {
169 const { document, restore } = withDocument()
170 try {
171 const made = stubVideos(document)
172 const root = el('div', {}, [clip('loop')])
173 const clips = mount(root, { reducedMotion: true })
174 clips.sync()
175 assert.equal(made.length, 1)
176 assert.equal(made[0].plays, 0)
177 clips.destroy()
178 } finally {
179 restore()
180 }
181})
182