@@ -5,6 +5,24 @@ import { createWebModuleLoader } from "../helpers/load-web-module.mjs";
55const loader = createWebModuleLoader ( ) ;
66const fontFamily = loader . loadModule ( "src/lib/shared/fontFamily.ts" ) ;
77
8+ async function withNavigator ( value , task ) {
9+ const previous = Object . getOwnPropertyDescriptor ( globalThis , "navigator" ) ;
10+ Object . defineProperty ( globalThis , "navigator" , {
11+ configurable : true ,
12+ enumerable : true ,
13+ value,
14+ } ) ;
15+ try {
16+ return await task ( ) ;
17+ } finally {
18+ if ( previous ) {
19+ Object . defineProperty ( globalThis , "navigator" , previous ) ;
20+ } else {
21+ delete globalThis . navigator ;
22+ }
23+ }
24+ }
25+
826test ( "font family normalizer keeps freeform stacks and rejects unsafe values" , ( ) => {
927 assert . equal ( fontFamily . normalizeFontFamily ( "" ) , "" ) ;
1028 assert . equal ( fontFamily . normalizeFontFamily ( "system" ) , "system" ) ;
@@ -125,7 +143,38 @@ test("applying font families updates CSS variables and only emits code changes",
125143 }
126144} ) ;
127145
128- test ( "listLocalFontFamilies uses queryLocalFonts when available" , async ( ) => {
146+ test ( "listLocalFontFamilies does not trigger a local-fonts permission prompt" , async ( ) => {
147+ const previous = globalThis . queryLocalFonts ;
148+ let queryCount = 0 ;
149+ globalThis . queryLocalFonts = async ( ) => {
150+ queryCount += 1 ;
151+ return [ { family : "Inter" } ] ;
152+ } ;
153+ try {
154+ await withNavigator (
155+ {
156+ permissions : {
157+ query : async ( descriptor ) => {
158+ assert . deepEqual ( descriptor , { name : "local-fonts" } ) ;
159+ return { state : "prompt" } ;
160+ } ,
161+ } ,
162+ } ,
163+ async ( ) => {
164+ assert . deepEqual ( await fontFamily . listLocalFontFamilies ( ) , [ ] ) ;
165+ } ,
166+ ) ;
167+ assert . equal ( queryCount , 0 ) ;
168+ } finally {
169+ if ( previous === undefined ) {
170+ delete globalThis . queryLocalFonts ;
171+ } else {
172+ globalThis . queryLocalFonts = previous ;
173+ }
174+ }
175+ } ) ;
176+
177+ test ( "listLocalFontFamilies uses queryLocalFonts when permission is already granted" , async ( ) => {
129178 const previous = globalThis . queryLocalFonts ;
130179 globalThis . queryLocalFonts = async ( ) => [
131180 { family : "Inter" } ,
@@ -134,7 +183,16 @@ test("listLocalFontFamilies uses queryLocalFonts when available", async () => {
134183 { family : " " } ,
135184 ] ;
136185 try {
137- assert . deepEqual ( await fontFamily . listLocalFontFamilies ( ) , [ "Inter" , "PingFang SC" ] ) ;
186+ await withNavigator (
187+ {
188+ permissions : {
189+ query : async ( ) => ( { state : "granted" } ) ,
190+ } ,
191+ } ,
192+ async ( ) => {
193+ assert . deepEqual ( await fontFamily . listLocalFontFamilies ( ) , [ "Inter" , "PingFang SC" ] ) ;
194+ } ,
195+ ) ;
138196 } finally {
139197 if ( previous === undefined ) {
140198 delete globalThis . queryLocalFonts ;
0 commit comments