oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// scene-update.test.mjs — a scene change must fit one live update (the
2// final review's call 3), the twin of core/tests/scene_update.rs. Data
3// within 960 KiB of canonical JSON can still encode past the 1 MiB a
4// posted update may be (internal/slides/live.go maxUpdateBytes): a
5// number takes nine bytes in the update and four in JSON. So a batch
6// carrying add_scene or set_scene is refused, having written nothing,
7// when the update it would make is over 1 MiB.
8import test from 'node:test'
9import assert from 'node:assert/strict'
10import * as Y from 'yjs'
11import { applyOps, view, OpsError, MAX_UPDATE_BYTES } from './ops.mjs'
12import { canonicalJSON } from './scenes.mjs'
13
14const CODE = 'a'.repeat(64)
15
16function deck () {
17 const doc = new Y.Doc({ guid: 'scene-update' })
18 doc.clientID = 1
19 applyOps(doc, [
20 { op: 'add_scene', id: 'worldsceneaa', name: 'World', code: CODE, states: [{ id: 'a' }], data: { n: 1 } },
21 { op: 'add_slide', id: 's1', layout: 'flow' }
22 ])
23 return doc
24}
25
26const tenths = n => new Array(n).fill(0.1)
27
28test('the live update limit is 1 MiB', () => {
29 assert.equal(MAX_UPDATE_BYTES, 1 << 20)
30})
31
32test('150,000 tenths fit the data limit but not one live update, and are refused', () => {
33 const doc = deck()
34 const data = tenths(150000)
35 assert.equal(canonicalJSON(data).length, 600001)
36 const before = JSON.stringify(view(doc))
37 let updates = 0
38 doc.on('update', () => { updates++ })
39 assert.throws(() => applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', data }]),
40 e => e instanceof OpsError && e.tooBig === true && e.index === 0 &&
41 /^op 0: scene "worldsceneaa" would make an update larger than 1 MiB/.test(e.message))
42 assert.equal(JSON.stringify(view(doc)), before)
43 assert.equal(updates, 0)
44 // add_scene too, named by its index in the batch.
45 assert.throws(() => applyOps(doc, [
46 { op: 'add_slide', id: 's2', layout: 'flow' },
47 { op: 'add_scene', id: 'secondscene2', name: 'Second', code: CODE, states: [{ id: 'a' }], data }
48 ]), e => e instanceof OpsError && e.tooBig === true && /^op 1: scene "secondscene2" /.test(e.message))
49 assert.equal(JSON.stringify(view(doc)), before)
50})
51
52test('just under the cap is taken, and its update is what was measured', () => {
53 const doc = deck()
54 let size = 0
55 doc.on('update', u => { size = u.length })
56 applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', data: tenths(116000) }])
57 assert.equal(view(doc).scenes.worldsceneaa.data.length, 116000)
58 assert.ok(size > MAX_UPDATE_BYTES - 8 * 1024 && size <= MAX_UPDATE_BYTES, `the update is ${size} bytes`)
59})
60
61test('two scene changes in one batch are measured together', () => {
62 const doc = deck()
63 applyOps(doc, [{ op: 'add_scene', id: 'secondscene2', name: 'Second', code: CODE, states: [{ id: 'a' }] }])
64 assert.throws(() => applyOps(doc, [
65 { op: 'set_scene', id: 'worldsceneaa', data: tenths(70000) },
66 { op: 'set_scene', id: 'secondscene2', data: tenths(70000) }
67 ]), e => e instanceof OpsError && e.tooBig === true && /^op 0: /.test(e.message))
68 applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', data: tenths(70000) }])
69 applyOps(doc, [{ op: 'set_scene', id: 'secondscene2', data: tenths(70000) }])
70})
71
72test('a batch with no scene op is not measured, and other refusals are not tooBig', () => {
73 const doc = deck()
74 applyOps(doc, [{ op: 'add_block', slide: 's1', id: 'b1', type: 'text', region: 'body', markdown: 'x'.repeat(1100000) }])
75 assert.throws(() => applyOps(doc, [{ op: 'set_scene', id: 'nope', name: 'x' }]), e => e instanceof OpsError && !e.tooBig)
76})
77
78// The exact boundary, on a deck whose own client has a long history (a
79// clock past 16,384, so it is three bytes in the update): an update of
80// exactly 1 MiB is taken and one of a byte more refused. The measure is
81// the update this document itself would make, so the copy it is made on
82// must write as this client, at this client's clock.
83test('exactly 1 MiB is taken and one byte more is refused', () => {
84 const long = () => {
85 const doc = deck()
86 applyOps(doc, [{ op: 'add_block', slide: 's1', id: 'b1', type: 'text', region: 'body', markdown: 'y'.repeat(20000) }])
87 return doc
88 }
89 const data = s => ['x'.repeat(s), ...tenths(20000)]
90 const sizeOf = s => {
91 const doc = long()
92 let size = 0
93 doc.on('update', u => { size = u.length })
94 applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', data: data(s) }])
95 return size
96 }
97 const probe = 800000
98 const at = probe + (MAX_UPDATE_BYTES - sizeOf(probe))
99 assert.equal(sizeOf(at), MAX_UPDATE_BYTES)
100 const doc = long()
101 let emitted = 0
102 doc.on('update', () => { emitted++ })
103 assert.throws(() => applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', data: data(at + 1) }]),
104 e => e instanceof OpsError && e.tooBig === true)
105 assert.equal(emitted, 0)
106})
107