oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// view.mjs over documents no op can produce — the twin of the tests in
2// core/src/view.rs. The server reads the view through typed structs, so
3// a malformed leaf passed through untyped fails the parse of the whole
4// deck; each fixed-shape leaf is rebuilt from the fields that have the
5// right type and omitted otherwise. Both sides must agree, because the
6// vectors compare them.
7import test from 'node:test'
8import assert from 'node:assert/strict'
9import * as Y from 'yjs'
10import { readdirSync, readFileSync } from 'node:fs'
11import { view } from './view.mjs'
12import { poisonDoc, poisonUpdates, shapes } from './poison.mjs'
13
14test('malformed fonts are null', () => {
15 assert.equal(view(poisonDoc('fonts')).meta.fonts, null)
16})
17
18test('a malformed fontFiles list is absent; a good one is rebuilt from known fields', () => {
19 const ref = '0123456789abcdef'.repeat(4)
20 const face = weight => ({ ref, format: 'ttf', weight, italic: false, pictures: true })
21 const read = value => {
22 const doc = new Y.Doc({ guid: 'fontfiles' })
23 doc.clientID = 1
24 doc.getMap('meta').set('fontFiles', value)
25 return view(doc).meta
26 }
27 assert.deepEqual(read([{ family: 'F', css: 'F', faces: [face([400, 700])], extra: 1 }]).fontFiles,
28 [{ family: 'F', css: 'F', faces: [face([400, 700])] }])
29 for (const bad of [
30 [{ family: 'F', css: 'F', faces: [face([700, 400])] }],
31 [{ family: 'F', css: 'F', faces: [face(['a', 1])] }],
32 'nope',
33 { family: 'F' }
34 ]) assert.equal('fontFiles' in read(bad), false, JSON.stringify(bad))
35})
36
37test('a malformed background is the theme default', () => {
38 assert.deepEqual(view(poisonDoc('background')).slides[0].background, { kind: 'theme', value: '' })
39})
40
41test('a malformed box is null', () => {
42 assert.equal(view(poisonDoc('box')).slides[0].blocks[0].box, null)
43})
44
45test('the phone layer keeps only its typed fields, order as a whole number', () => {
46 const doc = new Y.Doc({ guid: 'phone' })
47 doc.clientID = 1
48 const slide = new Y.Map()
49 doc.getArray('slides').push([slide])
50 const blocks = new Y.Array()
51 slide.set('blocks', blocks)
52 const block = new Y.Map()
53 blocks.push([block])
54 const phone = new Y.Map()
55 block.set('phone', phone)
56 phone.set('hidden', 'yes')
57 phone.set('order', 2.7)
58 phone.set('size', 7)
59 phone.set('box', { x: 0, y: 0, w: 1, h: 0.5 })
60 assert.deepEqual(view(doc).slides[0].blocks[0].phone, { order: 2, box: { x: 0, y: 0, w: 1, h: 0.5 } })
61})
62
63test('assets are typed or skipped', () => {
64 assert.deepEqual(view(poisonDoc('assets')).assets, {
65 poisonbadnm: { ref: 'poisonref', name: '', width: 0, height: 9, alt: '' }
66 })
67})
68
69test('every poison update is deterministic bytes that apply to any deck', () => {
70 const first = poisonUpdates()
71 const second = poisonUpdates()
72 for (const name of Object.keys(shapes)) {
73 assert.deepEqual(Buffer.from(first[name]), Buffer.from(second[name]), name)
74 const doc = new Y.Doc({ guid: 'anydeck' })
75 Y.applyUpdate(doc, first[name])
76 view(doc) // must not throw
77 }
78})
79
80test('step, items, talk and arrive outside their vocabularies are absent', () => {
81 const doc = new Y.Doc({ guid: 'steps' })
82 doc.clientID = 1
83 doc.getMap('meta').set('arrive', 'zoom')
84 const slide = new Y.Map()
85 doc.getArray('slides').push([slide])
86 slide.set('id', 'sld')
87 slide.set('talk', 'maybe')
88 const blocks = new Y.Array()
89 slide.set('blocks', blocks)
90 for (const [id, step, items] of [['b1', '2', 5], ['b2', -1, 'bogus'], ['b3', 0, true], ['b4', 0.25, 'each']]) {
91 const block = new Y.Map()
92 blocks.push([block])
93 block.set('id', id)
94 block.set('type', 'list')
95 block.set('step', step)
96 block.set('items', items)
97 }
98 const v = view(doc)
99 assert.equal(Object.hasOwn(v.meta, 'arrive'), false)
100 assert.equal(Object.hasOwn(v.slides[0], 'talk'), false)
101 const got = v.slides[0].blocks.map(b => [b.step, b.items])
102 assert.deepEqual(got, [[undefined, undefined], [undefined, undefined], [undefined, undefined], [0.25, 'each']])
103})
104
105test('scenes keep only their typed fields; a scene that is not a map is skipped', () => {
106 const code = '0123456789abcdef'.repeat(4)
107 const doc = new Y.Doc({ guid: 'scenes' })
108 doc.clientID = 1
109 const scenes = doc.getMap('scenes')
110 scenes.set('str', 'not a map')
111 const m = new Y.Map()
112 scenes.set('w', m)
113 m.set('name', 3)
114 m.set('code', code)
115 m.set('author', ['x'])
116 m.set('states', [{ id: 'a', name: 5 }, 'b', { name: 'no id' }, { id: 'a', name: 'twice' }, { id: 'c', description: 'C' }])
117 const words = new Y.Map()
118 m.set('words', words)
119 words.set('k', 'v')
120 words.set('n', 4)
121 m.set('data', { deep: [1, { x: 'y' }] })
122 m.set('stills', { a: { ref: code, made: code }, b: { ref: 3, made: code }, c: 'x' })
123 assert.deepEqual(view(doc).scenes, {
124 w: {
125 name: '',
126 author: '',
127 code,
128 states: [{ id: 'a', name: '', description: '' }, { id: 'c', name: '', description: 'C' }],
129 words: { k: 'v' },
130 data: { deep: [1, { x: 'y' }] },
131 stills: { a: { ref: code, made: code } }
132 }
133 })
134})
135
136test('a key named __proto__ is an ordinary key everywhere in the view', () => {
137 const code = '0123456789abcdef'.repeat(4)
138 const doc = new Y.Doc({ guid: 'proto' })
139 doc.clientID = 1
140 const scene = new Y.Map()
141 doc.getMap('scenes').set('__proto__', scene)
142 scene.set('code', code)
143 scene.set('states', [{ id: '__proto__' }])
144 const words = new Y.Map()
145 scene.set('words', words)
146 words.set('__proto__', 'w')
147 scene.set('data', JSON.parse('{"__proto__": {"deep": [1]}}'))
148 const stills = new Y.Map()
149 scene.set('stills', stills)
150 stills.set('__proto__', { ref: code, made: code })
151 doc.getMap('assets').set('__proto__', { ref: 'r' })
152 const v = view(doc)
153 const own = (o, k) => Object.hasOwn(o, k) && Object.getPrototypeOf(o) === Object.prototype
154 assert.ok(own(v.scenes, '__proto__'), 'scene id')
155 const s = v.scenes.__proto__
156 assert.ok(s && own(s.words, '__proto__') && own(s.stills, '__proto__') && own(s.data, '__proto__'), JSON.stringify(s))
157 assert.deepEqual(JSON.parse(JSON.stringify(v.scenes)), JSON.parse(`{"__proto__": {"name": "", "author": "", "code": "${code}",
158 "states": [{"id": "__proto__", "name": "", "description": ""}], "words": {"__proto__": "w"},
159 "data": {"__proto__": {"deep": [1]}}, "stills": {"__proto__": {"ref": "${code}", "made": "${code}"}}}}`))
160 assert.ok(own(v.assets, '__proto__'), 'asset id')
161})
162
163test('every vector decoded from its bytes views as its committed view', () => {
164 // What a browser does: decode the update (lib0's readAny), then view.
165 for (const name of readdirSync(new URL('./vectors/', import.meta.url)).filter(f => f.endsWith('.yjs'))) {
166 const doc = new Y.Doc()
167 Y.applyUpdate(doc, readFileSync(new URL(`./vectors/${name}`, import.meta.url)))
168 const want = JSON.parse(readFileSync(new URL(`./vectors/${name.replace(/\.yjs$/, '.view.json')}`, import.meta.url)))
169 assert.deepEqual(JSON.parse(JSON.stringify(view(doc))), want, name)
170 }
171})
172