|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { lintHyperframeHtml } from "../hyperframeLinter.js"; |
| 3 | + |
| 4 | +function findByCode(html: string, code: string, isSubComposition = true) { |
| 5 | + const result = lintHyperframeHtml(html, { isSubComposition }); |
| 6 | + return result.findings.filter((f) => f.code === code); |
| 7 | +} |
| 8 | + |
| 9 | +describe("font rules", () => { |
| 10 | + describe("google_fonts_import", () => { |
| 11 | + it("flags @import url with fonts.googleapis.com", () => { |
| 12 | + const html = `<div data-composition-id="test" data-width="1920" data-height="1080"> |
| 13 | + <style>@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500&display=swap');</style> |
| 14 | + </div>`; |
| 15 | + const findings = findByCode(html, "google_fonts_import"); |
| 16 | + expect(findings).toHaveLength(1); |
| 17 | + expect(findings[0]!.severity).toBe("warning"); |
| 18 | + }); |
| 19 | + |
| 20 | + it("flags <link> to fonts.googleapis.com", () => { |
| 21 | + const html = `<div data-composition-id="test" data-width="1920" data-height="1080"> |
| 22 | + <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter"> |
| 23 | + </div>`; |
| 24 | + const findings = findByCode(html, "google_fonts_import"); |
| 25 | + expect(findings).toHaveLength(1); |
| 26 | + }); |
| 27 | + |
| 28 | + it("does not flag local @font-face usage", () => { |
| 29 | + const html = `<div data-composition-id="test" data-width="1920" data-height="1080"> |
| 30 | + <style>@font-face { font-family: 'Inter'; src: url('../capture/assets/fonts/Inter.woff2'); }</style> |
| 31 | + </div>`; |
| 32 | + const findings = findByCode(html, "google_fonts_import"); |
| 33 | + expect(findings).toHaveLength(0); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + describe("font_family_without_font_face", () => { |
| 38 | + it("flags font-family used without @font-face", () => { |
| 39 | + const html = `<div data-composition-id="test" data-width="1920" data-height="1080"> |
| 40 | + <style>body { font-family: 'GT Walsheim', sans-serif; }</style> |
| 41 | + </div>`; |
| 42 | + const findings = findByCode(html, "font_family_without_font_face"); |
| 43 | + expect(findings).toHaveLength(1); |
| 44 | + expect(findings[0]!.message).toContain("gt walsheim"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("does not flag when @font-face is declared", () => { |
| 48 | + const html = `<div data-composition-id="test" data-width="1920" data-height="1080"> |
| 49 | + <style> |
| 50 | + @font-face { font-family: 'GT Walsheim'; src: url('../fonts/gt.woff2'); } |
| 51 | + body { font-family: 'GT Walsheim', sans-serif; } |
| 52 | + </style> |
| 53 | + </div>`; |
| 54 | + const findings = findByCode(html, "font_family_without_font_face"); |
| 55 | + expect(findings).toHaveLength(0); |
| 56 | + }); |
| 57 | + |
| 58 | + it("does not flag generic font families", () => { |
| 59 | + const html = `<div data-composition-id="test" data-width="1920" data-height="1080"> |
| 60 | + <style>body { font-family: monospace; }</style> |
| 61 | + </div>`; |
| 62 | + const findings = findByCode(html, "font_family_without_font_face"); |
| 63 | + expect(findings).toHaveLength(0); |
| 64 | + }); |
| 65 | + |
| 66 | + it("reports multiple missing families in one finding", () => { |
| 67 | + const html = `<div data-composition-id="test" data-width="1920" data-height="1080"> |
| 68 | + <style> |
| 69 | + h1 { font-family: 'Aeonik', sans-serif; } |
| 70 | + code { font-family: 'Feature Deck', monospace; } |
| 71 | + </style> |
| 72 | + </div>`; |
| 73 | + const findings = findByCode(html, "font_family_without_font_face"); |
| 74 | + expect(findings).toHaveLength(1); |
| 75 | + expect(findings[0]!.message).toContain("aeonik"); |
| 76 | + expect(findings[0]!.message).toContain("feature deck"); |
| 77 | + }); |
| 78 | + |
| 79 | + it("does not flag fonts the producer has pre-bundled", () => { |
| 80 | + const html = `<div data-composition-id="test" data-width="1920" data-height="1080"> |
| 81 | + <style> |
| 82 | + body { font-family: 'Inter', sans-serif; } |
| 83 | + code { font-family: 'JetBrains Mono', monospace; } |
| 84 | + h1 { font-family: 'Roboto', sans-serif; } |
| 85 | + </style> |
| 86 | + </div>`; |
| 87 | + const findings = findByCode(html, "font_family_without_font_face"); |
| 88 | + expect(findings).toHaveLength(0); |
| 89 | + }); |
| 90 | + |
| 91 | + it("still flags Google-Fonts-only fonts not pre-bundled", () => { |
| 92 | + const html = `<div data-composition-id="test" data-width="1920" data-height="1080"> |
| 93 | + <style>body { font-family: 'Geist', sans-serif; }</style> |
| 94 | + </div>`; |
| 95 | + const findings = findByCode(html, "font_family_without_font_face"); |
| 96 | + expect(findings).toHaveLength(1); |
| 97 | + expect(findings[0]!.message).toContain("geist"); |
| 98 | + }); |
| 99 | + |
| 100 | + it("is case-insensitive when matching @font-face to font-family", () => { |
| 101 | + const html = `<div data-composition-id="test" data-width="1920" data-height="1080"> |
| 102 | + <style> |
| 103 | + @font-face { font-family: 'Inter'; src: url('../fonts/inter.woff2'); } |
| 104 | + body { font-family: 'inter', sans-serif; } |
| 105 | + </style> |
| 106 | + </div>`; |
| 107 | + const findings = findByCode(html, "font_family_without_font_face"); |
| 108 | + expect(findings).toHaveLength(0); |
| 109 | + }); |
| 110 | + |
| 111 | + it("ignores font-family inside @font-face blocks", () => { |
| 112 | + const html = `<div data-composition-id="test" data-width="1920" data-height="1080"> |
| 113 | + <style> |
| 114 | + @font-face { font-family: 'CustomFont'; src: url('../fonts/custom.woff2'); } |
| 115 | + </style> |
| 116 | + </div>`; |
| 117 | + const findings = findByCode(html, "font_family_without_font_face"); |
| 118 | + expect(findings).toHaveLength(0); |
| 119 | + }); |
| 120 | + }); |
| 121 | +}); |
0 commit comments