package export import ( "strings" "testing" ) func TestPrimaryFamily(t *testing.T) { cases := map[string]string{ "Awke": "Awke", "Untitled Sans": "Untitled Sans", "Inter, system-ui, sans-serif": "Inter", `"Untitled Sans", sans-serif`: "Untitled Sans", "'Maple Mono', ui-monospace": "Maple Mono", " Spaced , x": "Spaced", "": "", "system-ui": "", // generic-only → nothing to embed "Georgia, serif": "Georgia", } for in, want := range cases { if got := primaryFamily(in); got != want { t.Errorf("primaryFamily(%q) = %q, want %q", in, got, want) } } } func TestWeightAndItalicOf(t *testing.T) { cases := []struct { sub string weight int italic bool }{ {"Regular", 400, false}, {"Bold", 700, false}, {"Italic", 400, true}, {"Bold Italic", 700, true}, {"Thin", 100, false}, {"Light", 300, false}, {"Medium", 500, false}, {"SemiBold", 600, false}, {"Black", 900, false}, {"Bold Oblique", 700, true}, } for _, c := range cases { if w := weightOf(c.sub); w != c.weight { t.Errorf("weightOf(%q) = %d, want %d", c.sub, w, c.weight) } if it := italicOf(c.sub); it != c.italic { t.Errorf("italicOf(%q) = %v, want %v", c.sub, it, c.italic) } } } func TestFontFaceCSS(t *testing.T) { f := fontFace{family: "Untitled Sans", weight: 700, italic: true, ext: ".otf", data: []byte("FAKE")} css := f.faceCSS() for _, want := range []string{ `font-family:"Untitled Sans"`, "font-weight:700", "font-style:italic", `format("opentype")`, "data:font/otf;base64,RkFLRQ==", // base64("FAKE") } { if !strings.Contains(css, want) { t.Errorf("@font-face missing %q:\n%s", want, css) } } } func TestFontFaceCSSTruetype(t *testing.T) { f := fontFace{family: "Maple Mono", weight: 400, italic: false, ext: ".ttf", data: []byte("x")} css := f.faceCSS() if !strings.Contains(css, `format("truetype")`) { t.Errorf("ttf should be truetype format:\n%s", css) } if !strings.Contains(css, "data:font/ttf;base64,") { t.Errorf("ttf mime wrong:\n%s", css) } }