oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// scene-card.test.mjs — editing a scene by hand (scenes and clips spec
2// §5): the card's ops (scene-card.mjs), the data as a table
3// (scene-data.mjs), and the code's save and words (scene-code.mjs).
4import test from 'node:test'
5import assert from 'node:assert/strict'
6import { scenesByName, insertProps, pickOps, rangeOps, wordOps, picturesNote } from './scene-card.mjs'
7import { tableOf, withCell, withRow, withoutRow, parseData } from './scene-data.mjs'
8import { saveSceneCode, refusalText, authorLine } from './scene-code.mjs'
9import { sceneVersion } from './scene-version.mjs'
10
11const states = (...ids) => ids.map(id => ({ id, name: id.toUpperCase(), description: '' }))
12const scenes = {
13 w: { name: 'World', states: states('a', 'b', 'c'), words: {}, data: null, stills: {} },
14 o: { name: 'Other', states: states('x'), words: {}, data: null, stills: {} }
15}
16const slide = { id: 's1', blocks: [] }
17const block = { id: 'b1', type: 'scene', props: { scene: 'w', from: 'a', to: 'c' } }
18
19test('scenesByName: the deck\'s scenes in name order; Insert places the first, first state to last', () => {
20 assert.deepEqual(scenesByName(scenes).map(s => s.id), ['o', 'w'])
21 assert.deepEqual(insertProps({ w: scenes.w }), { scene: 'w', from: 'a', to: 'c' })
22 assert.deepEqual(insertProps(scenes), { scene: 'o', from: 'x', to: 'x' })
23 assert.equal(insertProps({}), null)
24 assert.equal(insertProps(null), null)
25})
26
27test('pickOps: another scene starts at its first state and walks to its last', () => {
28 assert.deepEqual(pickOps(slide, block, scenes, 'o'), [{ op: 'set_block', slide: 's1', id: 'b1', props: { scene: 'o', from: 'x', to: 'x' } }])
29 assert.deepEqual(pickOps(slide, block, scenes, 'w'), [])
30 assert.deepEqual(pickOps(slide, block, scenes, 'nosuch'), [])
31})
32
33test('rangeOps: From and To are one set_block each, and stay in order', () => {
34 const set = props => [{ op: 'set_block', slide: 's1', id: 'b1', props }]
35 assert.deepEqual(rangeOps(slide, block, scenes.w, 'to', 'b'), set({ to: 'b' }))
36 assert.deepEqual(rangeOps(slide, block, scenes.w, 'from', 'b'), set({ from: 'b' }))
37 // From past To takes To with it, and To before From takes From.
38 const ab = { ...block, props: { scene: 'w', from: 'a', to: 'b' } }
39 assert.deepEqual(rangeOps(slide, ab, scenes.w, 'from', 'c'), set({ from: 'c', to: 'c' }))
40 const bc = { ...block, props: { scene: 'w', from: 'b', to: 'c' } }
41 assert.deepEqual(rangeOps(slide, bc, scenes.w, 'to', 'a'), set({ from: 'a', to: 'a' }))
42 // Nothing to write, or a state the scene does not have.
43 assert.deepEqual(rangeOps(slide, block, scenes.w, 'to', 'c'), [])
44 assert.deepEqual(rangeOps(slide, block, scenes.w, 'to', 'z'), [])
45})
46
47test('wordOps: one set_scene of the one word', () => {
48 assert.deepEqual(wordOps('w', 'title', 'Wien'), [{ op: 'set_scene', id: 'w', words: { title: 'Wien' } }])
49})
50
51test('picturesNote: stale when a still was made from another version; the states with none, counted', () => {
52 const texts = { missing: '{n} states have no picture yet.', missingOne: '1 state has no picture yet.' }
53 const sc = { ...scenes.w, code: 'c'.repeat(64) }
54 assert.deepEqual(picturesNote(sc, texts), { stale: false, missing: '3 states have no picture yet.' })
55 const v = sceneVersion(sc)
56 const still = made => ({ ref: 'f'.repeat(64), made })
57 assert.deepEqual(picturesNote({ ...sc, stills: { a: still(v), b: still(v) } }, texts), { stale: false, missing: '1 state has no picture yet.' })
58 assert.deepEqual(picturesNote({ ...sc, stills: { a: still(v), b: still(v), c: still(v) } }, texts), { stale: false, missing: '' })
59 assert.deepEqual(picturesNote({ ...sc, stills: { a: still(v), b: still(v), c: still('0'.repeat(64)) } }, texts), { stale: true, missing: '' })
60})
61
62const world = { pieces: [{ name: 'Tower', floors: 3, lit: true }, { name: 'Garden', floors: 0, lit: false }], fonts: ['Brand'] }
63
64test('tableOf: a list of objects with scalar fields, or the first such list in an object', () => {
65 assert.deepEqual(tableOf(world), { key: 'pieces', rows: world.pieces, cols: ['floors', 'lit', 'name'] })
66 const rows = [{ b: null }, { a: 1 }]
67 assert.deepEqual(tableOf(rows), { key: null, rows, cols: ['a', 'b'] })
68 // Not a table: a nested value, an empty list, a list of scalars, a scalar.
69 assert.equal(tableOf([{ a: { deep: 1 } }]), null)
70 assert.equal(tableOf({ pieces: [] }), null)
71 assert.equal(tableOf({ fonts: ['a'] }), null)
72 assert.equal(tableOf(3), null)
73 assert.equal(tableOf(null), null)
74})
75
76test('withCell, withRow, withoutRow: the whole data back, the value keeping its kind', () => {
77 const t = tableOf(world)
78 const five = withCell(world, t, 0, 'floors', '5')
79 assert.deepEqual(five.pieces[0], { name: 'Tower', floors: 5, lit: true })
80 assert.deepEqual(five.fonts, ['Brand'])
81 assert.equal(world.pieces[0].floors, 3, 'the old data is not changed under the view')
82 assert.equal(withCell(world, t, 0, 'floors', 'many').pieces[0].floors, 'many')
83 assert.equal(withCell(world, t, 1, 'lit', 'true').pieces[1].lit, true)
84 assert.equal(withCell(world, t, 1, 'name', '').pieces[1].name, '')
85 assert.deepEqual(withRow(world, t).pieces[2], { name: '', floors: 0, lit: false })
86 assert.deepEqual(withoutRow(world, t, 0).pieces, [world.pieces[1]])
87 const bare = [{ a: 1 }]
88 assert.deepEqual(withCell(bare, tableOf(bare), 0, 'a', '2'), [{ a: 2 }])
89})
90
91test('parseData: JSON or a refusal', () => {
92 assert.deepEqual(parseData('{"a": [1]}'), { ok: true, value: { a: [1] } })
93 assert.deepEqual(parseData('{"a": ['), { ok: false })
94 assert.deepEqual(parseData(''), { ok: false })
95})
96
97test('saveSceneCode: signs, puts and registers the text as a scene; the ref back', async () => {
98 const calls = []
99 const ref = await saveSceneCode({
100 text: '<!doctype html><p>hi',
101 sign: async body => { calls.push(['sign', body]); return { ref: 'k', url: '/put', headers: { a: 'b' } } },
102 put: async (url, blob, headers) => { calls.push(['put', url, blob.size, headers]) },
103 register: async body => { calls.push(['register', body]); return { kind: 'scene', ref: 'r'.repeat(64) } },
104 hash: async () => 'h'.repeat(64)
105 })
106 assert.equal(ref, 'r'.repeat(64))
107 assert.deepEqual(calls, [
108 ['sign', { name: 'scene.html', size: 20, type: 'text/html', hash: 'h'.repeat(64) }],
109 ['put', '/put', 20, { a: 'b' }],
110 ['register', { ref: 'k', name: 'scene.html' }]
111 ])
112 // Too large or empty is refused before anything moves.
113 const none = () => { throw new Error('nothing should be sent') }
114 await assert.rejects(saveSceneCode({ text: 'x'.repeat(2 * 1024 * 1024 + 1), sign: none, put: none, register: none, hash: none }), { code: 'too_large' })
115 await assert.rejects(saveSceneCode({ text: '', sign: none, put: none, register: none, hash: none }), { code: 'not_html' })
116})
117
118test('refusalText and authorLine: the approved words, the name in them', () => {
119 const texts = { refused: '{name} isn\'t a single HTML page.', refusedBig: '{name} is over 2 MB.', failed: 'That upload didn\'t finish. Try again.' }
120 assert.equal(refusalText('not_html', 'World', texts), 'World isn\'t a single HTML page.')
121 assert.equal(refusalText('too_large', 'World', texts), 'World is over 2 MB.')
122 assert.equal(refusalText('internal', 'World', texts), 'That upload didn\'t finish. Try again.')
123 const labels = { 'person:1': 'paul@example.com', 'agent:2': '🤖 builder' }
124 assert.equal(authorLine('person:1', labels, 'Last changed by {name}'), 'Last changed by paul@example.com')
125 assert.equal(authorLine('agent:2', labels, 'Last changed by {name}'), 'Last changed by 🤖 builder')
126 assert.equal(authorLine('agent:9', labels, 'Last changed by {name}'), '')
127 assert.equal(authorLine('', labels, 'Last changed by {name}'), '')
128})
129
130test('dataTooBig: 960 KiB of canonical JSON is the most, one byte over is refused', async () => {
131 const { dataTooBig } = await import('./scene-data.mjs')
132 const { MAX_SCENE_DATA } = await import('./scenes.mjs')
133 // {"s":"…"} is the string's length plus 8 bytes.
134 const sized = n => ({ s: 'x'.repeat(n - 8) })
135 assert.equal(dataTooBig(sized(MAX_SCENE_DATA)), false)
136 assert.equal(dataTooBig(sized(MAX_SCENE_DATA + 1)), true)
137 // Measured in UTF-8 bytes, not UTF-16 units: é is two.
138 assert.equal(dataTooBig({ s: 'é'.repeat((MAX_SCENE_DATA - 8) / 2) }), false)
139 assert.equal(dataTooBig({ s: 'é'.repeat((MAX_SCENE_DATA - 8) / 2) + 'x' }), true)
140})
141
142test('stillsList: each scene with pictures missing or stale, by name, in the card\'s words', async () => {
143 const { stillsList } = await import('./scene-card.mjs')
144 const texts = { missing: '{n} states have no picture yet.', missingOne: '1 state has no picture yet.', stale: 'These pictures are older than the scene.' }
145 const w = { ...scenes.w, code: 'c'.repeat(64) }
146 const o = { ...scenes.o, code: 'c'.repeat(64), stills: { x: { ref: 'f'.repeat(64), made: '0'.repeat(64) } } }
147 const done = { name: 'Done', states: states('d'), code: 'c'.repeat(64), words: {}, data: null }
148 done.stills = { d: { ref: 'f'.repeat(64), made: sceneVersion(done) } }
149 assert.deepEqual(stillsList({ w, o, done }, texts), [
150 { id: 'o', name: 'Other', lines: ['These pictures are older than the scene.'] },
151 { id: 'w', name: 'World', lines: ['3 states have no picture yet.'] }
152 ])
153 assert.deepEqual(stillsList({ done }, texts), [])
154 assert.deepEqual(stillsList(null, texts), [])
155})
156
157test('stillsList and picturesNote count a still with no registered asset as missing', async () => {
158 const { stillsList } = await import('./scene-card.mjs')
159 const texts = { missing: '{n} states have no picture yet.', missingOne: '1 state has no picture yet.', stale: 'These pictures are older than the scene.' }
160 const done = { name: 'Done', states: states('d'), code: 'c'.repeat(64), words: {}, data: null }
161 done.stills = { d: { ref: 'f'.repeat(64), made: sceneVersion(done) } }
162 const assets = { stillasset01: { ref: 'f'.repeat(64) } }
163 assert.deepEqual(stillsList({ done }, texts, assets), [])
164 assert.deepEqual(stillsList({ done }, texts, {}), [{ id: 'done', name: 'Done', lines: ['1 state has no picture yet.'] }])
165 assert.deepEqual(picturesNote(done, texts, {}), { stale: false, missing: '1 state has no picture yet.' })
166})
167
168test('mountSceneCode: Save is off again while new code loads, and stays off when it fails to load', async () => {
169 const { mountSceneCode } = await import('./scene-code.mjs')
170 const node = () => ({ value: '', textContent: '', hidden: false, disabled: false, addEventListener () {} })
171 const parts = { textarea: node(), '[slides-scene-status]': node(), '[slides-scene-author]': node(), '[slides-scene-save]': node(), '[slides-scene-retry]': node() }
172 const el = { querySelector: s => parts[s], addEventListener () {} }
173 const answers = { one: { ok: true, text: '<!doctype html>one' }, two: { ok: false, status: 500 } }
174 const realFetch = globalThis.fetch
175 const realError = console.error
176 console.error = () => {}
177 globalThis.fetch = async url => {
178 const a = answers[url.split('/').pop()]
179 return { ok: a.ok, status: a.status || 200, text: async () => a.text }
180 }
181 const settle = () => new Promise(resolve => setTimeout(resolve, 0))
182 try {
183 const code = mountSceneCode({ el, base: '/d/x', texts: { loadFailed: 'That didn’t work' }, commit () {}, save () {}, preview () {} })
184 const save = parts['[slides-scene-save]']
185 code.fill('w', { name: 'World', code: 'one', author: '' }, true)
186 assert.equal(save.disabled, true, 'off before the first text')
187 await settle()
188 assert.equal(parts.textarea.value, '<!doctype html>one')
189 assert.equal(save.disabled, false, 'on once it has loaded')
190 // The code moves on under a text nobody changed: off while it loads…
191 code.fill('w', { name: 'World', code: 'two', author: '' }, true)
192 assert.equal(save.disabled, true, 'off while the new code loads')
193 await settle()
194 // …and off when it does not come, so the old text cannot replace it.
195 assert.equal(save.disabled, true, 'off when the new code failed to load')
196 assert.equal(parts['[slides-scene-status]'].textContent, 'That didn’t work')
197 assert.equal(parts['[slides-scene-retry]'].hidden, false)
198 code.fill('w', { name: 'World', code: 'two', author: '' }, true)
199 assert.equal(save.disabled, true, 'a fill after the failure leaves it off')
200 } finally {
201 globalThis.fetch = realFetch
202 console.error = realError
203 }
204})
205