1
1
/* eslint-disable sonarjs/no-duplicate-string */
2
2
import Cache , { SOFT_TAG_PREFIX } from "@opennextjs/aws/adapters/cache.js" ;
3
- import { vi } from "vitest" ;
3
+ import { type Mock , vi } from "vitest" ;
4
4
5
5
declare global {
6
6
var openNextConfig : {
@@ -40,6 +40,7 @@ describe("CacheHandler", () => {
40
40
. fn ( )
41
41
. mockResolvedValue ( new Date ( "2024-01-02T00:00:00Z" ) . getTime ( ) ) ,
42
42
writeTags : vi . fn ( ) ,
43
+ getPathsByTags : undefined as Mock | undefined ,
43
44
} ;
44
45
globalThis . tagCache = tagCache ;
45
46
@@ -71,6 +72,8 @@ describe("CacheHandler", () => {
71
72
} ,
72
73
} ;
73
74
globalThis . isNextAfter15 = false ;
75
+ tagCache . mode = "original" ;
76
+ tagCache . getPathsByTags = undefined ;
74
77
} ) ;
75
78
76
79
describe ( "get" , ( ) => {
@@ -147,11 +150,13 @@ describe("CacheHandler", () => {
147
150
tagCache . mode = "nextMode" ;
148
151
tagCache . hasBeenRevalidated . mockResolvedValueOnce ( true ) ;
149
152
150
- const result = await cache . get ( "key" , { kind : "FETCH" } ) ;
153
+ const result = await cache . get ( "key" , {
154
+ kind : "FETCH" ,
155
+ tags : [ "tag" ] ,
156
+ } ) ;
151
157
expect ( getFetchCacheSpy ) . toHaveBeenCalled ( ) ;
158
+ expect ( tagCache . hasBeenRevalidated ) . toHaveBeenCalled ( ) ;
152
159
expect ( result ) . toBeNull ( ) ;
153
- // Reset the tagCache mode
154
- tagCache . mode = "original" ;
155
160
} ) ;
156
161
157
162
it ( "Should return null when incremental cache throws" , async ( ) => {
@@ -198,16 +203,20 @@ describe("CacheHandler", () => {
198
203
incrementalCache . get . mockResolvedValueOnce ( {
199
204
value : {
200
205
type : "route" ,
206
+ meta : {
207
+ headers : {
208
+ "x-next-cache-tags" : "tag" ,
209
+ } ,
210
+ } ,
201
211
} ,
202
212
lastModified : Date . now ( ) ,
203
213
} ) ;
204
214
205
215
const result = await cache . get ( "key" , { kindHint : "app" } ) ;
206
216
207
217
expect ( getIncrementalCache ) . toHaveBeenCalled ( ) ;
218
+ expect ( tagCache . hasBeenRevalidated ) . toHaveBeenCalled ( ) ;
208
219
expect ( result ) . toBeNull ( ) ;
209
- // Reset the tagCache mode
210
- tagCache . mode = "original" ;
211
220
} ) ;
212
221
213
222
it ( "Should return value when cache data type is route" , async ( ) => {
@@ -536,10 +545,10 @@ describe("CacheHandler", () => {
536
545
} ) ;
537
546
538
547
it ( "Should call tagCache.writeTags" , async ( ) => {
539
- globalThis . tagCache . getByTag . mockResolvedValueOnce ( [ "/path" ] ) ;
548
+ tagCache . getByTag . mockResolvedValueOnce ( [ "/path" ] ) ;
540
549
await cache . revalidateTag ( "tag" ) ;
541
550
542
- expect ( globalThis . tagCache . getByTag ) . toHaveBeenCalledWith ( "tag" ) ;
551
+ expect ( tagCache . getByTag ) . toHaveBeenCalledWith ( "tag" ) ;
543
552
544
553
expect ( tagCache . writeTags ) . toHaveBeenCalledTimes ( 1 ) ;
545
554
expect ( tagCache . writeTags ) . toHaveBeenCalledWith ( [
@@ -551,8 +560,8 @@ describe("CacheHandler", () => {
551
560
} ) ;
552
561
553
562
it ( "Should call invalidateCdnHandler.invalidatePaths" , async ( ) => {
554
- globalThis . tagCache . getByTag . mockResolvedValueOnce ( [ "/path" ] ) ;
555
- globalThis . tagCache . getByPath . mockResolvedValueOnce ( [ ] ) ;
563
+ tagCache . getByTag . mockResolvedValueOnce ( [ "/path" ] ) ;
564
+ tagCache . getByPath . mockResolvedValueOnce ( [ ] ) ;
556
565
await cache . revalidateTag ( `${ SOFT_TAG_PREFIX } path` ) ;
557
566
558
567
expect ( tagCache . writeTags ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -567,7 +576,7 @@ describe("CacheHandler", () => {
567
576
} ) ;
568
577
569
578
it ( "Should not call invalidateCdnHandler.invalidatePaths for fetch cache key " , async ( ) => {
570
- globalThis . tagCache . getByTag . mockResolvedValueOnce ( [ "123456" ] ) ;
579
+ tagCache . getByTag . mockResolvedValueOnce ( [ "123456" ] ) ;
571
580
await cache . revalidateTag ( "tag" ) ;
572
581
573
582
expect ( tagCache . writeTags ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -582,7 +591,7 @@ describe("CacheHandler", () => {
582
591
} ) ;
583
592
584
593
it ( "Should only call writeTags for nextMode" , async ( ) => {
585
- globalThis . tagCache . mode = "nextMode" ;
594
+ tagCache . mode = "nextMode" ;
586
595
await cache . revalidateTag ( [ "tag1" , "tag2" ] ) ;
587
596
588
597
expect ( tagCache . writeTags ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -591,18 +600,16 @@ describe("CacheHandler", () => {
591
600
} ) ;
592
601
593
602
it ( "Should not call writeTags when the tag list is empty for nextMode" , async ( ) => {
594
- globalThis . tagCache . mode = "nextMode" ;
603
+ tagCache . mode = "nextMode" ;
595
604
await cache . revalidateTag ( [ ] ) ;
596
605
597
606
expect ( tagCache . writeTags ) . not . toHaveBeenCalled ( ) ;
598
607
expect ( invalidateCdnHandler . invalidatePaths ) . not . toHaveBeenCalled ( ) ;
599
608
} ) ;
600
609
601
610
it ( "Should call writeTags and invalidateCdnHandler.invalidatePaths for nextMode that supports getPathsByTags" , async ( ) => {
602
- globalThis . tagCache . mode = "nextMode" ;
603
- globalThis . tagCache . getPathsByTags = vi
604
- . fn ( )
605
- . mockResolvedValueOnce ( [ "/path" ] ) ;
611
+ tagCache . mode = "nextMode" ;
612
+ tagCache . getPathsByTags = vi . fn ( ) . mockResolvedValueOnce ( [ "/path" ] ) ;
606
613
await cache . revalidateTag ( "tag" ) ;
607
614
608
615
expect ( tagCache . writeTags ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -619,8 +626,6 @@ describe("CacheHandler", () => {
619
626
] ,
620
627
} ,
621
628
] ) ;
622
- // Reset the getPathsByTags
623
- globalThis . tagCache . getPathsByTags = undefined ;
624
629
} ) ;
625
630
} ) ;
626
631
@@ -662,7 +667,7 @@ describe("CacheHandler", () => {
662
667
} ) ;
663
668
664
669
it ( "Should not bypass tag cache validation when shouldBypassTagCache is false" , async ( ) => {
665
- globalThis . tagCache . mode = "nextMode" ;
670
+ tagCache . mode = "nextMode" ;
666
671
incrementalCache . get . mockResolvedValueOnce ( {
667
672
value : {
668
673
kind : "FETCH" ,
@@ -688,7 +693,7 @@ describe("CacheHandler", () => {
688
693
} ) ;
689
694
690
695
it ( "Should not bypass tag cache validation when shouldBypassTagCache is undefined" , async ( ) => {
691
- globalThis . tagCache . mode = "nextMode" ;
696
+ tagCache . mode = "nextMode" ;
692
697
tagCache . hasBeenRevalidated . mockResolvedValueOnce ( false ) ;
693
698
incrementalCache . get . mockResolvedValueOnce ( {
694
699
value : {
@@ -762,11 +767,12 @@ describe("CacheHandler", () => {
762
767
} ) ;
763
768
764
769
it ( "Should not bypass tag cache validation when shouldBypassTagCache is false" , async ( ) => {
765
- globalThis . tagCache . mode = "nextMode" ;
770
+ tagCache . mode = "nextMode" ;
766
771
incrementalCache . get . mockResolvedValueOnce ( {
767
772
value : {
768
773
type : "route" ,
769
774
body : "{}" ,
775
+ meta : { headers : { "x-next-cache-tags" : "tag" } } ,
770
776
} ,
771
777
lastModified : Date . now ( ) ,
772
778
shouldBypassTagCache : false ,
@@ -780,12 +786,13 @@ describe("CacheHandler", () => {
780
786
} ) ;
781
787
782
788
it ( "Should return null when tag cache indicates revalidation and shouldBypassTagCache is false" , async ( ) => {
783
- globalThis . tagCache . mode = "nextMode" ;
789
+ tagCache . mode = "nextMode" ;
784
790
tagCache . hasBeenRevalidated . mockResolvedValueOnce ( true ) ;
785
791
incrementalCache . get . mockResolvedValueOnce ( {
786
792
value : {
787
793
type : "route" ,
788
794
body : "{}" ,
795
+ meta : { headers : { "x-next-cache-tags" : "tag" } } ,
789
796
} ,
790
797
lastModified : Date . now ( ) ,
791
798
shouldBypassTagCache : false ,
0 commit comments