| 1 | // editor-fonts.test.mjs — the editor's font gate, in Node with a fake |
| 2 | // font session and the few elements it touches. What is held here is the |
| 3 | // queue: however a wait ends — the family loads, fails, misses its |
| 4 | // deadline, the change itself throws, or telling the person about it |
| 5 | // throws — every waiter queued behind it (a collaborator's batch, a stage |
| 6 | // install) is released, in the order it queued, and the editor comes |
| 7 | // back. Astra's finding 1 on #27 was a refusal that threw first and left |
| 8 | // collaborators' updates stuck for the rest of the visit. |
| 9 | import { test } from 'node:test' |
| 10 | import assert from 'node:assert/strict' |
| 11 | import { createEditorFontGate } from './editor-fonts.mjs' |
| 12 | |
| 13 | function fakeElement () { |
| 14 | const attrs = {} |
| 15 | const kids = {} |
| 16 | return { |
| 17 | inert: false, |
| 18 | textContent: '', |
| 19 | getAttribute: k => (k in attrs ? attrs[k] : null), |
| 20 | setAttribute: (k, v) => { attrs[k] = String(v) }, |
| 21 | removeAttribute: k => { delete attrs[k] }, |
| 22 | querySelector: q => (kids[q] = kids[q] || fakeElement()), |
| 23 | addEventListener () {}, |
| 24 | contains: () => false, |
| 25 | focus () {} |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | function world ({ loads = true, throwOnRefused = false } = {}) { |
| 30 | globalThis.document = { activeElement: null, body: {}, documentElement: {} } |
| 31 | globalThis.addEventListener = () => {} |
| 32 | globalThis.removeEventListener = () => {} |
| 33 | const parts = {} |
| 34 | const root = { querySelector: q => (parts[q] = parts[q] || fakeElement()) } |
| 35 | let settle |
| 36 | const fonts = { |
| 37 | web: true, |
| 38 | inUse: ['sans'], |
| 39 | ready: k => k === 'sans', |
| 40 | use () {}, |
| 41 | waitFor: () => new Promise(resolve => { settle = resolve }), |
| 42 | familiesOfView: () => [] |
| 43 | } |
| 44 | let refusals = 0 |
| 45 | const gate = createEditorFontGate({ |
| 46 | fonts, |
| 47 | root, |
| 48 | getDoc: () => null, |
| 49 | onRefused: () => { |
| 50 | refusals++ |
| 51 | if (throwOnRefused) throw new Error('the inspector is gone') |
| 52 | } |
| 53 | }) |
| 54 | return { |
| 55 | gate, |
| 56 | parts, |
| 57 | finish: ok => settle(ok === undefined ? loads : ok), |
| 58 | get refusals () { return refusals } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | const tick = () => new Promise(resolve => setImmediate(resolve)) |
| 63 | |
| 64 | // Queues three waiters behind a wait and reports the order they ran in. |
| 65 | async function queued (w, endWait) { |
| 66 | const order = [] |
| 67 | const started = w.gate.gate(['mono'], () => endWait.run()) |
| 68 | await tick() |
| 69 | assert.equal(w.gate.waiting, true) |
| 70 | const queue = [1, 2, 3].map(n => w.gate.free().then(() => order.push(n))) |
| 71 | await tick() |
| 72 | assert.deepEqual(order, [], 'nothing queued runs during the wait') |
| 73 | w.finish(endWait.loaded) |
| 74 | const made = await started |
| 75 | await Promise.all(queue) |
| 76 | return { order, made } |
| 77 | } |
| 78 | |
| 79 | test('a change that loads runs once and releases the queue in order', async () => { |
| 80 | const w = world() |
| 81 | let runs = 0 |
| 82 | const { order, made } = await queued(w, { loaded: true, run: () => { runs++ } }) |
| 83 | assert.equal(made, true) |
| 84 | assert.equal(runs, 1) |
| 85 | assert.deepEqual(order, [1, 2, 3]) |
| 86 | assert.equal(w.gate.waiting, false) |
| 87 | assert.equal(w.parts['[slides-stage-pane]'].inert, false) |
| 88 | }) |
| 89 | |
| 90 | test('a refusal releases the queue in order and refills the inspector', async () => { |
| 91 | const w = world() |
| 92 | const { order, made } = await queued(w, { loaded: false, run: () => assert.fail('a refused change must not run') }) |
| 93 | assert.equal(made, false) |
| 94 | assert.deepEqual(order, [1, 2, 3]) |
| 95 | assert.equal(w.refusals, 1) |
| 96 | assert.equal(w.parts['[slides-fonts-status]'].getAttribute('data-state'), 'unavailable') |
| 97 | }) |
| 98 | |
| 99 | test('a refusal whose notice throws still releases the queue', async () => { |
| 100 | const w = world({ throwOnRefused: true }) |
| 101 | const errors = [] |
| 102 | const was = console.error |
| 103 | console.error = (...a) => errors.push(a.join(' ')) |
| 104 | try { |
| 105 | const { order, made } = await queued(w, { loaded: false, run: () => {} }) |
| 106 | assert.equal(made, false) |
| 107 | assert.deepEqual(order, [1, 2, 3]) |
| 108 | assert.ok(errors.some(e => e.includes('the inspector is gone')), 'the error is reported, not swallowed') |
| 109 | } finally { |
| 110 | console.error = was |
| 111 | } |
| 112 | }) |
| 113 | |
| 114 | test('a change that throws still releases the queue, and is refused', async () => { |
| 115 | const w = world() |
| 116 | const was = console.error |
| 117 | console.error = () => {} |
| 118 | try { |
| 119 | const { order, made } = await queued(w, { loaded: true, run: () => { throw new Error('boom') } }) |
| 120 | assert.equal(made, false) |
| 121 | assert.deepEqual(order, [1, 2, 3]) |
| 122 | assert.equal(w.parts['[slides-stage-pane]'].inert, false) |
| 123 | } finally { |
| 124 | console.error = was |
| 125 | } |
| 126 | }) |
| 127 | |
| 128 | test('a change that can no longer be made once its font is in is said as interrupted, not as a font that failed', async () => { |
| 129 | const w = world() |
| 130 | const { made } = await queued(w, { loaded: true, run: () => false }) |
| 131 | assert.equal(made, false) |
| 132 | assert.equal(w.parts['[slides-fonts-status]'].getAttribute('data-state'), 'interrupted') |
| 133 | }) |
| 134 | |
| 135 | test('a second change during a wait is refused at once and never queued', async () => { |
| 136 | const w = world() |
| 137 | const first = w.gate.gate(['mono'], () => {}) |
| 138 | await tick() |
| 139 | let ran = false |
| 140 | assert.equal(await w.gate.gate(['sans'], () => { ran = true }), false) |
| 141 | assert.equal(ran, false) |
| 142 | assert.equal(w.parts['[slides-fonts-status]'].getAttribute('data-state'), 'refused') |
| 143 | w.finish(true) |
| 144 | assert.equal(await first, true) |
| 145 | }) |
| 146 | |
| 147 | test('free() with no wait on resolves at once', async () => { |
| 148 | const w = world() |
| 149 | let done = false |
| 150 | w.gate.free().then(() => { done = true }) |
| 151 | await tick() |
| 152 | assert.equal(done, true) |
| 153 | }) |
| 154 | |
| 155 | // Deck settings is a dialog under File (spec §8), outside the card: |
| 156 | // its theme and font choices go inert with the rest of the editor |
| 157 | // while a font is awaited, and come back after. |
| 158 | test('a wait makes the Deck settings dialog inert with the card', async () => { |
| 159 | const w = world() |
| 160 | const started = w.gate.gate(['mono'], () => {}) |
| 161 | await tick() |
| 162 | for (const q of ['[slides-inspector]', '[slides-deck-dialog]']) assert.equal(w.parts[q].inert, true, q) |
| 163 | w.finish(true) |
| 164 | await started |
| 165 | for (const q of ['[slides-inspector]', '[slides-deck-dialog]']) assert.equal(w.parts[q].inert, false, q) |
| 166 | }) |
| 167 | |