|
| 1 | +// @vitest-environment node |
| 2 | +import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 3 | +import { mkdtempSync, rmSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { injectDeterministicFontFaces } from "./deterministicFonts.js"; |
| 7 | + |
| 8 | +let previousCacheDir: string | undefined; |
| 9 | +let testCacheDir: string; |
| 10 | + |
| 11 | +beforeAll(() => { |
| 12 | + previousCacheDir = process.env.HYPERFRAMES_FONT_CACHE_DIR; |
| 13 | + testCacheDir = mkdtempSync(join(tmpdir(), "hf-font-shorthand-")); |
| 14 | + process.env.HYPERFRAMES_FONT_CACHE_DIR = testCacheDir; |
| 15 | +}); |
| 16 | + |
| 17 | +afterAll(() => { |
| 18 | + if (previousCacheDir === undefined) delete process.env.HYPERFRAMES_FONT_CACHE_DIR; |
| 19 | + else process.env.HYPERFRAMES_FONT_CACHE_DIR = previousCacheDir; |
| 20 | + rmSync(testCacheDir, { recursive: true, force: true }); |
| 21 | +}); |
| 22 | + |
| 23 | +const offline = (async () => { |
| 24 | + throw new Error("offline"); |
| 25 | +}) as unknown as typeof fetch; |
| 26 | + |
| 27 | +const inject = (css: string) => |
| 28 | + injectDeterministicFontFaces(`<html><head><style>${css}</style></head><body>x</body></html>`, { |
| 29 | + fetchImpl: offline, |
| 30 | + allowSystemFontCapture: false, |
| 31 | + }); |
| 32 | + |
| 33 | +describe("a bundled family named only in the font shorthand loads", () => { |
| 34 | + it.each([ |
| 35 | + ["size and family", "p { font: 24px Inter, sans-serif; }"], |
| 36 | + ["weight, size, line-height", "p { font: 700 24px/1.2 Inter, sans-serif; }"], |
| 37 | + ["style, variant, weight, size", 'p { font: italic small-caps bold 1.5rem "Inter", serif; }'], |
| 38 | + ["a keyword size", "p { font: bold large Inter; }"], |
| 39 | + ["a calc() size", "p { font: calc(10px + 2vw) Inter; }"], |
| 40 | + ])("%s", async (_case, css) => { |
| 41 | + expect(await inject(css)).toMatch(/@font-face\s*\{[^}]*font-family:\s*"Inter"/i); |
| 42 | + }); |
| 43 | + |
| 44 | + it("ignores a system-font keyword", async () => { |
| 45 | + expect(await inject("p { font: caption; }")).not.toContain("@font-face"); |
| 46 | + }); |
| 47 | +}); |
0 commit comments