import fs from 'node:fs'; import path from 'node:path'; import { describe, expect, it } from 'vitest'; /** * The shipped Whitney SSm binaries, and the contract between them and `fonts.css`. * * These files are the only binary assets in the package, and a binary is exactly the kind of thing * a reviewer cannot check by reading the diff. So the properties that would otherwise be taken on * trust are asserted here: that every `url()` in the stylesheet resolves to a file that exists, * that every file is referenced, that each one is a real woff2 rather than an HTML error page saved * with the wrong extension, and that the twelve weight/style pairs the type roles need are all * declared exactly once. * * WHY THEY ARE SHIPPED AT ALL — hotlinking was tried twice and failed twice. The Signal CloudFront * host does not resolve on every network, and the Twilio CDN that carries all twelve faces sends no * CORS headers, so a cross-origin `@font-face` fails in a browser even though `curl` gets a 200. * `scripts/build-tokens.mjs` records the evidence for both. */ const tokensDir = path.join(process.cwd(), 'src/tokens'); const fontsDir = path.join(tokensDir, 'fonts'); const css = fs.readFileSync(path.join(tokensDir, 'fonts.css'), 'utf8'); /** Every `@font-face` block in source order, with the fields this file asserts on. */ const faces = css .split('@font-face') .slice(1) .map((chunk) => chunk.slice(0, chunk.indexOf('}'))) .map((block) => ({ family: /font-family:\s*"([^"]+)"/.exec(block)?.[1], url: /url\("([^"]+)"\)/.exec(block)?.[1], weight: /font-weight:\s*(\d+)/.exec(block)?.[1], style: /font-style:\s*(\w+)/.exec(block)?.[1], display: /font-display:\s*(\w+)/.exec(block)?.[1], })); const whitney = faces.filter((f) => f.family === 'Whitney SSm'); describe('shipped Whitney SSm binaries', () => { it('declares twelve Whitney faces', () => { // Six cuts, upright and italic. Fewer means a cut was dropped and some role now falls back. expect(whitney).toHaveLength(12); }); it('references each file by a relative path, never a CDN', () => { // A relative `src` is what makes a consumer's bundler resolve and fingerprint these, and what // makes them same-origin at runtime. An absolute URL here would reintroduce both failure modes. for (const face of whitney) { expect(face.url, JSON.stringify(face)).toMatch(/^\.\/fonts\/WhitneySSm-[A-Za-z]+-Pro_Web\.woff2$/); } }); it('resolves every reference to a file that exists', () => { const missing = whitney .map((f) => f.url!.replace('./fonts/', '')) .filter((name) => !fs.existsSync(path.join(fontsDir, name))); expect(missing).toEqual([]); }); it('ships no font the stylesheet does not reference', () => { // Dead weight in the tarball, and a sign a cut was renamed rather than replaced. const referenced = new Set(whitney.map((f) => f.url!.replace('./fonts/', ''))); const orphans = fs.readdirSync(fontsDir).filter((name) => !referenced.has(name)); expect(orphans).toEqual([]); }); it('is a real woff2 in every case, not an error page with the wrong extension', () => { // The download that produced these went through a CDN; an HTML error body saved as .woff2 would // be the wrong size and the wrong magic number, and would fail silently at runtime. for (const name of fs.readdirSync(fontsDir)) { const file = fs.readFileSync(path.join(fontsDir, name)); expect(file.subarray(0, 4).toString('latin1'), `${name} magic number`).toBe('wOF2'); // The real cuts are 66-87 kB; anything far outside that is not a Whitney face. expect(file.byteLength, `${name} size`).toBeGreaterThan(50_000); expect(file.byteLength, `${name} size`).toBeLessThan(150_000); } }); it('declares each weight/style pair exactly once', () => { // A duplicate would mean one silently shadows the other, which is how a wrong cut ships. const pairs = whitney.map((f) => `${f.weight}/${f.style}`); expect(new Set(pairs).size).toBe(pairs.length); expect([...new Set(pairs)].sort()).toEqual( [ '300/italic', '300/normal', '400/italic', '400/normal', '500/italic', '500/normal', '600/italic', '600/normal', '700/italic', '700/normal', '800/italic', '800/normal', ].sort() ); }); it('covers every CSS weight the type roles ask for', () => { // The roles reach for 400, 500, 600 and 800, plus 420 which CSS font matching resolves to the // nearest declared weight. Medium 500 and Black 800 are the two the internal font repos lack, // so they are the ones worth naming explicitly. const theme = fs.readFileSync(path.join(tokensDir, 'theme.css'), 'utf8'); const wanted = new Set([...theme.matchAll(/--font-weight-[a-z0-9-]+:\s*(\d+)/g)].map((m) => Number(m[1]))); const declared = new Set(whitney.map((f) => Number(f.weight))); for (const w of [500, 800]) expect(declared, `weight ${w}`).toContain(w); // 420 has no cut and is not meant to: it matches against 400/500. const unmatched = [...wanted].filter((w) => !declared.has(w)); expect(unmatched).toEqual([420]); }); it('keeps font-display: swap on every face', () => { // Text stays visible while a face loads. Now that the files are local this matters less, but a // face without it blocks paint. for (const face of whitney) expect(face.display, JSON.stringify(face)).toBe('swap'); }); }); describe('Twilio Sans Mono stays hotlinked', () => { const mono = faces.filter((f) => f.family === 'Twilio Sans Mono'); it('points at the paste-fonts CDN, which sends CORS headers', () => { // Deliberately not shipped: that host resolves everywhere tested and sends // `access-control-allow-origin: *`, so it has neither problem that forced Whitney into the // package. Asserted so a later "make it consistent" change is a decision rather than a drift. expect(mono).toHaveLength(4); for (const face of mono) { expect(face.url, JSON.stringify(face)).toMatch(/^https:\/\/assets\.twilio\.com\/public_assets\/paste-fonts\//); } }); });