2020 */
2121
2222import { afterAll , beforeAll , beforeEach , describe , expect , it } from "bun:test" ;
23- import { mkdtempSync , rmSync } from "node:fs" ;
24- import { tmpdir } from "node:os" ;
23+ import { mkdirSync , mkdtempSync , rmSync , writeFileSync } from "node:fs" ;
24+ import { homedir , tmpdir } from "node:os" ;
2525import { join } from "node:path" ;
2626import { _clearGoogleFontCssCacheForTests } from "./deterministicFonts.js" ;
2727
2828beforeEach ( ( ) => _clearGoogleFontCssCacheForTests ( ) ) ;
2929
3030let cacheDir : string ;
3131let prevCacheEnv : string | undefined ;
32+ const LOCAL_FONT_DIR = join ( homedir ( ) , ".local" , "share" , "fonts" ) ;
33+ const LOCAL_FONT_FILE = join ( LOCAL_FONT_DIR , "hf-authored-fail-test.woff2" ) ;
34+ const LOCAL_FONT_BYTES = "LOCAL_ONLY_BYTES" ;
3235
3336beforeAll ( ( ) => {
3437 prevCacheEnv = process . env . HYPERFRAMES_FONT_CACHE_DIR ;
3538 cacheDir = mkdtempSync ( join ( tmpdir ( ) , "hf-font-cache-" ) ) ;
3639 process . env . HYPERFRAMES_FONT_CACHE_DIR = cacheDir ;
40+ mkdirSync ( LOCAL_FONT_DIR , { recursive : true } ) ;
41+ writeFileSync ( LOCAL_FONT_FILE , LOCAL_FONT_BYTES ) ;
3742} ) ;
3843
3944afterAll ( ( ) => {
4045 if ( prevCacheEnv === undefined ) delete process . env . HYPERFRAMES_FONT_CACHE_DIR ;
4146 else process . env . HYPERFRAMES_FONT_CACHE_DIR = prevCacheEnv ;
4247 rmSync ( cacheDir , { recursive : true , force : true } ) ;
48+ rmSync ( LOCAL_FONT_FILE , { force : true } ) ;
4349} ) ;
4450
4551const VIET_RANGE = "U+0102-0103, U+1EA0-1EF9, U+20AB" ;
@@ -140,12 +146,16 @@ function authoredPage(head: string): string {
140146 return `<!doctype html><html><head>${ head } </head><body><h1>Seconds</h1></body></html>` ;
141147}
142148
149+ function isAuthoredRequest ( url : string , href : string ) : boolean {
150+ return url === href || url . startsWith ( `${ href } &text=` ) ;
151+ }
152+
143153function authoredFetch ( cssStatus : number ) : { fetchImpl : typeof fetch ; urls : string [ ] } {
144154 const urls : string [ ] = [ ] ;
145155 const fetchImpl = ( async ( input : unknown ) => {
146156 const url = String ( input ) ;
147157 urls . push ( url ) ;
148- if ( url === AUTHORED_HREF )
158+ if ( isAuthoredRequest ( url , AUTHORED_HREF ) )
149159 return new Response ( cssStatus === 200 ? AUTHORED_CSS : "" , { status : cssStatus } ) ;
150160 if ( url . includes ( "family=Fraunces:ital,wght@" ) ) {
151161 return new Response (
@@ -180,9 +190,10 @@ describe("authored Google font stylesheet", () => {
180190 { fetchImpl, allowSystemFontCapture : false } ,
181191 ) ;
182192
183- expect ( urls . filter ( ( url ) => url . startsWith ( "https://fonts.googleapis.com/" ) ) ) . toEqual ( [
184- AUTHORED_HREF ,
185- ] ) ;
193+ const cssUrls = urls . filter ( ( url ) => url . startsWith ( "https://fonts.googleapis.com/" ) ) ;
194+ expect ( cssUrls ) . toHaveLength ( 1 ) ;
195+ expect ( cssUrls [ 0 ] ?. startsWith ( `${ AUTHORED_HREF } &text=` ) ) . toBe ( true ) ;
196+ expect ( new URL ( cssUrls [ 0 ] ?? "" ) . searchParams . get ( "text" ) ?? "" ) . toContain ( "S" ) ;
186197 expect ( result ) . toContain ( b64 ( "FRAUNCES_LINKED" ) ) ;
187198 expect ( result ) . toContain ( b64 ( "NUNITO_LINKED" ) ) ;
188199 expect ( result ) . not . toContain ( b64 ( "FRAUNCES_WIDE" ) ) ;
@@ -223,6 +234,50 @@ describe("authored Google font stylesheet", () => {
223234 expect ( result ) . toContain ( b64 ( "FRAUNCES_WIDE" ) ) ;
224235 } ) ;
225236
237+ it ( "keeps a 400 900 weight span on every embedded subset" , async ( ) => {
238+ const href =
239+ "https://fonts.googleapis.com/css2?family=Bodoni+Moda:ital,opsz,wght@0,6..96,400..900&display=swap" ;
240+ const latin = "https://fonts.gstatic.com/s/bodonimoda/latin.woff2" ;
241+ const latinExt = "https://fonts.gstatic.com/s/bodonimoda/latin-ext.woff2" ;
242+ const urls : string [ ] = [ ] ;
243+ const fetchImpl = ( async ( input : unknown ) => {
244+ const url = String ( input ) ;
245+ urls . push ( url ) ;
246+ if ( isAuthoredRequest ( url , href ) ) {
247+ return new Response (
248+ `@font-face { font-family: 'Bodoni Moda'; font-style: normal; font-weight: 400 900; src: url(${ latinExt } ) format('woff2'); unicode-range: U+0100-02BA; }
249+ @font-face { font-family: 'Bodoni Moda'; font-style: normal; font-weight: 400 900; src: url(${ latin } ) format('woff2'); unicode-range: U+0000-00FF; }` ,
250+ { status : 200 } ,
251+ ) ;
252+ }
253+ if ( url === latin ) return new Response ( "BODONI_LATIN" , { status : 200 } ) ;
254+ if ( url === latinExt ) return new Response ( "BODONI_LATIN_EXT" , { status : 200 } ) ;
255+ return new Response ( "" , { status : 404 } ) ;
256+ } ) as unknown as typeof fetch ;
257+
258+ const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
259+ const result = await injectDeterministicFontFaces (
260+ authoredPage (
261+ `<link rel="stylesheet" href="${ href } "><style>h1 { font-family: "Bodoni Moda", serif; font-weight: 700; }</style>` ,
262+ ) ,
263+ { fetchImpl, allowSystemFontCapture : false } ,
264+ ) ;
265+
266+ const cssUrls = urls . filter ( ( url ) => url . startsWith ( "https://fonts.googleapis.com/" ) ) ;
267+ expect ( cssUrls ) . toHaveLength ( 1 ) ;
268+ expect ( cssUrls [ 0 ] ?. startsWith ( `${ href } &text=` ) ) . toBe ( true ) ;
269+ const faces = result
270+ . split ( "@font-face" )
271+ . slice ( 1 )
272+ . filter ( ( block ) => block . includes ( 'font-family: "Bodoni Moda"' ) ) ;
273+ expect ( faces ) . toHaveLength ( 2 ) ;
274+ for ( const face of faces ) {
275+ expect ( face ) . toContain ( "font-weight: 400 900;" ) ;
276+ }
277+ expect ( result ) . toContain ( b64 ( "BODONI_LATIN" ) ) ;
278+ expect ( result ) . toContain ( b64 ( "BODONI_LATIN_EXT" ) ) ;
279+ } ) ;
280+
226281 it ( "still embeds Inter for Arial when the page links Arial" , async ( ) => {
227282 const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
228283 const { fetchImpl, urls } = authoredFetch ( 200 ) ;
@@ -237,4 +292,174 @@ describe("authored Google font stylesheet", () => {
237292 expect ( urls . some ( ( url ) => url . includes ( "family=Arial" ) ) ) . toBe ( false ) ;
238293 expect ( urls . some ( ( url ) => url . includes ( "family=Inter:" ) ) ) . toBe ( true ) ;
239294 } ) ;
295+
296+ it ( "embeds the Google file named by an import, not the weight-only file" , async ( ) => {
297+ const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
298+ const { fetchImpl, urls } = authoredFetch ( 200 ) ;
299+ const result = await injectDeterministicFontFaces (
300+ authoredPage (
301+ `<style>@import url("${ AUTHORED_HREF } "); h1 { font-family: "Fraunces", serif; }</style>` ,
302+ ) ,
303+ { fetchImpl, allowSystemFontCapture : false } ,
304+ ) ;
305+
306+ const cssUrls = urls . filter ( ( url ) => url . startsWith ( "https://fonts.googleapis.com/" ) ) ;
307+ expect ( cssUrls . some ( ( url ) => url . startsWith ( `${ AUTHORED_HREF } &text=` ) ) ) . toBe ( true ) ;
308+ expect ( cssUrls . some ( ( url ) => url . includes ( "ital,wght@" ) ) ) . toBe ( false ) ;
309+ expect ( result ) . toContain ( b64 ( "FRAUNCES_LINKED" ) ) ;
310+ expect ( result ) . not . toContain ( b64 ( "FRAUNCES_WIDE" ) ) ;
311+ } ) ;
312+
313+ it ( "uses a later link when it names a weight range and the first does not" , async ( ) => {
314+ const narrow = "https://fonts.googleapis.com/css2?family=Bodoni+Moda:wght@400&display=swap" ;
315+ const wide =
316+ "https://fonts.googleapis.com/css2?family=Bodoni+Moda:ital,opsz,wght@0,6..96,400..900&display=swap" ;
317+ const file = "https://fonts.gstatic.com/s/bodonimoda/range.woff2" ;
318+ const urls : string [ ] = [ ] ;
319+ const fetchImpl = ( async ( input : unknown ) => {
320+ const url = String ( input ) ;
321+ urls . push ( url ) ;
322+ if ( isAuthoredRequest ( url , wide ) ) {
323+ return new Response (
324+ `@font-face { font-family: 'Bodoni Moda'; font-style: normal; font-weight: 400 900; src: url(${ file } ) format('woff2'); unicode-range: U+0000-00FF; }` ,
325+ { status : 200 } ,
326+ ) ;
327+ }
328+ if ( url === file ) return new Response ( "BODONI_RANGE" , { status : 200 } ) ;
329+ return new Response ( "" , { status : 404 } ) ;
330+ } ) as unknown as typeof fetch ;
331+
332+ const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
333+ const result = await injectDeterministicFontFaces (
334+ authoredPage (
335+ `<link rel="stylesheet" href="${ narrow } ">` +
336+ `<link rel="stylesheet" href="${ wide } ">` +
337+ `<style>h1 { font-family: "Bodoni Moda", serif; font-weight: 700; }</style>` ,
338+ ) ,
339+ { fetchImpl, allowSystemFontCapture : false } ,
340+ ) ;
341+
342+ const cssUrls = urls . filter ( ( url ) => url . startsWith ( "https://fonts.googleapis.com/" ) ) ;
343+ expect ( cssUrls ) . toHaveLength ( 1 ) ;
344+ expect ( cssUrls [ 0 ] ?. startsWith ( `${ wide } &text=` ) ) . toBe ( true ) ;
345+ expect ( result ) . toContain ( "font-weight: 400 900;" ) ;
346+ expect ( result ) . toContain ( b64 ( "BODONI_RANGE" ) ) ;
347+ } ) ;
348+
349+ it ( "keeps the first link when a later one names fewer ranges" , async ( ) => {
350+ const wide =
351+ "https://fonts.googleapis.com/css2?family=Bodoni+Moda:ital,opsz,wght@0,6..96,400..900&display=swap" ;
352+ const narrow = "https://fonts.googleapis.com/css2?family=Bodoni+Moda:wght@400&display=swap" ;
353+ const file = "https://fonts.gstatic.com/s/bodonimoda/first-range.woff2" ;
354+ const urls : string [ ] = [ ] ;
355+ const fetchImpl = ( async ( input : unknown ) => {
356+ const url = String ( input ) ;
357+ urls . push ( url ) ;
358+ if ( isAuthoredRequest ( url , wide ) ) {
359+ return new Response (
360+ `@font-face { font-family: 'Bodoni Moda'; font-style: normal; font-weight: 400 900; src: url(${ file } ) format('woff2'); unicode-range: U+0000-00FF; }` ,
361+ { status : 200 } ,
362+ ) ;
363+ }
364+ return new Response ( "" , { status : 404 } ) ;
365+ } ) as unknown as typeof fetch ;
366+
367+ const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
368+ await injectDeterministicFontFaces (
369+ authoredPage (
370+ `<link rel="stylesheet" href="${ wide } ">` +
371+ `<link rel="stylesheet" href="${ narrow } ">` +
372+ `<style>h1 { font-family: "Bodoni Moda", serif; }</style>` ,
373+ ) ,
374+ { fetchImpl, allowSystemFontCapture : false } ,
375+ ) ;
376+
377+ const cssUrls = urls . filter ( ( url ) => url . startsWith ( "https://fonts.googleapis.com/" ) ) ;
378+ expect ( cssUrls ) . toHaveLength ( 1 ) ;
379+ expect ( cssUrls [ 0 ] ?. startsWith ( `${ wide } &text=` ) ) . toBe ( true ) ;
380+ } ) ;
381+
382+ it ( "leaves a link's own text list unchanged" , async ( ) => {
383+ const href = "https://fonts.googleapis.com/css2?family=Fraunces:opsz,[email protected] ,500&text=Hi" ; 384+ const urls : string [ ] = [ ] ;
385+ const fetchImpl = ( async ( input : unknown ) => {
386+ const url = String ( input ) ;
387+ urls . push ( url ) ;
388+ if ( url === href ) {
389+ return new Response (
390+ `@font-face { font-family: 'Fraunces'; font-style: normal; font-weight: 500; src: url(${ FRAUNCES_FILE } ) format('woff2'); unicode-range: U+0000-00FF; }` ,
391+ { status : 200 } ,
392+ ) ;
393+ }
394+ if ( url === FRAUNCES_FILE ) return new Response ( "FRAUNCES_LINKED" , { status : 200 } ) ;
395+ return new Response ( "" , { status : 404 } ) ;
396+ } ) as unknown as typeof fetch ;
397+
398+ const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
399+ await injectDeterministicFontFaces (
400+ authoredPage (
401+ `<link rel="stylesheet" href="${ href } "><style>h1 { font-family: "Fraunces", serif; }</style>` ,
402+ ) ,
403+ { fetchImpl, allowSystemFontCapture : false } ,
404+ ) ;
405+
406+ expect ( urls . filter ( ( url ) => url . startsWith ( "https://fonts.googleapis.com/" ) ) ) . toEqual ( [ href ] ) ;
407+ } ) ;
408+
409+ it ( "does not append page text when the character set does not fit on the font URL" , async ( ) => {
410+ const href = "https://fonts.googleapis.com/css2?family=Noto+Sans+JP:wght@400" ;
411+ const file = "https://fonts.gstatic.com/s/notosansjp/full.woff2" ;
412+ const many = Array . from ( { length : 600 } , ( _ , i ) => String . fromCodePoint ( 0x4e00 + i ) ) . join ( "" ) ;
413+ const urls : string [ ] = [ ] ;
414+ const fetchImpl = ( async ( input : unknown ) => {
415+ const url = String ( input ) ;
416+ urls . push ( url ) ;
417+ if ( url === href ) {
418+ return new Response (
419+ `@font-face { font-family: 'Noto Sans JP'; font-style: normal; font-weight: 400; src: url(${ file } ) format('woff2'); unicode-range: U+3000-30FF; }` ,
420+ { status : 200 } ,
421+ ) ;
422+ }
423+ if ( url === file ) return new Response ( "NOTO_FULL" , { status : 200 } ) ;
424+ return new Response ( "" , { status : 404 } ) ;
425+ } ) as unknown as typeof fetch ;
426+
427+ const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
428+ await injectDeterministicFontFaces (
429+ `<!doctype html><html><head><link rel="stylesheet" href="${ href } "><style>p { font-family: "Noto Sans JP", sans-serif; }</style></head><body>${ many } </body></html>` ,
430+ { fetchImpl, allowSystemFontCapture : false } ,
431+ ) ;
432+
433+ expect ( urls . filter ( ( url ) => url . startsWith ( "https://fonts.googleapis.com/" ) ) ) . toEqual ( [ href ] ) ;
434+ } ) ;
435+
436+ it ( "does not embed a local file when the page's link failed" , async ( ) => {
437+ const href = "https://fonts.googleapis.com/css2?family=Hf+Authored+Fail+Test" ;
438+ const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
439+ const urls : string [ ] = [ ] ;
440+ const fetchImpl = ( async ( input : unknown ) => {
441+ urls . push ( String ( input ) ) ;
442+ return new Response ( "" , { status : 400 } ) ;
443+ } ) as unknown as typeof fetch ;
444+ const result = await injectDeterministicFontFaces (
445+ authoredPage (
446+ `<link rel="stylesheet" href="${ href } "><style>h1 { font-family: "Hf Authored Fail Test", serif; }</style>` ,
447+ ) ,
448+ { fetchImpl, allowSystemFontCapture : true } ,
449+ ) ;
450+
451+ expect ( urls . some ( ( url ) => url . includes ( "ital,wght@" ) ) ) . toBe ( false ) ;
452+ expect ( result ) . not . toContain ( b64 ( LOCAL_FONT_BYTES ) ) ;
453+ } ) ;
454+
455+ it ( "still embeds a local file when the page has no Google link" , async ( ) => {
456+ const { injectDeterministicFontFaces } = await import ( "./deterministicFonts.js" ) ;
457+ const fetchImpl = ( async ( ) => new Response ( "" , { status : 400 } ) ) as unknown as typeof fetch ;
458+ const result = await injectDeterministicFontFaces (
459+ authoredPage ( `<style>h1 { font-family: "Hf Authored Fail Test", serif; }</style>` ) ,
460+ { fetchImpl, allowSystemFontCapture : true } ,
461+ ) ;
462+
463+ expect ( result ) . toContain ( b64 ( LOCAL_FONT_BYTES ) ) ;
464+ } ) ;
240465} ) ;
0 commit comments