@@ -19,7 +19,7 @@ import {
1919jest . mock ( 'inquirer' )
2020
2121describe ( 'history-util' , ( ) => {
22- describe ( 'epochTime ' , ( ) => {
22+ describe ( 'toEpochTime ' , ( ) => {
2323 it ( 'handles ISO input' , ( ) => {
2424 expect ( toEpochTime ( '2022-08-01T22:41:42.559Z' ) ) . toBe ( 1659393702559 )
2525 } )
@@ -29,7 +29,11 @@ describe('history-util', () => {
2929 expect ( toEpochTime ( '8/1/2022, 6:41:42 PM' ) ) . toBe ( expected )
3030 } )
3131
32- it ( 'handles undefined input' , ( ) => {
32+ it ( 'handles input in millis since epoch' , ( ) => {
33+ expect ( toEpochTime ( '1700596752000' ) ) . toBe ( 1700596752000 )
34+ } )
35+
36+ it ( 'handles undefined input' , ( ) => {
3337 expect ( toEpochTime ( undefined ) ) . toBeUndefined ( )
3438 } )
3539 } )
@@ -189,8 +193,8 @@ describe('history-util', () => {
189193 hasNext . mockReturnValueOnce ( true )
190194 hasNext . mockReturnValueOnce ( true )
191195 hasNext . mockReturnValueOnce ( false )
192- promptSpy . mockResolvedValueOnce ( { more : '' } )
193- promptSpy . mockResolvedValueOnce ( { more : 'y' } )
196+ promptSpy . mockResolvedValueOnce ( { more : true } )
197+ promptSpy . mockResolvedValueOnce ( { more : true } )
194198
195199 await writeDeviceEventsTable ( commandMock , dataMock )
196200
@@ -201,8 +205,8 @@ describe('history-util', () => {
201205
202206 it ( 'returns next page until canceled' , async ( ) => {
203207 hasNext . mockReturnValue ( true )
204- promptSpy . mockResolvedValueOnce ( { more : '' } )
205- promptSpy . mockResolvedValueOnce ( { more : 'n' } )
208+ promptSpy . mockResolvedValueOnce ( { more : true } )
209+ promptSpy . mockResolvedValueOnce ( { more : false } )
206210
207211 await writeDeviceEventsTable ( commandMock , dataMock )
208212
@@ -231,8 +235,12 @@ describe('history-util', () => {
231235
232236 const params = { locationId : 'location-id' , deviceId : 'device-id' }
233237 const items : DeviceActivity [ ] = [ ]
234- for ( let index = 0 ; index < maxItemsPerRequest * maxRequestsBeforeWarning + 10 ; index ++ ) {
235- items . push ( { deviceId : 'history-device-id' , text : `item${ index } ` } as DeviceActivity )
238+ const totalNumItems = maxItemsPerRequest * maxRequestsBeforeWarning + 10
239+ for ( let index = 0 ; index < totalNumItems ; index ++ ) {
240+ items . push ( {
241+ deviceId : 'history-device-id' ,
242+ text : `item${ index } ` ,
243+ } as DeviceActivity )
236244 }
237245
238246 const promptMock = jest . mocked ( inquirer . prompt )
@@ -353,5 +361,38 @@ describe('history-util', () => {
353361 expect ( hasNextMock ) . toHaveBeenCalledTimes ( 6 )
354362 expect ( nextMock ) . toHaveBeenCalledTimes ( 6 )
355363 } )
364+
365+ it ( 'stops paging when results are before specified after' , async ( ) => {
366+ const epochTimestamp = 1701097200 // 2023/11/27 9:00 a.m. CST
367+ const makeActivity = ( index : number , epoch : number ) : DeviceActivity => ( {
368+ deviceId : 'history-device-id' ,
369+ text : `item${ index } ` ,
370+ epoch,
371+ } as DeviceActivity )
372+ const firstPage = [
373+ makeActivity ( 0 , epochTimestamp + 600 ) ,
374+ makeActivity ( 1 , epochTimestamp + 500 ) ,
375+ makeActivity ( 2 , epochTimestamp + 400 ) ,
376+ ]
377+ const secondPage = [
378+ makeActivity ( 3 , epochTimestamp + 300 ) ,
379+ makeActivity ( 4 , epochTimestamp + 200 ) ,
380+ makeActivity ( 5 , epochTimestamp + 100 ) ,
381+ ]
382+ const historyResponse = makeHistoryResponse ( firstPage )
383+ historyDevicesMock . mockResolvedValueOnce ( historyResponse )
384+ hasNextMock . mockReturnValue ( true )
385+ nextMock . mockImplementationOnce ( async ( ) => historyResponse . items = secondPage )
386+
387+ const paramsWithAfter = { ...params , after : epochTimestamp + 350 }
388+
389+ const result = await getHistory ( client , 300 , 3 , paramsWithAfter )
390+ expect ( result ) . toStrictEqual ( firstPage )
391+
392+ expect ( historyDevicesMock ) . toHaveBeenCalledTimes ( 1 )
393+ expect ( historyDevicesMock ) . toHaveBeenCalledWith ( paramsWithAfter )
394+ expect ( hasNextMock ) . toHaveBeenCalledTimes ( 1 )
395+ expect ( nextMock ) . toHaveBeenCalledTimes ( 1 )
396+ } )
356397 } )
357398} )
0 commit comments