// scene-stills.test.mjs: stillsCurrent (scenes and clips spec ยง4, plan task 5), // which says whether a scene's stills match the scene as it is now. import test from 'node:test' import assert from 'node:assert/strict' import * as Y from 'yjs' import { applyOps, view } from './ops.mjs' import { sceneVersion, stillsCurrent } from './scenes.mjs' const STILL = '1'.repeat(64) const CODE = 'a'.repeat(64) function deck () { const doc = new Y.Doc({ guid: 'scenes' }) doc.clientID = 1 applyOps(doc, [{ op: 'add_scene', id: 'worldsceneaa', name: 'The world', code: CODE, states: [{ id: 'a' }, { id: 'b' }, { id: 'c' }], words: { title: 'Vienna' }, data: { n: 3 } }]) return doc } test('stillsCurrent: missing, stale and current', () => { const doc = deck() const states = ['a', 'b', 'c'] let r = stillsCurrent(view(doc).scenes.worldsceneaa) assert.deepEqual(r, { current: false, missing: states, stale: [] }) const made = sceneVersion(view(doc).scenes.worldsceneaa) const stills = Object.fromEntries(states.map(s => [s, { ref: STILL, made }])) applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', stills }]) assert.deepEqual(stillsCurrent(view(doc).scenes.worldsceneaa), { current: true, missing: [], stale: [] }) // A hand edit of the words, the data or the code makes every still stale. applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', words: { title: 'Wien' } }]) assert.deepEqual(stillsCurrent(view(doc).scenes.worldsceneaa), { current: false, missing: [], stale: states }) // A still made again is current again; a state added later is missing. const now = sceneVersion(view(doc).scenes.worldsceneaa) applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', stills: { a: { ref: STILL, made: now } } }]) assert.deepEqual(stillsCurrent(view(doc).scenes.worldsceneaa), { current: false, missing: [], stale: ['b', 'c'] }) applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', stills: { b: { ref: STILL, made: now }, c: { ref: STILL, made: now } } }]) assert.equal(stillsCurrent(view(doc).scenes.worldsceneaa).current, true) applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', states: [{ id: 'a' }, { id: 'b' }, { id: 'c' }, { id: 'd' }] }]) assert.deepEqual(stillsCurrent(view(doc).scenes.worldsceneaa), { current: false, missing: ['d'], stale: [] }) // A still for a state the scene no longer has is not counted. applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', states: [{ id: 'a' }] }]) assert.equal(stillsCurrent(view(doc).scenes.worldsceneaa).current, true) }) test('stillsCurrent tolerates a scene with no states or stills', () => { assert.deepEqual(stillsCurrent({}), { current: true, missing: [], stale: [] }) assert.deepEqual(stillsCurrent(undefined), { current: true, missing: [], stale: [] }) }) test('a still whose asset is no longer registered is missing', () => { const doc = deck() const made = sceneVersion(view(doc).scenes.worldsceneaa) applyOps(doc, [ { op: 'register_asset', id: 'stillasset01', ref: STILL, name: 'a.png', width: 1, height: 1, alt: '' }, { op: 'set_scene', id: 'worldsceneaa', stills: { a: { ref: STILL, made }, b: { ref: STILL, made }, c: { ref: '2'.repeat(64), made } } } ]) const v = view(doc) assert.deepEqual(stillsCurrent(v.scenes.worldsceneaa, v.assets), { current: false, missing: ['c'], stale: [] }) applyOps(doc, [{ op: 'remove_asset', id: 'stillasset01' }]) const w = view(doc) assert.deepEqual(stillsCurrent(w.scenes.worldsceneaa, w.assets || {}), { current: false, missing: ['a', 'b', 'c'], stale: [] }) })