oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1// The op vocabulary over a Y.Doc (ops.mjs), one test per op family, plus
2// the properties the core guarantees: a refused batch applies nothing,
3// columns.children is validated, a layout switch round-trips, and the
4// committed vectors are what generate.mjs would write today.
5import test from 'node:test'
6import assert from 'node:assert/strict'
7import { readFileSync, readdirSync } from 'node:fs'
8import { join, dirname } from 'node:path'
9import { fileURLToPath } from 'node:url'
10import * as Y from 'yjs'
11import { applyOps, view, OpsError } from './ops.mjs'
12import { buildVector, sortedJSON } from './generate.mjs'
13
14const here = dirname(fileURLToPath(import.meta.url))
15
16function fresh () {
17 const doc = new Y.Doc({ guid: 'test' })
18 doc.clientID = 1
19 return doc
20}
21
22function withSlide (layout = 'flow') {
23 const doc = fresh()
24 applyOps(doc, [{ op: 'add_slide', id: 's1', layout, template: 'title-body' }])
25 return doc
26}
27
28const heading = (id, md, extra = {}) => ({
29 op: 'add_block', slide: 's1', id, type: 'heading', props: { level: 1 }, region: 'heading', markdown: md, ...extra
30})
31const text = (id, md, extra = {}) => ({
32 op: 'add_block', slide: 's1', id, type: 'text', props: {}, region: 'body', markdown: md, ...extra
33})
34
35test('empty_doc_views_as_defaults', () => {
36 assert.deepEqual(view(fresh()), {
37 meta: { title: '', theme: 'paper', accent: null, fonts: null, aspect: '16:9' },
38 slides: [],
39 assets: {}
40 })
41})
42
43test('set_meta', () => {
44 const doc = fresh()
45 applyOps(doc, [{ op: 'set_meta', title: 'T', theme: 'ink', accent: '#f00', fonts: { heading: 'A', body: 'B' } }])
46 assert.deepEqual(view(doc).meta, { title: 'T', theme: 'ink', accent: '#f00', fonts: { heading: 'A', body: 'B' }, aspect: '16:9' })
47 applyOps(doc, [{ op: 'set_meta', accent: null, fonts: null }])
48 assert.deepEqual(view(doc).meta, { title: 'T', theme: 'ink', accent: null, fonts: null, aspect: '16:9' })
49 assert.throws(() => applyOps(doc, [{ op: 'set_meta', title: 5 }]), OpsError)
50 assert.throws(() => applyOps(doc, [{ op: 'set_meta', fonts: { heading: 'A' } }]), OpsError)
51})
52
53test('slides: add, move, remove, set_slide, set_notes', () => {
54 const doc = fresh()
55 applyOps(doc, [
56 { op: 'add_slide', id: 's1', layout: 'flow', template: 'title' },
57 { op: 'add_slide', id: 's2', layout: 'flow' },
58 { op: 'add_slide', id: 's0', layout: 'canvas', after: null },
59 { op: 'add_slide', id: 's1b', layout: 'flow', after: 's1' }
60 ])
61 let v = view(doc)
62 assert.deepEqual(v.slides.map(s => s.id), ['s0', 's1', 's1b', 's2'])
63 assert.deepEqual(v.slides[1], {
64 id: 's1', layout: 'flow', template: 'title', background: { kind: 'theme', value: '' },
65 phone: 'inherit', transition: 'none', notes: [], blocks: []
66 })
67 assert.equal(v.slides[3].template, 'blank')
68
69 applyOps(doc, [{ op: 'move_slide', id: 's2', after: null }, { op: 'move_slide', id: 's0', after: 's1b' }])
70 assert.deepEqual(view(doc).slides.map(s => s.id), ['s2', 's1', 's1b', 's0'])
71
72 applyOps(doc, [
73 { op: 'remove_slide', id: 's1b' },
74 { op: 'set_slide', id: 's1', template: 'quote', background: { kind: 'color', value: '#123' }, phone: 'reflow', transition: 'fade' },
75 { op: 'set_notes', id: 's1', markdown: 'Say **hello**\n\n- one\n- two' }
76 ])
77 v = view(doc)
78 assert.deepEqual(v.slides.map(s => s.id), ['s2', 's1', 's0'])
79 const s1 = v.slides[1]
80 assert.equal(s1.template, 'quote')
81 assert.deepEqual(s1.background, { kind: 'color', value: '#123' })
82 assert.equal(s1.phone, 'reflow')
83 assert.equal(s1.transition, 'fade')
84 assert.deepEqual(s1.notes, [[{ t: 'Say ' }, { t: 'hello', b: true }], [{ t: 'one' }], [{ t: 'two' }]])
85
86 assert.throws(() => applyOps(doc, [{ op: 'add_slide', id: 's1', layout: 'flow' }]), /already exists/)
87 assert.throws(() => applyOps(doc, [{ op: 'add_slide', id: 'x', layout: 'grid' }]), /layout "grid" is not one of/)
88 assert.throws(() => applyOps(doc, [{ op: 'move_slide', id: 's1', after: 's1' }]), /after is the slide itself/)
89 assert.throws(() => applyOps(doc, [{ op: 'set_slide', id: 'nope' }]), /no slide "nope"/)
90})
91
92test('duplicate_slide', () => {
93 const doc = withSlide()
94 applyOps(doc, [
95 heading('h1', 'Hi'),
96 text('t1', 'a'),
97 text('t2', 'b'),
98 { op: 'add_block', slide: 's1', id: 'c1', type: 'columns', props: { count: 2, children: ['t1', 't2'] } },
99 { op: 'duplicate_slide', id: 's1', new_id: 's2', new_block_ids: ['h2', 't3', 't4', 'c2'] }
100 ])
101 const v = view(doc)
102 assert.deepEqual(v.slides.map(s => s.id), ['s1', 's2'])
103 const copy = v.slides[1]
104 assert.deepEqual(copy.blocks.map(b => b.id), ['h2', 't3', 't4', 'c2'])
105 assert.deepEqual(copy.blocks[3].props, { count: 2, children: ['t3', 't4'] })
106 assert.deepEqual(copy.blocks[0].text, v.slides[0].blocks[0].text)
107 assert.throws(() => applyOps(doc, [{ op: 'duplicate_slide', id: 's1', new_id: 's3', new_block_ids: ['a'] }]), /4 blocks but 1 new_block_ids/)
108 assert.throws(() => applyOps(doc, [{ op: 'duplicate_slide', id: 's1', new_id: 's3', new_block_ids: ['a', 'a', 'b', 'c'] }]), /distinct and non-empty/)
109})
110
111test('blocks: add, move, remove, set_block', () => {
112 const doc = withSlide()
113 applyOps(doc, [
114 heading('h1', 'Hello **world**'),
115 text('t1', 'one'),
116 text('t0', 'zero', { after: null }),
117 text('t1b', 'x', { after: 't1' }),
118 { op: 'add_block', slide: 's1', id: 'img', type: 'image', props: { asset: 'a1', alt: '' } },
119 { op: 'add_block', slide: 's1', id: 'div', type: 'divider' }
120 ])
121 let v = view(doc)
122 assert.deepEqual(v.slides[0].blocks.map(b => b.id), ['t0', 'h1', 't1', 't1b', 'img', 'div'])
123 assert.deepEqual(v.slides[0].blocks[1], {
124 id: 'h1', type: 'heading', props: { level: 1 }, region: 'heading', box: null, z: 0, phone: {},
125 text: [[{ t: 'Hello ' }, { t: 'world', b: true }]]
126 })
127 assert.equal(v.slides[0].blocks[4].region, 'media')
128 assert.equal('text' in v.slides[0].blocks[4], false)
129 assert.equal(v.slides[0].blocks[5].region, 'body')
130
131 applyOps(doc, [
132 { op: 'move_block', slide: 's1', id: 'div', after: null },
133 { op: 'move_block', slide: 's1', id: 't0', after: 'img' },
134 { op: 'remove_block', slide: 's1', id: 't1b' },
135 { op: 'set_block', slide: 's1', id: 'h1', props: { level: 2, extra: 'y' }, region: 'body' },
136 { op: 'set_block', slide: 's1', id: 'h1', props: { extra: null } },
137 { op: 'set_text', slide: 's1', id: 'h1', markdown: '*new*' }
138 ])
139 v = view(doc)
140 assert.deepEqual(v.slides[0].blocks.map(b => b.id), ['div', 'h1', 't1', 'img', 't0'])
141 assert.deepEqual(v.slides[0].blocks[1].props, { level: 2 })
142 assert.equal(v.slides[0].blocks[1].region, 'body')
143 assert.deepEqual(v.slides[0].blocks[1].text, [[{ t: 'new', i: true }]])
144
145 assert.throws(() => applyOps(doc, [{ op: 'add_block', slide: 's1', id: 'h1', type: 'text' }]), /already exists/)
146 assert.throws(() => applyOps(doc, [{ op: 'add_block', slide: 's1', id: 'n', type: 'video' }]), /type "video" is not one of/)
147 assert.throws(() => applyOps(doc, [{ op: 'set_text', slide: 's1', id: 'img', markdown: 'x' }]), /is a image and carries no text/)
148 assert.throws(() => applyOps(doc, [{ op: 'move_block', slide: 's1', id: 'h1', after: 'h1' }]), /after is the block itself/)
149 assert.throws(() => applyOps(doc, [{ op: 'remove_block', slide: 's1', id: 'zz' }]), /no block "zz" in slide "s1"/)
150})
151
152test('set_phone', () => {
153 const doc = withSlide()
154 applyOps(doc, [text('t1', 'x'), { op: 'set_phone', slide: 's1', id: 't1', phone: { hidden: true, order: 2, size: 's', box: { x: 0, y: 0.5, w: 1, h: 0.25 } } }])
155 assert.deepEqual(view(doc).slides[0].blocks[0].phone, { hidden: true, order: 2, size: 's', box: { x: 0, y: 0.5, w: 1, h: 0.25 } })
156 applyOps(doc, [{ op: 'set_phone', slide: 's1', id: 't1', phone: { size: 'l' } }])
157 assert.deepEqual(view(doc).slides[0].blocks[0].phone, { size: 'l' })
158 assert.throws(() => applyOps(doc, [{ op: 'set_phone', slide: 's1', id: 't1', phone: { size: 'xl' } }]), /phone.size "xl" is not one of/)
159 assert.throws(() => applyOps(doc, [{ op: 'set_phone', slide: 's1', id: 't1', phone: { colour: 'red' } }]), OpsError)
160 assert.throws(() => applyOps(doc, [{ op: 'set_phone', slide: 's1', id: 't1', phone: { order: 1.5 } }]), OpsError)
161})
162
163test('table cells follow rows and cols', () => {
164 const doc = withSlide()
165 applyOps(doc, [{ op: 'add_block', slide: 's1', id: 'tb', type: 'table', props: { header: true } }])
166 let b = view(doc).slides[0].blocks[0]
167 assert.deepEqual(b.props, { header: true, rows: 2, cols: 2, cells: [[], [], [], []] })
168 assert.equal('text' in b, false)
169 applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'tb', props: { rows: 3 } }])
170 b = view(doc).slides[0].blocks[0]
171 assert.equal(b.props.cells.length, 6)
172 applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'tb', props: { rows: 1, cols: 1 } }])
173 assert.equal(view(doc).slides[0].blocks[0].props.cells.length, 1)
174 assert.throws(() => applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'tb', props: { rows: 0 } }]), /table rows must be a whole number from 1 to 100/)
175 assert.throws(() => applyOps(doc, [{ op: 'add_block', slide: 's1', id: 't2', type: 'table', props: { cols: 101 } }]), /table cols/)
176})
177
178test('canvas blocks: box defaults from the region, z from the index', () => {
179 const doc = withSlide('canvas')
180 applyOps(doc, [
181 heading('h1', 'x'),
182 { op: 'add_block', slide: 's1', id: 't1', type: 'text', box: { x: 0.1, y: 0.2, w: 0.3, h: 0.44444 }, z: 7 },
183 { op: 'set_block', slide: 's1', id: 'h1', box: { x: 0.5, y: 0.5, w: 0.1, h: 0.1 }, z: 3 }
184 ])
185 const v = view(doc)
186 assert.deepEqual(v.slides[0].blocks[0], {
187 id: 'h1', type: 'heading', props: { level: 1 }, region: null, box: { x: 0.5, y: 0.5, w: 0.1, h: 0.1 }, z: 3, phone: {}, text: [[{ t: 'x' }]]
188 })
189 assert.deepEqual(v.slides[0].blocks[1].box, { x: 0.1, y: 0.2, w: 0.3, h: 0.4444 })
190 assert.equal(v.slides[0].blocks[1].z, 7)
191 applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'h1', box: null }])
192 assert.equal(view(doc).slides[0].blocks[0].box, null)
193})
194
195test('assets: register and remove', () => {
196 const doc = fresh()
197 applyOps(doc, [{ op: 'register_asset', id: 'a1', ref: 'blob/1', name: 'pic.png', width: 10, height: 20, alt: 'A' }, { op: 'register_asset', id: 'a2', ref: 'blob/2' }])
198 assert.deepEqual(view(doc).assets, {
199 a1: { ref: 'blob/1', name: 'pic.png', width: 10, height: 20, alt: 'A' },
200 a2: { ref: 'blob/2', name: '', width: 0, height: 0, alt: '' }
201 })
202 applyOps(doc, [{ op: 'remove_asset', id: 'a1' }])
203 assert.deepEqual(Object.keys(view(doc).assets), ['a2'])
204 assert.throws(() => applyOps(doc, [{ op: 'register_asset', id: 'a2', ref: 'x' }]), /already registered/)
205 assert.throws(() => applyOps(doc, [{ op: 'remove_asset', id: 'a1' }]), /no asset "a1"/)
206 assert.throws(() => applyOps(doc, [{ op: 'register_asset', id: 'a3', ref: '' }]), /ref is empty/)
207})
208
209test('assets: a video carries its poster and duration; a picture has neither', () => {
210 const doc = fresh()
211 applyOps(doc, [
212 { op: 'register_asset', id: 'v1', ref: 'videoref', name: 'clip.webm', width: 1280, height: 720, alt: 'A screen recording', poster: 'posterref', duration: 4250 },
213 { op: 'register_asset', id: 'p1', ref: 'posterref', name: 'clip.png', width: 1280, height: 720 },
214 { op: 'register_asset', id: 'p2', ref: 'plain', poster: '', duration: 0 }
215 ])
216 assert.deepEqual(view(doc).assets, {
217 v1: { ref: 'videoref', name: 'clip.webm', width: 1280, height: 720, alt: 'A screen recording', poster: 'posterref', duration: 4250 },
218 p1: { ref: 'posterref', name: 'clip.png', width: 1280, height: 720, alt: '' },
219 p2: { ref: 'plain', name: '', width: 0, height: 0, alt: '' }
220 })
221 for (const bad of [{ duration: -1 }, { duration: 1.5 }, { poster: 3 }]) {
222 assert.throws(() => applyOps(doc, [{ op: 'register_asset', id: 'v2', ref: 'r', ...bad }]), OpsError, JSON.stringify(bad))
223 }
224})
225
226test('markdown: every mark, code blocks are plain lines', () => {
227 const doc = withSlide()
228 applyOps(doc, [
229 text('t1', '**b** *i* `c *x*` ==h== [t](u) \\* lone* **open'),
230 text('t2', '**a *b* c** [**x**](y)'),
231 { op: 'add_block', slide: 's1', id: 'c1', type: 'code', props: { language: 'go' }, markdown: 'x\n\n**y**' }
232 ])
233 const [t1, t2, c1] = view(doc).slides[0].blocks
234 assert.deepEqual(t1.text, [[
235 { t: 'b', b: true }, { t: ' ' }, { t: 'i', i: true }, { t: ' ' }, { t: 'c *x*', c: true }, { t: ' ' },
236 { t: 'h', h: true }, { t: ' ' }, { t: 't', href: 'u' }, { t: ' * lone* **open' }
237 ]])
238 assert.deepEqual(t2.text, [[{ t: 'a ', b: true }, { t: 'b', b: true, i: true }, { t: ' c', b: true }, { t: ' ' }, { t: 'x', b: true, href: 'y' }]])
239 assert.deepEqual(c1.text, [[{ t: 'x' }], [], [{ t: '**y**' }]])
240})
241
242test('batch_is_atomic', () => {
243 const doc = withSlide()
244 const before = Y.encodeStateAsUpdate(doc)
245 let origins = 0
246 doc.on('afterTransaction', () => { origins++ })
247 let err = null
248 try {
249 applyOps(doc, [
250 heading('h1', 'x'),
251 { op: 'add_slide', id: 's2', layout: 'flow' },
252 { op: 'remove_block', slide: 's1', id: 'missing' }
253 ])
254 } catch (e) { err = e }
255 assert.ok(err instanceof OpsError)
256 assert.equal(err.index, 2)
257 assert.match(err.message, /^op 2: no block "missing" in slide "s1"/)
258 assert.deepEqual(Y.encodeStateAsUpdate(doc), before)
259 assert.equal(origins, 0)
260 assert.throws(() => applyOps(doc, [{ op: 'sing' }]), /op 0:/)
261 assert.throws(() => applyOps(doc, 'nope'), /ops must be a JSON array/)
262})
263
264test('a batch applies in one transaction with the given origin', () => {
265 const doc = withSlide()
266 const seen = []
267 doc.on('afterTransaction', tr => { seen.push(tr.origin) })
268 applyOps(doc, [heading('h1', 'x'), text('t1', 'y')], 'inspector')
269 assert.deepEqual(seen, ['inspector'])
270})
271
272test('columns_children_validated', () => {
273 const doc = withSlide()
274 applyOps(doc, [text('t1', 'a'), text('t2', 'b')])
275 assert.throws(() => applyOps(doc, [{ op: 'add_block', slide: 's1', id: 'c1', type: 'columns', props: { children: ['t1', 'zz'] } }]), /names "zz", not a block on slide "s1"/)
276 assert.throws(() => applyOps(doc, [{ op: 'add_block', slide: 's1', id: 'c1', type: 'columns', props: { children: ['c1'] } }]), /cannot name itself/)
277 assert.throws(() => applyOps(doc, [{ op: 'add_block', slide: 's1', id: 'c1', type: 'columns', props: { children: 't1' } }]), /must be an array of block ids/)
278 applyOps(doc, [{ op: 'add_block', slide: 's1', id: 'c1', type: 'columns', props: { count: 2, children: ['t1', 't2'] } }])
279 assert.throws(() => applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'c1', props: { children: ['t1', 't9'] } }]), /names "t9"/)
280 // Removing a child prunes it from the columns block.
281 applyOps(doc, [{ op: 'remove_block', slide: 's1', id: 't1' }])
282 assert.deepEqual(view(doc).slides[0].blocks.find(b => b.id === 'c1').props.children, ['t2'])
283 // The model follows the batch: a block removed earlier in the same batch is gone.
284 assert.throws(() => applyOps(doc, [
285 { op: 'remove_block', slide: 's1', id: 't2' },
286 { op: 'set_block', slide: 's1', id: 'c1', props: { children: ['t2'] } }
287 ]), /op 1: columns children names "t2"/)
288 applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'c1', props: { children: null } }])
289 assert.deepEqual(view(doc).slides[0].blocks.find(b => b.id === 'c1').props, { count: 2 })
290})
291
292test('layout_switch_round_trip', () => {
293 const doc = withSlide()
294 applyOps(doc, [heading('h1', 'x'), text('t1', 'a'), text('t2', 'b'), { op: 'add_block', slide: 's1', id: 'as', type: 'text', region: 'aside' }])
295 applyOps(doc, [{ op: 'set_slide', id: 's1', layout: 'canvas' }])
296 let blocks = view(doc).slides[0].blocks
297 assert.deepEqual(blocks.map(b => [b.id, b.region, b.box, b.z]), [
298 ['h1', null, { x: 0.06, y: 0.08, w: 0.88, h: 0.18 }, 0],
299 ['t1', null, { x: 0.06, y: 0.3, w: 0.44, h: 0.31 }, 1],
300 ['t2', null, { x: 0.06, y: 0.61, w: 0.44, h: 0.31 }, 2],
301 ['as', null, { x: 0.54, y: 0.3, w: 0.4, h: 0.62 }, 3]
302 ])
303 // Back to flow: regions from position, order top to bottom then left to right.
304 applyOps(doc, [{ op: 'set_block', slide: 's1', id: 't2', box: { x: 0.5, y: 0.05, w: 0.2, h: 0.2 } }])
305 applyOps(doc, [{ op: 'set_slide', id: 's1', layout: 'flow' }])
306 blocks = view(doc).slides[0].blocks
307 assert.deepEqual(blocks.map(b => [b.id, b.region, b.box, b.z]), [
308 ['t2', 'body', null, 0],
309 ['h1', 'heading', null, 0],
310 ['t1', 'body', null, 0],
311 ['as', 'body', null, 0]
312 ])
313 // Setting the same layout again is a no-op.
314 const before = view(doc)
315 applyOps(doc, [{ op: 'set_slide', id: 's1', layout: 'flow' }])
316 assert.deepEqual(view(doc), before)
317})
318
319test('view_matches_snapshot_fixtures', () => {
320 const dataDir = join(here, 'data')
321 const names = readdirSync(dataDir).filter(f => f.endsWith('.json')).map(f => f.slice(0, -5))
322 assert.ok(names.length >= 5, 'five vectors at least')
323 const ops = new Set()
324 for (const name of names) {
325 const data = JSON.parse(readFileSync(join(dataDir, `${name}.json`), 'utf8'))
326 data.ops.forEach(o => ops.add(o.op))
327 const { update, view: v } = buildVector(name, data)
328 const wantView = readFileSync(join(here, 'vectors', `${name}.view.json`), 'utf8')
329 assert.equal(sortedJSON(v), wantView, `${name}.view.json is stale: run node generate.mjs`)
330 const wantUpdate = readFileSync(join(here, 'vectors', `${name}.yjs`))
331 assert.ok(Buffer.from(update).equals(wantUpdate), `${name}.yjs is stale: run node generate.mjs`)
332 }
333 const every = ['set_meta', 'add_slide', 'remove_slide', 'move_slide', 'duplicate_slide', 'set_slide', 'set_notes',
334 'add_block', 'remove_block', 'move_block', 'set_block', 'set_text', 'set_phone', 'register_asset', 'remove_asset']
335 assert.deepEqual(every.filter(o => !ops.has(o)), [], 'every op appears in some vector')
336})
337
338test('columns children have one parent and no cycles (codex 7)', () => {
339 const doc = withSlide()
340 applyOps(doc, [
341 text('t1', 'a'), text('t2', 'b'),
342 { op: 'add_block', slide: 's1', id: 'a', type: 'columns', props: { children: ['t1'] } },
343 { op: 'add_block', slide: 's1', id: 'b', type: 'columns', props: { children: ['a'] } },
344 { op: 'add_block', slide: 's1', id: 'c', type: 'columns', props: { children: [] } }
345 ])
346 const before = view(doc)
347 // A → B, B → A: the cycle codex reproduced, with the core's message.
348 assert.throws(() => applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'a', props: { children: ['t1', 'b'] } }]),
349 /op 0: columns children names "b", which would put columns "a" inside itself$/)
350 // A shared child.
351 assert.throws(() => applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'c', props: { children: ['t1'] } }]),
352 /op 0: columns children names "t1", already a child of columns "a"$/)
353 // The same child twice.
354 assert.throws(() => applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'c', props: { children: ['t2', 't2'] } }]),
355 /op 0: columns children names "t2" twice$/)
356 assert.deepEqual(view(doc), before, 'nothing applied')
357 // A longer cycle: c → b → a → c.
358 applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'c', props: { children: ['b'] } }])
359 assert.throws(() => applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'a', props: { children: ['t1', 'c'] } }]), /inside itself/)
360 // Reparenting inside one batch is fine: the model follows the batch.
361 applyOps(doc, [
362 { op: 'set_block', slide: 's1', id: 'a', props: { children: [] } },
363 { op: 'set_block', slide: 's1', id: 'c', props: { children: ['b', 't1'] } }
364 ])
365 assert.deepEqual(view(doc).slides[0].blocks.find(b => b.id === 'c').props.children, ['b', 't1'])
366})
367
368/** Two replicas of one document, and the exchange of what each did since. */
369function replicas () {
370 const a = fresh()
371 const b = new Y.Doc({ guid: 'test' })
372 b.clientID = 2
373 const sync = () => {
374 const fromA = Y.encodeStateAsUpdate(a, Y.encodeStateVector(b))
375 const fromB = Y.encodeStateAsUpdate(b, Y.encodeStateVector(a))
376 Y.applyUpdate(a, fromB)
377 Y.applyUpdate(b, fromA)
378 }
379 return { a, b, sync }
380}
381
382test('canvas-to-flow keeps a concurrent text edit (codex 4)', () => {
383 const { a, b, sync } = replicas()
384 applyOps(a, [
385 { op: 'add_slide', id: 's1', layout: 'canvas' },
386 { op: 'add_block', slide: 's1', id: 'low', type: 'text', markdown: 'below', box: { x: 0.1, y: 0.6, w: 0.3, h: 0.2 } },
387 { op: 'add_block', slide: 's1', id: 'high', type: 'text', markdown: 'above', box: { x: 0.1, y: 0.1, w: 0.3, h: 0.2 } }
388 ])
389 sync()
390 // A converts, which reorders: "high" reads first now.
391 applyOps(a, [{ op: 'set_slide', id: 's1', layout: 'flow' }])
392 // B, concurrently, types into "low" the way a keystroke through
393 // y-prosemirror does: straight into the shared XmlText.
394 const low = b.getArray('slides').get(0).get('blocks').toArray().find(m => m.get('id') === 'low')
395 const t = low.get('text').get(0).get(0)
396 t.insert(t.length, ' and more')
397 sync()
398 for (const doc of [a, b]) {
399 const blocks = view(doc).slides[0].blocks
400 assert.deepEqual(blocks.map(x => x.id), ['high', 'low'])
401 assert.deepEqual(blocks[1].text, [[{ t: 'below and more' }]])
402 assert.equal(blocks[1].region, 'body')
403 }
404 assert.deepEqual(view(a), view(b))
405 // The array was never rebuilt: the maps are the ones the ops made.
406 assert.equal(a.getArray('slides').get(0).get('blocks').toArray()[0].get('id'), 'low')
407})
408
409test('move_block keeps a concurrent text edit, and later inserts land where after says (codex 4)', () => {
410 const { a, b, sync } = replicas()
411 applyOps(a, [
412 { op: 'add_slide', id: 's1', layout: 'flow' },
413 text('one', 'one'), text('two', 'two'), text('three', 'three')
414 ])
415 sync()
416 applyOps(a, [{ op: 'move_block', slide: 's1', id: 'three' }])
417 applyOps(b, [{ op: 'set_text', slide: 's1', id: 'three', markdown: 'three, edited' }])
418 sync()
419 for (const doc of [a, b]) {
420 const blocks = view(doc).slides[0].blocks
421 assert.deepEqual(blocks.map(x => x.id), ['three', 'one', 'two'])
422 assert.deepEqual(blocks[0].text, [[{ t: 'three, edited' }]])
423 }
424 applyOps(a, [
425 text('four', 'four', { after: 'three' }),
426 text('five', 'five'),
427 text('zero', 'zero', { after: null })
428 ])
429 assert.deepEqual(view(a).slides[0].blocks.map(x => x.id), ['zero', 'three', 'four', 'one', 'two', 'five'])
430 // A duplicate pairs new ids with blocks in reading order.
431 applyOps(a, [{ op: 'duplicate_slide', id: 's1', new_id: 's2', new_block_ids: ['z', 't', 'f', 'o', 'w', 'v'] }])
432 assert.deepEqual(view(a).slides[1].blocks.map(x => [x.id, x.text[0][0].t]),
433 [['z', 'zero'], ['t', 'three, edited'], ['f', 'four'], ['o', 'one'], ['w', 'two'], ['v', 'five']])
434})
435
436test('move_slide keeps a concurrent text edit, and later inserts land where after says (codex 4 at slide level)', () => {
437 const { a, b, sync } = replicas()
438 applyOps(a, [
439 { op: 'add_slide', id: 's1', layout: 'flow' },
440 { op: 'add_slide', id: 's2', layout: 'flow' },
441 { op: 'add_slide', id: 's3', layout: 'flow' },
442 { op: 'add_block', slide: 's3', id: 't3', type: 'text', region: 'body', markdown: 'three' }
443 ])
444 sync()
445 // A reorders the deck; B, who has not seen that, types into the
446 // slide being moved and into its notes.
447 applyOps(a, [{ op: 'move_slide', id: 's3' }])
448 applyOps(b, [
449 { op: 'set_text', slide: 's3', id: 't3', markdown: 'three, edited' },
450 { op: 'set_notes', id: 's3', markdown: 'say this' }
451 ])
452 sync()
453 for (const doc of [a, b]) {
454 const slides = view(doc).slides
455 assert.deepEqual(slides.map(s => s.id), ['s3', 's1', 's2'])
456 assert.deepEqual(slides[0].blocks[0].text, [[{ t: 'three, edited' }]])
457 assert.deepEqual(slides[0].notes, [[{ t: 'say this' }]])
458 // The order key is the document's, not the view's.
459 assert.equal(Object.hasOwn(slides[0], 'order'), false)
460 }
461 applyOps(a, [
462 { op: 'add_slide', id: 's4', layout: 'flow', after: 's3' },
463 { op: 'add_slide', id: 's5', layout: 'flow' },
464 { op: 'add_slide', id: 's0', layout: 'flow', after: null },
465 { op: 'duplicate_slide', id: 's3', new_id: 's3b', new_block_ids: ['t3b'] }
466 ])
467 assert.deepEqual(view(a).slides.map(s => s.id), ['s0', 's3', 's3b', 's4', 's1', 's2', 's5'])
468 applyOps(a, [{ op: 'remove_slide', id: 's3' }])
469 assert.deepEqual(view(a).slides.map(s => s.id), ['s0', 's3b', 's4', 's1', 's2', 's5'])
470})
471
472// --- steps, items, talk and arrive (spec 2026-11-18 §2); core/tests/ops.rs has the same cases ---
473
474function stepsDeck () {
475 const doc = withSlide()
476 applyOps(doc, [text('t1', 'Words'), { op: 'add_block', slide: 's1', id: 'l1', type: 'list', props: {}, region: 'body', markdown: '- One\n- Two' }])
477 return doc
478}
479
480test('set_block step round-trips, fractions included, survives a duplicate and clears', () => {
481 const doc = stepsDeck()
482 applyOps(doc, [
483 { op: 'set_block', slide: 's1', id: 't1', step: 1.5 },
484 { op: 'set_block', slide: 's1', id: 'l1', step: 2 },
485 { op: 'duplicate_slide', id: 's1', new_id: 's2', new_block_ids: ['t2', 'l2'] }
486 ])
487 for (const slide of view(doc).slides) assert.deepEqual(slide.blocks.map(b => b.step), [1.5, 2])
488 applyOps(doc, [{ op: 'set_block', slide: 's1', id: 't1', step: null }])
489 const [first, copy] = view(doc).slides
490 assert.equal(Object.hasOwn(first.blocks[0], 'step'), false)
491 assert.equal(first.blocks[1].step, 2)
492 assert.equal(copy.blocks[0].step, 1.5)
493})
494
495test('set_block refuses a step of zero, below zero, or not finite', () => {
496 const doc = stepsDeck()
497 const before = view(doc)
498 for (const step of [0, -1, -0.5, NaN, Infinity, -Infinity]) {
499 assert.throws(() => applyOps(doc, [{ op: 'set_block', slide: 's1', id: 't1', step }]), /step must be a number above 0/, String(step))
500 }
501 assert.throws(() => applyOps(doc, [{ op: 'set_block', slide: 's1', id: 't1', step: '1' }]), OpsError)
502 assert.deepEqual(view(doc), before)
503})
504
505test('set_block refuses items on a non-list; on a list it round-trips and clears', () => {
506 const doc = stepsDeck()
507 const before = view(doc)
508 assert.throws(() => applyOps(doc, [{ op: 'set_block', slide: 's1', id: 't1', items: 'each' }]), /block "t1" is a text; items is for lists only/)
509 assert.throws(() => applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'l1', items: 'some' }]), /items "some" is not one of together, each/)
510 assert.deepEqual(view(doc), before)
511 applyOps(doc, [
512 { op: 'set_block', slide: 's1', id: 'l1', items: 'each' },
513 { op: 'duplicate_slide', id: 's1', new_id: 's2', new_block_ids: ['t2', 'l2'] }
514 ])
515 assert.deepEqual(view(doc).slides.map(s => s.blocks[1].items), ['each', 'each'])
516 applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'l1', items: 'together' }])
517 assert.equal(view(doc).slides[0].blocks[1].items, 'together')
518 applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'l1', items: null }])
519 assert.equal(Object.hasOwn(view(doc).slides[0].blocks[1], 'items'), false)
520})
521
522test('set_slide talk round-trips, survives a duplicate and clears; an unknown one is refused', () => {
523 const doc = stepsDeck()
524 applyOps(doc, [{ op: 'set_slide', id: 's1', talk: 'parked' }])
525 assert.equal(view(doc).slides[0].talk, 'parked')
526 applyOps(doc, [
527 { op: 'set_slide', id: 's1', talk: 'full' },
528 { op: 'duplicate_slide', id: 's1', new_id: 's2', new_block_ids: ['t2', 'l2'] }
529 ])
530 assert.deepEqual(view(doc).slides.map(s => s.talk), ['full', 'full'])
531 applyOps(doc, [{ op: 'set_slide', id: 's1', talk: null }])
532 assert.equal(Object.hasOwn(view(doc).slides[0], 'talk'), false)
533 const before = view(doc)
534 assert.throws(() => applyOps(doc, [{ op: 'set_slide', id: 's1', talk: 'core' }]), /talk "core" is not one of full, parked/)
535 assert.deepEqual(view(doc), before)
536})
537
538test('set_meta arrive round-trips and clears; an unknown one is refused', () => {
539 const doc = stepsDeck()
540 for (const arrive of ['appear', 'fade', 'rise']) {
541 applyOps(doc, [{ op: 'set_meta', arrive }])
542 assert.equal(view(doc).meta.arrive, arrive)
543 }
544 assert.throws(() => applyOps(doc, [{ op: 'set_meta', arrive: 'zoom' }]), /arrive "zoom" is not one of appear, fade, rise/)
545 applyOps(doc, [{ op: 'set_meta', arrive: null }])
546 assert.equal(Object.hasOwn(view(doc).meta, 'arrive'), false)
547})
548
549test('a deck without the fields has no such keys in its view', () => {
550 const v = view(stepsDeck())
551 assert.equal(Object.hasOwn(v.meta, 'arrive'), false)
552 for (const s of v.slides) {
553 assert.equal(Object.hasOwn(s, 'talk'), false)
554 for (const b of s.blocks) assert.equal(Object.hasOwn(b, 'step') || Object.hasOwn(b, 'items'), false)
555 }
556})
557
558const FONT_REF = '0123456789abcdef'.repeat(4)
559const fontFamily = css => ({
560 family: 'Fira Sans',
561 css,
562 faces: [
563 { ref: FONT_REF, format: 'woff2', weight: [400, 700], italic: false, pictures: true },
564 { ref: FONT_REF, format: 'ttf', weight: [400, 400], italic: true, pictures: false }
565 ]
566})
567
568test('meta.fontFiles: round trip, absent key, clear', () => {
569 const doc = fresh()
570 applyOps(doc, [{ op: 'set_meta', title: 'T' }])
571 assert.equal('fontFiles' in view(doc).meta, false)
572 const list = [fontFamily('Fira Sans'), fontFamily('Fira Mono')]
573 applyOps(doc, [{ op: 'set_meta', fontFiles: list }])
574 assert.deepEqual(view(doc).meta.fontFiles, list)
575 applyOps(doc, [{ op: 'set_meta', title: 'U' }])
576 assert.deepEqual(view(doc).meta.fontFiles, list)
577 applyOps(doc, [{ op: 'set_meta', fontFiles: null }])
578 assert.equal('fontFiles' in view(doc).meta, false)
579})
580
581test('meta.fontFiles: refusals apply nothing', () => {
582 const good = fontFamily('Fira Sans')
583 const face = () => ({ ...good.faces[0] })
584 const one = patch => [{ ...good, ...patch }]
585 const withFace = patch => one({ faces: [{ ...face(), ...patch }] })
586 const bad = [
587 'x',
588 [good, good],
589 Array.from({ length: 33 }, (_, i) => fontFamily(`F${i}`)),
590 one({ faces: Array.from({ length: 17 }, face) }),
591 one({ css: 'a;b' }), one({ css: '' }), one({ css: 'x'.repeat(201) }),
592 // One alias pattern with the library and layout (final review, I5).
593 one({ css: '_Brand' }), one({ css: ' Brand' }), one({ css: 'a,b' }), one({ css: "a'b" }),
594 // A name is one name without regard to case, and never one a stack
595 // already means something by (final review, I3).
596 [fontFamily('Brand'), fontFamily('brand')],
597 one({ css: 'Liberation Sans' }), one({ css: 'liberation mono' }), one({ css: 'SANS-SERIF' }), one({ css: 'system-ui' }),
598 one({ family: 'x'.repeat(201) }), one({ family: 5 }),
599 withFace({ ref: 'ABCDEF' }), withFace({ ref: FONT_REF.toUpperCase() }),
600 withFace({ format: 'eot' }),
601 withFace({ weight: [0, 400] }), withFace({ weight: [400, 1001] }), withFace({ weight: [700, 400] }),
602 withFace({ weight: [400.5, 600] }), withFace({ weight: [400] }),
603 withFace({ italic: 'no' }), withFace({ pictures: null }),
604 [{ family: 'F', css: 'F' }]
605 ]
606 const doc = fresh()
607 const before = JSON.stringify(view(doc))
608 for (const b of bad) {
609 assert.throws(() => applyOps(doc, [{ op: 'set_meta', fontFiles: b }]), OpsError, JSON.stringify(b))
610 assert.equal(JSON.stringify(view(doc)), before)
611 }
612 const edge = Array.from({ length: 32 }, (_, i) => fontFamily(`F${i}`))
613 applyOps(doc, [{ op: 'set_meta', fontFiles: edge }])
614 const f = { ...fontFamily('x'.repeat(200)), family: 'é'.repeat(200), faces: Array.from({ length: 16 }, face) }
615 applyOps(doc, [{ op: 'set_meta', fontFiles: [f] }])
616})
617
618// A follow-along code (the qr block) stores the follow address of the
619// audience link it was made with; the core's qr_url_must_be_a_follow_link
620// holds the same shape, message for message.
621test('a qr block takes a follow link as its url, or none, and refuses anything else', () => {
622 const doc = withSlide()
623 const good = 'https://slides.example/s/AbCdEfGhIjKlMnOpQrStUvWxYz0123456789_-abcde?follow=1'
624 applyOps(doc, [
625 { op: 'add_block', slide: 's1', id: 'q1', type: 'qr', props: { url: good } },
626 { op: 'add_block', slide: 's1', id: 'q2', type: 'qr' }
627 ])
628 let v = view(doc)
629 assert.equal(v.slides[0].blocks[0].type, 'qr')
630 assert.equal(v.slides[0].blocks[0].props.url, good)
631 assert.deepEqual(v.slides[0].blocks[1].props, {})
632 applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'q2', props: { url: 'http://127.0.0.1:8091/s/0123456789abcdefghij?follow=1' } }])
633 applyOps(doc, [{ op: 'set_block', slide: 's1', id: 'q2', props: { url: null } }])
634 v = view(doc)
635 assert.equal(v.slides[0].blocks[1].props.url, undefined)
636
637 const before = view(doc)
638 for (const bad of [
639 'https://slides.example/s/AbCdEfGhIjKlMnOpQrSt',
640 'https://slides.example/s/AbCdEfGhIjKlMnOpQrSt?follow=0',
641 'https://slides.example/s/AbCdEfGhIjKlMnOpQrSt?follow=1&x=1',
642 'https://slides.example/d/AbCdEfGhIjKlMnOpQrSt?follow=1',
643 'https://slides.example/s/short?follow=1',
644 'https://slides.example/s/AbCdEf/GhIjKlMnOpQrSt?follow=1',
645 'https://slides.example/s/AbCdEfGh%20IjKlMnOpQrSt?follow=1',
646 'javascript://slides.example/s/AbCdEfGhIjKlMnOpQrSt?follow=1',
647 'https://user@slides.example/s/AbCdEfGhIjKlMnOpQrSt?follow=1',
648 'https:///s/AbCdEfGhIjKlMnOpQrSt?follow=1',
649 '', 5, true
650 ]) {
651 for (const op of [
652 { op: 'set_block', slide: 's1', id: 'q1', props: { url: bad } },
653 { op: 'add_block', slide: 's1', id: 'q9', type: 'qr', props: { url: bad } }
654 ]) {
655 assert.throws(() => applyOps(doc, [op]), /qr url must be a follow link: http\(s\):\/\/<host>\/s\/<token>\?follow=1/, JSON.stringify(bad))
656 }
657 }
658 assert.deepEqual(view(doc), before)
659})
660