// scene-blocks.test.mjs — the scene and clip block types (scenes and // clips spec §2, plan task 2), the blocks' half of core/tests/scenes.rs, // split from scenes.test.mjs: a scene block names a scene and two of its // states in order, a removed scene or state leaves it as it was, a clip // plays on a press, in a loop or once, and a duplicated slide's scene // block is still checked. import test from 'node:test' import assert from 'node:assert/strict' import * as Y from 'yjs' import { applyOps, view, OpsError } from './ops.mjs' function deck () { const doc = new Y.Doc({ guid: 'scenes' }) doc.clientID = 1 applyOps(doc, [{ op: 'add_scene', id: 'worldsceneaa', name: 'The world', code: '0123456789abcdef'.repeat(4), states: [{ id: 'a' }, { id: 'b' }, { id: 'c' }] }, { op: 'add_slide', id: 's1', layout: 'flow' }]) return doc } const sceneBlock = (id, props) => ({ op: 'add_block', slide: 's1', id, type: 'scene', props }) const json = doc => JSON.stringify(view(doc)) test('a scene block names a scene and two of its states', () => { const doc = deck() applyOps(doc, [sceneBlock('sb', { scene: 'worldsceneaa', from: 'a', to: 'c' })]) applyOps(doc, [sceneBlock('eq', { scene: 'worldsceneaa', from: 'b', to: 'b' })]) applyOps(doc, [sceneBlock('empty', {})]) const b = view(doc).slides[0].blocks[0] assert.equal(b.type, 'scene') assert.deepEqual(b.props, { scene: 'worldsceneaa', from: 'a', to: 'c' }) const before = json(doc) for (const props of [ { scene: 'nope', from: 'a', to: 'c' }, { scene: 'worldsceneaa', from: 'a', to: 'zz' }, { scene: 'worldsceneaa', from: 'zz', to: 'a' }, { scene: 'worldsceneaa', from: 'c', to: 'a' }, { scene: 5 }, { scene: 'worldsceneaa', from: 1 }, { from: 'a', to: 'b' } ]) { assert.throws(() => applyOps(doc, [sceneBlock('x', props)]), OpsError, JSON.stringify(props)) assert.equal(json(doc), before) } for (const props of [{ to: 'zz' }, { from: 'b', to: 'a' }, { scene: 'nope' }]) { assert.throws(() => applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'sb', props }]), OpsError, JSON.stringify(props)) assert.equal(json(doc), before) } applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'sb', props: { from: 'b' } }]) }) test('a removed scene or state leaves the block as it was', () => { const doc = deck() applyOps(doc, [sceneBlock('sb', { scene: 'worldsceneaa', from: 'a', to: 'c' }), { op: 'set_scene', id: 'worldsceneaa', states: [{ id: 'a' }, { id: 'b' }] }]) applyOps(doc, [{ op: 'remove_scene', id: 'worldsceneaa' }]) const v = view(doc) assert.equal('scenes' in v, false) assert.deepEqual(v.slides[0].blocks[0].props, { scene: 'worldsceneaa', from: 'a', to: 'c' }) applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'sb', step: 2, props: { alt: 'x' } }]) const d2 = deck() assert.throws(() => applyOps(d2, [{ op: 'remove_scene', id: 'worldsceneaa' }, sceneBlock('sb', { scene: 'worldsceneaa', from: 'a', to: 'b' })]), OpsError) }) test('a clip block plays on a press, in a loop or once', () => { const doc = deck() for (const [id, play] of [['c1', 'press'], ['c2', 'loop'], ['c3', 'once']]) { applyOps(doc, [{ op: 'add_block', slide: 's1', id, type: 'clip', props: { asset: 'v1', play } }]) } const before = json(doc) for (const op of [ { op: 'add_block', slide: 's1', id: 'c4', type: 'clip', props: { asset: 'v1', play: 'bounce' } }, { op: 'add_block', slide: 's1', id: 'c4', type: 'clip', props: { asset: 3 } }, { op: 'set_block', slide: 's1', id: 'c1', props: { play: 'forever' } } ]) { assert.throws(() => applyOps(doc, [op]), OpsError, JSON.stringify(op)) assert.equal(json(doc), before) } applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'c1', props: { play: 'loop' } }]) assert.equal(view(doc).slides[0].blocks[0].props.play, 'loop') }) test('duplicate_slide keeps a scene block checkable', () => { const doc = deck() applyOps(doc, [ sceneBlock('sb', { scene: 'worldsceneaa', from: 'a', to: 'b' }), { op: 'duplicate_slide', id: 's1', new_id: 's2', new_block_ids: ['sb2'] }, { op: 'set_block', slide: 's2', id: 'sb2', props: { to: 'c' } } ]) assert.throws(() => applyOps(doc, [{ op: 'set_block', slide: 's2', id: 'sb2', props: { from: 'zz' } }]), OpsError) })