| 1 | // font-session.test.mjs — the one rule every path to a slide goes |
| 2 | // through, in Node with a fake FontFace and FontFaceSet: a family joins |
| 3 | // the page only once all four of its faces have loaded, together; a |
| 4 | // family that misses its deadline is committed and never joins; a |
| 5 | // person's own action waits and then runs or is refused; content from |
| 6 | // elsewhere is never held back past its timeout. |
| 7 | import { test } from 'node:test' |
| 8 | import assert from 'node:assert/strict' |
| 9 | import { createFontSession, createSystemSession } from './font-session.mjs' |
| 10 | |
| 11 | const face = (weight, style, url) => ({ weight, style, url }) |
| 12 | const manifest = { |
| 13 | need: ['sans', 'serif'], |
| 14 | families: { |
| 15 | 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')] }, |
| 16 | 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')] }, |
| 17 | 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')] } |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | // A world where each URL's load is a promise the test settles by hand, |
| 22 | // and time is a list of timers the test fires. |
| 23 | function world () { |
| 24 | const loads = new Map() // url -> { resolve, reject, started } |
| 25 | const set = [] |
| 26 | const timers = [] |
| 27 | class FakeFace { |
| 28 | constructor (family, source, desc) { |
| 29 | this.family = family |
| 30 | this.url = /url\("([^"]+)"\)/.exec(source)[1] |
| 31 | this.desc = desc |
| 32 | } |
| 33 | |
| 34 | load () { |
| 35 | let r |
| 36 | const p = new Promise((resolve, reject) => { r = { resolve, reject } }) |
| 37 | loads.set(this.url, { ...r, started: true }) |
| 38 | return p |
| 39 | } |
| 40 | } |
| 41 | const fontSet = { add: f => set.push(f) } |
| 42 | const attrs = {} |
| 43 | const root = { getAttribute: k => attrs[k] ?? null, setAttribute: (k, v) => { attrs[k] = v } } |
| 44 | const setTimer = (fn, ms) => { timers.push({ fn, ms }) } |
| 45 | const session = createFontSession({ manifest, fontSet, FontFaceCtor: FakeFace, root, setTimer }) |
| 46 | const flush = () => new Promise(resolve => setImmediate(resolve)) |
| 47 | return { |
| 48 | session, |
| 49 | set, |
| 50 | attrs, |
| 51 | loads, |
| 52 | fire: () => { for (const t of timers.splice(0)) t.fn() }, |
| 53 | arrive: async (...urls) => { for (const u of urls) loads.get(u).resolve(); await flush() }, |
| 54 | fail: async (...urls) => { for (const u of urls) loads.get(u).reject(new Error('404')); await flush() }, |
| 55 | flush |
| 56 | } |
| 57 | } |
| 58 | const urlsOf = key => manifest.families[key].faces.map(f => f.url) |
| 59 | const familiesIn = set => [...new Set(set.map(f => f.family))] |
| 60 | |
| 61 | test('nothing joins the page until all four faces of a family are in, and then all four do', async () => { |
| 62 | const w = world() |
| 63 | const gated = w.session.gate(['sans'], 2500) |
| 64 | await w.flush() |
| 65 | await w.arrive('/s-r', '/s-i', '/s-b') |
| 66 | assert.equal(w.set.length, 0, 'three of four faces in: nothing may be drawn in the family yet') |
| 67 | await w.arrive('/s-bi') |
| 68 | assert.deepEqual(await gated, []) |
| 69 | assert.equal(w.set.length, 4) |
| 70 | assert.deepEqual(familiesIn(w.set), ['Liberation Sans']) |
| 71 | assert.ok(w.session.ready('sans') && w.session.loaded('sans')) |
| 72 | assert.deepEqual(w.session.inUse, ['sans']) |
| 73 | assert.equal(w.attrs['data-slides-fonts-fallback'] || '', '') |
| 74 | }) |
| 75 | |
| 76 | test('every face is a block face of the manifest\'s family, weight and style', async () => { |
| 77 | const w = world() |
| 78 | const gated = w.session.gate(['serif'], 2500) |
| 79 | await w.flush() |
| 80 | await w.arrive(...urlsOf('serif')) |
| 81 | await gated |
| 82 | assert.deepEqual(w.set.map(f => [f.family, f.desc.weight, f.desc.style, f.desc.display]), [ |
| 83 | ['Liberation Serif', '400', 'normal', 'block'], |
| 84 | ['Liberation Serif', '400', 'italic', 'block'], |
| 85 | ['Liberation Serif', '700', 'normal', 'block'], |
| 86 | ['Liberation Serif', '700', 'italic', 'block'] |
| 87 | ]) |
| 88 | }) |
| 89 | |
| 90 | test('a family that misses the deadline is committed and never joins, even when it arrives', async () => { |
| 91 | const w = world() |
| 92 | const gated = w.session.gate(['sans', 'serif'], 2500) |
| 93 | await w.flush() |
| 94 | await w.arrive(...urlsOf('sans'), '/r-r', '/r-i') |
| 95 | w.fire() |
| 96 | assert.deepEqual(await gated, ['serif']) |
| 97 | assert.deepEqual(familiesIn(w.set), ['Liberation Sans']) |
| 98 | assert.ok(w.session.committed('serif') && w.session.ready('serif')) |
| 99 | assert.equal(w.attrs['data-slides-fonts-fallback'], 'serif') |
| 100 | await w.arrive('/r-b', '/r-bi') |
| 101 | w.session.use(['serif']) |
| 102 | await w.session.prepare(['serif']) |
| 103 | assert.deepEqual(familiesIn(w.set), ['Liberation Sans'], 'a committed family never swaps in') |
| 104 | }) |
| 105 | |
| 106 | test('a family whose file fails is committed by the gate: wrong but stable', async () => { |
| 107 | const w = world() |
| 108 | const gated = w.session.gate(['sans'], 2500) |
| 109 | await w.flush() |
| 110 | await w.arrive('/s-r', '/s-i', '/s-b') |
| 111 | await w.fail('/s-bi') |
| 112 | assert.deepEqual(await gated, ['sans']) |
| 113 | assert.equal(w.set.length, 0) |
| 114 | assert.ok(w.session.committed('sans')) |
| 115 | }) |
| 116 | |
| 117 | test('a person\'s action waits for its family and then may run, once it is in', async () => { |
| 118 | const w = world() |
| 119 | const waited = w.session.waitFor(['mono'], 4000) |
| 120 | await w.flush() |
| 121 | assert.equal(w.set.length, 0, 'waiting adds nothing') |
| 122 | await w.arrive(...urlsOf('mono')) |
| 123 | assert.equal(await waited, true) |
| 124 | assert.equal(w.set.length, 0, 'the caller puts it on, just before the change') |
| 125 | w.session.use(['mono']) |
| 126 | assert.deepEqual(familiesIn(w.set), ['Liberation Mono']) |
| 127 | }) |
| 128 | |
| 129 | test('a person\'s action is refused when its family fails or is late, and nothing is committed', async () => { |
| 130 | const w = world() |
| 131 | const failed = w.session.waitFor(['mono'], 4000) |
| 132 | await w.flush() |
| 133 | await w.fail('/m-r') |
| 134 | await w.arrive('/m-i', '/m-b', '/m-bi') |
| 135 | assert.equal(await failed, false) |
| 136 | assert.ok(w.session.failed('mono') && !w.session.ready('mono') && !w.session.committed('mono')) |
| 137 | |
| 138 | const v = world() |
| 139 | const late = v.session.waitFor(['serif'], 4000) |
| 140 | await v.flush() |
| 141 | v.fire() |
| 142 | assert.equal(await late, false) |
| 143 | assert.ok(!v.session.committed('serif')) |
| 144 | assert.equal(v.set.length, 0) |
| 145 | }) |
| 146 | |
| 147 | test('content from elsewhere waits for a new family, then is committed past the timeout, never held', async () => { |
| 148 | const w = world() |
| 149 | const prepared = w.session.prepare(['mono'], { timeout: 3000 }) |
| 150 | await w.flush() |
| 151 | w.fire() |
| 152 | await prepared |
| 153 | assert.ok(w.session.committed('mono')) |
| 154 | assert.equal(w.set.length, 0) |
| 155 | assert.deepEqual(w.session.inUse, ['mono']) |
| 156 | |
| 157 | const v = world() |
| 158 | const fast = v.session.prepare(['mono']) |
| 159 | await v.flush() |
| 160 | await v.arrive(...urlsOf('mono')) |
| 161 | await fast |
| 162 | assert.deepEqual(familiesIn(v.set), ['Liberation Mono']) |
| 163 | }) |
| 164 | |
| 165 | test('a family on screen in its web face is never committed', async () => { |
| 166 | const w = world() |
| 167 | const gated = w.session.gate(['sans'], 2500) |
| 168 | await w.flush() |
| 169 | await w.arrive(...urlsOf('sans')) |
| 170 | await gated |
| 171 | w.session.commitFallback(['sans']) |
| 172 | assert.ok(!w.session.committed('sans')) |
| 173 | assert.equal(w.set.length, 4) |
| 174 | }) |
| 175 | |
| 176 | test('a composition commits a family that is not in, rather than waiting', () => { |
| 177 | const w = world() |
| 178 | w.session.commitFallback(['mono']) |
| 179 | assert.ok(w.session.committed('mono') && w.session.ready('mono')) |
| 180 | assert.equal(w.set.length, 0) |
| 181 | }) |
| 182 | |
| 183 | test('prefetch loads the families not asked for yet, one at a time, and puts none on', async () => { |
| 184 | const w = world() |
| 185 | const gated = w.session.gate(['sans'], 2500) |
| 186 | await w.flush() |
| 187 | await w.arrive(...urlsOf('sans')) |
| 188 | await gated |
| 189 | let idles = 0 |
| 190 | const pre = w.session.prefetch({ idle: async () => { idles++ } }) |
| 191 | await w.flush() |
| 192 | assert.ok(w.loads.has('/r-r') && !w.loads.has('/m-r'), 'one family at a time') |
| 193 | await w.arrive(...urlsOf('serif')) |
| 194 | await w.flush() |
| 195 | await w.arrive(...urlsOf('mono')) |
| 196 | await pre |
| 197 | assert.equal(idles, 2) |
| 198 | assert.ok(w.session.loaded('serif') && w.session.loaded('mono')) |
| 199 | assert.deepEqual(familiesIn(w.set), ['Liberation Sans'], 'loaded is not on: the family joins when it is used') |
| 200 | }) |
| 201 | |
| 202 | test('an unknown family is no family', async () => { |
| 203 | const w = world() |
| 204 | assert.equal(w.session.ready('nosuch'), true) |
| 205 | assert.deepEqual(await w.session.gate(['nosuch'], 10), []) |
| 206 | assert.equal(await w.session.waitFor(['nosuch']), true) |
| 207 | }) |
| 208 | |
| 209 | test('the system session is ready for everything and loads nothing', async () => { |
| 210 | const s = createSystemSession() |
| 211 | assert.equal(s.web, false) |
| 212 | assert.equal(s.ready('mono'), true) |
| 213 | assert.equal(await s.waitFor(['mono']), true) |
| 214 | await s.prepare(['mono']) |
| 215 | assert.deepEqual(s.inUse, []) |
| 216 | }) |
| 217 | |