oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// fixtures/talk-link.test.mjs — the link between a window and the
2// server (talk-link.mjs), on a fake clock and a fake network: what is
3// sent, in what order, and which of the server's positions the window
4// is asked to take.
5import { test } from 'node:test'
6import assert from 'node:assert/strict'
7import { createLink, pressID } from './talk-link.mjs'
8
9// fakeNet is the network: every post waits until the test answers or
10// fails it, and the stream's handlers are the test's to call.
11function fakeNet () {
12 const posts = []
13 const net = {
14 posts,
15 handlers: null,
16 post (fields) {
17 return new Promise((resolve, reject) => posts.push({ fields, resolve, reject }))
18 },
19 stream (handlers) {
20 net.handlers = handlers
21 return () => { net.handlers = null }
22 }
23 }
24 return net
25}
26
27// fakeClock is time that moves only when the test says so.
28function fakeClock () {
29 let t = 0
30 let timers = []
31 return {
32 now: () => t,
33 after (ms, fn) {
34 const timer = { at: t + ms, fn }
35 timers.push(timer)
36 return () => { timers = timers.filter(x => x !== timer) }
37 },
38 advance (ms) {
39 t += ms
40 const due = timers.filter(x => x.at <= t)
41 timers = timers.filter(x => x.at > t)
42 for (const x of due) x.fn()
43 }
44 }
45}
46
47const settle = () => new Promise(resolve => setImmediate(resolve))
48const at = (slide, step, seq) => ({ slide, step, mode: 'core', black: false, seq, deckSeq: 1 })
49
50function rig (start = at('a', 0, 1)) {
51 const net = fakeNet()
52 const clock = fakeClock()
53 const moved = []
54 const statuses = []
55 const timers = []
56 const link = createLink({
57 post: net.post,
58 stream: net.stream,
59 now: clock.now,
60 after: clock.after,
61 onPosition: pos => moved.push(pos),
62 onStatus: s => statuses.push(s),
63 onTimer: ms => timers.push(ms)
64 })
65 link.resume(start)
66 net.handlers.open()
67 net.handlers.position(start)
68 return { net, clock, link, moved, statuses, timers }
69}
70
71test('a press is sent once and in order', async () => {
72 const { net, link } = rig()
73 link.press({ op: 'next' }, at('a', 1))
74 link.press({ op: 'next' }, at('a', 2))
75 link.press({ op: 'back' }, at('a', 1))
76 link.press({ op: 'goto', slide: 'c' }, at('c', 0))
77 await settle()
78 assert.equal(net.posts.length, 1, 'one press on the wire at a time')
79 const ids = new Set()
80 const ops = []
81 for (let i = 0; i < 4; i++) {
82 const p = net.posts[i]
83 assert.ok(p, `press ${i + 1} was sent`)
84 ops.push(p.fields.op + (p.fields.slide ? ':' + p.fields.slide : ''))
85 assert.match(p.fields.press, /^[A-Za-z0-9_-]{22}$/)
86 ids.add(p.fields.press)
87 p.resolve(at('x', 0, 2 + i))
88 await settle()
89 }
90 assert.deepEqual(ops, ['next', 'next', 'prev', 'goto:c'])
91 assert.equal(ids.size, 4, 'each press has its own id')
92 assert.equal(net.posts.length, 4, 'nothing sent twice')
93})
94
95test('a late answer does not move a window with presses waiting', async () => {
96 const { net, link, moved } = rig()
97 link.press({ op: 'next' }, at('a', 1))
98 link.press({ op: 'next' }, at('a', 2))
99 await settle()
100 // The phone pressed meanwhile: heard, not taken.
101 net.handlers.position(at('b', 0, 2))
102 net.posts[0].resolve(at('b', 0, 3))
103 await settle()
104 assert.deepEqual(moved, [], 'nothing applied while a press waits')
105 net.posts[1].resolve(at('b', 1, 4))
106 await settle()
107 assert.deepEqual(moved.map(p => [p.slide, p.step]), [['b', 1]], 'the newest, once the queue is empty')
108 // An older event arriving late changes nothing.
109 net.handlers.position(at('a', 2, 3))
110 await settle()
111 assert.equal(moved.length, 1)
112 // Its own answer, where the window already stands, moves nothing.
113 link.press({ op: 'next' }, at('b', 2))
114 await settle()
115 net.posts[2].resolve(at('b', 2, 5))
116 await settle()
117 assert.equal(moved.length, 1)
118 net.handlers.position(at('c', 0, 6))
119 await settle()
120 assert.deepEqual(moved.map(p => p.slide), ['b', 'c'])
121})
122
123test('offline presses end in one resume', async () => {
124 const { net, clock, link, statuses } = rig()
125 link.press({ op: 'next' }, at('a', 1))
126 link.press({ op: 'next' }, at('a', 2))
127 await settle()
128 // No answer in three seconds: offline, and the waiting press dropped.
129 clock.advance(3001)
130 await settle()
131 assert.equal(link.status(), 'offline')
132 assert.equal(statuses.at(-1), 'offline')
133 link.press({ op: 'next' }, at('b', 0))
134 link.press({ op: 'next' }, at('c', 0))
135 await settle()
136 assert.equal(net.posts.length, 1, 'nothing sent while offline')
137 // The late answer to the dropped press changes nothing.
138 net.posts[0].resolve(at('a', 1, 2))
139 net.handlers.error()
140 await settle()
141 net.handlers.open()
142 await settle()
143 assert.equal(link.status(), 'online')
144 assert.equal(net.posts.length, 2, 'one resume')
145 assert.deepEqual(
146 { op: net.posts[1].fields.op, slide: net.posts[1].fields.at_slide, step: net.posts[1].fields.at_step, mode: net.posts[1].fields.at_mode, black: net.posts[1].fields.at_black },
147 { op: 'resume', slide: 'c', step: '0', mode: 'core', black: 'false' })
148 net.handlers.position(at('z', 0, 9)) // the phone's press, made while cut
149 net.posts[1].resolve(at('c', 0, 10))
150 await settle()
151 net.handlers.open()
152 await settle()
153 assert.equal(net.posts.length, 2, 'still one resume')
154})
155
156test('a failed send is offline at once', async () => {
157 const { net, link } = rig()
158 link.press({ op: 'next' }, at('a', 1))
159 await settle()
160 net.posts[0].reject(new Error('offline'))
161 await settle()
162 assert.equal(link.status(), 'offline')
163})
164
165test('a stream that comes back by itself is not a split', async () => {
166 const { net, clock, link, statuses } = rig()
167 net.handlers.error()
168 clock.advance(3000)
169 net.handlers.open()
170 clock.advance(5000)
171 await settle()
172 assert.equal(link.status(), 'online')
173 assert.deepEqual(statuses, [])
174})
175
176test('a window that pressed nothing takes the server\'s position', async () => {
177 const { net, clock, link, moved, statuses } = rig()
178 net.handlers.error()
179 clock.advance(5001)
180 await settle()
181 assert.equal(link.status(), 'offline')
182 net.handlers.open()
183 net.handlers.position(at('d', 1, 7))
184 await settle()
185 assert.equal(link.status(), 'online')
186 assert.deepEqual(statuses, ['offline', 'online'])
187 assert.equal(net.posts.length, 0, 'no resume')
188 assert.deepEqual(moved.map(p => [p.slide, p.step]), [['d', 1]])
189})
190
191test('a press id is 22 characters of the allowed alphabet', () => {
192 const seen = new Set()
193 for (let i = 0; i < 200; i++) {
194 const id = pressID()
195 assert.match(id, /^[A-Za-z0-9_-]{22}$/)
196 seen.add(id)
197 }
198 assert.equal(seen.size, 200)
199})
200
201test('a press given up on is numbered below the resume that replaces it', async () => {
202 const { net, clock, link } = rig()
203 link.press({ op: 'next' }, at('a', 1))
204 await settle()
205 link.press({ op: 'next' }, at('a', 2))
206 clock.advance(3001)
207 await settle()
208 link.press({ op: 'next' }, at('b', 0))
209 net.handlers.open()
210 net.handlers.position(at('a', 0, 2))
211 await settle()
212 const [abandoned, resume] = net.posts
213 assert.equal(resume.fields.op, 'resume')
214 assert.match(abandoned.fields.link, /^[A-Za-z0-9_-]{22}$/)
215 assert.equal(resume.fields.link, abandoned.fields.link, 'one link for the window')
216 // The server refuses a command of this link numbered at or below one
217 // it has applied: the resume outnumbers every press sent before it.
218 assert.ok(Number(resume.fields.n) > Number(abandoned.fields.n), `resume n ${resume.fields.n} above the abandoned ${abandoned.fields.n}`)
219})
220
221test('every command carries the link and a rising number', async () => {
222 const { net, link } = rig()
223 for (let i = 0; i < 3; i++) link.press({ op: 'next' }, at('a', i + 1))
224 for (let i = 0; i < 3; i++) {
225 await settle()
226 net.posts[i].resolve(at('a', i + 1, 2 + i))
227 }
228 await settle()
229 const ns = net.posts.map(p => Number(p.fields.n))
230 assert.deepEqual(ns, [1, 2, 3])
231 assert.equal(new Set(net.posts.map(p => p.fields.link)).size, 1)
232})
233
234test('the black screen pressed offline goes with the resume, either way', async () => {
235 for (const black of [true, false]) {
236 const { net, clock, link } = rig({ ...at('a', 0, 1), black: !black })
237 link.press({ op: 'black' }, { ...at('a', 0), black })
238 await settle()
239 net.posts[0].reject(new Error('offline'))
240 await settle()
241 clock.advance(2001)
242 await settle()
243 assert.equal(net.posts[1].fields.op, 'resume')
244 assert.equal(net.posts[1].fields.at_black, String(black))
245 }
246})
247
248test('the timer is taken from the server even where the position is the same', async () => {
249 const { net, moved, timers } = rig()
250 assert.deepEqual(timers, [], 'a position that says nothing of a timer reports none')
251 net.handlers.position({ ...at('a', 0, 2), timerAt: 5000 })
252 assert.deepEqual(timers, [5000], 'another window pressed: the timer started')
253 assert.equal(moved.length, 0, 'and the window did not move')
254 net.handlers.position({ ...at('a', 0, 3), timerAt: null })
255 assert.deepEqual(timers, [5000, null], 'a reset elsewhere is heard as null')
256 net.handlers.position({ ...at('a', 0, 2), timerAt: 7000 })
257 assert.deepEqual(timers, [5000, null], 'an older event says nothing')
258})
259
260test('a stale timer never undoes a press still waiting for its answer', async () => {
261 const { net, link, timers } = rig()
262 link.press({ op: 'next' }, at('a', 1))
263 await settle()
264 net.handlers.position({ ...at('a', 0, 2), timerAt: null })
265 assert.deepEqual(timers, [], 'nothing is taken while a press waits')
266 net.posts[0].resolve({ ...at('a', 1, 3), timerAt: 9000 })
267 await settle()
268 assert.deepEqual(timers, [9000], 'the answer brings the timer the press started')
269})
270