@@ -165,4 +165,76 @@ describe('unifont', () => {
165165 await expect ( ( ) => unifont . listFonts ( ) ) . rejects . toThrow ( )
166166 } )
167167 } )
168+
169+ describe ( 'cache isolation' , ( ) => {
170+ it ( 'uses isolated namespace per provider\'s name' , async ( ) => {
171+ const storage = {
172+ getItem : vi . fn ( ) ,
173+ setItem : vi . fn ( ) ,
174+ }
175+
176+ const getProvider = ( name : string ) =>
177+ defineFontProvider ( name , async ( _options , ctx ) => {
178+ return {
179+ async resolveFont ( ) {
180+ await ctx . storage . setItem ( 'key' , 'value' )
181+ return { fonts : [ ] }
182+ } ,
183+ }
184+ } ) ( )
185+
186+ const unifontA = await createUnifont ( [
187+ getProvider ( 'provider-A' ) ,
188+ ] , { storage } )
189+ const unifontB = await createUnifont ( [
190+ getProvider ( 'provider-B' ) ,
191+ ] , { storage } )
192+
193+ await unifontA . resolveFont ( 'Poppins' )
194+ await unifontB . resolveFont ( 'Poppins' )
195+
196+ const providerACacheKey = storage . setItem . mock . calls . at ( 0 ) ?. at ( 0 ) as string | undefined
197+ const providerBCacheKey = storage . setItem . mock . calls . at ( 1 ) ?. at ( 0 ) as string | undefined
198+
199+ expect ( storage . setItem ) . toHaveBeenCalledTimes ( 2 )
200+ expect ( providerACacheKey ) . toBeDefined ( )
201+ expect ( providerBCacheKey ) . toBeDefined ( )
202+ expect ( providerACacheKey ) . not . toBe ( providerBCacheKey )
203+ } )
204+
205+ it ( 'uses isolated namespace per provider\'s options' , async ( ) => {
206+ const storage = {
207+ getItem : vi . fn ( ) ,
208+ setItem : vi . fn ( ) ,
209+ }
210+
211+ const getProvider = ( options : { variant : string } ) =>
212+ defineFontProvider ( 'optioned-provider' , async ( _options : { variant : string } , ctx ) => {
213+ return {
214+ async resolveFont ( ) {
215+ await ctx . storage . setItem ( 'key' , 'value' )
216+ return { fonts : [ ] }
217+ } ,
218+ }
219+ } ) ( options )
220+
221+ const unifontA = await createUnifont ( [
222+ getProvider ( { variant : 'A' } ) ,
223+ ] , { storage } )
224+ const unifontB = await createUnifont ( [
225+ getProvider ( { variant : 'B' } ) ,
226+ ] , { storage } )
227+
228+ await unifontA . resolveFont ( 'Poppins' )
229+ await unifontB . resolveFont ( 'Poppins' )
230+
231+ const variantACacheKey = storage . setItem . mock . calls . at ( 0 ) ?. at ( 0 ) as string | undefined
232+ const variantBCacheKey = storage . setItem . mock . calls . at ( 1 ) ?. at ( 0 ) as string | undefined
233+
234+ expect ( storage . setItem ) . toHaveBeenCalledTimes ( 2 )
235+ expect ( variantACacheKey ) . toBeDefined ( )
236+ expect ( variantBCacheKey ) . toBeDefined ( )
237+ expect ( variantACacheKey ) . not . toBe ( variantBCacheKey )
238+ } )
239+ } )
168240} )
0 commit comments