// font-session.test.mjs — the one rule every path to a slide goes // through, in Node with a fake FontFace and FontFaceSet: a family joins // the page only once all four of its faces have loaded, together; a // family that misses its deadline is committed and never joins; a // person's own action waits and then runs or is refused; content from // elsewhere is never held back past its timeout. import { test } from 'node:test' import assert from 'node:assert/strict' import { createFontSession, createSystemSession } from './font-session.mjs' const face = (weight, style, url) => ({ weight, style, url }) const manifest = { need: ['sans', 'serif'], families: { sans: { css: 'Liberation Sans', faces: [face(400, 'normal', '/s-r'), face(400, 'italic', '/s-i'), face(700, 'normal', '/s-b'), face(700, 'italic', '/s-bi')] }, serif: { css: 'Liberation Serif', faces: [face(400, 'normal', '/r-r'), face(400, 'italic', '/r-i'), face(700, 'normal', '/r-b'), face(700, 'italic', '/r-bi')] }, mono: { css: 'Liberation Mono', faces: [face(400, 'normal', '/m-r'), face(400, 'italic', '/m-i'), face(700, 'normal', '/m-b'), face(700, 'italic', '/m-bi')] } } } // A world where each URL's load is a promise the test settles by hand, // and time is a list of timers the test fires. function world () { const loads = new Map() // url -> { resolve, reject, started } const set = [] const timers = [] class FakeFace { constructor (family, source, desc) { this.family = family this.url = /url\("([^"]+)"\)/.exec(source)[1] this.desc = desc } load () { let r const p = new Promise((resolve, reject) => { r = { resolve, reject } }) loads.set(this.url, { ...r, started: true }) return p } } const fontSet = { add: f => set.push(f) } const attrs = {} const root = { getAttribute: k => attrs[k] ?? null, setAttribute: (k, v) => { attrs[k] = v } } const setTimer = (fn, ms) => { timers.push({ fn, ms }) } const session = createFontSession({ manifest, fontSet, FontFaceCtor: FakeFace, root, setTimer }) const flush = () => new Promise(resolve => setImmediate(resolve)) return { session, set, attrs, loads, fire: () => { for (const t of timers.splice(0)) t.fn() }, arrive: async (...urls) => { for (const u of urls) loads.get(u).resolve(); await flush() }, fail: async (...urls) => { for (const u of urls) loads.get(u).reject(new Error('404')); await flush() }, flush } } const urlsOf = key => manifest.families[key].faces.map(f => f.url) const familiesIn = set => [...new Set(set.map(f => f.family))] test('nothing joins the page until all four faces of a family are in, and then all four do', async () => { const w = world() const gated = w.session.gate(['sans'], 2500) await w.flush() await w.arrive('/s-r', '/s-i', '/s-b') assert.equal(w.set.length, 0, 'three of four faces in: nothing may be drawn in the family yet') await w.arrive('/s-bi') assert.deepEqual(await gated, []) assert.equal(w.set.length, 4) assert.deepEqual(familiesIn(w.set), ['Liberation Sans']) assert.ok(w.session.ready('sans') && w.session.loaded('sans')) assert.deepEqual(w.session.inUse, ['sans']) assert.equal(w.attrs['data-slides-fonts-fallback'] || '', '') }) test('every face is a block face of the manifest\'s family, weight and style', async () => { const w = world() const gated = w.session.gate(['serif'], 2500) await w.flush() await w.arrive(...urlsOf('serif')) await gated assert.deepEqual(w.set.map(f => [f.family, f.desc.weight, f.desc.style, f.desc.display]), [ ['Liberation Serif', '400', 'normal', 'block'], ['Liberation Serif', '400', 'italic', 'block'], ['Liberation Serif', '700', 'normal', 'block'], ['Liberation Serif', '700', 'italic', 'block'] ]) }) test('a family that misses the deadline is committed and never joins, even when it arrives', async () => { const w = world() const gated = w.session.gate(['sans', 'serif'], 2500) await w.flush() await w.arrive(...urlsOf('sans'), '/r-r', '/r-i') w.fire() assert.deepEqual(await gated, ['serif']) assert.deepEqual(familiesIn(w.set), ['Liberation Sans']) assert.ok(w.session.committed('serif') && w.session.ready('serif')) assert.equal(w.attrs['data-slides-fonts-fallback'], 'serif') await w.arrive('/r-b', '/r-bi') w.session.use(['serif']) await w.session.prepare(['serif']) assert.deepEqual(familiesIn(w.set), ['Liberation Sans'], 'a committed family never swaps in') }) test('a family whose file fails is committed by the gate: wrong but stable', async () => { const w = world() const gated = w.session.gate(['sans'], 2500) await w.flush() await w.arrive('/s-r', '/s-i', '/s-b') await w.fail('/s-bi') assert.deepEqual(await gated, ['sans']) assert.equal(w.set.length, 0) assert.ok(w.session.committed('sans')) }) test('a person\'s action waits for its family and then may run, once it is in', async () => { const w = world() const waited = w.session.waitFor(['mono'], 4000) await w.flush() assert.equal(w.set.length, 0, 'waiting adds nothing') await w.arrive(...urlsOf('mono')) assert.equal(await waited, true) assert.equal(w.set.length, 0, 'the caller puts it on, just before the change') w.session.use(['mono']) assert.deepEqual(familiesIn(w.set), ['Liberation Mono']) }) test('a person\'s action is refused when its family fails or is late, and nothing is committed', async () => { const w = world() const failed = w.session.waitFor(['mono'], 4000) await w.flush() await w.fail('/m-r') await w.arrive('/m-i', '/m-b', '/m-bi') assert.equal(await failed, false) assert.ok(w.session.failed('mono') && !w.session.ready('mono') && !w.session.committed('mono')) const v = world() const late = v.session.waitFor(['serif'], 4000) await v.flush() v.fire() assert.equal(await late, false) assert.ok(!v.session.committed('serif')) assert.equal(v.set.length, 0) }) test('content from elsewhere waits for a new family, then is committed past the timeout, never held', async () => { const w = world() const prepared = w.session.prepare(['mono'], { timeout: 3000 }) await w.flush() w.fire() await prepared assert.ok(w.session.committed('mono')) assert.equal(w.set.length, 0) assert.deepEqual(w.session.inUse, ['mono']) const v = world() const fast = v.session.prepare(['mono']) await v.flush() await v.arrive(...urlsOf('mono')) await fast assert.deepEqual(familiesIn(v.set), ['Liberation Mono']) }) test('a family on screen in its web face is never committed', async () => { const w = world() const gated = w.session.gate(['sans'], 2500) await w.flush() await w.arrive(...urlsOf('sans')) await gated w.session.commitFallback(['sans']) assert.ok(!w.session.committed('sans')) assert.equal(w.set.length, 4) }) test('a composition commits a family that is not in, rather than waiting', () => { const w = world() w.session.commitFallback(['mono']) assert.ok(w.session.committed('mono') && w.session.ready('mono')) assert.equal(w.set.length, 0) }) test('prefetch loads the families not asked for yet, one at a time, and puts none on', async () => { const w = world() const gated = w.session.gate(['sans'], 2500) await w.flush() await w.arrive(...urlsOf('sans')) await gated let idles = 0 const pre = w.session.prefetch({ idle: async () => { idles++ } }) await w.flush() assert.ok(w.loads.has('/r-r') && !w.loads.has('/m-r'), 'one family at a time') await w.arrive(...urlsOf('serif')) await w.flush() await w.arrive(...urlsOf('mono')) await pre assert.equal(idles, 2) assert.ok(w.session.loaded('serif') && w.session.loaded('mono')) assert.deepEqual(familiesIn(w.set), ['Liberation Sans'], 'loaded is not on: the family joins when it is used') }) test('an unknown family is no family', async () => { const w = world() assert.equal(w.session.ready('nosuch'), true) assert.deepEqual(await w.session.gate(['nosuch'], 10), []) assert.equal(await w.session.waitFor(['nosuch']), true) }) test('the system session is ready for everything and loads nothing', async () => { const s = createSystemSession() assert.equal(s.web, false) assert.equal(s.ready('mono'), true) assert.equal(await s.waitFor(['mono']), true) await s.prepare(['mono']) assert.deepEqual(s.inUse, []) })