oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// status.mjs: the save line's state from what the save queue and the
2// resync controller report, in estilo savestatus's words, and the load
3// error in its own line. The case this exists for: a batch the server
4// refused with a 4xx is dropped, not retried, and the line has to say
5// so rather than "Offline, retrying" — and keep saying so.
6import test from 'node:test'
7import assert from 'node:assert/strict'
8import { queueStatusState, statusState, createEditorStatus, mountSaveStatus } from './status.mjs'
9
10test('the queue words map to savestatus states, failed staying failed', () => {
11 assert.equal(queueStatusState('ok'), 'saved')
12 assert.equal(queueStatusState('retrying'), 'retrying')
13 assert.equal(queueStatusState('saving'), 'saving', 'a save still owed after one was answered is saving, not retrying')
14 assert.equal(queueStatusState('failed'), 'failed')
15})
16
17test('a dropped batch outranks the queue, for the rest of the session', () => {
18 assert.equal(statusState({ queueState: 'saved' }), 'saved')
19 assert.equal(statusState({ queueState: 'saving' }), 'saving')
20 assert.equal(statusState({ queueState: 'retrying' }), 'retrying')
21 assert.equal(statusState({ queueState: 'retrying', sessionFailed: true }), 'failed')
22 // A later successful save does not erase a drop from view.
23 assert.equal(statusState({ queueState: 'saved', sessionFailed: true }), 'failed')
24 assert.equal(statusState(), 'saved')
25})
26
27test('the editor status: the save line and the load error are two lines', () => {
28 const states = []
29 const error = { textContent: '' }
30 const status = createEditorStatus({ setSave: s => states.push(s), errorEl: error, errorText: 'load-error' })
31 status.save('saving')
32 assert.equal(states.at(-1), 'saving')
33 status.loadFailed(true)
34 assert.equal(error.textContent, 'load-error')
35 assert.equal(states.at(-1), 'saving', 'a load error does not touch the save line')
36 status.save('saved')
37 assert.equal(states.at(-1), 'saved')
38 assert.equal(error.textContent, 'load-error', 'a save does not clear the load error')
39 status.loadFailed(false)
40 assert.equal(error.textContent, '')
41 status.drop()
42 assert.equal(states.at(-1), 'failed')
43 status.save('saved')
44 assert.equal(states.at(-1), 'failed', 'dropped stays dropped')
45})
46
47test('mountSaveStatus loads estilo\'s module and mounts it on the line', async () => {
48 const line = { id: 'line' }
49 const error = { textContent: '' }
50 const root = {
51 dataset: { saveStatusModule: '/static/estilo/save-status.x.mjs', textError: 'load-error' },
52 querySelector: sel => (sel === '[estilo-save-status]' ? line : sel === '[slides-load-error]' ? error : null)
53 }
54 let mounted = null
55 let set = []
56 const status = await mountSaveStatus(root, async src => {
57 assert.equal(src, '/static/estilo/save-status.x.mjs')
58 return { mount: el => { mounted = el; return s => set.push(s) } }
59 })
60 assert.equal(mounted, line)
61 status.save('saving')
62 assert.deepEqual(set, ['saving'])
63})
64
65test('a save-status module that will not load says so and refuses to mount', async () => {
66 const error = { textContent: '' }
67 const root = {
68 dataset: { saveStatusModule: '/nope.mjs', textError: 'load-error' },
69 querySelector: sel => (sel === '[slides-load-error]' ? error : null)
70 }
71 await assert.rejects(mountSaveStatus(root, async () => { throw new Error('unavailable') }))
72 assert.equal(error.textContent, 'load-error')
73})
74