oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1// font-uploaded.test.mjs — a deck's own uploaded fonts in the gate
2// (fontgate.go's manifest.uploaded, keyed "u:" + css): each face loads
3// in its own format and weight, under its own name, by the same rules
4// as the catalog's three families; a slide the server rendered names an
5// uploaded family by leading its stack with it; and a family a change
6// brings in during the visit (a pick here, a collaborator's) is learned
7// from the changed deck, while one already on screen stays where it is.
8import { test } from 'node:test'
9import assert from 'node:assert/strict'
10import { createFontSession } from './font-session.mjs'
11import { familiesOfSection, uploadedFamily } from './font-families.mjs'
12import * as Y from 'yjs'
13import { familiesOfView } from './font-families.mjs'
14import { createEditorFontGate } from './editor-fonts.mjs'
15import { applyOps } from './ops.mjs'
16
17const ref = c => c.repeat(64)
18const manifest = {
19 need: ['sans', 'u:Oddity Sans 2'],
20 families: {
21 sans: { css: 'Liberation Sans', faces: [{ weight: 400, style: 'normal', url: '/s-r' }] }
22 },
23 uploaded: {
24 'u:Oddity Sans 2': {
25 css: 'Oddity Sans 2',
26 faces: [
27 { weight: 400, style: 'normal', url: `/d/abc/fonts/${ref('a')}`, format: 'truetype' },
28 { weight: '100 900', style: 'italic', url: `/d/abc/fonts/${ref('b')}`, format: 'woff2' }
29 ]
30 },
31 'u:Unused Face': { css: 'Unused Face', faces: [{ weight: 400, style: 'normal', url: `/d/abc/fonts/${ref('c')}`, format: 'opentype' }] }
32 }
33}
34
35function world (m = manifest) {
36 const loads = new Map()
37 const made = []
38 const set = []
39 const timers = []
40 class FakeFace {
41 constructor (family, source, desc) {
42 Object.assign(this, { family, source, desc })
43 this.url = /url\("([^"]+)"\)/.exec(source)[1]
44 made.push(this)
45 }
46
47 load () {
48 return new Promise((resolve, reject) => loads.set(this.url, { resolve, reject }))
49 }
50 }
51 const attrs = {}
52 const root = { getAttribute: k => attrs[k] ?? null, setAttribute: (k, v) => { attrs[k] = v } }
53 const session = createFontSession({ manifest: m, fontSet: { add: f => set.push(f) }, FontFaceCtor: FakeFace, root, setTimer: (fn, ms) => timers.push({ fn, ms }) })
54 const flush = () => new Promise(resolve => setImmediate(resolve))
55 return {
56 session,
57 made,
58 set,
59 attrs,
60 flush,
61 fire: () => { for (const t of timers.splice(0)) t.fn() },
62 arrive: async (...urls) => { for (const u of urls) loads.get(u).resolve(); await flush() }
63 }
64}
65
66test('an uploaded face loads in its own format and weight, under its name as it is', async () => {
67 const w = world()
68 const gated = w.session.gate(['u:Oddity Sans 2'], 2500)
69 await w.flush()
70 assert.deepEqual(w.made.map(f => [f.family, f.source, f.desc.weight, f.desc.style, f.desc.display]), [
71 ['Oddity Sans 2', `url("/d/abc/fonts/${ref('a')}") format("truetype")`, '400', 'normal', 'block'],
72 ['Oddity Sans 2', `url("/d/abc/fonts/${ref('b')}") format("woff2")`, '100 900', 'italic', 'block']
73 ])
74 assert.equal(w.set.length, 0)
75 await w.arrive(...w.made.map(f => f.url))
76 assert.deepEqual(await gated, [])
77 assert.equal(w.set.length, 2, 'both faces join the page together, once both are in')
78 assert.ok(w.session.loaded('u:Oddity Sans 2'))
79})
80
81test('a catalog face is woff2', async () => {
82 const w = world()
83 w.session.gate(['sans'], 2500)
84 await w.flush()
85 assert.deepEqual(w.made.map(f => [f.family, f.source]), [['Liberation Sans', 'url("/s-r") format("woff2")']])
86})
87
88test('an uploaded family late past the deadline is committed and never joins', async () => {
89 const w = world()
90 const gated = w.session.gate(['sans', 'u:Oddity Sans 2'], 2500)
91 await w.flush()
92 await w.arrive('/s-r')
93 w.fire()
94 assert.deepEqual(await gated, ['u:Oddity Sans 2'])
95 assert.ok(w.session.committed('u:Oddity Sans 2'))
96 assert.deepEqual(w.session.fallback, ['u:Oddity Sans 2'])
97 assert.equal(w.attrs['data-slides-fonts-fallback'], 'u:Oddity Sans 2')
98 await w.arrive(`/d/abc/fonts/${ref('a')}`, `/d/abc/fonts/${ref('b')}`)
99 w.session.use(['u:Oddity Sans 2'])
100 assert.deepEqual(w.set.map(f => f.family), ['Liberation Sans'], 'the late faces never join the page')
101})
102
103test('the editor brings in the deck\'s uploaded families too', async () => {
104 const w = world()
105 assert.deepEqual(w.session.keys, ['sans', 'u:Oddity Sans 2', 'u:Unused Face'])
106 const done = w.session.prefetch()
107 for (let i = 0; i < 3; i++) {
108 await w.flush()
109 for (const f of w.made.splice(0)) await w.arrive(f.url)
110 }
111 await done
112 assert.ok(w.session.loaded('u:Unused Face'))
113})
114
115test('a family the deck takes on during the visit is learned, once', async () => {
116 const w = world()
117 const picked = {
118 family: 'New Pick™',
119 css: 'New Pick',
120 faces: [{ ref: ref('d'), format: 'woff', weight: [300, 700], italic: false, pictures: false }, { ref: '../x', format: 'ttf', weight: [400, 400], italic: false, pictures: true }]
121 }
122 assert.equal(w.session.ready('u:New Pick'), true, 'unknown, so nothing to wait for')
123 w.session.learn([picked, { css: 'Oddity Sans 2', faces: [{ ref: ref('e'), format: 'ttf', weight: [400, 400] }] }], '/s/tok')
124 assert.deepEqual(w.session.uploaded['u:New Pick'], {
125 css: 'New Pick',
126 faces: [{ weight: '300 700', style: 'normal', url: `/s/tok/fonts/${ref('d')}`, format: 'woff' }]
127 }, 'a ref that is not a hash is no URL')
128 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)')
129 assert.equal(w.session.ready('u:New Pick'), false)
130 const ok = w.session.waitFor(['u:New Pick'])
131 await w.flush()
132 await w.arrive(`/s/tok/fonts/${ref('d')}`)
133 assert.equal(await ok, true)
134})
135
136test('uploadedFamily is the manifest\'s entry for one of meta.fontFiles', () => {
137 assert.deepEqual(uploadedFamily({
138 css: 'X',
139 faces: [
140 { ref: ref('1'), format: 'ttf', weight: [400, 400], italic: false },
141 { ref: ref('2'), format: 'otf', weight: [700, 700], italic: true },
142 { ref: ref('3'), format: 'woff', weight: [400, 400], italic: false },
143 { ref: ref('4'), format: 'woff2', weight: [100, 900], italic: true },
144 { ref: ref('5'), format: 'svg', weight: [400, 400], italic: false }
145 ]
146 }, '/p/sess'), {
147 css: 'X',
148 faces: [
149 { weight: 400, style: 'normal', url: `/p/sess/fonts/${ref('1')}`, format: 'truetype' },
150 { weight: 700, style: 'italic', url: `/p/sess/fonts/${ref('2')}`, format: 'opentype' },
151 { weight: 400, style: 'normal', url: `/p/sess/fonts/${ref('3')}`, format: 'woff' },
152 { weight: '100 900', style: 'italic', url: `/p/sess/fonts/${ref('4')}`, format: 'woff2' }
153 ]
154 })
155 assert.equal(uploadedFamily({ css: 'X', faces: [] }, '/d/a'), null)
156 assert.equal(uploadedFamily(null, '/d/a'), null)
157})
158
159const reader = {
160 styleOf: html => (/<section[^>]*\sstyle="([^"]*)"/.exec(html) || [])[1] || '',
161 hasCodeIn: html => /<(pre|code)[\s>]/.test(html)
162}
163const section = style => `<section slides-slide data-layout="flow" style="${style}"><p>x</p></section>`
164
165test('a rendered slide names an uploaded family by leading a stack with it', () => {
166 const html = section("--slides-font-heading:'Oddity Sans 2', Liberation Serif, serif;--slides-font-body:Liberation Sans, sans-serif")
167 assert.deepEqual(familiesOfSection(html, manifest, reader), ['sans', 'serif', 'u:Oddity Sans 2'])
168 const later = section("--slides-font-heading:Georgia, 'Unused Face', serif;--slides-font-body:'Not Listed', sans-serif")
169 assert.deepEqual(familiesOfSection(later, manifest, reader), [])
170})
171
172
173// The editor's gate (editor-fonts.mjs) over a real session and a real
174// deck: what a change brings in is worked out on a copy, as for any
175// family, and an uploaded family it names is learned from that copy.
176function editorWorld () {
177 globalThis.document = { activeElement: null, body: {}, documentElement: {} }
178 globalThis.addEventListener = () => {}
179 globalThis.removeEventListener = () => {}
180 const el = () => ({ getAttribute: () => null, setAttribute () {}, removeAttribute () {}, querySelector: () => null, addEventListener () {} })
181 const root = { dataset: { deck: 'abc' }, querySelector: () => el() }
182 const w = world({ need: [], families: manifest.families, themes: { ink: ['Liberation Sans, sans-serif', 'Liberation Sans, sans-serif'] }, defaultTheme: 'ink', code: 'Liberation Mono, monospace' })
183 Object.assign(w.session, { familiesOfView: v => familiesOfView(v, w.session.manifest) })
184 const doc = new Y.Doc()
185 applyOps(doc, [{ op: 'set_meta', title: 'T', theme: 'ink' }, { op: 'add_slide', id: 'slideone0001', layout: 'flow', template: 'title-body' }])
186 const gate = createEditorFontGate({ fonts: w.session, root, getDoc: () => doc })
187 return { ...w, doc, gate }
188}
189const pick = css => ({
190 op: 'set_meta',
191 fonts: { heading: `'${css}', Liberation Sans, sans-serif`, body: '' },
192 fontFiles: [{ family: css, css, faces: [{ ref: ref('f'), format: 'woff2', weight: [400, 400], italic: false, pictures: false }] }]
193})
194
195test('a change that brings in an uploaded family this tab never knew is checked, and waits for it', async () => {
196 const w = editorWorld()
197 w.session.use(['sans'])
198 w.session.gate(['sans'], 2500)
199 await w.flush()
200 await w.arrive('/s-r')
201 assert.equal(w.gate.cold(), true, 'a collaborator can bring in a family nobody here has heard of')
202 const keys = w.gate.familiesAfter(d => applyOps(d, [pick('New Pick')]))
203 assert.deepEqual(keys, ['sans', 'u:New Pick'])
204 assert.equal(w.session.uploaded['u:New Pick'].faces[0].url, `/d/abc/fonts/${ref('f')}`)
205 assert.equal(w.session.state('u:New Pick'), 'idle', 'learned, not yet loaded: the change waits for it')
206 assert.equal(w.session.ready('u:New Pick'), false)
207})
208
209test('an uploaded family already on screen in its fallback is committed there when learned, never loaded', async () => {
210 const w = editorWorld()
211 // A change this tab took in before it could learn the family.
212 applyOps(w.doc, [pick('Seen Face')])
213 w.gate.familiesAfter(d => applyOps(d, [{ op: 'set_meta', title: 'T2' }]))
214 assert.equal(w.session.committed('u:Seen Face'), true)
215 assert.ok(w.session.inUse.includes('u:Seen Face'))
216 await w.flush()
217 assert.equal(w.made.length, 0, 'no face of it is ever fetched')
218})
219