| 1 | import { test } from 'node:test' |
| 2 | import assert from 'node:assert/strict' |
| 3 | import * as Y from 'yjs' |
| 4 | |
| 5 | import { leadFamily, fontChoice, pickOp, plainFontOp, themeStack, mountDeckFonts } from './deck-fonts.mjs' |
| 6 | import { applyOps, view } from './ops.mjs' |
| 7 | |
| 8 | const face = ref => ({ ref: ref.repeat(64), format: 'ttf', weight: [400, 400], italic: false, pictures: true }) |
| 9 | const odd = { family: 'Oddity Sans™ 2', css: 'Oddity Sans 2', faces: [face('a')] } |
| 10 | const serif = { family: 'Liberation Serif', css: 'Liberation Serif', faces: [face('b')] } |
| 11 | const manifest = { themes: { ink: ['Liberation Sans, sans-serif', 'Liberation Sans, sans-serif'], paper: ['Liberation Serif, serif', 'Liberation Sans, sans-serif'] }, defaultTheme: 'ink' } |
| 12 | |
| 13 | test('a stack leads with its first family, unquoted', () => { |
| 14 | assert.equal(leadFamily("'Oddity Sans 2', Liberation Sans, sans-serif"), 'Oddity Sans 2') |
| 15 | assert.equal(leadFamily('Georgia, serif'), 'Georgia') |
| 16 | assert.equal(leadFamily(''), '') |
| 17 | assert.equal(leadFamily(undefined), '') |
| 18 | }) |
| 19 | |
| 20 | test("a field shows the uploaded family its stack leads with, else the stack", () => { |
| 21 | const files = [odd] |
| 22 | assert.equal(fontChoice("'Oddity Sans 2', Liberation Sans", files), 'Oddity Sans 2') |
| 23 | assert.equal(fontChoice('Georgia, serif', files), 'Georgia, serif') |
| 24 | assert.equal(fontChoice("'Oddity Sans 2', x", []), "'Oddity Sans 2', x") |
| 25 | assert.equal(fontChoice('', files), '') |
| 26 | }) |
| 27 | |
| 28 | test("the theme's stack for a role, an unknown theme's being the default's", () => { |
| 29 | assert.equal(themeStack({ theme: 'paper' }, manifest, 'heading'), 'Liberation Serif, serif') |
| 30 | assert.equal(themeStack({ theme: 'paper' }, manifest, 'body'), 'Liberation Sans, sans-serif') |
| 31 | assert.equal(themeStack({ theme: 'nope' }, manifest, 'heading'), 'Liberation Sans, sans-serif') |
| 32 | assert.equal(themeStack({ theme: 'nope' }, null, 'heading'), 'sans-serif') |
| 33 | }) |
| 34 | |
| 35 | test('a pick sets the role in front of the theme stack and lists the family, one op', () => { |
| 36 | const meta = { theme: 'paper', fonts: { heading: '', body: 'Georgia, serif' } } |
| 37 | assert.deepEqual(pickOp('heading', odd, meta, manifest), { |
| 38 | op: 'set_meta', |
| 39 | fonts: { heading: "'Oddity Sans 2', Liberation Serif, serif", body: 'Georgia, serif' }, |
| 40 | fontFiles: [odd] |
| 41 | }) |
| 42 | // The same family again replaces its entry; one no role leads with goes. |
| 43 | const was = { theme: 'ink', fonts: { heading: "'Oddity Sans 2', x", body: '' }, fontFiles: [{ ...odd, faces: [] }, serif] } |
| 44 | assert.deepEqual(pickOp('heading', odd, was, manifest).fontFiles, [odd]) |
| 45 | // The other role's family stays. |
| 46 | const both = { theme: 'ink', fonts: { heading: "'Liberation Serif', x", body: '' }, fontFiles: [serif] } |
| 47 | assert.deepEqual(pickOp('body', odd, both, manifest), { |
| 48 | op: 'set_meta', |
| 49 | fonts: { heading: "'Liberation Serif', x", body: "'Oddity Sans 2', Liberation Sans, sans-serif" }, |
| 50 | fontFiles: [serif, odd] |
| 51 | }) |
| 52 | }) |
| 53 | |
| 54 | // The deck names each family (POST /d/{sqid}/fonts): two people's Brands |
| 55 | // are Brand and Brand 2, and picking one never takes the other's place |
| 56 | // (final review, I3). A name differing only in case is the same name. |
| 57 | test("two people's Brands: picking one for the body keeps the other in the heading", () => { |
| 58 | const brandA = { family: 'Brand', css: 'Brand', faces: [face('a')] } |
| 59 | const brandB = { family: 'Brand', css: 'Brand 2', faces: [face('b')] } |
| 60 | const meta = { theme: 'ink', fonts: { heading: "'Brand', Liberation Sans, sans-serif", body: '' }, fontFiles: [brandA] } |
| 61 | const op = pickOp('body', brandB, meta, manifest) |
| 62 | assert.deepEqual(op.fontFiles, [brandA, brandB]) |
| 63 | assert.equal(op.fonts.heading, "'Brand', Liberation Sans, sans-serif") |
| 64 | // The same name under another case replaces it, the other role's use |
| 65 | // of it included. |
| 66 | const grown = { family: 'Brand', css: 'BRAND', faces: [face('a'), face('c')] } |
| 67 | const both = { ...meta, fonts: { heading: "'Brand', x", body: "'Brand', y" } } |
| 68 | assert.deepEqual(pickOp('heading', grown, both, manifest).fontFiles, [grown]) |
| 69 | }) |
| 70 | |
| 71 | test("another choice drops a family no role leads with any more, in the same op", () => { |
| 72 | const meta = { fonts: { heading: "'Oddity Sans 2', x", body: '' }, fontFiles: [odd] } |
| 73 | assert.deepEqual(plainFontOp('font-heading', '', meta), { op: 'set_meta', fonts: null, fontFiles: null }) |
| 74 | assert.deepEqual(plainFontOp('font-heading', 'Georgia, serif', meta), |
| 75 | { op: 'set_meta', fonts: { heading: 'Georgia, serif', body: '' }, fontFiles: null }) |
| 76 | // The body still uses it: kept, and fontFiles left alone. |
| 77 | const used = { fonts: { heading: "'Oddity Sans 2', x", body: "'Oddity Sans 2', y" }, fontFiles: [odd] } |
| 78 | assert.deepEqual(plainFontOp('font-heading', '', used), { op: 'set_meta', fonts: { heading: '', body: "'Oddity Sans 2', y" } }) |
| 79 | // No uploaded fonts: the op deckOp has always written. |
| 80 | assert.deepEqual(plainFontOp('font-body', 'Georgia, serif', { fonts: null }), { op: 'set_meta', fonts: { heading: '', body: 'Georgia, serif' } }) |
| 81 | }) |
| 82 | |
| 83 | // A field as change() reads it: its value, and whether the chosen option |
| 84 | // is in the "Your fonts" group. |
| 85 | const field = (value, uploaded) => ({ value, selectedOptions: [{ parentElement: { tagName: uploaded ? 'OPTGROUP' : 'SELECT' } }] }) |
| 86 | |
| 87 | // A server whose answers come back when the test says, in any order. |
| 88 | function deferredServer () { |
| 89 | const pending = [] |
| 90 | const fetchFn = (url, init) => new Promise(resolve => { |
| 91 | const css = JSON.parse(init.body).family |
| 92 | pending.push({ css, answer: () => resolve({ ok: true, json: async () => ({ family: css, css, faces: [face('c')] }) }) }) |
| 93 | }) |
| 94 | return { fetchFn, answer: css => pending.find(p => p.css === css).answer() } |
| 95 | } |
| 96 | |
| 97 | function mounted (server) { |
| 98 | const commits = [] |
| 99 | const meta = { theme: 'ink', fonts: null } |
| 100 | const fonts = mountDeckFonts({ base: '/d/x', getMeta: () => meta, commit: ops => commits.push(ops[0]), refill: () => {}, fetchFn: server.fetchFn, manifest }) |
| 101 | return { fonts, commits } |
| 102 | } |
| 103 | |
| 104 | test('a pick answered after a newer choice for its field commits nothing', async () => { |
| 105 | const server = deferredServer() |
| 106 | const { fonts, commits } = mounted(server) |
| 107 | const late = fonts.change('font-heading', field('Liberation Serif', true)) |
| 108 | fonts.change('font-heading', field('', false)) // Theme's, before the answer |
| 109 | server.answer('Liberation Serif') |
| 110 | await late |
| 111 | assert.equal(commits.length, 1) |
| 112 | assert.deepEqual(commits[0], { op: 'set_meta', fonts: null }) |
| 113 | }) |
| 114 | |
| 115 | test('two picks answered out of order: the newer one is the one written', async () => { |
| 116 | const server = deferredServer() |
| 117 | const { fonts, commits } = mounted(server) |
| 118 | const first = fonts.change('font-heading', field('Liberation Serif', true)) |
| 119 | const second = fonts.change('font-heading', field('Oddity Sans 2', true)) |
| 120 | server.answer('Oddity Sans 2') |
| 121 | await second |
| 122 | server.answer('Liberation Serif') |
| 123 | await first |
| 124 | assert.equal(commits.length, 1) |
| 125 | assert.equal(commits[0].fonts.heading, "'Oddity Sans 2', Liberation Sans, sans-serif") |
| 126 | }) |
| 127 | |
| 128 | test("a change to one field does not cancel the other field's pick", async () => { |
| 129 | const server = deferredServer() |
| 130 | const { fonts, commits } = mounted(server) |
| 131 | const body = fonts.change('font-body', field('Liberation Serif', true)) |
| 132 | fonts.change('font-heading', field('Georgia, serif', false)) |
| 133 | server.answer('Liberation Serif') |
| 134 | await body |
| 135 | assert.equal(commits.length, 2) |
| 136 | assert.equal(commits[1].fonts.body, "'Liberation Serif', Liberation Sans, sans-serif") |
| 137 | }) |
| 138 | |
| 139 | test('the ops are ones the document takes', () => { |
| 140 | const doc = new Y.Doc() |
| 141 | applyOps(doc, [{ op: 'set_meta', title: 'T', theme: 'ink' }]) |
| 142 | const meta = { theme: 'ink', fonts: null } |
| 143 | applyOps(doc, [pickOp('heading', odd, meta, manifest)]) |
| 144 | assert.deepEqual(view(doc).meta.fontFiles, [odd]) |
| 145 | assert.equal(view(doc).meta.fonts.heading, "'Oddity Sans 2', Liberation Sans, sans-serif") |
| 146 | applyOps(doc, [plainFontOp('font-heading', '', view(doc).meta)]) |
| 147 | assert.equal('fontFiles' in view(doc).meta, false) |
| 148 | assert.equal(view(doc).meta.fonts, null) |
| 149 | }) |
| 150 | |