@@ -135,6 +135,59 @@ describe("AssetDataRouter chart history", () => {
135135 persistence . close ( ) ;
136136 } ) ;
137137
138+ test ( "ignores the previous chart history cache generation" , async ( ) => {
139+ const dbPath = createTempDbPath ( "previous-chart-cache-generation" ) ;
140+ const persistence = new AppPersistence ( dbPath ) ;
141+ const latestDate = new Date ( Date . now ( ) - 60_000 ) ;
142+ const previousDate = new Date ( latestDate . getTime ( ) - 5 * 60_000 ) ;
143+
144+ persistence . resources . set (
145+ {
146+ namespace : "market" ,
147+ kind : "price-history" ,
148+ entityKey : "META" ,
149+ variantKey : "exchange=NASDAQ;range=1M;resolution=5m;version=2" ,
150+ sourceKey : "provider:gloomberb-cloud" ,
151+ } ,
152+ [
153+ { date : previousDate , close : 620 } ,
154+ { date : latestDate , close : 680 } ,
155+ ] ,
156+ {
157+ cachePolicy : { staleMs : 60_000 , expireMs : 60_000 } ,
158+ } ,
159+ ) ;
160+
161+ let providerCalls = 0 ;
162+ const router = new AssetDataRouter ( {
163+ ...fallbackProvider ,
164+ id : "gloomberb-cloud" ,
165+ name : "Gloomberb Cloud" ,
166+ async getPriceHistoryForResolution ( ) {
167+ providerCalls += 1 ;
168+ return [
169+ { date : previousDate , close : 620 } ,
170+ { date : latestDate , close : 560 } ,
171+ ] ;
172+ } ,
173+ } , [ ] , persistence . resources ) ;
174+
175+ const history = await router . getPriceHistoryForResolution ( "META" , "NASDAQ" , "1M" , "5m" ) ;
176+
177+ expect ( providerCalls ) . toBe ( 1 ) ;
178+ expect ( history . map ( ( point ) => point . close ) ) . toEqual ( [ 620 , 560 ] ) ;
179+
180+ const cachedRows = persistence . database . connection
181+ . query ( "SELECT variant_key FROM resource_cache WHERE namespace = ? AND kind = ? AND entity_key = ? ORDER BY variant_key" )
182+ . all ( "market" , "price-history" , "META" ) as Array < { variant_key : string } > ;
183+ expect ( cachedRows . map ( ( row ) => row . variant_key ) ) . toEqual ( [
184+ "exchange=NASDAQ;range=1M;resolution=5m;version=2" ,
185+ "exchange=NASDAQ;range=1M;resolution=5m;version=3" ,
186+ ] ) ;
187+
188+ persistence . close ( ) ;
189+ } ) ;
190+
138191 test ( "ignores legacy unversioned sub-unit chart history caches" , async ( ) => {
139192 const dbPath = createTempDbPath ( "subunit-chart-cache" ) ;
140193 const persistence = new AppPersistence ( dbPath ) ;
@@ -180,7 +233,7 @@ describe("AssetDataRouter chart history", () => {
180233 . all ( "market" , "price-history" , "FTC" ) as Array < { variant_key : string } > ;
181234 expect ( cachedRows . map ( ( row ) => row . variant_key ) ) . toEqual ( [
182235 "exchange=LSE;range=ALL;resolution=1wk" ,
183- "exchange=LSE;range=ALL;resolution=1wk;version=2 ;unit=GBP" ,
236+ "exchange=LSE;range=ALL;resolution=1wk;version=3 ;unit=GBP" ,
184237 ] ) ;
185238
186239 persistence . close ( ) ;
0 commit comments