| 1 | // scene-blocks.test.mjs — the scene and clip block types (scenes and |
| 2 | // clips spec §2, plan task 2), the blocks' half of core/tests/scenes.rs, |
| 3 | // split from scenes.test.mjs: a scene block names a scene and two of its |
| 4 | // states in order, a removed scene or state leaves it as it was, a clip |
| 5 | // plays on a press, in a loop or once, and a duplicated slide's scene |
| 6 | // block is still checked. |
| 7 | import test from 'node:test' |
| 8 | import assert from 'node:assert/strict' |
| 9 | import * as Y from 'yjs' |
| 10 | import { applyOps, view, OpsError } from './ops.mjs' |
| 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: '0123456789abcdef'.repeat(4), |
| 20 | states: [{ id: 'a' }, { id: 'b' }, { id: 'c' }] |
| 21 | }, { op: 'add_slide', id: 's1', layout: 'flow' }]) |
| 22 | return doc |
| 23 | } |
| 24 | |
| 25 | const sceneBlock = (id, props) => ({ op: 'add_block', slide: 's1', id, type: 'scene', props }) |
| 26 | const json = doc => JSON.stringify(view(doc)) |
| 27 | |
| 28 | test('a scene block names a scene and two of its states', () => { |
| 29 | const doc = deck() |
| 30 | applyOps(doc, [sceneBlock('sb', { scene: 'worldsceneaa', from: 'a', to: 'c' })]) |
| 31 | applyOps(doc, [sceneBlock('eq', { scene: 'worldsceneaa', from: 'b', to: 'b' })]) |
| 32 | applyOps(doc, [sceneBlock('empty', {})]) |
| 33 | const b = view(doc).slides[0].blocks[0] |
| 34 | assert.equal(b.type, 'scene') |
| 35 | assert.deepEqual(b.props, { scene: 'worldsceneaa', from: 'a', to: 'c' }) |
| 36 | const before = json(doc) |
| 37 | for (const props of [ |
| 38 | { scene: 'nope', from: 'a', to: 'c' }, |
| 39 | { scene: 'worldsceneaa', from: 'a', to: 'zz' }, |
| 40 | { scene: 'worldsceneaa', from: 'zz', to: 'a' }, |
| 41 | { scene: 'worldsceneaa', from: 'c', to: 'a' }, |
| 42 | { scene: 5 }, |
| 43 | { scene: 'worldsceneaa', from: 1 }, |
| 44 | { from: 'a', to: 'b' } |
| 45 | ]) { |
| 46 | assert.throws(() => applyOps(doc, [sceneBlock('x', props)]), OpsError, JSON.stringify(props)) |
| 47 | assert.equal(json(doc), before) |
| 48 | } |
| 49 | for (const props of [{ to: 'zz' }, { from: 'b', to: 'a' }, { scene: 'nope' }]) { |
| 50 | assert.throws(() => applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'sb', props }]), OpsError, JSON.stringify(props)) |
| 51 | assert.equal(json(doc), before) |
| 52 | } |
| 53 | applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'sb', props: { from: 'b' } }]) |
| 54 | }) |
| 55 | |
| 56 | test('a removed scene or state leaves the block as it was', () => { |
| 57 | const doc = deck() |
| 58 | applyOps(doc, [sceneBlock('sb', { scene: 'worldsceneaa', from: 'a', to: 'c' }), { op: 'set_scene', id: 'worldsceneaa', states: [{ id: 'a' }, { id: 'b' }] }]) |
| 59 | applyOps(doc, [{ op: 'remove_scene', id: 'worldsceneaa' }]) |
| 60 | const v = view(doc) |
| 61 | assert.equal('scenes' in v, false) |
| 62 | assert.deepEqual(v.slides[0].blocks[0].props, { scene: 'worldsceneaa', from: 'a', to: 'c' }) |
| 63 | applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'sb', step: 2, props: { alt: 'x' } }]) |
| 64 | const d2 = deck() |
| 65 | assert.throws(() => applyOps(d2, [{ op: 'remove_scene', id: 'worldsceneaa' }, sceneBlock('sb', { scene: 'worldsceneaa', from: 'a', to: 'b' })]), OpsError) |
| 66 | }) |
| 67 | |
| 68 | test('a clip block plays on a press, in a loop or once', () => { |
| 69 | const doc = deck() |
| 70 | for (const [id, play] of [['c1', 'press'], ['c2', 'loop'], ['c3', 'once']]) { |
| 71 | applyOps(doc, [{ op: 'add_block', slide: 's1', id, type: 'clip', props: { asset: 'v1', play } }]) |
| 72 | } |
| 73 | const before = json(doc) |
| 74 | for (const op of [ |
| 75 | { op: 'add_block', slide: 's1', id: 'c4', type: 'clip', props: { asset: 'v1', play: 'bounce' } }, |
| 76 | { op: 'add_block', slide: 's1', id: 'c4', type: 'clip', props: { asset: 3 } }, |
| 77 | { op: 'set_block', slide: 's1', id: 'c1', props: { play: 'forever' } } |
| 78 | ]) { |
| 79 | assert.throws(() => applyOps(doc, [op]), OpsError, JSON.stringify(op)) |
| 80 | assert.equal(json(doc), before) |
| 81 | } |
| 82 | applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'c1', props: { play: 'loop' } }]) |
| 83 | assert.equal(view(doc).slides[0].blocks[0].props.play, 'loop') |
| 84 | }) |
| 85 | |
| 86 | test('duplicate_slide keeps a scene block checkable', () => { |
| 87 | const doc = deck() |
| 88 | applyOps(doc, [ |
| 89 | sceneBlock('sb', { scene: 'worldsceneaa', from: 'a', to: 'b' }), |
| 90 | { op: 'duplicate_slide', id: 's1', new_id: 's2', new_block_ids: ['sb2'] }, |
| 91 | { op: 'set_block', slide: 's2', id: 'sb2', props: { to: 'c' } } |
| 92 | ]) |
| 93 | assert.throws(() => applyOps(doc, [{ op: 'set_block', slide: 's2', id: 'sb2', props: { from: 'zz' } }]), OpsError) |
| 94 | }) |
| 95 | |