| 1 | // scene-stills.test.mjs: stillsCurrent (scenes and clips spec §4, plan task 5), |
| 2 | // which says whether a scene's stills match the scene as it is now. |
| 3 | import test from 'node:test' |
| 4 | import assert from 'node:assert/strict' |
| 5 | import * as Y from 'yjs' |
| 6 | import { applyOps, view } from './ops.mjs' |
| 7 | import { sceneVersion, stillsCurrent } from './scenes.mjs' |
| 8 | |
| 9 | const STILL = '1'.repeat(64) |
| 10 | const CODE = 'a'.repeat(64) |
| 11 | |
| 12 | function deck () { |
| 13 | const doc = new Y.Doc({ guid: 'scenes' }) |
| 14 | doc.clientID = 1 |
| 15 | applyOps(doc, [{ |
| 16 | op: 'add_scene', |
| 17 | id: 'worldsceneaa', |
| 18 | name: 'The world', |
| 19 | code: CODE, |
| 20 | states: [{ id: 'a' }, { id: 'b' }, { id: 'c' }], |
| 21 | words: { title: 'Vienna' }, |
| 22 | data: { n: 3 } |
| 23 | }]) |
| 24 | return doc |
| 25 | } |
| 26 | |
| 27 | test('stillsCurrent: missing, stale and current', () => { |
| 28 | const doc = deck() |
| 29 | const states = ['a', 'b', 'c'] |
| 30 | let r = stillsCurrent(view(doc).scenes.worldsceneaa) |
| 31 | assert.deepEqual(r, { current: false, missing: states, stale: [] }) |
| 32 | |
| 33 | const made = sceneVersion(view(doc).scenes.worldsceneaa) |
| 34 | const stills = Object.fromEntries(states.map(s => [s, { ref: STILL, made }])) |
| 35 | applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', stills }]) |
| 36 | assert.deepEqual(stillsCurrent(view(doc).scenes.worldsceneaa), { current: true, missing: [], stale: [] }) |
| 37 | |
| 38 | // A hand edit of the words, the data or the code makes every still stale. |
| 39 | applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', words: { title: 'Wien' } }]) |
| 40 | assert.deepEqual(stillsCurrent(view(doc).scenes.worldsceneaa), { current: false, missing: [], stale: states }) |
| 41 | |
| 42 | // A still made again is current again; a state added later is missing. |
| 43 | const now = sceneVersion(view(doc).scenes.worldsceneaa) |
| 44 | applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', stills: { a: { ref: STILL, made: now } } }]) |
| 45 | assert.deepEqual(stillsCurrent(view(doc).scenes.worldsceneaa), { current: false, missing: [], stale: ['b', 'c'] }) |
| 46 | applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', stills: { b: { ref: STILL, made: now }, c: { ref: STILL, made: now } } }]) |
| 47 | assert.equal(stillsCurrent(view(doc).scenes.worldsceneaa).current, true) |
| 48 | applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', states: [{ id: 'a' }, { id: 'b' }, { id: 'c' }, { id: 'd' }] }]) |
| 49 | assert.deepEqual(stillsCurrent(view(doc).scenes.worldsceneaa), { current: false, missing: ['d'], stale: [] }) |
| 50 | // A still for a state the scene no longer has is not counted. |
| 51 | applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', states: [{ id: 'a' }] }]) |
| 52 | assert.equal(stillsCurrent(view(doc).scenes.worldsceneaa).current, true) |
| 53 | }) |
| 54 | |
| 55 | test('stillsCurrent tolerates a scene with no states or stills', () => { |
| 56 | assert.deepEqual(stillsCurrent({}), { current: true, missing: [], stale: [] }) |
| 57 | assert.deepEqual(stillsCurrent(undefined), { current: true, missing: [], stale: [] }) |
| 58 | }) |
| 59 | |
| 60 | test('a still whose asset is no longer registered is missing', () => { |
| 61 | const doc = deck() |
| 62 | const made = sceneVersion(view(doc).scenes.worldsceneaa) |
| 63 | applyOps(doc, [ |
| 64 | { op: 'register_asset', id: 'stillasset01', ref: STILL, name: 'a.png', width: 1, height: 1, alt: '' }, |
| 65 | { op: 'set_scene', id: 'worldsceneaa', stills: { a: { ref: STILL, made }, b: { ref: STILL, made }, c: { ref: '2'.repeat(64), made } } } |
| 66 | ]) |
| 67 | const v = view(doc) |
| 68 | assert.deepEqual(stillsCurrent(v.scenes.worldsceneaa, v.assets), { current: false, missing: ['c'], stale: [] }) |
| 69 | applyOps(doc, [{ op: 'remove_asset', id: 'stillasset01' }]) |
| 70 | const w = view(doc) |
| 71 | assert.deepEqual(stillsCurrent(w.scenes.worldsceneaa, w.assets || {}), { current: false, missing: ['a', 'b', 'c'], stale: [] }) |
| 72 | }) |
| 73 | |