// editor-fonts.test.mjs โ€” the editor's font gate, in Node with a fake // font session and the few elements it touches. What is held here is the // queue: however a wait ends โ€” the family loads, fails, misses its // deadline, the change itself throws, or telling the person about it // throws โ€” every waiter queued behind it (a collaborator's batch, a stage // install) is released, in the order it queued, and the editor comes // back. Astra's finding 1 on #27 was a refusal that threw first and left // collaborators' updates stuck for the rest of the visit. import { test } from 'node:test' import assert from 'node:assert/strict' import { createEditorFontGate } from './editor-fonts.mjs' function fakeElement () { const attrs = {} const kids = {} return { inert: false, textContent: '', getAttribute: k => (k in attrs ? attrs[k] : null), setAttribute: (k, v) => { attrs[k] = String(v) }, removeAttribute: k => { delete attrs[k] }, querySelector: q => (kids[q] = kids[q] || fakeElement()), addEventListener () {}, contains: () => false, focus () {} } } function world ({ loads = true, throwOnRefused = false } = {}) { globalThis.document = { activeElement: null, body: {}, documentElement: {} } globalThis.addEventListener = () => {} globalThis.removeEventListener = () => {} const parts = {} const root = { querySelector: q => (parts[q] = parts[q] || fakeElement()) } let settle const fonts = { web: true, inUse: ['sans'], ready: k => k === 'sans', use () {}, waitFor: () => new Promise(resolve => { settle = resolve }), familiesOfView: () => [] } let refusals = 0 const gate = createEditorFontGate({ fonts, root, getDoc: () => null, onRefused: () => { refusals++ if (throwOnRefused) throw new Error('the inspector is gone') } }) return { gate, parts, finish: ok => settle(ok === undefined ? loads : ok), get refusals () { return refusals } } } const tick = () => new Promise(resolve => setImmediate(resolve)) // Queues three waiters behind a wait and reports the order they ran in. async function queued (w, endWait) { const order = [] const started = w.gate.gate(['mono'], () => endWait.run()) await tick() assert.equal(w.gate.waiting, true) const queue = [1, 2, 3].map(n => w.gate.free().then(() => order.push(n))) await tick() assert.deepEqual(order, [], 'nothing queued runs during the wait') w.finish(endWait.loaded) const made = await started await Promise.all(queue) return { order, made } } test('a change that loads runs once and releases the queue in order', async () => { const w = world() let runs = 0 const { order, made } = await queued(w, { loaded: true, run: () => { runs++ } }) assert.equal(made, true) assert.equal(runs, 1) assert.deepEqual(order, [1, 2, 3]) assert.equal(w.gate.waiting, false) assert.equal(w.parts['[slides-stage-pane]'].inert, false) }) test('a refusal releases the queue in order and refills the inspector', async () => { const w = world() const { order, made } = await queued(w, { loaded: false, run: () => assert.fail('a refused change must not run') }) assert.equal(made, false) assert.deepEqual(order, [1, 2, 3]) assert.equal(w.refusals, 1) assert.equal(w.parts['[slides-fonts-status]'].getAttribute('data-state'), 'unavailable') }) test('a refusal whose notice throws still releases the queue', async () => { const w = world({ throwOnRefused: true }) const errors = [] const was = console.error console.error = (...a) => errors.push(a.join(' ')) try { const { order, made } = await queued(w, { loaded: false, run: () => {} }) assert.equal(made, false) assert.deepEqual(order, [1, 2, 3]) assert.ok(errors.some(e => e.includes('the inspector is gone')), 'the error is reported, not swallowed') } finally { console.error = was } }) test('a change that throws still releases the queue, and is refused', async () => { const w = world() const was = console.error console.error = () => {} try { const { order, made } = await queued(w, { loaded: true, run: () => { throw new Error('boom') } }) assert.equal(made, false) assert.deepEqual(order, [1, 2, 3]) assert.equal(w.parts['[slides-stage-pane]'].inert, false) } finally { console.error = was } }) 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 () => { const w = world() const { made } = await queued(w, { loaded: true, run: () => false }) assert.equal(made, false) assert.equal(w.parts['[slides-fonts-status]'].getAttribute('data-state'), 'interrupted') }) test('a second change during a wait is refused at once and never queued', async () => { const w = world() const first = w.gate.gate(['mono'], () => {}) await tick() let ran = false assert.equal(await w.gate.gate(['sans'], () => { ran = true }), false) assert.equal(ran, false) assert.equal(w.parts['[slides-fonts-status]'].getAttribute('data-state'), 'refused') w.finish(true) assert.equal(await first, true) }) test('free() with no wait on resolves at once', async () => { const w = world() let done = false w.gate.free().then(() => { done = true }) await tick() assert.equal(done, true) }) // Deck settings is a dialog under File (spec ยง8), outside the card: // its theme and font choices go inert with the rest of the editor // while a font is awaited, and come back after. test('a wait makes the Deck settings dialog inert with the card', async () => { const w = world() const started = w.gate.gate(['mono'], () => {}) await tick() for (const q of ['[slides-inspector]', '[slides-deck-dialog]']) assert.equal(w.parts[q].inert, true, q) w.finish(true) await started for (const q of ['[slides-inspector]', '[slides-deck-dialog]']) assert.equal(w.parts[q].inert, false, q) })