oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// font-versions.test.mjs — an uploaded family whose faces change during
2// the visit (final review, I4). A family is known by its css name AND
3// its set of faces: add a bold face to Brand and pick it again, and the
4// deck's Brand is a new set the tab has not loaded. It is learned again
5// and made ready before the change shows, like any family the change
6// brings in; its faces then join the page together, in place of the
7// old set's, in one go. Undo and redo move between sets the tab has
8// already loaded, so they swap at once.
9import { test } from 'node:test'
10import assert from 'node:assert/strict'
11import * as Y from 'yjs'
12import { createFontSession } from './font-session.mjs'
13import { familiesOfView } from './font-families.mjs'
14import { createEditorFontGate } from './editor-fonts.mjs'
15import { applyOps, view } from './ops.mjs'
16
17const ref = c => c.repeat(64)
18const face = (c, weight) => ({ ref: ref(c), format: 'ttf', weight: [weight, weight], italic: false, pictures: true })
19const regular = { family: 'Brand', css: 'Brand', faces: [face('a', 400)] }
20const withBold = { family: 'Brand', css: 'Brand', faces: [face('a', 400), face('b', 700)] }
21const pick = fam => ({ op: 'set_meta', fonts: { heading: "'Brand', Liberation Sans, sans-serif", body: '' }, fontFiles: [fam] })
22
23function world () {
24 const loads = new Map()
25 const made = []
26 const set = new Set()
27 const timers = []
28 class FakeFace {
29 constructor (family, source, desc) {
30 Object.assign(this, { family, source, desc })
31 this.url = /url\("([^"]+)"\)/.exec(source)[1]
32 made.push(this)
33 }
34
35 load () {
36 return new Promise((resolve, reject) => loads.set(this.url, { resolve, reject }))
37 }
38 }
39 const attrs = {}
40 const root = { getAttribute: k => attrs[k] ?? null, setAttribute: (k, v) => { attrs[k] = v } }
41 const manifest = {
42 need: [],
43 families: {},
44 themes: { ink: ['Liberation Sans, sans-serif', 'Liberation Sans, sans-serif'] },
45 defaultTheme: 'ink',
46 code: 'Liberation Mono, monospace'
47 }
48 const fontSet = { add: f => set.add(f), delete: f => set.delete(f) }
49 const session = createFontSession({ manifest, fontSet, FontFaceCtor: FakeFace, root, setTimer: (fn, ms) => timers.push({ fn, ms }) })
50 const flush = () => new Promise(resolve => setImmediate(resolve))
51 return {
52 session,
53 made,
54 set,
55 flush,
56 fire: () => { for (const t of timers.splice(0)) t.fn() },
57 // Every face made so far arrives.
58 arriveAll: async () => { for (const [, l] of loads) l.resolve(); await flush() },
59 // The page's Brand faces: [url, weight] of each in document.fonts.
60 onPage: () => [...set].filter(f => f.family === 'Brand').map(f => [f.url.slice(-4), f.desc.weight]).sort()
61 }
62}
63
64test('a family whose faces change is learned again, and its new set joins in place of the old, once all have loaded', async () => {
65 const w = world()
66 w.session.learn([regular], '/d/abc')
67 const first = w.session.prepare(['u:Brand'])
68 await w.flush()
69 await w.arriveAll()
70 await first
71 assert.deepEqual(w.onPage(), [['aaaa', '400']])
72
73 // A bold added and the family picked again: a set this tab has not got.
74 w.session.learn([withBold], '/d/abc')
75 assert.equal(w.session.ready('u:Brand'), false, 'the new set is not ready: a change that shows it waits')
76 const second = w.session.prepare(['u:Brand'])
77 await w.flush()
78 assert.deepEqual(w.onPage(), [['aaaa', '400']], 'nothing changes on the page while the new set loads')
79 await w.arriveAll()
80 await second
81 assert.deepEqual(w.onPage(), [['aaaa', '400'], ['bbbb', '700']], 'both faces of the new set, the old set gone')
82
83 // Undo: back to the set the tab already has, at once.
84 w.session.learn([regular], '/d/abc')
85 assert.equal(w.session.ready('u:Brand'), true)
86 w.session.use(['u:Brand'])
87 assert.deepEqual(w.onPage(), [['aaaa', '400']])
88 // Redo.
89 w.session.learn([withBold], '/d/abc')
90 assert.equal(w.session.ready('u:Brand'), true)
91 w.session.use(['u:Brand'])
92 assert.deepEqual(w.onPage(), [['aaaa', '400'], ['bbbb', '700']])
93})
94
95test('a new set late past its timeout leaves the old set on the page, never a fallback', async () => {
96 const w = world()
97 w.session.learn([regular], '/d/abc')
98 const first = w.session.prepare(['u:Brand'])
99 await w.flush()
100 await w.arriveAll()
101 await first
102 w.session.learn([withBold], '/d/abc')
103 const late = w.session.prepare(['u:Brand'])
104 await w.flush()
105 w.fire()
106 await late
107 assert.deepEqual(w.onPage(), [['aaaa', '400']], 'the old set stays; the late one never joins')
108 await w.arriveAll()
109 w.session.use(['u:Brand'])
110 assert.deepEqual(w.onPage(), [['aaaa', '400']])
111})
112
113// The editor: a pick of the grown family waits for its new set, and an
114// undo or a redo moves between sets in the same task as the change.
115function editorWorld () {
116 globalThis.document = { activeElement: null, body: {}, documentElement: {} }
117 globalThis.addEventListener = () => {}
118 globalThis.removeEventListener = () => {}
119 const el = () => ({ getAttribute: () => null, setAttribute () {}, removeAttribute () {}, querySelector: () => null, addEventListener () {} })
120 const root = { dataset: { deck: 'abc' }, querySelector: () => el() }
121 const w = world()
122 Object.assign(w.session, { familiesOfView: v => familiesOfView(v, w.session.manifest) })
123 const doc = new Y.Doc()
124 applyOps(doc, [{ op: 'set_meta', title: 'T', theme: 'ink' }, { op: 'add_slide', id: 'slideone0001', layout: 'flow', template: 'title-body' }])
125 const gate = createEditorFontGate({ fonts: w.session, root, getDoc: () => doc })
126 return { ...w, doc, gate }
127}
128
129test('the editor: picking the grown family waits for its new set; undo and redo swap sets with the change', async () => {
130 const w = editorWorld()
131 // Regular picked and on the page.
132 let keys = w.gate.familiesAfter(d => applyOps(d, [pick(regular)]))
133 const one = w.gate.gate(keys, () => { applyOps(w.doc, [pick(regular)]); return true })
134 await w.flush()
135 await w.arriveAll()
136 assert.equal(await one, true)
137 assert.deepEqual(w.onPage(), [['aaaa', '400']])
138
139 // Bold added and picked again: the change waits for the new set.
140 keys = w.gate.familiesAfter(d => applyOps(d, [pick(withBold)]))
141 assert.deepEqual(keys, ['sans', 'u:Brand'])
142 assert.equal(w.session.ready('u:Brand'), false)
143 let ran = false
144 const two = w.gate.gate(keys, () => { applyOps(w.doc, [pick(withBold)]); ran = true; return true })
145 await w.flush()
146 assert.equal(ran, false, 'not before the new set is in')
147 await w.arriveAll()
148 assert.equal(await two, true)
149 assert.deepEqual(w.onPage(), [['aaaa', '400'], ['bbbb', '700']])
150
151 // Undo, then redo: the deck's set changes and the page's with it.
152 applyOps(w.doc, [pick(regular)])
153 w.gate.afterHistory()
154 assert.deepEqual(w.onPage(), [['aaaa', '400']])
155 applyOps(w.doc, [pick(withBold)])
156 w.gate.afterHistory()
157 assert.deepEqual(w.onPage(), [['aaaa', '400'], ['bbbb', '700']])
158 assert.deepEqual(view(w.doc).meta.fontFiles, [withBold])
159})
160
161test('a change refused while its new set loads leaves the tab on the deck\'s own set', async () => {
162 const w = editorWorld()
163 let keys = w.gate.familiesAfter(d => applyOps(d, [pick(regular)]))
164 const one = w.gate.gate(keys, () => { applyOps(w.doc, [pick(regular)]); return true })
165 await w.flush()
166 await w.arriveAll()
167 await one
168 keys = w.gate.familiesAfter(d => applyOps(d, [pick(withBold)]))
169 // Worked out but never made: the next look at the deck is the deck's.
170 w.gate.familiesAfter(d => applyOps(d, [{ op: 'set_meta', title: 'T2' }]))
171 assert.equal(w.session.ready('u:Brand'), true, 'the deck still has the regular set, which is in')
172 assert.equal(w.session.uploaded['u:Brand'].faces.length, 1)
173})
174