import { test } from 'node:test' import assert from 'node:assert/strict' import * as Y from 'yjs' import { leadFamily, fontChoice, pickOp, plainFontOp, themeStack, mountDeckFonts } from './deck-fonts.mjs' import { applyOps, view } from './ops.mjs' const face = ref => ({ ref: ref.repeat(64), format: 'ttf', weight: [400, 400], italic: false, pictures: true }) const odd = { family: 'Oddity Sans™ 2', css: 'Oddity Sans 2', faces: [face('a')] } const serif = { family: 'Liberation Serif', css: 'Liberation Serif', faces: [face('b')] } const manifest = { themes: { ink: ['Liberation Sans, sans-serif', 'Liberation Sans, sans-serif'], paper: ['Liberation Serif, serif', 'Liberation Sans, sans-serif'] }, defaultTheme: 'ink' } test('a stack leads with its first family, unquoted', () => { assert.equal(leadFamily("'Oddity Sans 2', Liberation Sans, sans-serif"), 'Oddity Sans 2') assert.equal(leadFamily('Georgia, serif'), 'Georgia') assert.equal(leadFamily(''), '') assert.equal(leadFamily(undefined), '') }) test("a field shows the uploaded family its stack leads with, else the stack", () => { const files = [odd] assert.equal(fontChoice("'Oddity Sans 2', Liberation Sans", files), 'Oddity Sans 2') assert.equal(fontChoice('Georgia, serif', files), 'Georgia, serif') assert.equal(fontChoice("'Oddity Sans 2', x", []), "'Oddity Sans 2', x") assert.equal(fontChoice('', files), '') }) test("the theme's stack for a role, an unknown theme's being the default's", () => { assert.equal(themeStack({ theme: 'paper' }, manifest, 'heading'), 'Liberation Serif, serif') assert.equal(themeStack({ theme: 'paper' }, manifest, 'body'), 'Liberation Sans, sans-serif') assert.equal(themeStack({ theme: 'nope' }, manifest, 'heading'), 'Liberation Sans, sans-serif') assert.equal(themeStack({ theme: 'nope' }, null, 'heading'), 'sans-serif') }) test('a pick sets the role in front of the theme stack and lists the family, one op', () => { const meta = { theme: 'paper', fonts: { heading: '', body: 'Georgia, serif' } } assert.deepEqual(pickOp('heading', odd, meta, manifest), { op: 'set_meta', fonts: { heading: "'Oddity Sans 2', Liberation Serif, serif", body: 'Georgia, serif' }, fontFiles: [odd] }) // The same family again replaces its entry; one no role leads with goes. const was = { theme: 'ink', fonts: { heading: "'Oddity Sans 2', x", body: '' }, fontFiles: [{ ...odd, faces: [] }, serif] } assert.deepEqual(pickOp('heading', odd, was, manifest).fontFiles, [odd]) // The other role's family stays. const both = { theme: 'ink', fonts: { heading: "'Liberation Serif', x", body: '' }, fontFiles: [serif] } assert.deepEqual(pickOp('body', odd, both, manifest), { op: 'set_meta', fonts: { heading: "'Liberation Serif', x", body: "'Oddity Sans 2', Liberation Sans, sans-serif" }, fontFiles: [serif, odd] }) }) // The deck names each family (POST /d/{sqid}/fonts): two people's Brands // are Brand and Brand 2, and picking one never takes the other's place // (final review, I3). A name differing only in case is the same name. test("two people's Brands: picking one for the body keeps the other in the heading", () => { const brandA = { family: 'Brand', css: 'Brand', faces: [face('a')] } const brandB = { family: 'Brand', css: 'Brand 2', faces: [face('b')] } const meta = { theme: 'ink', fonts: { heading: "'Brand', Liberation Sans, sans-serif", body: '' }, fontFiles: [brandA] } const op = pickOp('body', brandB, meta, manifest) assert.deepEqual(op.fontFiles, [brandA, brandB]) assert.equal(op.fonts.heading, "'Brand', Liberation Sans, sans-serif") // The same name under another case replaces it, the other role's use // of it included. const grown = { family: 'Brand', css: 'BRAND', faces: [face('a'), face('c')] } const both = { ...meta, fonts: { heading: "'Brand', x", body: "'Brand', y" } } assert.deepEqual(pickOp('heading', grown, both, manifest).fontFiles, [grown]) }) test("another choice drops a family no role leads with any more, in the same op", () => { const meta = { fonts: { heading: "'Oddity Sans 2', x", body: '' }, fontFiles: [odd] } assert.deepEqual(plainFontOp('font-heading', '', meta), { op: 'set_meta', fonts: null, fontFiles: null }) assert.deepEqual(plainFontOp('font-heading', 'Georgia, serif', meta), { op: 'set_meta', fonts: { heading: 'Georgia, serif', body: '' }, fontFiles: null }) // The body still uses it: kept, and fontFiles left alone. const used = { fonts: { heading: "'Oddity Sans 2', x", body: "'Oddity Sans 2', y" }, fontFiles: [odd] } assert.deepEqual(plainFontOp('font-heading', '', used), { op: 'set_meta', fonts: { heading: '', body: "'Oddity Sans 2', y" } }) // No uploaded fonts: the op deckOp has always written. assert.deepEqual(plainFontOp('font-body', 'Georgia, serif', { fonts: null }), { op: 'set_meta', fonts: { heading: '', body: 'Georgia, serif' } }) }) // A field as change() reads it: its value, and whether the chosen option // is in the "Your fonts" group. const field = (value, uploaded) => ({ value, selectedOptions: [{ parentElement: { tagName: uploaded ? 'OPTGROUP' : 'SELECT' } }] }) // A server whose answers come back when the test says, in any order. function deferredServer () { const pending = [] const fetchFn = (url, init) => new Promise(resolve => { const css = JSON.parse(init.body).family pending.push({ css, answer: () => resolve({ ok: true, json: async () => ({ family: css, css, faces: [face('c')] }) }) }) }) return { fetchFn, answer: css => pending.find(p => p.css === css).answer() } } function mounted (server) { const commits = [] const meta = { theme: 'ink', fonts: null } const fonts = mountDeckFonts({ base: '/d/x', getMeta: () => meta, commit: ops => commits.push(ops[0]), refill: () => {}, fetchFn: server.fetchFn, manifest }) return { fonts, commits } } test('a pick answered after a newer choice for its field commits nothing', async () => { const server = deferredServer() const { fonts, commits } = mounted(server) const late = fonts.change('font-heading', field('Liberation Serif', true)) fonts.change('font-heading', field('', false)) // Theme's, before the answer server.answer('Liberation Serif') await late assert.equal(commits.length, 1) assert.deepEqual(commits[0], { op: 'set_meta', fonts: null }) }) test('two picks answered out of order: the newer one is the one written', async () => { const server = deferredServer() const { fonts, commits } = mounted(server) const first = fonts.change('font-heading', field('Liberation Serif', true)) const second = fonts.change('font-heading', field('Oddity Sans 2', true)) server.answer('Oddity Sans 2') await second server.answer('Liberation Serif') await first assert.equal(commits.length, 1) assert.equal(commits[0].fonts.heading, "'Oddity Sans 2', Liberation Sans, sans-serif") }) test("a change to one field does not cancel the other field's pick", async () => { const server = deferredServer() const { fonts, commits } = mounted(server) const body = fonts.change('font-body', field('Liberation Serif', true)) fonts.change('font-heading', field('Georgia, serif', false)) server.answer('Liberation Serif') await body assert.equal(commits.length, 2) assert.equal(commits[1].fonts.body, "'Liberation Serif', Liberation Sans, sans-serif") }) test('the ops are ones the document takes', () => { const doc = new Y.Doc() applyOps(doc, [{ op: 'set_meta', title: 'T', theme: 'ink' }]) const meta = { theme: 'ink', fonts: null } applyOps(doc, [pickOp('heading', odd, meta, manifest)]) assert.deepEqual(view(doc).meta.fontFiles, [odd]) assert.equal(view(doc).meta.fonts.heading, "'Oddity Sans 2', Liberation Sans, sans-serif") applyOps(doc, [plainFontOp('font-heading', '', view(doc).meta)]) assert.equal('fontFiles' in view(doc).meta, false) assert.equal(view(doc).meta.fonts, null) })