Files
hyperframes/packages/producer/src/services/deterministicFonts.test.ts
T
Miguel Ángel 0bf15119f8 feat: font resolution pipeline — compositions capture and embed their own fonts (#1255)
Compositions are now self-contained: the compiler captures font files
and embeds them as woff2 data URIs, eliminating silent render-time
fallback when the render environment lacks the author's fonts.

Resolution order (each tier falls through to the next):
1. Existing @font-face → use as-is
2. Bundled alias (38 cross-platform mappings) → embed data URI
3. Google Fonts → fetch, cache, embed
4. Local system font → locate on OS, compress to woff2, embed
5. Local @font-face paths → read file, compress, inline as data URI
6. External CDN stylesheets → fetch CSS, extract @font-face, inline
7. Alias map fallback → closest bundled equivalent
8. Actionable error with guidance

Key changes:
- System font locator (macOS/Windows/Linux) with path-bounding and
  symlink defense (realpathSync + O_NOFOLLOW)
- woff2 compression via wawoff2 (WASM, cross-platform)
- Multi-weight/style variant capture with length-sorted token matching
- External stylesheet inlining with SSRF defense (assertPublicHttpsUrl,
  HTTPS-only, private-host blocking, 2MB cap, 4-concurrent limit)
- Studio auto-import via GET /fonts/file API + renderAliasFor() derived
  from shared FONT_ALIAS_MAP (no more hand-curated drift)
- failClosedFontFetch throws on unresolved fonts in distributed renders
- Single source of truth: @hyperframes/core/fonts/aliases
- system_font_will_alias lint rule (escalates to warning for distributed)
- Default to Inter + JetBrains Mono in templates and CSS reset
2026-06-07 17:52:19 -04:00

76 lines
3.5 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { FONT_ALIASES, FONT_ALIAS_KEYS } from "./deterministicFonts.js";
describe("FONT_ALIASES cross-platform coverage", () => {
it("maps macOS sans-serif system fonts to inter", () => {
expect(FONT_ALIASES["sf pro"]).toBe("inter");
expect(FONT_ALIASES["sf pro display"]).toBe("inter");
expect(FONT_ALIASES["sf pro text"]).toBe("inter");
expect(FONT_ALIASES["sf pro rounded"]).toBe("inter");
expect(FONT_ALIASES["avenir"]).toBe("inter");
expect(FONT_ALIASES["avenir next"]).toBe("inter");
expect(FONT_ALIASES["geneva"]).toBe("inter");
expect(FONT_ALIASES["optima"]).toBe("inter");
expect(FONT_ALIASES["lucida grande"]).toBe("inter");
});
it("maps Windows sans-serif system fonts to inter", () => {
expect(FONT_ALIASES["calibri"]).toBe("inter");
expect(FONT_ALIASES["candara"]).toBe("inter");
expect(FONT_ALIASES["corbel"]).toBe("inter");
expect(FONT_ALIASES["verdana"]).toBe("inter");
expect(FONT_ALIASES["tahoma"]).toBe("inter");
expect(FONT_ALIASES["trebuchet ms"]).toBe("inter");
expect(FONT_ALIASES["lucida sans"]).toBe("inter");
expect(FONT_ALIASES["lucida sans unicode"]).toBe("inter");
});
it("maps Linux sans-serif system fonts to inter", () => {
expect(FONT_ALIASES["noto sans"]).toBe("inter");
expect(FONT_ALIASES["dejavu sans"]).toBe("inter");
expect(FONT_ALIASES["liberation sans"]).toBe("inter");
});
it("maps monospace system fonts to jetbrains-mono", () => {
expect(FONT_ALIASES["sf mono"]).toBe("jetbrains-mono");
expect(FONT_ALIASES["menlo"]).toBe("jetbrains-mono");
expect(FONT_ALIASES["monaco"]).toBe("jetbrains-mono");
expect(FONT_ALIASES["consolas"]).toBe("jetbrains-mono");
expect(FONT_ALIASES["lucida console"]).toBe("jetbrains-mono");
expect(FONT_ALIASES["lucida sans typewriter"]).toBe("jetbrains-mono");
expect(FONT_ALIASES["andale mono"]).toBe("jetbrains-mono");
expect(FONT_ALIASES["dejavu sans mono"]).toBe("jetbrains-mono");
expect(FONT_ALIASES["liberation mono"]).toBe("jetbrains-mono");
});
it("maps serif system fonts to eb-garamond", () => {
expect(FONT_ALIASES["georgia"]).toBe("eb-garamond");
expect(FONT_ALIASES["palatino"]).toBe("eb-garamond");
expect(FONT_ALIASES["palatino linotype"]).toBe("eb-garamond");
expect(FONT_ALIASES["book antiqua"]).toBe("eb-garamond");
expect(FONT_ALIASES["cambria"]).toBe("eb-garamond");
expect(FONT_ALIASES["times"]).toBe("eb-garamond");
expect(FONT_ALIASES["times new roman"]).toBe("eb-garamond");
expect(FONT_ALIASES["dejavu serif"]).toBe("eb-garamond");
expect(FONT_ALIASES["liberation serif"]).toBe("eb-garamond");
});
it("preserves all existing aliases", () => {
expect(FONT_ALIASES["helvetica neue"]).toBe("inter");
expect(FONT_ALIASES["arial"]).toBe("inter");
expect(FONT_ALIASES["courier new"]).toBe("jetbrains-mono");
expect(FONT_ALIASES["segoe ui"]).toBe("roboto");
expect(FONT_ALIASES["futura"]).toBe("montserrat");
expect(FONT_ALIASES["bebas neue"]).toBe("league-gothic");
});
it("exports FONT_ALIAS_KEYS containing all alias entries", () => {
expect(FONT_ALIAS_KEYS).toBeInstanceOf(Set);
expect(FONT_ALIAS_KEYS.has("sf mono")).toBe(true);
expect(FONT_ALIAS_KEYS.has("menlo")).toBe(true);
expect(FONT_ALIAS_KEYS.has("consolas")).toBe(true);
expect(FONT_ALIAS_KEYS.has("inter")).toBe(true);
expect(FONT_ALIAS_KEYS.size).toBe(Object.keys(FONT_ALIASES).length);
});
});