@@ -25,10 +25,15 @@ import appConfig from '../config/app';
2525import * as helper from './appHelper' ;
2626
2727describe ( 'helpers/appHelper' , ( ) => {
28- afterEach ( ( ) => {
28+ beforeEach ( ( ) => {
29+ helper . __clearCaches ( ) ;
2930 jest . clearAllMocks ( ) ;
3031 } ) ;
3132
33+ afterEach ( ( ) => {
34+ jest . useRealTimers ( ) ;
35+ } ) ;
36+
3237 test ( 'fetchOmdbData returns empty object when query missing' , async ( ) => {
3338 const result = await helper . fetchOmdbData ( '' , true ) ;
3439 expect ( result ) . toEqual ( { } ) ;
@@ -63,6 +68,19 @@ describe('helpers/appHelper', () => {
6368 expect ( data ) . toEqual ( { } ) ;
6469 } ) ;
6570
71+ test ( 'fetchOmdbData caches results and expires' , async ( ) => {
72+ jest . useFakeTimers ( ) ;
73+ ( http . request as jest . Mock ) . mockResolvedValue ( { data : { Title : 'Test' } } ) ;
74+ await helper . fetchOmdbData ( 'tt123' , false , 'movie' ) ;
75+ await helper . fetchOmdbData ( 'tt123' , false , 'movie' ) ;
76+ expect ( http . request ) . toHaveBeenCalledTimes ( 1 ) ;
77+ jest . setSystemTime ( Date . now ( ) + helper . CACHE_TTL_MS + 1 ) ;
78+ ( http . request as jest . Mock ) . mockResolvedValue ( { data : { Title : 'Test2' } } ) ;
79+ await helper . fetchOmdbData ( 'tt123' , false , 'movie' ) ;
80+ expect ( http . request ) . toHaveBeenCalledTimes ( 2 ) ;
81+ jest . useRealTimers ( ) ;
82+ } ) ;
83+
6684 test ( 'fetchAndUpdatePosters validates poster availability' , async ( ) => {
6785 const shows : any [ ] = [
6886 { imdb_id : '1' } ,
@@ -95,36 +113,63 @@ describe('helpers/appHelper', () => {
95113 test ( 'getSeriesDetail retrieves seasons and episodes' , async ( ) => {
96114 ( http . request as jest . Mock )
97115 . mockResolvedValueOnce ( { data : { totalSeasons : '2' , Episodes : [ { Episode : '1' , Title : 'E1' } ] } } )
98- . mockResolvedValueOnce ( { data : { Episodes : [ { Episode : '1' , Title : 'E2' } , { Episode : '2' , Title : 'E3' } ] } } ) ;
99- const detail = await helper . getSeriesDetail ( 'tt1' ) ;
116+ . mockResolvedValueOnce ( { data : { totalSeasons : '2' , Episodes : [ { Episode : '1' , Title : 'E2' } , { Episode : '2' , Title : 'E3' } ] } } ) ;
117+ const detail = await helper . getSeriesDetail ( 'tt1' , 1 ) ;
100118 expect ( http . request ) . toHaveBeenCalledTimes ( 2 ) ;
101119 expect ( detail . totalSeasons ) . toBe ( 2 ) ;
102- expect ( detail . totalEpisodes ) . toBe ( 3 ) ;
103- expect ( detail . seasons [ 1 ] . episodes [ 1 ] . title ) . toBe ( 'E3' ) ;
120+ expect ( detail . currentSeason . episodes [ 0 ] . title ) . toBe ( 'E1' ) ;
121+ expect ( detail . nextSeason ? .episodes [ 1 ] . title ) . toBe ( 'E3' ) ;
104122 } ) ;
105123
106124 test ( 'getSeriesDetail handles missing titles and episode arrays' , async ( ) => {
107125 ( http . request as jest . Mock )
108126 . mockResolvedValueOnce ( { data : { totalSeasons : '2' , Episodes : [ { Episode : '1' , Title : 'N/A' } ] } } )
109- . mockResolvedValueOnce ( { data : { Episodes : 'N/A' } } ) ;
110- const detail = await helper . getSeriesDetail ( 'tt2' ) ;
111- expect ( detail . seasons [ 0 ] . episodes [ 0 ] . title ) . toBeUndefined ( ) ;
112- expect ( detail . seasons [ 1 ] . episodes ) . toEqual ( [ ] ) ;
127+ . mockResolvedValueOnce ( { data : { totalSeasons : '2' , Episodes : 'N/A' } } ) ;
128+ const detail = await helper . getSeriesDetail ( 'tt2' , 1 ) ;
129+ expect ( detail . currentSeason . episodes [ 0 ] . title ) . toBeUndefined ( ) ;
130+ expect ( detail . nextSeason ? .episodes ) . toEqual ( [ ] ) ;
113131 } ) ;
114132
115133 test ( 'getSeriesDetail defaults when totalSeasons missing' , async ( ) => {
116134 ( http . request as jest . Mock ) . mockResolvedValueOnce ( { data : { } } ) ;
117- const detail = await helper . getSeriesDetail ( 'tt3' ) ;
135+ const detail = await helper . getSeriesDetail ( 'tt3' , 1 ) ;
118136 expect ( detail . totalSeasons ) . toBe ( 0 ) ;
119- expect ( detail . seasons ) . toEqual ( [ ] ) ;
137+ expect ( detail . currentSeason . episodes ) . toEqual ( [ ] ) ;
120138 } ) ;
121139
122140 test ( 'getSeriesDetail returns empty when id missing' , async ( ) => {
123- const detail = await helper . getSeriesDetail ( '' ) ;
124- expect ( detail ) . toEqual ( { totalSeasons : 0 , totalEpisodes : 0 , seasons : [ ] } ) ;
141+ const detail = await helper . getSeriesDetail ( '' , 1 ) ;
142+ expect ( detail ) . toEqual ( { totalSeasons : 0 , currentSeason : { season : 0 , episodes : [ ] } } ) ;
125143 expect ( http . request ) . not . toHaveBeenCalled ( ) ;
126144 } ) ;
127145
146+ test ( 'getSeriesDetail caches seasons and expires' , async ( ) => {
147+ jest . useFakeTimers ( ) ;
148+ ( http . request as jest . Mock )
149+ . mockResolvedValueOnce ( { data : { totalSeasons : '2' , Episodes : [ { Episode : '1' , Title : 'E1' } ] } } )
150+ . mockResolvedValueOnce ( { data : { totalSeasons : '2' , Episodes : [ { Episode : '1' , Title : 'E2' } ] } } ) ;
151+ await helper . getSeriesDetail ( 'tt1' , 1 ) ;
152+ await helper . getSeriesDetail ( 'tt1' , 1 ) ;
153+ expect ( http . request ) . toHaveBeenCalledTimes ( 2 ) ;
154+ jest . setSystemTime ( Date . now ( ) + helper . CACHE_TTL_MS + 1 ) ;
155+ ( http . request as jest . Mock )
156+ . mockResolvedValueOnce ( { data : { totalSeasons : '2' , Episodes : [ { Episode : '1' , Title : 'E1' } ] } } )
157+ . mockResolvedValueOnce ( { data : { totalSeasons : '2' , Episodes : [ { Episode : '1' , Title : 'E2' } ] } } ) ;
158+ await helper . getSeriesDetail ( 'tt1' , 1 ) ;
159+ expect ( http . request ) . toHaveBeenCalledTimes ( 4 ) ;
160+ jest . useRealTimers ( ) ;
161+ } ) ;
162+
163+ test ( 'getSeriesDetail caches neighbour seasons' , async ( ) => {
164+ ( http . request as jest . Mock )
165+ . mockResolvedValueOnce ( { data : { totalSeasons : '3' , Episodes : [ { Episode : '1' , Title : 'A' } ] } } )
166+ . mockResolvedValueOnce ( { data : { totalSeasons : '3' , Episodes : [ { Episode : '1' , Title : 'B' } ] } } )
167+ . mockResolvedValueOnce ( { data : { totalSeasons : '3' , Episodes : [ { Episode : '1' , Title : 'C' } ] } } ) ;
168+ await helper . getSeriesDetail ( 'tt1' , 1 ) ;
169+ await helper . getSeriesDetail ( 'tt1' , 2 ) ;
170+ expect ( http . request ) . toHaveBeenCalledTimes ( 3 ) ;
171+ } ) ;
172+
128173 test ( 'useAuth is false when no mongo uri' , ( ) => {
129174 expect ( helper . useAuth ) . toBe ( false ) ;
130175 } ) ;
0 commit comments