// font-uploaded.test.mjs — a deck's own uploaded fonts in the gate // (fontgate.go's manifest.uploaded, keyed "u:" + css): each face loads // in its own format and weight, under its own name, by the same rules // as the catalog's three families; a slide the server rendered names an // uploaded family by leading its stack with it; and a family a change // brings in during the visit (a pick here, a collaborator's) is learned // from the changed deck, while one already on screen stays where it is. import { test } from 'node:test' import assert from 'node:assert/strict' import { createFontSession } from './font-session.mjs' import { familiesOfSection, uploadedFamily } from './font-families.mjs' import * as Y from 'yjs' import { familiesOfView } from './font-families.mjs' import { createEditorFontGate } from './editor-fonts.mjs' import { applyOps } from './ops.mjs' const ref = c => c.repeat(64) const manifest = { need: ['sans', 'u:Oddity Sans 2'], families: { sans: { css: 'Liberation Sans', faces: [{ weight: 400, style: 'normal', url: '/s-r' }] } }, uploaded: { 'u:Oddity Sans 2': { css: 'Oddity Sans 2', faces: [ { weight: 400, style: 'normal', url: `/d/abc/fonts/${ref('a')}`, format: 'truetype' }, { weight: '100 900', style: 'italic', url: `/d/abc/fonts/${ref('b')}`, format: 'woff2' } ] }, 'u:Unused Face': { css: 'Unused Face', faces: [{ weight: 400, style: 'normal', url: `/d/abc/fonts/${ref('c')}`, format: 'opentype' }] } } } function world (m = manifest) { const loads = new Map() const made = [] const 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 session = createFontSession({ manifest: m, fontSet: { add: f => set.push(f) }, FontFaceCtor: FakeFace, root, setTimer: (fn, ms) => timers.push({ fn, ms }) }) const flush = () => new Promise(resolve => setImmediate(resolve)) return { session, made, set, attrs, flush, fire: () => { for (const t of timers.splice(0)) t.fn() }, arrive: async (...urls) => { for (const u of urls) loads.get(u).resolve(); await flush() } } } test('an uploaded face loads in its own format and weight, under its name as it is', async () => { const w = world() const gated = w.session.gate(['u:Oddity Sans 2'], 2500) await w.flush() assert.deepEqual(w.made.map(f => [f.family, f.source, f.desc.weight, f.desc.style, f.desc.display]), [ ['Oddity Sans 2', `url("/d/abc/fonts/${ref('a')}") format("truetype")`, '400', 'normal', 'block'], ['Oddity Sans 2', `url("/d/abc/fonts/${ref('b')}") format("woff2")`, '100 900', 'italic', 'block'] ]) assert.equal(w.set.length, 0) await w.arrive(...w.made.map(f => f.url)) assert.deepEqual(await gated, []) assert.equal(w.set.length, 2, 'both faces join the page together, once both are in') assert.ok(w.session.loaded('u:Oddity Sans 2')) }) test('a catalog face is woff2', async () => { const w = world() w.session.gate(['sans'], 2500) await w.flush() assert.deepEqual(w.made.map(f => [f.family, f.source]), [['Liberation Sans', 'url("/s-r") format("woff2")']]) }) test('an uploaded family late past the deadline is committed and never joins', async () => { const w = world() const gated = w.session.gate(['sans', 'u:Oddity Sans 2'], 2500) await w.flush() await w.arrive('/s-r') w.fire() assert.deepEqual(await gated, ['u:Oddity Sans 2']) assert.ok(w.session.committed('u:Oddity Sans 2')) assert.deepEqual(w.session.fallback, ['u:Oddity Sans 2']) assert.equal(w.attrs['data-slides-fonts-fallback'], 'u:Oddity Sans 2') await w.arrive(`/d/abc/fonts/${ref('a')}`, `/d/abc/fonts/${ref('b')}`) w.session.use(['u:Oddity Sans 2']) assert.deepEqual(w.set.map(f => f.family), ['Liberation Sans'], 'the late faces never join the page') }) test('the editor brings in the deck\'s uploaded families too', async () => { const w = world() assert.deepEqual(w.session.keys, ['sans', 'u:Oddity Sans 2', 'u:Unused Face']) const done = w.session.prefetch() for (let i = 0; i < 3; i++) { await w.flush() for (const f of w.made.splice(0)) await w.arrive(f.url) } await done assert.ok(w.session.loaded('u:Unused Face')) }) test('a family the deck takes on during the visit is learned, once', async () => { const w = world() const picked = { family: 'New Pick™', css: 'New Pick', faces: [{ ref: ref('d'), format: 'woff', weight: [300, 700], italic: false, pictures: false }, { ref: '../x', format: 'ttf', weight: [400, 400], italic: false, pictures: true }] } assert.equal(w.session.ready('u:New Pick'), true, 'unknown, so nothing to wait for') w.session.learn([picked, { css: 'Oddity Sans 2', faces: [{ ref: ref('e'), format: 'ttf', weight: [400, 400] }] }], '/s/tok') assert.deepEqual(w.session.uploaded['u:New Pick'], { css: 'New Pick', faces: [{ weight: '300 700', style: 'normal', url: `/s/tok/fonts/${ref('d')}`, format: 'woff' }] }, 'a ref that is not a hash is no URL') assert.equal(w.session.uploaded['u:Oddity Sans 2'].faces[0].url, `/s/tok/fonts/${ref('e')}`, 'a family the page knows with other faces is that set now (font-versions.test.mjs)') assert.equal(w.session.ready('u:New Pick'), false) const ok = w.session.waitFor(['u:New Pick']) await w.flush() await w.arrive(`/s/tok/fonts/${ref('d')}`) assert.equal(await ok, true) }) test('uploadedFamily is the manifest\'s entry for one of meta.fontFiles', () => { assert.deepEqual(uploadedFamily({ css: 'X', faces: [ { ref: ref('1'), format: 'ttf', weight: [400, 400], italic: false }, { ref: ref('2'), format: 'otf', weight: [700, 700], italic: true }, { ref: ref('3'), format: 'woff', weight: [400, 400], italic: false }, { ref: ref('4'), format: 'woff2', weight: [100, 900], italic: true }, { ref: ref('5'), format: 'svg', weight: [400, 400], italic: false } ] }, '/p/sess'), { css: 'X', faces: [ { weight: 400, style: 'normal', url: `/p/sess/fonts/${ref('1')}`, format: 'truetype' }, { weight: 700, style: 'italic', url: `/p/sess/fonts/${ref('2')}`, format: 'opentype' }, { weight: 400, style: 'normal', url: `/p/sess/fonts/${ref('3')}`, format: 'woff' }, { weight: '100 900', style: 'italic', url: `/p/sess/fonts/${ref('4')}`, format: 'woff2' } ] }) assert.equal(uploadedFamily({ css: 'X', faces: [] }, '/d/a'), null) assert.equal(uploadedFamily(null, '/d/a'), null) }) const reader = { styleOf: html => (/]*\sstyle="([^"]*)"/.exec(html) || [])[1] || '', hasCodeIn: html => /<(pre|code)[\s>]/.test(html) } const section = style => `

x

` test('a rendered slide names an uploaded family by leading a stack with it', () => { const html = section("--slides-font-heading:'Oddity Sans 2', Liberation Serif, serif;--slides-font-body:Liberation Sans, sans-serif") assert.deepEqual(familiesOfSection(html, manifest, reader), ['sans', 'serif', 'u:Oddity Sans 2']) const later = section("--slides-font-heading:Georgia, 'Unused Face', serif;--slides-font-body:'Not Listed', sans-serif") assert.deepEqual(familiesOfSection(later, manifest, reader), []) }) // The editor's gate (editor-fonts.mjs) over a real session and a real // deck: what a change brings in is worked out on a copy, as for any // family, and an uploaded family it names is learned from that copy. 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({ need: [], families: manifest.families, themes: { ink: ['Liberation Sans, sans-serif', 'Liberation Sans, sans-serif'] }, defaultTheme: 'ink', code: 'Liberation Mono, monospace' }) 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 } } const pick = css => ({ op: 'set_meta', fonts: { heading: `'${css}', Liberation Sans, sans-serif`, body: '' }, fontFiles: [{ family: css, css, faces: [{ ref: ref('f'), format: 'woff2', weight: [400, 400], italic: false, pictures: false }] }] }) test('a change that brings in an uploaded family this tab never knew is checked, and waits for it', async () => { const w = editorWorld() w.session.use(['sans']) w.session.gate(['sans'], 2500) await w.flush() await w.arrive('/s-r') assert.equal(w.gate.cold(), true, 'a collaborator can bring in a family nobody here has heard of') const keys = w.gate.familiesAfter(d => applyOps(d, [pick('New Pick')])) assert.deepEqual(keys, ['sans', 'u:New Pick']) assert.equal(w.session.uploaded['u:New Pick'].faces[0].url, `/d/abc/fonts/${ref('f')}`) assert.equal(w.session.state('u:New Pick'), 'idle', 'learned, not yet loaded: the change waits for it') assert.equal(w.session.ready('u:New Pick'), false) }) test('an uploaded family already on screen in its fallback is committed there when learned, never loaded', async () => { const w = editorWorld() // A change this tab took in before it could learn the family. applyOps(w.doc, [pick('Seen Face')]) w.gate.familiesAfter(d => applyOps(d, [{ op: 'set_meta', title: 'T2' }])) assert.equal(w.session.committed('u:Seen Face'), true) assert.ok(w.session.inUse.includes('u:Seen Face')) await w.flush() assert.equal(w.made.length, 0, 'no face of it is ever fetched') })