@@ -24,7 +24,10 @@ import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
2424import { mkdirSync , mkdtempSync , rmSync , writeFileSync } from "node:fs" ;
2525import { homedir , tmpdir } from "node:os" ;
2626import { join } from "node:path" ;
27- import { _clearGoogleFontCssCacheForTests } from "./deterministicFonts.js" ;
27+ import {
28+ _clearGoogleFontCssCacheForTests ,
29+ injectDeterministicFontFaces ,
30+ } from "./deterministicFonts.js" ;
2831import { fontDirectories } from "./systemFontLocator.js" ;
2932
3033beforeEach ( ( ) => _clearGoogleFontCssCacheForTests ( ) ) ;
@@ -96,7 +99,6 @@ const HTML = `<!doctype html><html><head><style>
9699
97100describe ( "Google Fonts multi-subset embedding" , ( ) => {
98101 it ( "downloads and embeds EACH subset distinctly (no cache collision)" , async ( ) => {
99- const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
100102 const result = await injectDeterministicFontFaces ( HTML , { fetchImpl : makeGoogleFetch ( ) } ) ;
101103
102104 // Both subsets' distinct bytes must be present — the latin subset must NOT
@@ -107,7 +109,6 @@ describe("Google Fonts multi-subset embedding", () => {
107109 } ) ;
108110
109111 it ( "preserves each face's unicode-range so the browser picks the right subset per codepoint" , async ( ) => {
110- const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
111112 const result = await injectDeterministicFontFaces ( HTML , { fetchImpl : makeGoogleFetch ( ) } ) ;
112113
113114 expect ( result ) . toContain ( VIET_RANGE ) ;
@@ -182,7 +183,6 @@ function authoredFetch(cssStatus: number): { fetchImpl: typeof fetch; urls: stri
182183
183184describe ( "authored Google font stylesheet" , ( ) => {
184185 it ( "embeds the linked file and leaves it after the link" , async ( ) => {
185- const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
186186 const { fetchImpl, urls } = authoredFetch ( 200 ) ;
187187 const result = await injectDeterministicFontFaces (
188188 authoredPage (
@@ -209,23 +209,175 @@ describe("authored Google font stylesheet", () => {
209209 ) ;
210210 } ) ;
211211
212- it ( "does not replace a failed link with the weight-only file" , async ( ) => {
213- const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
212+ it ( "embeds the default request when Google rejects the page's link" , async ( ) => {
214213 const { fetchImpl, urls } = authoredFetch ( 400 ) ;
214+ const warnings : string [ ] = [ ] ;
215215 const result = await injectDeterministicFontFaces (
216216 authoredPage (
217217 `<link rel="stylesheet" href="${ AUTHORED_HREF } "><style>h1 { font-family: "Fraunces", serif; }</style>` ,
218218 ) ,
219- { fetchImpl, allowSystemFontCapture : false } ,
219+ {
220+ fetchImpl,
221+ allowSystemFontCapture : false ,
222+ failClosedFontFetch : true ,
223+ logger : { warn : ( message ) => warnings . push ( message ) , info : ( ) => { } } ,
224+ } ,
225+ ) ;
226+
227+ expect ( urls . some ( ( url ) => url . includes ( "family=Fraunces:ital,wght@" ) ) ) . toBe ( true ) ;
228+ expect ( result ) . toContain ( b64 ( "FRAUNCES_WIDE" ) ) ;
229+ const fallbackWarning = warnings . find ( ( message ) =>
230+ message . includes ( "google_font_link_fallback" ) ,
231+ ) ;
232+ expect ( fallbackWarning ) . toContain ( "link_outcomes=http_400" ) ;
233+ expect ( fallbackWarning ) . toContain ( "fallback_faces=1" ) ;
234+ expect ( fallbackWarning ) . not . toContain ( "googleapis" ) ;
235+ } ) ;
236+
237+ it ( "does not swap a link a transient failure blocked under fail-closed" , async ( ) => {
238+ const { fetchImpl, urls } = authoredFetch ( 503 ) ;
239+ const compile = injectDeterministicFontFaces (
240+ authoredPage (
241+ `<link rel="stylesheet" href="${ AUTHORED_HREF } "><style>h1 { font-family: "Fraunces", serif; }</style>` ,
242+ ) ,
243+ {
244+ fetchImpl,
245+ allowSystemFontCapture : false ,
246+ failClosedFontFetch : true ,
247+ fontFetchRetryPolicy : { maxAttempts : 1 } ,
248+ } ,
220249 ) ;
221250
251+ await expect ( compile ) . rejects . toThrow ( ) ;
222252 expect ( urls . some ( ( url ) => url . includes ( "ital,wght@" ) ) ) . toBe ( false ) ;
223- expect ( result ) . not . toContain ( b64 ( "FRAUNCES_WIDE" ) ) ;
224- expect ( result ) . not . toContain ( "data-hyperframes-deterministic-fonts" ) ;
253+ } ) ;
254+
255+ it . each ( [
256+ [ 403 , true ] ,
257+ [ 404 , true ] ,
258+ [ 503 , false ] ,
259+ [ 200 , true ] ,
260+ ] ) (
261+ "embeds the default request when the link answers HTTP %s with no faces (fail-closed: %s)" ,
262+ async ( status , failClosedFontFetch ) => {
263+ const urls : string [ ] = [ ] ;
264+ const base = authoredFetch ( 200 ) . fetchImpl ;
265+ const fetchImpl = ( async ( input : unknown ) => {
266+ const url = String ( input ) ;
267+ urls . push ( url ) ;
268+ if ( isAuthoredRequest ( url , AUTHORED_HREF ) ) return new Response ( "" , { status } ) ;
269+ return base ( url ) ;
270+ } ) as unknown as typeof fetch ;
271+ const result = await injectDeterministicFontFaces (
272+ authoredPage (
273+ `<link rel="stylesheet" href="${ AUTHORED_HREF } "><style>h1 { font-family: "Fraunces", serif; }</style>` ,
274+ ) ,
275+ {
276+ fetchImpl,
277+ allowSystemFontCapture : false ,
278+ failClosedFontFetch,
279+ fontFetchRetryPolicy : { maxAttempts : 1 } ,
280+ } ,
281+ ) ;
282+
283+ expect ( urls . some ( ( url ) => url . includes ( "family=Fraunces:ital,wght@" ) ) ) . toBe ( true ) ;
284+ expect ( result ) . toContain ( b64 ( "FRAUNCES_WIDE" ) ) ;
285+ } ,
286+ ) ;
287+
288+ it ( "keeps the linked faces without the default request when the link works" , async ( ) => {
289+ const { fetchImpl, urls } = authoredFetch ( 200 ) ;
290+ await injectDeterministicFontFaces (
291+ authoredPage (
292+ `<link rel="stylesheet" href="${ AUTHORED_HREF } "><style>h1 { font-family: "Fraunces", serif; }</style>` ,
293+ ) ,
294+ { fetchImpl, allowSystemFontCapture : false , failClosedFontFetch : true } ,
295+ ) ;
296+
297+ expect ( urls . some ( ( url ) => url . includes ( "ital,wght@" ) ) ) . toBe ( false ) ;
298+ } ) ;
299+
300+ it ( "embeds the same faces in preview and render when the page's link is rejected" , async ( ) => {
301+ const html = authoredPage (
302+ `<link rel="stylesheet" href="${ AUTHORED_HREF } "><style>h1 { font-family: "Fraunces", serif; }</style>` ,
303+ ) ;
304+ const preview = await injectDeterministicFontFaces ( html , {
305+ fetchImpl : authoredFetch ( 400 ) . fetchImpl ,
306+ allowSystemFontCapture : false ,
307+ } ) ;
308+ _clearGoogleFontCssCacheForTests ( ) ;
309+ const render = await injectDeterministicFontFaces ( html , {
310+ fetchImpl : authoredFetch ( 400 ) . fetchImpl ,
311+ allowSystemFontCapture : false ,
312+ failClosedFontFetch : true ,
313+ } ) ;
314+
315+ expect ( preview ) . toContain ( b64 ( "FRAUNCES_WIDE" ) ) ;
316+ expect ( render ) . toBe ( preview ) ;
317+ } ) ;
318+
319+ it ( "renders fail-closed when a link asks for weights past the family's range" , async ( ) => {
320+ // Bricolage Grotesque spans 200..800; Google answers `[email protected] ` with HTTP 400. 321+ const href = "https://fonts.googleapis.com/css2?family=Bricolage+Grotesque:[email protected] " ; 322+ const file = "https://fonts.gstatic.com/s/bricolagegrotesque/default.woff2" ;
323+ const fetchImpl = ( async ( input : unknown ) => {
324+ const url = String ( input ) ;
325+ if ( url . startsWith ( href ) ) return new Response ( "" , { status : 400 } ) ;
326+ if ( url . includes ( "family=Bricolage%20Grotesque:ital,wght@" ) ) {
327+ return new Response (
328+ `@font-face { font-family: 'Bricolage Grotesque'; font-style: normal; font-weight: 200 800; src: url(${ file } ) format('woff2'); }` ,
329+ ) ;
330+ }
331+ if ( url === file ) return new Response ( "BRICOLAGE_DEFAULT" ) ;
332+ return new Response ( "" , { status : 404 } ) ;
333+ } ) as unknown as typeof fetch ;
334+
335+ const result = await injectDeterministicFontFaces (
336+ authoredPage (
337+ `<link rel="stylesheet" href="${ href } "><style>h1 { font-family: "Bricolage Grotesque", sans-serif; }</style>` ,
338+ ) ,
339+ { fetchImpl, allowSystemFontCapture : false , failClosedFontFetch : true } ,
340+ ) ;
341+
342+ expect ( result ) . toContain ( b64 ( "BRICOLAGE_DEFAULT" ) ) ;
343+ } ) ;
344+
345+ it ( "renders fail-closed when a multi-family link silently drops one family" , async ( ) => {
346+ // Bricolage's opsz axis starts at 12; Google answers this link with 200 and only Hanken Grotesk.
347+ const href =
348+ "https://fonts.googleapis.com/css2?family=Bricolage+Grotesque:opsz,[email protected] ,500&family=Hanken+Grotesk:wght@400;500;600&display=swap" ; 349+ const hanken = "https://fonts.gstatic.com/s/hankengrotesk/linked.woff2" ;
350+ const bricolage = "https://fonts.gstatic.com/s/bricolagegrotesque/default.woff2" ;
351+ const fetchImpl = ( async ( input : unknown ) => {
352+ const url = String ( input ) ;
353+ if ( url . startsWith ( href ) ) {
354+ return new Response (
355+ `@font-face { font-family: 'Hanken Grotesk'; font-style: normal; font-weight: 400; src: url(${ hanken } ) format('woff2'); }` ,
356+ ) ;
357+ }
358+ if ( url . includes ( "family=Bricolage%20Grotesque:ital,wght@" ) ) {
359+ return new Response (
360+ `@font-face { font-family: 'Bricolage Grotesque'; font-style: normal; font-weight: 200 800; src: url(${ bricolage } ) format('woff2'); }` ,
361+ ) ;
362+ }
363+ if ( url === hanken ) return new Response ( "HANKEN_LINKED" ) ;
364+ if ( url === bricolage ) return new Response ( "BRICOLAGE_DEFAULT" ) ;
365+ return new Response ( "" , { status : 404 } ) ;
366+ } ) as unknown as typeof fetch ;
367+
368+ const result = await injectDeterministicFontFaces (
369+ authoredPage (
370+ `<link rel="stylesheet" href="${ href } ">` +
371+ `<style>h1 { font-family: "Bricolage Grotesque"; } p { font-family: "Hanken Grotesk"; }</style>` ,
372+ ) ,
373+ { fetchImpl, allowSystemFontCapture : false , failClosedFontFetch : true } ,
374+ ) ;
375+
376+ expect ( result ) . toContain ( b64 ( "BRICOLAGE_DEFAULT" ) ) ;
377+ expect ( result ) . toContain ( b64 ( "HANKEN_LINKED" ) ) ;
225378 } ) ;
226379
227380 it ( "still requests by family name when the page has no Google link" , async ( ) => {
228- const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
229381 const { fetchImpl, urls } = authoredFetch ( 200 ) ;
230382 const result = await injectDeterministicFontFaces (
231383 authoredPage ( `<style>h1 { font-family: "Fraunces", serif; }</style>` ) ,
@@ -257,7 +409,6 @@ describe("authored Google font stylesheet", () => {
257409 return new Response ( "" , { status : 404 } ) ;
258410 } ) as unknown as typeof fetch ;
259411
260- const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
261412 const result = await injectDeterministicFontFaces (
262413 authoredPage (
263414 `<link rel="stylesheet" href="${ href } "><style>h1 { font-family: "Bodoni Moda", serif; font-weight: 700; }</style>` ,
@@ -281,7 +432,6 @@ describe("authored Google font stylesheet", () => {
281432 } ) ;
282433
283434 it ( "still embeds Inter for Arial when the page links Arial" , async ( ) => {
284- const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
285435 const { fetchImpl, urls } = authoredFetch ( 200 ) ;
286436 await injectDeterministicFontFaces (
287437 authoredPage (
@@ -296,7 +446,6 @@ describe("authored Google font stylesheet", () => {
296446 } ) ;
297447
298448 it ( "embeds the Google file named by an import, not the weight-only file" , async ( ) => {
299- const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
300449 const { fetchImpl, urls } = authoredFetch ( 200 ) ;
301450 const result = await injectDeterministicFontFaces (
302451 authoredPage (
@@ -331,7 +480,6 @@ describe("authored Google font stylesheet", () => {
331480 return new Response ( "" , { status : 404 } ) ;
332481 } ) as unknown as typeof fetch ;
333482
334- const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
335483 const result = await injectDeterministicFontFaces (
336484 authoredPage (
337485 `<link rel="stylesheet" href="${ narrow } ">` +
@@ -364,10 +512,10 @@ describe("authored Google font stylesheet", () => {
364512 { status : 200 } ,
365513 ) ;
366514 }
515+ if ( url === file ) return new Response ( "BODONI_RANGE" ) ;
367516 return new Response ( "" , { status : 404 } ) ;
368517 } ) as unknown as typeof fetch ;
369518
370- const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
371519 await injectDeterministicFontFaces (
372520 authoredPage (
373521 `<link rel="stylesheet" href="${ wide } ">` +
@@ -399,7 +547,6 @@ describe("authored Google font stylesheet", () => {
399547 return new Response ( "" , { status : 404 } ) ;
400548 } ) as unknown as typeof fetch ;
401549
402- const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
403550 await injectDeterministicFontFaces (
404551 authoredPage (
405552 `<link rel="stylesheet" href="${ href } "><style>h1 { font-family: "Fraunces", serif; }</style>` ,
@@ -428,7 +575,6 @@ describe("authored Google font stylesheet", () => {
428575 return new Response ( "" , { status : 404 } ) ;
429576 } ) as unknown as typeof fetch ;
430577
431- const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
432578 await injectDeterministicFontFaces (
433579 `<!doctype html><html><head><link rel="stylesheet" href="${ href } "><style>p { font-family: "Noto Serif JP", serif; }</style></head><body>${ many } </body></html>` ,
434580 { fetchImpl, allowSystemFontCapture : false } ,
@@ -439,7 +585,6 @@ describe("authored Google font stylesheet", () => {
439585
440586 it ( "does not embed a local file when the page's link failed" , async ( ) => {
441587 const href = "https://fonts.googleapis.com/css2?family=Hf+Authored+Fail+Test" ;
442- const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
443588 const urls : string [ ] = [ ] ;
444589 const fetchImpl = ( async ( input : unknown ) => {
445590 urls . push ( String ( input ) ) ;
@@ -452,12 +597,11 @@ describe("authored Google font stylesheet", () => {
452597 { fetchImpl, allowSystemFontCapture : true } ,
453598 ) ;
454599
455- expect ( urls . some ( ( url ) => url . includes ( "ital,wght@" ) ) ) . toBe ( false ) ;
600+ expect ( urls . some ( ( url ) => url . includes ( "ital,wght@" ) ) ) . toBe ( true ) ;
456601 expect ( result ) . not . toContain ( b64 ( LOCAL_FONT_BYTES ) ) ;
457602 } ) ;
458603
459604 it ( "still embeds a local file when the page has no Google link" , async ( ) => {
460- const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
461605 const fetchImpl = ( async ( ) => new Response ( "" , { status : 400 } ) ) as unknown as typeof fetch ;
462606 const result = await injectDeterministicFontFaces (
463607 authoredPage ( `<style>h1 { font-family: "Hf Authored Fail Test", serif; }</style>` ) ,
0 commit comments