@@ -8,6 +8,7 @@ import { mockDocumentLoader } from "../testing/docloader.ts";
8
8
import { test } from "../testing/mod.ts" ;
9
9
import preloadedContexts from "./contexts.ts" ;
10
10
import {
11
+ type DocumentLoader ,
11
12
FetchError ,
12
13
getDocumentLoader ,
13
14
getUserAgent ,
@@ -516,6 +517,89 @@ test("kvCache()", async (t) => {
516
517
} ,
517
518
} ) ;
518
519
} ) ;
520
+
521
+ await t . step ( "preloaded contexts bypass cache" , async ( ) => {
522
+ const kv = new MemoryKvStore ( ) ;
523
+ let loaderCalled = false ;
524
+ const mockLoader : DocumentLoader = ( url : string ) => {
525
+ loaderCalled = true ;
526
+ return Promise . resolve ( {
527
+ contextUrl : null ,
528
+ documentUrl : url ,
529
+ document : { "mock" : "document" } ,
530
+ } ) ;
531
+ } ;
532
+
533
+ const loader = kvCache ( {
534
+ kv,
535
+ loader : mockLoader ,
536
+ prefix : [ "_test" , "preloaded" ] ,
537
+ } ) ;
538
+
539
+ // Test that preloaded context URLs return preloaded data without calling the wrapped loader
540
+ const activityStreamsUrl = "https://www.w3.org/ns/activitystreams" ;
541
+ loaderCalled = false ;
542
+ const result = await loader ( activityStreamsUrl ) ;
543
+
544
+ assertEquals ( result , {
545
+ contextUrl : null ,
546
+ documentUrl : activityStreamsUrl ,
547
+ document : preloadedContexts [ activityStreamsUrl ] ,
548
+ } ) ;
549
+ assertEquals (
550
+ loaderCalled ,
551
+ false ,
552
+ "Loader should not be called for preloaded contexts" ,
553
+ ) ;
554
+
555
+ // Verify that the preloaded context was not cached in KV store
556
+ const cachedValue = await kv . get ( [
557
+ "_test" ,
558
+ "preloaded" ,
559
+ activityStreamsUrl ,
560
+ ] ) ;
561
+ assertEquals (
562
+ cachedValue ,
563
+ undefined ,
564
+ "Preloaded contexts should not be cached in KV store" ,
565
+ ) ;
566
+
567
+ // Test another preloaded context
568
+ const securityUrl = "https://w3id.org/security/v1" ;
569
+ loaderCalled = false ;
570
+ const result2 = await loader ( securityUrl ) ;
571
+
572
+ assertEquals ( result2 , {
573
+ contextUrl : null ,
574
+ documentUrl : securityUrl ,
575
+ document : preloadedContexts [ securityUrl ] ,
576
+ } ) ;
577
+ assertEquals (
578
+ loaderCalled ,
579
+ false ,
580
+ "Loader should not be called for preloaded contexts" ,
581
+ ) ;
582
+
583
+ // Test that non-preloaded URLs still use the cache normally
584
+ const nonPreloadedUrl = "https://example.com/not-preloaded" ;
585
+ loaderCalled = false ;
586
+ const result3 = await loader ( nonPreloadedUrl ) ;
587
+
588
+ assertEquals ( result3 , {
589
+ contextUrl : null ,
590
+ documentUrl : nonPreloadedUrl ,
591
+ document : { "mock" : "document" } ,
592
+ } ) ;
593
+ assertEquals (
594
+ loaderCalled ,
595
+ true ,
596
+ "Loader should be called for non-preloaded URLs" ,
597
+ ) ;
598
+
599
+ // Verify that non-preloaded URL was cached
600
+ const cachedValue2 = await kv . get ( [ "_test" , "preloaded" , nonPreloadedUrl ] ) ;
601
+ assertEquals ( cachedValue2 , result3 , "Non-preloaded URLs should be cached" ) ;
602
+ } ) ;
519
603
} ) ;
520
604
521
605
test ( "getUserAgent()" , ( ) => {
0 commit comments