| 1 | // scenes.test.mjs — scenes in the document (scenes and clips spec §2, |
| 2 | // plan task 2), the twin of core/tests/scenes.rs: the deck-level scenes |
| 3 | // map and its three ops, and a scene's version (plan call R1) against |
| 4 | // vectors/scene-version.json, which the core and internal/slides/deck |
| 5 | // read too. The scene and clip block types are scene-blocks.test.mjs. |
| 6 | import test from 'node:test' |
| 7 | import assert from 'node:assert/strict' |
| 8 | import { readFileSync } from 'node:fs' |
| 9 | import * as Y from 'yjs' |
| 10 | import { applyOps, view, OpsError } from './ops.mjs' |
| 11 | import { sceneCanonical, sceneVersion } from './scenes.mjs' |
| 12 | |
| 13 | const CODE = '0123456789abcdef'.repeat(4) |
| 14 | const CODE2 = 'fedcba9876543210'.repeat(4) |
| 15 | const STILL = '1'.repeat(64) |
| 16 | // The data limit: 960 KiB of canonical JSON. A scene change must also fit |
| 17 | // one live update (1 MiB), which scene-update.test.mjs is about. |
| 18 | const LIMIT = 960 * 1024 |
| 19 | |
| 20 | function fresh () { |
| 21 | const doc = new Y.Doc({ guid: 'scenes' }) |
| 22 | doc.clientID = 1 |
| 23 | return doc |
| 24 | } |
| 25 | |
| 26 | const world = () => ({ |
| 27 | op: 'add_scene', |
| 28 | id: 'worldsceneaa', |
| 29 | name: 'The world', |
| 30 | code: CODE, |
| 31 | states: [ |
| 32 | { id: 'a', name: 'Start', description: 'An empty field' }, |
| 33 | { id: 'b', name: 'Town', description: 'A town appears' }, |
| 34 | { id: 'c', name: 'City', description: 'The town grows' } |
| 35 | ], |
| 36 | words: { title: 'Vienna', sign: 'HalfStack' }, |
| 37 | data: { pieces: [{ id: 'p1', x: 0.5 }], n: 3 } |
| 38 | }) |
| 39 | |
| 40 | function deck () { |
| 41 | const doc = fresh() |
| 42 | applyOps(doc, [world(), { op: 'add_slide', id: 's1', layout: 'flow' }]) |
| 43 | return doc |
| 44 | } |
| 45 | |
| 46 | const json = doc => JSON.stringify(view(doc)) |
| 47 | |
| 48 | test('a deck without scenes has no scenes key', () => { |
| 49 | const doc = fresh() |
| 50 | applyOps(doc, [{ op: 'add_slide', id: 's1', layout: 'flow' }]) |
| 51 | assert.equal('scenes' in view(doc), false) |
| 52 | }) |
| 53 | |
| 54 | test('add_scene round trips', () => { |
| 55 | assert.deepEqual(view(deck()).scenes, { |
| 56 | worldsceneaa: { |
| 57 | name: 'The world', |
| 58 | author: '', |
| 59 | code: CODE, |
| 60 | states: [ |
| 61 | { id: 'a', name: 'Start', description: 'An empty field' }, |
| 62 | { id: 'b', name: 'Town', description: 'A town appears' }, |
| 63 | { id: 'c', name: 'City', description: 'The town grows' } |
| 64 | ], |
| 65 | words: { title: 'Vienna', sign: 'HalfStack' }, |
| 66 | data: { pieces: [{ id: 'p1', x: 0.5 }], n: 3 }, |
| 67 | stills: {} |
| 68 | } |
| 69 | }) |
| 70 | }) |
| 71 | |
| 72 | test('add_scene defaults words, data and state names', () => { |
| 73 | const doc = fresh() |
| 74 | applyOps(doc, [{ op: 'add_scene', id: 'wwwwwwwwwwww', name: '', code: CODE, states: [{ id: 'only' }] }]) |
| 75 | assert.deepEqual(view(doc).scenes.wwwwwwwwwwww, { |
| 76 | name: '', author: '', code: CODE, states: [{ id: 'only', name: '', description: '' }], words: {}, data: null, stills: {} |
| 77 | }) |
| 78 | }) |
| 79 | |
| 80 | test('set_scene patches words per key, replaces data and sets stills', () => { |
| 81 | const doc = deck() |
| 82 | applyOps(doc, [{ |
| 83 | op: 'set_scene', |
| 84 | id: 'worldsceneaa', |
| 85 | words: { sign: null, title: 'Wien', new: 'Neu' }, |
| 86 | data: [1, 2], |
| 87 | stills: { a: { ref: STILL, made: CODE2 }, b: { ref: STILL, made: CODE2 } } |
| 88 | }]) |
| 89 | let s = view(doc).scenes.worldsceneaa |
| 90 | assert.deepEqual(s.words, { title: 'Wien', new: 'Neu' }) |
| 91 | assert.deepEqual(s.data, [1, 2]) |
| 92 | assert.equal(s.name, 'The world') |
| 93 | assert.deepEqual(s.stills, { a: { ref: STILL, made: CODE2 }, b: { ref: STILL, made: CODE2 } }) |
| 94 | |
| 95 | applyOps(doc, [{ |
| 96 | op: 'set_scene', id: 'worldsceneaa', name: 'World', code: CODE2, stills: { a: null }, data: null, states: [{ id: 'c', name: 'City', description: 'd' }, { id: 'b' }] |
| 97 | }]) |
| 98 | s = view(doc).scenes.worldsceneaa |
| 99 | assert.equal(s.name, 'World') |
| 100 | assert.equal(s.code, CODE2) |
| 101 | assert.equal(s.data, null) |
| 102 | assert.deepEqual(s.stills, { b: { ref: STILL, made: CODE2 } }) |
| 103 | assert.deepEqual(s.states, [{ id: 'c', name: 'City', description: 'd' }, { id: 'b', name: '', description: '' }]) |
| 104 | assert.deepEqual(s.words, { title: 'Wien', new: 'Neu' }) |
| 105 | }) |
| 106 | |
| 107 | test('author is written when the op carries one, and left alone otherwise', () => { |
| 108 | const doc = fresh() |
| 109 | applyOps(doc, [{ ...world(), author: 'agent:7' }]) |
| 110 | assert.equal(view(doc).scenes.worldsceneaa.author, 'agent:7') |
| 111 | applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', code: CODE2, author: 'person:1' }]) |
| 112 | assert.equal(view(doc).scenes.worldsceneaa.author, 'person:1') |
| 113 | applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', name: 'x' }]) |
| 114 | assert.equal(view(doc).scenes.worldsceneaa.author, 'person:1') |
| 115 | }) |
| 116 | |
| 117 | test('remove_scene drops it, and an empty map leaves the view', () => { |
| 118 | const doc = deck() |
| 119 | applyOps(doc, [{ op: 'remove_scene', id: 'worldsceneaa' }]) |
| 120 | assert.equal('scenes' in view(doc), false) |
| 121 | }) |
| 122 | |
| 123 | test('scene refusals apply nothing', () => { |
| 124 | const big = 'x'.repeat(LIMIT - 1) |
| 125 | const withOp = f => { const op = { ...world(), id: 'secondscenea' }; f(op); return [op] } |
| 126 | const bad = [ |
| 127 | [world()], |
| 128 | withOp(o => { o.id = '' }), |
| 129 | withOp(o => { o.code = 'abc' }), |
| 130 | withOp(o => { o.code = CODE.toUpperCase() }), |
| 131 | withOp(o => { o.states = [] }), |
| 132 | withOp(o => { o.states = 'a' }), |
| 133 | withOp(o => { o.states = ['a'] }), |
| 134 | withOp(o => { o.states = [{ name: 'x' }] }), |
| 135 | withOp(o => { o.states = [{ id: '' }] }), |
| 136 | withOp(o => { o.states = [{ id: 3 }] }), |
| 137 | withOp(o => { o.states = [{ id: 'a', name: 3 }] }), |
| 138 | withOp(o => { o.states = [{ id: 'a', description: [] }] }), |
| 139 | withOp(o => { o.states = [{ id: 'a' }, { id: 'b' }, { id: 'a' }] }), |
| 140 | withOp(o => { o.words = ['a'] }), |
| 141 | withOp(o => { o.words = { k: 1 } }), |
| 142 | withOp(o => { o.words = { k: null } }), |
| 143 | withOp(o => { o.data = big }), |
| 144 | [{ op: 'set_scene', id: 'nope', name: 'x' }], |
| 145 | [{ op: 'set_scene', id: 'worldsceneaa', code: 'x' }], |
| 146 | [{ op: 'set_scene', id: 'worldsceneaa', states: [] }], |
| 147 | [{ op: 'set_scene', id: 'worldsceneaa', states: [{ id: 'a' }, { id: 'a' }] }], |
| 148 | [{ op: 'set_scene', id: 'worldsceneaa', words: { k: 2 } }], |
| 149 | [{ op: 'set_scene', id: 'worldsceneaa', data: { s: big } }], |
| 150 | [{ op: 'set_scene', id: 'worldsceneaa', stills: { zz: { ref: STILL, made: CODE } } }], |
| 151 | [{ op: 'set_scene', id: 'worldsceneaa', stills: { a: { ref: 'x', made: CODE } } }], |
| 152 | [{ op: 'set_scene', id: 'worldsceneaa', stills: { a: { ref: STILL, made: 'x' } } }], |
| 153 | [{ op: 'set_scene', id: 'worldsceneaa', stills: { a: { ref: STILL } } }], |
| 154 | [{ op: 'set_scene', id: 'worldsceneaa', stills: { a: 'x' } }], |
| 155 | [{ op: 'set_scene', id: 'worldsceneaa', stills: [1] }], |
| 156 | [{ op: 'set_scene', id: 'worldsceneaa', states: [{ id: 'a' }], stills: { b: { ref: STILL, made: CODE } } }], |
| 157 | [{ op: 'remove_scene', id: 'nope' }], |
| 158 | [{ op: 'remove_scene', id: 'worldsceneaa' }, { op: 'remove_scene', id: 'worldsceneaa' }] |
| 159 | ] |
| 160 | const doc = deck() |
| 161 | const before = json(doc) |
| 162 | for (const ops of bad) { |
| 163 | assert.throws(() => applyOps(doc, ops), e => e instanceof OpsError && /^op \d+: /.test(e.message), JSON.stringify(ops).slice(0, 200)) |
| 164 | assert.equal(json(doc), before) |
| 165 | } |
| 166 | }) |
| 167 | |
| 168 | test('data up to 960 KiB of canonical JSON is taken', () => { |
| 169 | const doc = deck() |
| 170 | applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', data: 'x'.repeat(LIMIT - 2) }]) |
| 171 | applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', data: ['x'.repeat(LIMIT - 4)] }]) |
| 172 | // UTF-8 bytes, not UTF-16 units: an é is two of them. |
| 173 | assert.throws(() => applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', data: 'é'.repeat(LIMIT / 2) }]), OpsError) |
| 174 | }) |
| 175 | |
| 176 | test('scene version vectors', async t => { |
| 177 | const cases = JSON.parse(readFileSync(new URL('./vectors/scene-version.json', import.meta.url))) |
| 178 | assert.ok(cases.length >= 5) |
| 179 | const seen = new Map() |
| 180 | for (const c of cases) { |
| 181 | // Distinct scenes never share a version (the collision cases). |
| 182 | if (seen.has(c.version)) assert.equal(seen.get(c.version), c.canonical, `${c.name} shares a version with a different scene`) |
| 183 | seen.set(c.version, c.canonical) |
| 184 | await t.test(c.name, () => { |
| 185 | assert.equal(sceneCanonical(c.scene), c.canonical) |
| 186 | assert.equal(sceneVersion(c.scene), c.version) |
| 187 | }) |
| 188 | } |
| 189 | }) |
| 190 | |
| 191 | test('a scene version changes with code, data and words only', () => { |
| 192 | const doc = deck() |
| 193 | const v0 = sceneVersion(view(doc).scenes.worldsceneaa) |
| 194 | applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', name: 'Renamed', stills: { a: { ref: STILL, made: v0 } } }]) |
| 195 | assert.equal(sceneVersion(view(doc).scenes.worldsceneaa), v0) |
| 196 | for (const op of [ |
| 197 | { op: 'set_scene', id: 'worldsceneaa', code: CODE2 }, |
| 198 | { op: 'set_scene', id: 'worldsceneaa', data: { n: 4 } }, |
| 199 | { op: 'set_scene', id: 'worldsceneaa', words: { title: 'Wien' } } |
| 200 | ]) { |
| 201 | const before = sceneVersion(view(doc).scenes.worldsceneaa) |
| 202 | applyOps(doc, [op]) |
| 203 | assert.notEqual(sceneVersion(view(doc).scenes.worldsceneaa), before, JSON.stringify(op)) |
| 204 | } |
| 205 | }) |
| 206 | |
| 207 | test('a scene id is twelve lowercase base32 characters', () => { |
| 208 | const doc = fresh() |
| 209 | const before = json(doc) |
| 210 | for (const id of ['', 'World', 'short', 'toolongsceneid', 'scene0000001', '__proto__', 'SCENEAAAAAAA']) { |
| 211 | assert.throws(() => applyOps(doc, [{ ...world(), id }]), e => e instanceof OpsError && /base32/.test(e.message), JSON.stringify(id)) |
| 212 | assert.equal(json(doc), before) |
| 213 | } |
| 214 | applyOps(doc, [{ ...world(), id: 'abcdefghij27' }]) |
| 215 | }) |
| 216 | |
| 217 | test('__proto__ is refused as a state id, a word key and anywhere in data', () => { |
| 218 | // lib0, Yjs's decoder, loses a plain object's "__proto__" key, so a |
| 219 | // scene may not depend on one (scenes review, finding 4). JSON.parse, |
| 220 | // because an object literal's __proto__ sets the prototype. |
| 221 | const doc = deck() |
| 222 | const before = json(doc) |
| 223 | const withOp = patch => [{ ...world(), id: 'secondscenea', ...JSON.parse(patch) }] |
| 224 | for (const [ops, what] of [ |
| 225 | [withOp('{"states": [{"id": "a"}, {"id": "__proto__"}]}'), 'states'], |
| 226 | [withOp('{"words": {"__proto__": "x"}}'), 'words'], |
| 227 | [withOp('{"data": {"__proto__": 1}}'), 'data'], |
| 228 | [withOp('{"data": [1, {"a": {"b": [{"__proto__": null}]}}]}'), 'data'], |
| 229 | [JSON.parse('[{"op": "set_scene", "id": "worldsceneaa", "states": [{"id": "__proto__"}]}]'), 'states'], |
| 230 | [JSON.parse('[{"op": "set_scene", "id": "worldsceneaa", "words": {"__proto__": "x"}}]'), 'words'], |
| 231 | [JSON.parse('[{"op": "set_scene", "id": "worldsceneaa", "words": {"__proto__": null}}]'), 'words'], |
| 232 | [JSON.parse('[{"op": "set_scene", "id": "worldsceneaa", "data": {"x": {"__proto__": []}}}]'), 'data'] |
| 233 | ]) { |
| 234 | assert.throws(() => applyOps(doc, ops), e => e instanceof OpsError && e.message.includes('__proto__') && e.message.includes(what), JSON.stringify(ops)) |
| 235 | assert.equal(json(doc), before) |
| 236 | } |
| 237 | applyOps(doc, [{ |
| 238 | op: 'set_scene', id: 'worldsceneaa', words: { k: '__proto__' }, data: ['__proto__', { v: '__proto__' }], states: [{ id: 'a', name: '__proto__', description: '__proto__' }] |
| 239 | }]) |
| 240 | }) |
| 241 | |