@@ -9,6 +9,8 @@ import { traktClientSettings } from '../trakt-client.service';
9
9
import { TraktClient } from './trakt-client' ;
10
10
11
11
import type { TraktAuthentication , TraktDeviceAuthentication } from '../../../models/trakt/trakt-authentication.model' ;
12
+ import type { TraktApiResponse } from '../../../models/trakt/trakt-client.model' ;
13
+ import type { CacheStore } from '../../../utils/cache.utils' ;
12
14
13
15
import type { RecursiveRecord } from '../../../utils/typescript.utils' ;
14
16
@@ -28,6 +30,7 @@ describe('trakt-client.ts', () => {
28
30
29
31
afterEach ( async ( ) => {
30
32
await traktClient . importAuthentication ( { } ) ;
33
+ await traktClient . clearCache ( ) ;
31
34
32
35
vi . clearAllMocks ( ) ;
33
36
} ) ;
@@ -75,6 +78,84 @@ describe('trakt-client.ts', () => {
75
78
expect ( fetch ) . toHaveBeenCalledWith ( new URL ( '/certifications/shows' , traktClientSettings . endpoint ) . toString ( ) , payload ) ;
76
79
} ) ;
77
80
81
+ describe ( 'cache' , ( ) => {
82
+ it ( 'should not cache calls' , async ( ) => {
83
+ expect . assertions ( 2 ) ;
84
+
85
+ await traktClient . certifications ( { type : 'movies' } ) ;
86
+ await traktClient . certifications ( { type : 'movies' } ) ;
87
+ await traktClient . certifications ( { type : 'movies' } ) ;
88
+
89
+ expect ( fetch ) . toHaveBeenCalledTimes ( 3 ) ;
90
+ expect ( fetch ) . toHaveBeenCalledWith ( new URL ( '/certifications/movies' , traktClientSettings . endpoint ) . toString ( ) , payload ) ;
91
+ } ) ;
92
+
93
+ it ( 'should cache subsequent calls' , async ( ) => {
94
+ expect . assertions ( 2 ) ;
95
+
96
+ await traktClient . certifications . cached ( { type : 'movies' } ) ;
97
+ await traktClient . certifications . cached ( { type : 'movies' } ) ;
98
+ await traktClient . certifications . cached ( { type : 'movies' } ) ;
99
+
100
+ expect ( fetch ) . toHaveBeenCalledTimes ( 1 ) ;
101
+ expect ( fetch ) . toHaveBeenCalledWith ( new URL ( '/certifications/movies' , traktClientSettings . endpoint ) . toString ( ) , payload ) ;
102
+ } ) ;
103
+
104
+ it ( 'should ignore cache if cache cleared' , async ( ) => {
105
+ expect . assertions ( 2 ) ;
106
+
107
+ await traktClient . certifications . cached ( { type : 'movies' } ) ;
108
+ await traktClient . certifications . cached ( { type : 'movies' } ) ;
109
+ await traktClient . clearCache ( ) ;
110
+ await traktClient . certifications . cached ( { type : 'movies' } ) ;
111
+
112
+ expect ( fetch ) . toHaveBeenCalledTimes ( 2 ) ;
113
+ expect ( fetch ) . toHaveBeenCalledWith ( new URL ( '/certifications/movies' , traktClientSettings . endpoint ) . toString ( ) , payload ) ;
114
+ } ) ;
115
+
116
+ it ( 'should clear cache after error' , async ( ) => {
117
+ expect . assertions ( 3 ) ;
118
+
119
+ const error = new Error ( 'Error' ) ;
120
+ fetch . mockRejectedValueOnce ( error ) ;
121
+
122
+ let err : unknown ;
123
+ try {
124
+ await traktClient . certifications . cached ( { type : 'movies' } ) ;
125
+ } catch ( e ) {
126
+ err = e ;
127
+ } finally {
128
+ expect ( err ) . toBe ( error ) ;
129
+ }
130
+ await traktClient . certifications . cached ( { type : 'movies' } ) ;
131
+ await traktClient . certifications . cached ( { type : 'movies' } ) ;
132
+
133
+ expect ( fetch ) . toHaveBeenCalledTimes ( 2 ) ;
134
+ expect ( fetch ) . toHaveBeenCalledWith ( new URL ( '/certifications/movies' , traktClientSettings . endpoint ) . toString ( ) , payload ) ;
135
+ } ) ;
136
+
137
+ it ( 'should ignore cache if cache expired' , async ( ) => {
138
+ expect . assertions ( 2 ) ;
139
+
140
+ const cacheStore : CacheStore < TraktApiResponse > = new Map ( ) ;
141
+ cacheStore . retention = 100 ;
142
+ const _traktClient = new TraktClient ( { ...traktClientSettings , cacheStore } ) ;
143
+
144
+ await _traktClient . certifications . cached ( { type : 'movies' } ) ;
145
+ await _traktClient . certifications . cached ( { type : 'movies' } ) ;
146
+
147
+ // Wait for cache to expire
148
+ await new Promise ( resolve => {
149
+ setTimeout ( resolve , 200 ) ;
150
+ } ) ;
151
+
152
+ await _traktClient . certifications . cached ( { type : 'movies' } ) ;
153
+
154
+ expect ( fetch ) . toHaveBeenCalledTimes ( 2 ) ;
155
+ expect ( fetch ) . toHaveBeenCalledWith ( new URL ( '/certifications/movies' , traktClientSettings . endpoint ) . toString ( ) , payload ) ;
156
+ } ) ;
157
+ } ) ;
158
+
78
159
const deviceAuthentication : TraktDeviceAuthentication = {
79
160
device_code : 'device_code' ,
80
161
user_code : 'user_code' ,
0 commit comments