// scene-update.test.mjs — a scene change must fit one live update (the // final review's call 3), the twin of core/tests/scene_update.rs. Data // within 960 KiB of canonical JSON can still encode past the 1 MiB a // posted update may be (internal/slides/live.go maxUpdateBytes): a // number takes nine bytes in the update and four in JSON. So a batch // carrying add_scene or set_scene is refused, having written nothing, // when the update it would make is over 1 MiB. import test from 'node:test' import assert from 'node:assert/strict' import * as Y from 'yjs' import { applyOps, view, OpsError, MAX_UPDATE_BYTES } from './ops.mjs' import { canonicalJSON } from './scenes.mjs' const CODE = 'a'.repeat(64) function deck () { const doc = new Y.Doc({ guid: 'scene-update' }) doc.clientID = 1 applyOps(doc, [ { op: 'add_scene', id: 'worldsceneaa', name: 'World', code: CODE, states: [{ id: 'a' }], data: { n: 1 } }, { op: 'add_slide', id: 's1', layout: 'flow' } ]) return doc } const tenths = n => new Array(n).fill(0.1) test('the live update limit is 1 MiB', () => { assert.equal(MAX_UPDATE_BYTES, 1 << 20) }) test('150,000 tenths fit the data limit but not one live update, and are refused', () => { const doc = deck() const data = tenths(150000) assert.equal(canonicalJSON(data).length, 600001) const before = JSON.stringify(view(doc)) let updates = 0 doc.on('update', () => { updates++ }) assert.throws(() => applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', data }]), e => e instanceof OpsError && e.tooBig === true && e.index === 0 && /^op 0: scene "worldsceneaa" would make an update larger than 1 MiB/.test(e.message)) assert.equal(JSON.stringify(view(doc)), before) assert.equal(updates, 0) // add_scene too, named by its index in the batch. assert.throws(() => applyOps(doc, [ { op: 'add_slide', id: 's2', layout: 'flow' }, { op: 'add_scene', id: 'secondscene2', name: 'Second', code: CODE, states: [{ id: 'a' }], data } ]), e => e instanceof OpsError && e.tooBig === true && /^op 1: scene "secondscene2" /.test(e.message)) assert.equal(JSON.stringify(view(doc)), before) }) test('just under the cap is taken, and its update is what was measured', () => { const doc = deck() let size = 0 doc.on('update', u => { size = u.length }) applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', data: tenths(116000) }]) assert.equal(view(doc).scenes.worldsceneaa.data.length, 116000) assert.ok(size > MAX_UPDATE_BYTES - 8 * 1024 && size <= MAX_UPDATE_BYTES, `the update is ${size} bytes`) }) test('two scene changes in one batch are measured together', () => { const doc = deck() applyOps(doc, [{ op: 'add_scene', id: 'secondscene2', name: 'Second', code: CODE, states: [{ id: 'a' }] }]) assert.throws(() => applyOps(doc, [ { op: 'set_scene', id: 'worldsceneaa', data: tenths(70000) }, { op: 'set_scene', id: 'secondscene2', data: tenths(70000) } ]), e => e instanceof OpsError && e.tooBig === true && /^op 0: /.test(e.message)) applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', data: tenths(70000) }]) applyOps(doc, [{ op: 'set_scene', id: 'secondscene2', data: tenths(70000) }]) }) test('a batch with no scene op is not measured, and other refusals are not tooBig', () => { const doc = deck() applyOps(doc, [{ op: 'add_block', slide: 's1', id: 'b1', type: 'text', region: 'body', markdown: 'x'.repeat(1100000) }]) assert.throws(() => applyOps(doc, [{ op: 'set_scene', id: 'nope', name: 'x' }]), e => e instanceof OpsError && !e.tooBig) }) // The exact boundary, on a deck whose own client has a long history (a // clock past 16,384, so it is three bytes in the update): an update of // exactly 1 MiB is taken and one of a byte more refused. The measure is // the update this document itself would make, so the copy it is made on // must write as this client, at this client's clock. test('exactly 1 MiB is taken and one byte more is refused', () => { const long = () => { const doc = deck() applyOps(doc, [{ op: 'add_block', slide: 's1', id: 'b1', type: 'text', region: 'body', markdown: 'y'.repeat(20000) }]) return doc } const data = s => ['x'.repeat(s), ...tenths(20000)] const sizeOf = s => { const doc = long() let size = 0 doc.on('update', u => { size = u.length }) applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', data: data(s) }]) return size } const probe = 800000 const at = probe + (MAX_UPDATE_BYTES - sizeOf(probe)) assert.equal(sizeOf(at), MAX_UPDATE_BYTES) const doc = long() let emitted = 0 doc.on('update', () => { emitted++ }) assert.throws(() => applyOps(doc, [{ op: 'set_scene', id: 'worldsceneaa', data: data(at + 1) }]), e => e instanceof OpsError && e.tooBig === true) assert.equal(emitted, 0) })