@@ -30,7 +30,7 @@ type TestStoreConfig = {
3030}
3131
3232const getEventStore = ( config : TestStoreConfig = { } ) => {
33- const mockStore = getMockAsyncCache < StoredEvent > ( ) ;
33+ const mockStore = getMockAsyncCache < string > ( ) ;
3434 const store = new EventStore ( { ...config , store : mockStore } ) ;
3535 return { mockStore, store }
3636}
@@ -213,17 +213,18 @@ describe('EventStore', () => {
213213 const originalSet = mockStore . set . bind ( mockStore ) ;
214214
215215 let call = 0 ;
216- const setSpy = vi . spyOn ( mockStore , 'set' ) . mockImplementation ( async ( key : string , value : StoredEvent ) => {
216+ const setSpy = vi . spyOn ( mockStore , 'set' ) . mockImplementation ( async ( key : string , value : string ) => {
217217 if ( call ++ > 0 ) {
218218 return originalSet ( key , value ) ;
219219 }
220220
221221 // Simulate old stored event without time info
222+ const stored : StoredEvent = JSON . parse ( value ) ;
222223 const eventWithoutTime : StoredEvent = {
223- id : value . id ,
224- event : value . event ,
224+ id : stored . id ,
225+ event : stored . event ,
225226 } ;
226- return originalSet ( key , eventWithoutTime ) ;
227+ return originalSet ( key , JSON . stringify ( eventWithoutTime ) ) ;
227228 } ) ;
228229
229230 await store . set ( 'test' , event ) ;
@@ -235,12 +236,12 @@ describe('EventStore', () => {
235236 await exhaustMicrotasks ( ) ;
236237 expect ( setSpy ) . toHaveBeenCalledTimes ( 2 ) ;
237238
238- const secondCall = setSpy . mock . calls [ 1 ] ;
239+ const resavedEvent : StoredEvent = JSON . parse ( setSpy . mock . calls [ 1 ] [ 1 ] ) ;
239240
240- expect ( secondCall [ 1 ] . _time ) . toBeDefined ( ) ;
241- expect ( secondCall [ 1 ] . _time ?. storedAt ) . toBeLessThanOrEqual ( Date . now ( ) ) ;
242- expect ( secondCall [ 1 ] . _time ?. storedAt ) . toBeGreaterThanOrEqual ( Date . now ( ) - 10 ) ;
243- expect ( secondCall [ 1 ] . _time ?. ttl ) . toBe ( ttl ) ;
241+ expect ( resavedEvent . _time ) . toBeDefined ( ) ;
242+ expect ( resavedEvent . _time ?. storedAt ) . toBeLessThanOrEqual ( Date . now ( ) ) ;
243+ expect ( resavedEvent . _time ?. storedAt ) . toBeGreaterThanOrEqual ( Date . now ( ) - 10 ) ;
244+ expect ( resavedEvent . _time ?. ttl ) . toBe ( ttl ) ;
244245 } ) ;
245246
246247 it ( 'should store event when key expires after store being full' , async ( ) => {
@@ -327,37 +328,38 @@ describe('EventStore', () => {
327328 const originalSet = mockStore . set . bind ( mockStore ) ;
328329
329330 let call = 0 ;
330- const setSpy = vi . spyOn ( mockStore , 'set' ) . mockImplementation ( async ( key : string , value : StoredEvent ) => {
331+ const setSpy = vi . spyOn ( mockStore , 'set' ) . mockImplementation ( async ( key : string , value : string ) => {
331332 if ( call ++ > 0 ) {
332333 return originalSet ( key , value ) ;
333334 }
334335
335336 // Simulate old stored event without time information
337+ const stored : StoredEvent = JSON . parse ( value ) ;
336338 const eventWithoutTime : StoredEvent = {
337- id : value . id ,
338- event : value . event ,
339+ id : stored . id ,
340+ event : stored . event ,
339341 } ;
340- return originalSet ( key , eventWithoutTime ) ;
342+ return originalSet ( key , JSON . stringify ( eventWithoutTime ) ) ;
341343 } ) ;
342344
343345 await store . set ( 'key-1' , event ) ;
344346 await store . set ( 'key-2' , event ) ;
345347
346348 const results = await store . getBatched ( [ 'key-1' , 'key-2' ] ) ;
347-
349+
348350 expect ( results ) . toHaveLength ( 2 ) ;
349351 expect ( results [ 0 ] ) . toEqual ( expect . objectContaining ( event ) ) ;
350352 expect ( results [ 1 ] ) . toEqual ( expect . objectContaining ( event ) ) ;
351353
352354 await exhaustMicrotasks ( ) ;
353355 expect ( setSpy ) . toHaveBeenCalledTimes ( 3 ) ;
354356
355- const secondCall = setSpy . mock . calls [ 1 ] ;
357+ const resavedEvent : StoredEvent = JSON . parse ( setSpy . mock . calls [ 1 ] [ 1 ] ) ;
356358
357- expect ( secondCall [ 1 ] . _time ) . toBeDefined ( ) ;
358- expect ( secondCall [ 1 ] . _time ?. storedAt ) . toBeLessThanOrEqual ( Date . now ( ) ) ;
359- expect ( secondCall [ 1 ] . _time ?. storedAt ) . toBeGreaterThanOrEqual ( Date . now ( ) - 10 ) ;
360- expect ( secondCall [ 1 ] . _time ?. ttl ) . toBe ( ttl ) ;
359+ expect ( resavedEvent . _time ) . toBeDefined ( ) ;
360+ expect ( resavedEvent . _time ?. storedAt ) . toBeLessThanOrEqual ( Date . now ( ) ) ;
361+ expect ( resavedEvent . _time ?. storedAt ) . toBeGreaterThanOrEqual ( Date . now ( ) - 10 ) ;
362+ expect ( resavedEvent . _time ?. ttl ) . toBe ( ttl ) ;
361363 } ) ;
362364
363365 it ( 'should store event when keys expire during getBatched after store being full' , async ( ) => {
@@ -401,7 +403,7 @@ describe('EventStore', () => {
401403 const originalSet = mockStore . set . bind ( mockStore ) ;
402404
403405 let call = 0 ;
404- vi . spyOn ( mockStore , 'set' ) . mockImplementation ( async ( key : string , value : StoredEvent ) => {
406+ vi . spyOn ( mockStore , 'set' ) . mockImplementation ( async ( key : string , value : string ) => {
405407 // only the seconde set call should fail
406408 if ( call ++ != 1 ) return originalSet ( key , value ) ;
407409 return Promise . reject ( new Error ( 'Simulated set failure' ) ) ;
0 commit comments