1- import { hasCacheExtension } from "@opennextjs/aws/adapters/cache.js" ;
1+ import S3Cache , { hasCacheExtension } from "@opennextjs/aws/adapters/cache.js" ;
2+ import { vi } from "vitest" ;
23
34describe ( "hasCacheExtension" , ( ) => {
45 it ( "Should returns true if has an extension and it is a CacheExtension" , ( ) => {
@@ -13,3 +14,58 @@ describe("hasCacheExtension", () => {
1314 expect ( hasCacheExtension ( "hello,json" ) ) . toBeFalsy ( ) ;
1415 } ) ;
1516} ) ;
17+
18+ describe ( "S3Cache" , ( ) => {
19+ let cache : S3Cache ;
20+
21+ const getFetchCacheSpy = vi . spyOn ( S3Cache . prototype , "getFetchCache" ) ;
22+ const getIncrementalCache = vi . spyOn (
23+ S3Cache . prototype ,
24+ "getIncrementalCache" ,
25+ ) ;
26+
27+ beforeEach ( ( ) => {
28+ cache = new S3Cache ( {
29+ _appDir : false ,
30+ _requestHeaders : undefined as never ,
31+ } ) ;
32+
33+ globalThis . disableIncrementalCache = false ;
34+ vi . clearAllMocks ( ) ;
35+ } ) ;
36+
37+ it ( "Should return null when incremental cache is disabled" , async ( ) => {
38+ globalThis . disableIncrementalCache = true ;
39+
40+ const result = await cache . get ( "key" ) ;
41+
42+ expect ( result ) . toBeNull ( ) ;
43+ } ) ;
44+
45+ it ( "Should return null for cache miss" , async ( ) => {
46+ const result = await cache . get ( "key" ) ;
47+
48+ expect ( result ) . toBeNull ( ) ;
49+ } ) ;
50+
51+ it ( "Should retrieve cache from fetch cache when fetch cache is true" , async ( ) => {
52+ await cache . get ( "key" , { fetchCache : true } ) ;
53+
54+ expect ( getFetchCacheSpy ) . toHaveBeenCalled ( ) ;
55+ } ) ;
56+
57+ it ( "Should retrieve cache from fetch cache when hint is fetch" , async ( ) => {
58+ await cache . get ( "key" , { kindHint : "fetch" } ) ;
59+
60+ expect ( getFetchCacheSpy ) . toHaveBeenCalled ( ) ;
61+ } ) ;
62+
63+ it . each ( [ "app" , "page" , undefined ] ) (
64+ "Should retrieve cache from incremental cache when hint is not fetch: %s" ,
65+ async ( kindHint ) => {
66+ await cache . get ( "key" , { kindHint : kindHint as any } ) ;
67+
68+ expect ( getIncrementalCache ) . toHaveBeenCalled ( ) ;
69+ } ,
70+ ) ;
71+ } ) ;
0 commit comments