|
| 1 | +import { afterEach, beforeEach, expect, it, spyOn } from "bun:test"; |
| 2 | +import { mkdtempSync, rmSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { |
| 6 | + _clearGoogleFontCssCacheForTests, |
| 7 | + injectDeterministicFontFaces, |
| 8 | +} from "./deterministicFonts.js"; |
| 9 | + |
| 10 | +let cacheDir: string; |
| 11 | +let previousCacheDir: string | undefined; |
| 12 | +const requests: string[] = []; |
| 13 | +let restoreFetch: () => void; |
| 14 | + |
| 15 | +beforeEach(() => { |
| 16 | + previousCacheDir = process.env.HYPERFRAMES_FONT_CACHE_DIR; |
| 17 | + cacheDir = mkdtempSync(join(tmpdir(), "hf-authored-resources-")); |
| 18 | + process.env.HYPERFRAMES_FONT_CACHE_DIR = cacheDir; |
| 19 | + requests.length = 0; |
| 20 | + _clearGoogleFontCssCacheForTests(); |
| 21 | + const mock = spyOn(globalThis, "fetch").mockImplementation(async (input) => { |
| 22 | + const url = new URL(String(input)); |
| 23 | + requests.push(url.href); |
| 24 | + if (url.hostname === "fonts.googleapis.com") { |
| 25 | + const weight = url.searchParams.get("family")?.endsWith("700") ? "700" : "400"; |
| 26 | + return new Response(`@font-face { |
| 27 | + font-family: 'Fraunces'; font-style: normal; font-weight: ${weight}; |
| 28 | + src: url(https://fonts.gstatic.com/fraunces-${weight}.woff2) format('woff2'); |
| 29 | + }`); |
| 30 | + } |
| 31 | + return new Response(url.pathname); |
| 32 | + }); |
| 33 | + restoreFetch = () => mock.mockRestore(); |
| 34 | +}); |
| 35 | + |
| 36 | +afterEach(() => { |
| 37 | + restoreFetch(); |
| 38 | + _clearGoogleFontCssCacheForTests(); |
| 39 | + if (previousCacheDir === undefined) delete process.env.HYPERFRAMES_FONT_CACHE_DIR; |
| 40 | + else process.env.HYPERFRAMES_FONT_CACHE_DIR = previousCacheDir; |
| 41 | + rmSync(cacheDir, { recursive: true, force: true }); |
| 42 | +}); |
| 43 | + |
| 44 | +const normal = "https://fonts.googleapis.com/css2?family=Fraunces:wght@400"; |
| 45 | +const bold = "https://fonts.googleapis.com/css2?family=Fraunces:wght@700"; |
| 46 | +const page = (head: string) => `<!doctype html><html><head>${head} |
| 47 | + <style>p { font-family: Fraunces; }</style></head><body><p>Hi</p></body></html>`; |
| 48 | + |
| 49 | +function requestedFamilies(): (string | null)[] { |
| 50 | + return requests |
| 51 | + .filter((url) => url.startsWith("https://fonts.googleapis.com/")) |
| 52 | + .map((url) => new URL(url).searchParams.get("family")); |
| 53 | +} |
| 54 | + |
| 55 | +it("decodes HTML entities and preserves an authored text subset", async () => { |
| 56 | + await injectDeterministicFontFaces(page(`<link rel=stylesheet href="${normal}&text=Hi">`)); |
| 57 | + expect(requests[0]).toBe(`${normal}&text=Hi`); |
| 58 | +}); |
| 59 | + |
| 60 | +it("embeds every separate weight resource once in document order", async () => { |
| 61 | + const result = await injectDeterministicFontFaces( |
| 62 | + page( |
| 63 | + `<link rel=stylesheet href="${normal}"><link rel=stylesheet href="${bold}">` + |
| 64 | + `<link rel=stylesheet href="${normal}">`, |
| 65 | + ), |
| 66 | + ); |
| 67 | + expect(requestedFamilies()).toEqual(["Fraunces:wght@700", "Fraunces:wght@400"]); |
| 68 | + expect(result).toContain(Buffer.from("/fraunces-400.woff2").toString("base64")); |
| 69 | + expect(result).toContain(Buffer.from("/fraunces-700.woff2").toString("base64")); |
| 70 | +}); |
| 71 | + |
| 72 | +it.each([ |
| 73 | + `<link rel="stylesheet" media="print" href="${bold}">`, |
| 74 | + `<link rel="stylesheet" disabled href="${bold}">`, |
| 75 | + `<link rel="alternate stylesheet" title="other" href="${bold}">`, |
| 76 | + `<style media="print">@import url("${bold}");</style>`, |
| 77 | + `<style>@import url("${bold}") print;</style>`, |
| 78 | + `<style>@import url("${bold}") supports(display: grid);</style>`, |
| 79 | + `<!-- <link rel="stylesheet" href="${bold}"> -->`, |
| 80 | +])("does not promote an inactive font source: %s", async (inactive) => { |
| 81 | + await injectDeterministicFontFaces(page(`${inactive}<link rel="stylesheet" href="${normal}">`)); |
| 82 | + expect(requestedFamilies()).toEqual(["Fraunces:wght@400"]); |
| 83 | +}); |
0 commit comments