oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// commands.mjs: the deck's own commands — the bar's title and Deck
2// settings' accent reset — are one set_meta each, through commit.
3import test from 'node:test'
4import assert from 'node:assert/strict'
5import { createCommands } from './commands.mjs'
6
7function commands () {
8 const batches = []
9 const c = createCommands({ commit: ops => { batches.push(ops); return Promise.resolve(true) } })
10 return { c, batches }
11}
12
13test('setTitle commits the deck\'s title, one set_meta', () => {
14 const { c, batches } = commands()
15 c.setTitle('A new talk')
16 assert.deepEqual(batches, [[{ op: 'set_meta', title: 'A new talk' }]])
17})
18
19test('accent-reset commits the theme\'s accent back, once', () => {
20 const { c, batches } = commands()
21 c.run('accent-reset')
22 assert.deepEqual(batches, [[{ op: 'set_meta', accent: null }]])
23})
24
25test('addBlock commits the kind after the selected box, then selects it; refused, selects nothing', async () => {
26 const slide = { id: 'slideaaaaaaa', layout: 'flow', template: 'title-body', blocks: [{ id: 'blockheading', type: 'heading' }] }
27 const batches = []
28 const selected = []
29 let answer = true
30 const c = createCommands({
31 commit: ops => { batches.push(ops); return Promise.resolve(answer) },
32 slideId: () => 'slideaaaaaaa',
33 slideJSON: id => id === 'slideaaaaaaa' ? slide : null,
34 selected: () => 'blockheading',
35 select: id => selected.push(id)
36 })
37 await c.addBlock('shape')
38 assert.equal(batches.length, 1)
39 const [op] = batches[0]
40 assert.equal(op.op, 'add_block')
41 assert.equal(op.slide, 'slideaaaaaaa')
42 assert.equal(op.type, 'shape')
43 assert.equal(op.after, 'blockheading')
44 assert.deepEqual(selected, [op.id])
45
46 answer = false
47 await c.addBlock('text')
48 assert.equal(batches.length, 2)
49 assert.deepEqual(selected, [op.id], 'a refused batch selects nothing')
50})
51
52test('addBlock with no slide on the stage commits nothing', async () => {
53 const batches = []
54 const c = createCommands({ commit: ops => { batches.push(ops); return Promise.resolve(true) }, slideId: () => '', slideJSON: () => null })
55 await c.addBlock('shape')
56 assert.deepEqual(batches, [])
57})
58
59test('the box menu\'s rows into the card: Format and Position and size, for a selected box only', () => {
60 const calls = []
61 const slide = { id: 'slideaaaaaaa', layout: 'canvas', blocks: [{ id: 'blockshape01', type: 'shape' }] }
62 let selected = 'blockshape01'
63 const c = createCommands({
64 commit: () => Promise.resolve(true),
65 slideId: () => slide.id,
66 selected: () => selected,
67 slideJSON: () => slide,
68 blockJSON: (s, id) => slide.blocks.find(b => b.id === id) || null,
69 showFormat: () => calls.push('format'),
70 openPlace: () => calls.push('place')
71 })
72 c.run('block-format')
73 c.run('block-place')
74 assert.deepEqual(calls, ['format', 'place'])
75 selected = ''
76 c.run('block-format')
77 c.run('block-place')
78 assert.deepEqual(calls, ['format', 'place'], 'nothing selected: nothing to show')
79})
80
81// A follow-along code (the qr block) is added with its link already in
82// it: the audience link is asked for first (POST /d/{sqid}/qr-link,
83// editor-entry.js's ctx.qrLink), then the box is one add_block carrying
84// the follow address, so one undo takes back the box and its link.
85// "Make an audience link" in its card points the selected code at a
86// new link, one set_block. With no link to be had (the request refused,
87// or nobody here may share the deck) the box is still added, empty.
88test('addBlock("qr") asks for an audience link and adds the box with it; the card\'s button replaces it', async () => {
89 const url = 'https://slides.example/s/AbCdEfGhIjKlMnOpQrStUvWxYz0123456789_-abcde?follow=1'
90 const slide = { id: 'slideaaaaaaa', layout: 'canvas', template: 'blank', blocks: [] }
91 const batches = []
92 const selected = []
93 let link = url
94 let asked = 0
95 const c = createCommands({
96 commit: ops => { batches.push(ops); return Promise.resolve(true) },
97 slideId: () => 'slideaaaaaaa',
98 slideJSON: id => id === 'slideaaaaaaa' ? slide : null,
99 selected: () => selected[selected.length - 1] || '',
100 select: id => selected.push(id),
101 blockJSON: (s, id) => slide.blocks.find(b => b.id === id) || null,
102 qrLink: () => { asked++; return Promise.resolve(link) }
103 })
104 await c.addBlock('qr')
105 assert.equal(asked, 1)
106 assert.equal(batches.length, 1)
107 const [op] = batches[0]
108 assert.equal(op.op, 'add_block')
109 assert.equal(op.type, 'qr')
110 assert.deepEqual(op.props, { url })
111 assert.deepEqual(selected, [op.id])
112
113 slide.blocks.push({ id: op.id, type: 'qr', props: { url } })
114 link = url.replace('AbCd', 'WxYz')
115 await c.run('qr-link')
116 assert.equal(asked, 2)
117 assert.deepEqual(batches[1], [{ op: 'set_block', slide: 'slideaaaaaaa', id: op.id, props: { url: link } }])
118
119 link = null
120 await c.run('qr-link')
121 assert.equal(batches.length, 2, 'no link, no change')
122 await c.addBlock('qr')
123 assert.equal(batches.length, 3)
124 assert.deepEqual(batches[2][0].props, {}, 'no link: the box is added empty')
125
126 await c.addBlock('text')
127 assert.equal(asked, 4, 'only a follow-along code asks for a link')
128})
129