oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// inspector-steps.test.mjs — how a box appears (spec §4, In the editor):
2// the three choices as ops, After… as a midpoint that renumbers nothing,
3// and the press numbers drawn beside the boxes.
4import test from 'node:test'
5import assert from 'node:assert/strict'
6import { appearsOf, appearsOps, afterChoices, afterOps } from './inspector-steps.mjs'
7import { appearsShown } from './box-menu-appears.mjs'
8import { marksFor } from './step-marks.mjs'
9import { presses } from './presses.mjs'
10
11const para = t => [{ t }]
12const box = (id, extra = {}) => ({ id, type: 'text', props: {}, text: [para(id)], ...extra })
13const list = (id, n, extra = {}) => ({ id, type: 'list', props: {}, text: Array.from({ length: n }, (_, i) => para(`${id} ${i}`)), ...extra })
14const slideOf = (...blocks) => ({ id: 's1', layout: 'flow', blocks })
15const set = (id, fields) => ({ op: 'set_block', slide: 's1', id, ...fields })
16
17test('appearsOf: with the slide, on a press, item by item', () => {
18 assert.equal(appearsOf(box('a')), 'slide')
19 assert.equal(appearsOf(box('a', { step: 2 })), 'press')
20 assert.equal(appearsOf(list('l', 3, { step: 1, items: 'each' })), 'each')
21 // A list that is each but has no step arrives with the slide.
22 assert.equal(appearsOf(list('l', 3, { items: 'each' })), 'slide')
23})
24
25test('appearsOps: On a press takes the next step after the largest', () => {
26 const a = box('a')
27 const s = slideOf(box('x', { step: 1 }), box('y', { step: 2.5 }), a)
28 assert.deepEqual(appearsOps(s, a, 'press'), [set('a', { step: 3.5 })])
29 // The first press on a slide is 1.
30 const lone = box('lone')
31 assert.deepEqual(appearsOps(slideOf(lone), lone, 'press'), [set('lone', { step: 1 })])
32 // A box already on a press keeps its step: nothing to write.
33 const on = box('on', { step: 2 })
34 assert.deepEqual(appearsOps(slideOf(on), on, 'press'), [])
35 // From item by item, the list keeps its press and arrives whole.
36 const l = list('l', 3, { step: 2, items: 'each' })
37 assert.deepEqual(appearsOps(slideOf(l), l, 'press'), [set('l', { items: null })])
38 // A list with items "each" left over but no step arrives with the
39 // slide; On a press gives it a step and takes the items away, so it
40 // is on a press, not item by item.
41 const latent = list('latent', 3, { items: 'each' })
42 assert.equal(appearsOf(latent), 'slide')
43 const ops = appearsOps(slideOf(box('x', { step: 2 }), latent), latent, 'press')
44 assert.deepEqual(ops, [set('latent', { step: 3, items: null })])
45 const { items, ...after } = latent
46 assert.equal(appearsOf({ ...after, step: ops[0].step }), 'press')
47})
48
49test('appearsOps: With the slide clears the step and the items', () => {
50 const a = box('a', { step: 4 })
51 assert.deepEqual(appearsOps(slideOf(a), a, 'slide'), [set('a', { step: null, items: null })])
52 const l = list('l', 2, { step: 1, items: 'each' })
53 assert.deepEqual(appearsOps(slideOf(l), l, 'slide'), [set('l', { step: null, items: null })])
54 // Already with the slide: nothing.
55 const b = box('b')
56 assert.deepEqual(appearsOps(slideOf(b), b, 'slide'), [])
57})
58
59test('appearsOps: Item by item is a list\'s, and gives it a press when it has none', () => {
60 const l = list('l', 3)
61 const s = slideOf(box('x', { step: 2 }), l)
62 assert.deepEqual(appearsOps(s, l, 'each'), [set('l', { step: 3, items: 'each' })])
63 const on = list('on', 3, { step: 1 })
64 assert.deepEqual(appearsOps(slideOf(on), on, 'each'), [set('on', { items: 'each' })])
65 const done = list('d', 3, { step: 1, items: 'each' })
66 assert.deepEqual(appearsOps(slideOf(done), done, 'each'), [])
67 // Not a list: nothing.
68 const t = box('t')
69 assert.deepEqual(appearsOps(slideOf(t), t, 'each'), [])
70 assert.deepEqual(appearsOps(slideOf(t), t, 'nonsense'), [])
71})
72
73test('afterOps: the midpoint between the press and the next one, nothing renumbered', () => {
74 const x = box('x', { step: 1 })
75 const y = box('y', { step: 2 })
76 const a = box('a')
77 const s = slideOf(x, y, a)
78 const ops = afterOps(s, a, 1)
79 assert.deepEqual(ops, [set('a', { step: 1.5 })])
80 // Only the box moved: every other step is as it was.
81 assert.ok(ops.every(op => op.id === 'a'))
82 // After the last press: one more than it.
83 assert.deepEqual(afterOps(s, a, 2), [set('a', { step: 3 })])
84 // Between two halves, a quarter.
85 const z = box('z', { step: 1.5 })
86 assert.deepEqual(afterOps(slideOf(x, z, y, a), a, 1), [set('a', { step: 1.25 })])
87 // The box's own step is not a press to fit between: already there,
88 // nothing to write.
89 const self = box('self', { step: 2 })
90 assert.deepEqual(afterOps(slideOf(x, self, box('w', { step: 3 })), self, 1), [])
91 assert.deepEqual(afterOps(slideOf(x, self, box('w', { step: 3 })), self, 3), [set('self', { step: 4 })])
92})
93
94test('afterOps: the box ends up in the press after the one chosen', () => {
95 const s = slideOf(box('x', { step: 1 }), box('y', { step: 2 }), box('a', { step: 5 }))
96 const [op] = afterOps(s, s.blocks[2], 1)
97 const moved = slideOf(s.blocks[0], s.blocks[1], { ...s.blocks[2], step: op.step })
98 assert.deepEqual(presses(moved).map(p => p.map(m => m.block)), [['x'], ['a'], ['y']])
99})
100
101test('afterChoices: each press of the other boxes, by number; an item-by-item list counts its items', () => {
102 const l = list('l', 3, { step: 1, items: 'each' })
103 const y = box('y', { step: 2 })
104 const a = box('a', { step: 7 })
105 const s = slideOf(l, y, a)
106 // Presses: l0, l1, l2 (value 1), y (value 2), a (value 7). After a
107 // value means after its last press.
108 assert.deepEqual(afterChoices(s, a), [{ n: 3, value: 1 }, { n: 4, value: 2 }])
109 // Nothing on a slide without steps, nor when the box is the only press.
110 const b = box('b')
111 assert.deepEqual(afterChoices(slideOf(b, box('c')), b), [])
112 const only = box('only', { step: 1 })
113 assert.deepEqual(afterChoices(slideOf(only, box('c')), only), [])
114})
115
116test('appearsShown: Item by item is offered on a list alone', () => {
117 assert.deepEqual(appearsShown(list('l', 2)), ['slide', 'press', 'each'])
118 assert.deepEqual(appearsShown(box('t')), ['slide', 'press'])
119})
120
121test('marksFor: press numbers in press order, not step values; a list item by item numbers each item', () => {
122 const l = list('l', 2, { step: 10, items: 'each' })
123 const s = slideOf(box('a', { step: 0.5 }), l, box('b'), box('c', { step: 0.5 }), box('d', { step: 40 }))
124 assert.deepEqual(marksFor(s), [
125 { block: 'a', item: -1, n: 1 },
126 { block: 'c', item: -1, n: 1 },
127 { block: 'l', item: 0, n: 2 },
128 { block: 'l', item: 1, n: 3 },
129 { block: 'd', item: -1, n: 4 }
130 ])
131 assert.deepEqual(marksFor(slideOf(box('a'), box('b'))), [])
132})
133
134test('a scene block counts in the editor\'s press numbering, and After… never offers press value 0', () => {
135 const scenes = { w: { states: [{ id: 'a' }, { id: 'b' }, { id: 'c' }] } }
136 const sc = { id: 'sc', type: 'scene', props: { scene: 'w', from: 'a', to: 'c' } }
137 const t1 = box('t1', { step: 1 })
138 const t2 = box('t2')
139 const s = slideOf(sc, t1, t2)
140 // The scene's two states after from are presses 1 and 2, with no step
141 // of its own (value 0); the text on step 1 is press 3. One mark for
142 // the scene, its first and last press.
143 assert.deepEqual(marksFor(s, scenes), [{ block: 'sc', item: -1, n: 1, last: 2 }, { block: 't1', item: -1, n: 3 }])
144 // After… lists the text's press by its number, never the scene's
145 // value 0, which is not a step a box can be put after.
146 assert.deepEqual(afterChoices(s, t2, scenes), [{ n: 3, value: 1 }])
147 assert.deepEqual(afterOps(s, t2, 1, scenes), [set('t2', { step: 2 })])
148 assert.deepEqual(afterChoices(slideOf(sc, t2), t2, scenes), [])
149 // A scene of one press is one number.
150 const one = { id: 'one', type: 'scene', props: { scene: 'w', from: 'b', to: 'c' } }
151 assert.deepEqual(marksFor(slideOf(one), scenes), [{ block: 'one', item: -1, n: 1 }])
152})
153