// scene-card.test.mjs — editing a scene by hand (scenes and clips spec // §5): the card's ops (scene-card.mjs), the data as a table // (scene-data.mjs), and the code's save and words (scene-code.mjs). import test from 'node:test' import assert from 'node:assert/strict' import { scenesByName, insertProps, pickOps, rangeOps, wordOps, picturesNote } from './scene-card.mjs' import { tableOf, withCell, withRow, withoutRow, parseData } from './scene-data.mjs' import { saveSceneCode, refusalText, authorLine } from './scene-code.mjs' import { sceneVersion } from './scene-version.mjs' const states = (...ids) => ids.map(id => ({ id, name: id.toUpperCase(), description: '' })) const scenes = { w: { name: 'World', states: states('a', 'b', 'c'), words: {}, data: null, stills: {} }, o: { name: 'Other', states: states('x'), words: {}, data: null, stills: {} } } const slide = { id: 's1', blocks: [] } const block = { id: 'b1', type: 'scene', props: { scene: 'w', from: 'a', to: 'c' } } test('scenesByName: the deck\'s scenes in name order; Insert places the first, first state to last', () => { assert.deepEqual(scenesByName(scenes).map(s => s.id), ['o', 'w']) assert.deepEqual(insertProps({ w: scenes.w }), { scene: 'w', from: 'a', to: 'c' }) assert.deepEqual(insertProps(scenes), { scene: 'o', from: 'x', to: 'x' }) assert.equal(insertProps({}), null) assert.equal(insertProps(null), null) }) test('pickOps: another scene starts at its first state and walks to its last', () => { assert.deepEqual(pickOps(slide, block, scenes, 'o'), [{ op: 'set_block', slide: 's1', id: 'b1', props: { scene: 'o', from: 'x', to: 'x' } }]) assert.deepEqual(pickOps(slide, block, scenes, 'w'), []) assert.deepEqual(pickOps(slide, block, scenes, 'nosuch'), []) }) test('rangeOps: From and To are one set_block each, and stay in order', () => { const set = props => [{ op: 'set_block', slide: 's1', id: 'b1', props }] assert.deepEqual(rangeOps(slide, block, scenes.w, 'to', 'b'), set({ to: 'b' })) assert.deepEqual(rangeOps(slide, block, scenes.w, 'from', 'b'), set({ from: 'b' })) // From past To takes To with it, and To before From takes From. const ab = { ...block, props: { scene: 'w', from: 'a', to: 'b' } } assert.deepEqual(rangeOps(slide, ab, scenes.w, 'from', 'c'), set({ from: 'c', to: 'c' })) const bc = { ...block, props: { scene: 'w', from: 'b', to: 'c' } } assert.deepEqual(rangeOps(slide, bc, scenes.w, 'to', 'a'), set({ from: 'a', to: 'a' })) // Nothing to write, or a state the scene does not have. assert.deepEqual(rangeOps(slide, block, scenes.w, 'to', 'c'), []) assert.deepEqual(rangeOps(slide, block, scenes.w, 'to', 'z'), []) }) test('wordOps: one set_scene of the one word', () => { assert.deepEqual(wordOps('w', 'title', 'Wien'), [{ op: 'set_scene', id: 'w', words: { title: 'Wien' } }]) }) test('picturesNote: stale when a still was made from another version; the states with none, counted', () => { const texts = { missing: '{n} states have no picture yet.', missingOne: '1 state has no picture yet.' } const sc = { ...scenes.w, code: 'c'.repeat(64) } assert.deepEqual(picturesNote(sc, texts), { stale: false, missing: '3 states have no picture yet.' }) const v = sceneVersion(sc) const still = made => ({ ref: 'f'.repeat(64), made }) assert.deepEqual(picturesNote({ ...sc, stills: { a: still(v), b: still(v) } }, texts), { stale: false, missing: '1 state has no picture yet.' }) assert.deepEqual(picturesNote({ ...sc, stills: { a: still(v), b: still(v), c: still(v) } }, texts), { stale: false, missing: '' }) assert.deepEqual(picturesNote({ ...sc, stills: { a: still(v), b: still(v), c: still('0'.repeat(64)) } }, texts), { stale: true, missing: '' }) }) const world = { pieces: [{ name: 'Tower', floors: 3, lit: true }, { name: 'Garden', floors: 0, lit: false }], fonts: ['Brand'] } test('tableOf: a list of objects with scalar fields, or the first such list in an object', () => { assert.deepEqual(tableOf(world), { key: 'pieces', rows: world.pieces, cols: ['floors', 'lit', 'name'] }) const rows = [{ b: null }, { a: 1 }] assert.deepEqual(tableOf(rows), { key: null, rows, cols: ['a', 'b'] }) // Not a table: a nested value, an empty list, a list of scalars, a scalar. assert.equal(tableOf([{ a: { deep: 1 } }]), null) assert.equal(tableOf({ pieces: [] }), null) assert.equal(tableOf({ fonts: ['a'] }), null) assert.equal(tableOf(3), null) assert.equal(tableOf(null), null) }) test('withCell, withRow, withoutRow: the whole data back, the value keeping its kind', () => { const t = tableOf(world) const five = withCell(world, t, 0, 'floors', '5') assert.deepEqual(five.pieces[0], { name: 'Tower', floors: 5, lit: true }) assert.deepEqual(five.fonts, ['Brand']) assert.equal(world.pieces[0].floors, 3, 'the old data is not changed under the view') assert.equal(withCell(world, t, 0, 'floors', 'many').pieces[0].floors, 'many') assert.equal(withCell(world, t, 1, 'lit', 'true').pieces[1].lit, true) assert.equal(withCell(world, t, 1, 'name', '').pieces[1].name, '') assert.deepEqual(withRow(world, t).pieces[2], { name: '', floors: 0, lit: false }) assert.deepEqual(withoutRow(world, t, 0).pieces, [world.pieces[1]]) const bare = [{ a: 1 }] assert.deepEqual(withCell(bare, tableOf(bare), 0, 'a', '2'), [{ a: 2 }]) }) test('parseData: JSON or a refusal', () => { assert.deepEqual(parseData('{"a": [1]}'), { ok: true, value: { a: [1] } }) assert.deepEqual(parseData('{"a": ['), { ok: false }) assert.deepEqual(parseData(''), { ok: false }) }) test('saveSceneCode: signs, puts and registers the text as a scene; the ref back', async () => { const calls = [] const ref = await saveSceneCode({ text: '

hi', sign: async body => { calls.push(['sign', body]); return { ref: 'k', url: '/put', headers: { a: 'b' } } }, put: async (url, blob, headers) => { calls.push(['put', url, blob.size, headers]) }, register: async body => { calls.push(['register', body]); return { kind: 'scene', ref: 'r'.repeat(64) } }, hash: async () => 'h'.repeat(64) }) assert.equal(ref, 'r'.repeat(64)) assert.deepEqual(calls, [ ['sign', { name: 'scene.html', size: 20, type: 'text/html', hash: 'h'.repeat(64) }], ['put', '/put', 20, { a: 'b' }], ['register', { ref: 'k', name: 'scene.html' }] ]) // Too large or empty is refused before anything moves. const none = () => { throw new Error('nothing should be sent') } await assert.rejects(saveSceneCode({ text: 'x'.repeat(2 * 1024 * 1024 + 1), sign: none, put: none, register: none, hash: none }), { code: 'too_large' }) await assert.rejects(saveSceneCode({ text: '', sign: none, put: none, register: none, hash: none }), { code: 'not_html' }) }) test('refusalText and authorLine: the approved words, the name in them', () => { const texts = { refused: '{name} isn\'t a single HTML page.', refusedBig: '{name} is over 2 MB.', failed: 'That upload didn\'t finish. Try again.' } assert.equal(refusalText('not_html', 'World', texts), 'World isn\'t a single HTML page.') assert.equal(refusalText('too_large', 'World', texts), 'World is over 2 MB.') assert.equal(refusalText('internal', 'World', texts), 'That upload didn\'t finish. Try again.') const labels = { 'person:1': 'paul@example.com', 'agent:2': '🤖 builder' } assert.equal(authorLine('person:1', labels, 'Last changed by {name}'), 'Last changed by paul@example.com') assert.equal(authorLine('agent:2', labels, 'Last changed by {name}'), 'Last changed by 🤖 builder') assert.equal(authorLine('agent:9', labels, 'Last changed by {name}'), '') assert.equal(authorLine('', labels, 'Last changed by {name}'), '') }) test('dataTooBig: 960 KiB of canonical JSON is the most, one byte over is refused', async () => { const { dataTooBig } = await import('./scene-data.mjs') const { MAX_SCENE_DATA } = await import('./scenes.mjs') // {"s":"…"} is the string's length plus 8 bytes. const sized = n => ({ s: 'x'.repeat(n - 8) }) assert.equal(dataTooBig(sized(MAX_SCENE_DATA)), false) assert.equal(dataTooBig(sized(MAX_SCENE_DATA + 1)), true) // Measured in UTF-8 bytes, not UTF-16 units: é is two. assert.equal(dataTooBig({ s: 'é'.repeat((MAX_SCENE_DATA - 8) / 2) }), false) assert.equal(dataTooBig({ s: 'é'.repeat((MAX_SCENE_DATA - 8) / 2) + 'x' }), true) }) test('stillsList: each scene with pictures missing or stale, by name, in the card\'s words', async () => { const { stillsList } = await import('./scene-card.mjs') const texts = { missing: '{n} states have no picture yet.', missingOne: '1 state has no picture yet.', stale: 'These pictures are older than the scene.' } const w = { ...scenes.w, code: 'c'.repeat(64) } const o = { ...scenes.o, code: 'c'.repeat(64), stills: { x: { ref: 'f'.repeat(64), made: '0'.repeat(64) } } } const done = { name: 'Done', states: states('d'), code: 'c'.repeat(64), words: {}, data: null } done.stills = { d: { ref: 'f'.repeat(64), made: sceneVersion(done) } } assert.deepEqual(stillsList({ w, o, done }, texts), [ { id: 'o', name: 'Other', lines: ['These pictures are older than the scene.'] }, { id: 'w', name: 'World', lines: ['3 states have no picture yet.'] } ]) assert.deepEqual(stillsList({ done }, texts), []) assert.deepEqual(stillsList(null, texts), []) }) test('stillsList and picturesNote count a still with no registered asset as missing', async () => { const { stillsList } = await import('./scene-card.mjs') const texts = { missing: '{n} states have no picture yet.', missingOne: '1 state has no picture yet.', stale: 'These pictures are older than the scene.' } const done = { name: 'Done', states: states('d'), code: 'c'.repeat(64), words: {}, data: null } done.stills = { d: { ref: 'f'.repeat(64), made: sceneVersion(done) } } const assets = { stillasset01: { ref: 'f'.repeat(64) } } assert.deepEqual(stillsList({ done }, texts, assets), []) assert.deepEqual(stillsList({ done }, texts, {}), [{ id: 'done', name: 'Done', lines: ['1 state has no picture yet.'] }]) assert.deepEqual(picturesNote(done, texts, {}), { stale: false, missing: '1 state has no picture yet.' }) }) test('mountSceneCode: Save is off again while new code loads, and stays off when it fails to load', async () => { const { mountSceneCode } = await import('./scene-code.mjs') const node = () => ({ value: '', textContent: '', hidden: false, disabled: false, addEventListener () {} }) const parts = { textarea: node(), '[slides-scene-status]': node(), '[slides-scene-author]': node(), '[slides-scene-save]': node(), '[slides-scene-retry]': node() } const el = { querySelector: s => parts[s], addEventListener () {} } const answers = { one: { ok: true, text: 'one' }, two: { ok: false, status: 500 } } const realFetch = globalThis.fetch const realError = console.error console.error = () => {} globalThis.fetch = async url => { const a = answers[url.split('/').pop()] return { ok: a.ok, status: a.status || 200, text: async () => a.text } } const settle = () => new Promise(resolve => setTimeout(resolve, 0)) try { const code = mountSceneCode({ el, base: '/d/x', texts: { loadFailed: 'That didn’t work' }, commit () {}, save () {}, preview () {} }) const save = parts['[slides-scene-save]'] code.fill('w', { name: 'World', code: 'one', author: '' }, true) assert.equal(save.disabled, true, 'off before the first text') await settle() assert.equal(parts.textarea.value, 'one') assert.equal(save.disabled, false, 'on once it has loaded') // The code moves on under a text nobody changed: off while it loads… code.fill('w', { name: 'World', code: 'two', author: '' }, true) assert.equal(save.disabled, true, 'off while the new code loads') await settle() // …and off when it does not come, so the old text cannot replace it. assert.equal(save.disabled, true, 'off when the new code failed to load') assert.equal(parts['[slides-scene-status]'].textContent, 'That didn’t work') assert.equal(parts['[slides-scene-retry]'].hidden, false) code.fill('w', { name: 'World', code: 'two', author: '' }, true) assert.equal(save.disabled, true, 'a fill after the failure leaves it off') } finally { globalThis.fetch = realFetch console.error = realError } })