// font-versions.test.mjs — an uploaded family whose faces change during // the visit (final review, I4). A family is known by its css name AND // its set of faces: add a bold face to Brand and pick it again, and the // deck's Brand is a new set the tab has not loaded. It is learned again // and made ready before the change shows, like any family the change // brings in; its faces then join the page together, in place of the // old set's, in one go. Undo and redo move between sets the tab has // already loaded, so they swap at once. import { test } from 'node:test' import assert from 'node:assert/strict' import * as Y from 'yjs' import { createFontSession } from './font-session.mjs' import { familiesOfView } from './font-families.mjs' import { createEditorFontGate } from './editor-fonts.mjs' import { applyOps, view } from './ops.mjs' const ref = c => c.repeat(64) const face = (c, weight) => ({ ref: ref(c), format: 'ttf', weight: [weight, weight], italic: false, pictures: true }) const regular = { family: 'Brand', css: 'Brand', faces: [face('a', 400)] } const withBold = { family: 'Brand', css: 'Brand', faces: [face('a', 400), face('b', 700)] } const pick = fam => ({ op: 'set_meta', fonts: { heading: "'Brand', Liberation Sans, sans-serif", body: '' }, fontFiles: [fam] }) function world () { const loads = new Map() const made = [] const set = new Set() const timers = [] class FakeFace { constructor (family, source, desc) { Object.assign(this, { family, source, desc }) this.url = /url\("([^"]+)"\)/.exec(source)[1] made.push(this) } load () { return new Promise((resolve, reject) => loads.set(this.url, { resolve, reject })) } } const attrs = {} const root = { getAttribute: k => attrs[k] ?? null, setAttribute: (k, v) => { attrs[k] = v } } const manifest = { need: [], families: {}, themes: { ink: ['Liberation Sans, sans-serif', 'Liberation Sans, sans-serif'] }, defaultTheme: 'ink', code: 'Liberation Mono, monospace' } const fontSet = { add: f => set.add(f), delete: f => set.delete(f) } const session = createFontSession({ manifest, fontSet, FontFaceCtor: FakeFace, root, setTimer: (fn, ms) => timers.push({ fn, ms }) }) const flush = () => new Promise(resolve => setImmediate(resolve)) return { session, made, set, flush, fire: () => { for (const t of timers.splice(0)) t.fn() }, // Every face made so far arrives. arriveAll: async () => { for (const [, l] of loads) l.resolve(); await flush() }, // The page's Brand faces: [url, weight] of each in document.fonts. onPage: () => [...set].filter(f => f.family === 'Brand').map(f => [f.url.slice(-4), f.desc.weight]).sort() } } test('a family whose faces change is learned again, and its new set joins in place of the old, once all have loaded', async () => { const w = world() w.session.learn([regular], '/d/abc') const first = w.session.prepare(['u:Brand']) await w.flush() await w.arriveAll() await first assert.deepEqual(w.onPage(), [['aaaa', '400']]) // A bold added and the family picked again: a set this tab has not got. w.session.learn([withBold], '/d/abc') assert.equal(w.session.ready('u:Brand'), false, 'the new set is not ready: a change that shows it waits') const second = w.session.prepare(['u:Brand']) await w.flush() assert.deepEqual(w.onPage(), [['aaaa', '400']], 'nothing changes on the page while the new set loads') await w.arriveAll() await second assert.deepEqual(w.onPage(), [['aaaa', '400'], ['bbbb', '700']], 'both faces of the new set, the old set gone') // Undo: back to the set the tab already has, at once. w.session.learn([regular], '/d/abc') assert.equal(w.session.ready('u:Brand'), true) w.session.use(['u:Brand']) assert.deepEqual(w.onPage(), [['aaaa', '400']]) // Redo. w.session.learn([withBold], '/d/abc') assert.equal(w.session.ready('u:Brand'), true) w.session.use(['u:Brand']) assert.deepEqual(w.onPage(), [['aaaa', '400'], ['bbbb', '700']]) }) test('a new set late past its timeout leaves the old set on the page, never a fallback', async () => { const w = world() w.session.learn([regular], '/d/abc') const first = w.session.prepare(['u:Brand']) await w.flush() await w.arriveAll() await first w.session.learn([withBold], '/d/abc') const late = w.session.prepare(['u:Brand']) await w.flush() w.fire() await late assert.deepEqual(w.onPage(), [['aaaa', '400']], 'the old set stays; the late one never joins') await w.arriveAll() w.session.use(['u:Brand']) assert.deepEqual(w.onPage(), [['aaaa', '400']]) }) // The editor: a pick of the grown family waits for its new set, and an // undo or a redo moves between sets in the same task as the change. function editorWorld () { globalThis.document = { activeElement: null, body: {}, documentElement: {} } globalThis.addEventListener = () => {} globalThis.removeEventListener = () => {} const el = () => ({ getAttribute: () => null, setAttribute () {}, removeAttribute () {}, querySelector: () => null, addEventListener () {} }) const root = { dataset: { deck: 'abc' }, querySelector: () => el() } const w = world() Object.assign(w.session, { familiesOfView: v => familiesOfView(v, w.session.manifest) }) const doc = new Y.Doc() applyOps(doc, [{ op: 'set_meta', title: 'T', theme: 'ink' }, { op: 'add_slide', id: 'slideone0001', layout: 'flow', template: 'title-body' }]) const gate = createEditorFontGate({ fonts: w.session, root, getDoc: () => doc }) return { ...w, doc, gate } } test('the editor: picking the grown family waits for its new set; undo and redo swap sets with the change', async () => { const w = editorWorld() // Regular picked and on the page. let keys = w.gate.familiesAfter(d => applyOps(d, [pick(regular)])) const one = w.gate.gate(keys, () => { applyOps(w.doc, [pick(regular)]); return true }) await w.flush() await w.arriveAll() assert.equal(await one, true) assert.deepEqual(w.onPage(), [['aaaa', '400']]) // Bold added and picked again: the change waits for the new set. keys = w.gate.familiesAfter(d => applyOps(d, [pick(withBold)])) assert.deepEqual(keys, ['sans', 'u:Brand']) assert.equal(w.session.ready('u:Brand'), false) let ran = false const two = w.gate.gate(keys, () => { applyOps(w.doc, [pick(withBold)]); ran = true; return true }) await w.flush() assert.equal(ran, false, 'not before the new set is in') await w.arriveAll() assert.equal(await two, true) assert.deepEqual(w.onPage(), [['aaaa', '400'], ['bbbb', '700']]) // Undo, then redo: the deck's set changes and the page's with it. applyOps(w.doc, [pick(regular)]) w.gate.afterHistory() assert.deepEqual(w.onPage(), [['aaaa', '400']]) applyOps(w.doc, [pick(withBold)]) w.gate.afterHistory() assert.deepEqual(w.onPage(), [['aaaa', '400'], ['bbbb', '700']]) assert.deepEqual(view(w.doc).meta.fontFiles, [withBold]) }) test('a change refused while its new set loads leaves the tab on the deck\'s own set', async () => { const w = editorWorld() let keys = w.gate.familiesAfter(d => applyOps(d, [pick(regular)])) const one = w.gate.gate(keys, () => { applyOps(w.doc, [pick(regular)]); return true }) await w.flush() await w.arriveAll() await one keys = w.gate.familiesAfter(d => applyOps(d, [pick(withBold)])) // Worked out but never made: the next look at the deck is the deck's. w.gate.familiesAfter(d => applyOps(d, [{ op: 'set_meta', title: 'T2' }])) assert.equal(w.session.ready('u:Brand'), true, 'the deck still has the regular set, which is in') assert.equal(w.session.uploaded['u:Brand'].faces.length, 1) })