AI-native design editor. Open-source Figma alternative.
Add unit tests for font helpers: isVariableFont, normalizeFontFamily, styleToVariant, fetchBundledFont
Danila Poyarkov283b836d5098
diff --git a/packages/core/src/fonts.ts b/packages/core/src/fonts.tsindex b292ee368..b11f5674e 100644--- a/packages/core/src/fonts.ts+++ b/packages/core/src/fonts.ts@@ -65,7 +65,7 @@ const BUNDLED_FONTS: Record<string, string> = {const googleFontsCache = new Map<string, Record<string, string>>()const googleFontsFailed = new Set<string>()-function normalizeFontFamily(family: string): string {+export function normalizeFontFamily(family: string): string {return family.replace(/\s+Variable$/i, '')}@@ -111,7 +111,7 @@ async function fetchGoogleFontFiles(family: string): Promise<Record<string, strireturn files}-function styleToVariant(style: string): string {+export function styleToVariant(style: string): string {const weight = styleToWeight(style)const italic = style.toLowerCase().includes('italic')if (weight === 400 && !italic) return 'regular'@@ -164,7 +164,7 @@ async function findLocalFont(}}-async function fetchBundledFont(url: string): Promise<ArrayBuffer | null> {+export async function fetchBundledFont(url: string): Promise<ArrayBuffer | null> {if (IS_BROWSER) {const response = await fetch(url)return response.arrayBuffer()@@ -217,7 +217,7 @@ export async function loadFont(family: string, style = 'Regular'): Promise<Arrayreturn null}-function isVariableFont(data: ArrayBuffer): boolean {+export function isVariableFont(data: ArrayBuffer): boolean {if (data.byteLength < 12) return falseconst view = new DataView(data)const numTables = view.getUint16(4)diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex 23976c338..3c02edc0f 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -95,7 +95,11 @@ export {getCJKFallbackFamily,setCJKFallbackFamily,styleToWeight,- weightToStyle+ weightToStyle,+ normalizeFontFamily,+ isVariableFont,+ styleToVariant,+ fetchBundledFont} from './fonts'export {parseColor,diff --git a/tests/engine/fonts.test.ts b/tests/engine/fonts.test.tsindex 42117d5a6..3e5eaa229 100644--- a/tests/engine/fonts.test.ts+++ b/tests/engine/fonts.test.ts@@ -2,8 +2,12 @@ import { describe, test, expect } from 'bun:test'import {collectFontKeys,+ fetchBundledFont,isFontLoaded,+ isVariableFont,markFontLoaded,+ normalizeFontFamily,+ styleToVariant,styleToWeight,weightToStyle,SceneGraph,@@ -289,3 +293,122 @@ describe('collectFontKeys', () => {expect(collectFontKeys(graph, ['nonexistent'])).toEqual([])})})++describe('normalizeFontFamily', () => {+ test('strips " Variable" suffix', () => {+ expect(normalizeFontFamily('Inter Variable')).toBe('Inter')+ expect(normalizeFontFamily('Roboto Flex Variable')).toBe('Roboto Flex')+ })++ test('case insensitive', () => {+ expect(normalizeFontFamily('Inter VARIABLE')).toBe('Inter')+ expect(normalizeFontFamily('Inter variable')).toBe('Inter')+ })++ test('handles extra whitespace before Variable', () => {+ expect(normalizeFontFamily('Inter Variable')).toBe('Inter')+ })++ test('returns unchanged when no Variable suffix', () => {+ expect(normalizeFontFamily('Inter')).toBe('Inter')+ expect(normalizeFontFamily('Roboto')).toBe('Roboto')+ expect(normalizeFontFamily('')).toBe('')+ })++ test('does not strip Variable in the middle', () => {+ expect(normalizeFontFamily('Variable Sans')).toBe('Variable Sans')+ })+})++describe('styleToVariant', () => {+ test('regular → "regular"', () => {+ expect(styleToVariant('Regular')).toBe('regular')+ })++ test('italic at 400 → "italic"', () => {+ expect(styleToVariant('Regular Italic')).toBe('italic')+ })++ test('bold → "700"', () => {+ expect(styleToVariant('Bold')).toBe('700')+ })++ test('bold italic → "700italic"', () => {+ expect(styleToVariant('Bold Italic')).toBe('700italic')+ })++ test('light → "300"', () => {+ expect(styleToVariant('Light')).toBe('300')+ })++ test('thin italic → "100italic"', () => {+ expect(styleToVariant('Thin Italic')).toBe('100italic')+ })++ test('semibold → "600"', () => {+ expect(styleToVariant('SemiBold')).toBe('600')+ })++ test('black → "900"', () => {+ expect(styleToVariant('Black')).toBe('900')+ })+})++describe('isVariableFont', () => {+ function makeFontBuffer(tables: string[]): ArrayBuffer {+ const numTables = tables.length+ const headerSize = 12+ const tableRecordSize = 16+ const totalSize = headerSize + numTables * tableRecordSize+ const buf = new ArrayBuffer(totalSize)+ const view = new DataView(buf)+ view.setUint32(0, 0x00010000)+ view.setUint16(4, numTables)+ for (let i = 0; i < numTables; i++) {+ const offset = headerSize + i * tableRecordSize+ for (let c = 0; c < 4; c++) {+ view.setUint8(offset + c, tables[i].charCodeAt(c))+ }+ }+ return buf+ }++ test('detects fvar table', () => {+ expect(isVariableFont(makeFontBuffer(['head', 'fvar', 'glyf']))).toBe(true)+ })++ test('returns false without fvar', () => {+ expect(isVariableFont(makeFontBuffer(['head', 'glyf', 'cmap']))).toBe(false)+ })++ test('returns false for empty buffer', () => {+ expect(isVariableFont(new ArrayBuffer(0))).toBe(false)+ })++ test('returns false for too-small buffer', () => {+ expect(isVariableFont(new ArrayBuffer(8))).toBe(false)+ })++ test('fvar as only table', () => {+ expect(isVariableFont(makeFontBuffer(['fvar']))).toBe(true)+ })+})++describe('fetchBundledFont', () => {+ test('loads Inter-Regular.ttf from assets in headless', async () => {+ const buffer = await fetchBundledFont('/Inter-Regular.ttf')+ expect(buffer).toBeInstanceOf(ArrayBuffer)+ expect(buffer!.byteLength).toBeGreaterThan(100_000)+ })++ test('returns valid TTF data', async () => {+ const buffer = await fetchBundledFont('/Inter-Regular.ttf')+ const view = new DataView(buffer!)+ // TrueType magic: 0x00010000+ expect(view.getUint32(0)).toBe(0x00010000)+ })++ test('throws for nonexistent font', async () => {+ expect(fetchBundledFont('/Nonexistent-Font.ttf')).rejects.toThrow()+ })+})