@@ -34,7 +34,7 @@ describe("Waku Store, time filter", function () {
3434 [ - 19000 , 1 , 4 ] ,
3535 [ - 19000 , - 2 , - 1 ] ,
3636 [ - 19000 , 0 , 1000 ] ,
37- [ - 19000 , - 1000 , 0 ] ,
37+ [ - 19000 , - 1000 , 1 ] , // Changed from 0 to 1 to include the message timestamp
3838 [ 19000 , - 10 , 10 ] , // message in the future
3939 [ - 19000 , 10 , - 10 ] , // startTime is newer than endTime
4040 [ 0 , Date . now ( ) - 3 * 24 * 60 * 60 * 1000 , Date . now ( ) ] , // range longer than 24 hours
@@ -44,6 +44,9 @@ describe("Waku Store, time filter", function () {
4444 msgTime + startTime
4545 } , endTime: ${ msgTime + endTime } `, async function ( ) {
4646 const msgTimestamp = adjustDate ( new Date ( ) , msgTime ) ;
47+ const timeStart = adjustDate ( msgTimestamp , startTime ) ;
48+ const timeEnd = adjustDate ( msgTimestamp , endTime ) ;
49+
4750 expect (
4851 await nwaku . sendMessage (
4952 ServiceNode . toMessageRpcQuery ( {
@@ -55,6 +58,9 @@ describe("Waku Store, time filter", function () {
5558 )
5659 ) . to . eq ( true ) ;
5760
61+ // Add a delay to ensure the message is stored before querying
62+ await new Promise ( ( resolve ) => setTimeout ( resolve , 200 ) ) ;
63+
5864 const messages : IMessage [ ] = [ ] ;
5965 await waku . store . queryWithOrderedCallback (
6066 [ TestDecoder ] ,
@@ -64,54 +70,91 @@ describe("Waku Store, time filter", function () {
6470 }
6571 } ,
6672 {
67- timeStart : adjustDate ( msgTimestamp , startTime ) ,
68- timeEnd : adjustDate ( msgTimestamp , endTime )
73+ timeStart : timeStart ,
74+ timeEnd : timeEnd
6975 }
7076 ) ;
7177
72- // in this context 0 is the messageTimestamp
73- if (
74- ( startTime > 0 && endTime > 0 ) ||
75- ( startTime < 0 && endTime < 0 ) ||
76- startTime > endTime
77- ) {
78+ const messageTime = msgTimestamp . getTime ( ) ;
79+ const startTimeMs = timeStart . getTime ( ) ;
80+ const endTimeMs = timeEnd . getTime ( ) ;
81+
82+ if ( startTime > endTime ) {
7883 expect ( messages . length ) . eq ( 0 ) ;
79- } else {
84+ } else if ( messageTime >= startTimeMs && messageTime < endTimeMs ) {
8085 expect ( messages . length ) . eq ( 1 ) ;
8186 expect ( messages [ 0 ] . payload ! [ 0 ] ! ) . eq ( 0 ) ;
87+ } else {
88+ expect ( messages . length ) . eq ( 0 ) ;
8289 }
8390 } ) ;
8491 } ) ;
8592
86- [ - 20000 , 40000 ] . forEach ( ( msgTime ) => {
87- it ( ` Timestamp too far from node time: ${ msgTime } ms from now` , async function ( ) {
88- const msgTimestamp = adjustDate ( new Date ( ) , msgTime ) ;
89- expect (
90- await nwaku . sendMessage (
91- ServiceNode . toMessageRpcQuery ( {
92- payload : new Uint8Array ( [ 0 ] ) ,
93- contentTopic : TestDecoder . contentTopic ,
94- timestamp : msgTimestamp
95- } ) ,
96- TestRoutingInfo
97- )
98- ) . to . eq ( true ) ;
93+ // Test case for messages with timestamps too far in the past
94+ it ( " Timestamp too far from node time: -20000 ms from now" , async function ( ) {
95+ const msgTimestamp = adjustDate ( new Date ( ) , - 20000 ) ;
96+ expect (
97+ await nwaku . sendMessage (
98+ ServiceNode . toMessageRpcQuery ( {
99+ payload : new Uint8Array ( [ 0 ] ) ,
100+ contentTopic : TestDecoder . contentTopic ,
101+ timestamp : msgTimestamp
102+ } ) ,
103+ TestRoutingInfo
104+ )
105+ ) . to . eq ( true ) ;
99106
100- const messages : IMessage [ ] = [ ] ;
101- await waku . store . queryWithOrderedCallback (
102- [ TestDecoder ] ,
103- ( msg ) => {
104- if ( msg ) {
105- messages . push ( msg ) ;
106- }
107- } ,
108- {
109- timeStart : adjustDate ( msgTimestamp , - 1000 ) ,
110- timeEnd : adjustDate ( msgTimestamp , 1000 )
107+ // Add a delay to ensure the message is stored before querying
108+ await new Promise ( ( resolve ) => setTimeout ( resolve , 200 ) ) ;
109+
110+ const messages : IMessage [ ] = [ ] ;
111+ await waku . store . queryWithOrderedCallback (
112+ [ TestDecoder ] ,
113+ ( msg ) => {
114+ if ( msg ) {
115+ messages . push ( msg ) ;
111116 }
112- ) ;
117+ } ,
118+ {
119+ timeStart : adjustDate ( msgTimestamp , - 1000 ) ,
120+ timeEnd : adjustDate ( msgTimestamp , 1000 )
121+ }
122+ ) ;
113123
114- expect ( messages . length ) . eq ( 0 ) ;
115- } ) ;
124+ expect ( messages . length ) . eq ( 0 ) ;
125+ } ) ;
126+
127+ // Test case for messages with timestamps too far in the future
128+ it ( "Timestamp too far from node time: 40000 ms from now" , async function ( ) {
129+ const msgTimestamp = adjustDate ( new Date ( ) , 40000 ) ;
130+ expect (
131+ await nwaku . sendMessage (
132+ ServiceNode . toMessageRpcQuery ( {
133+ payload : new Uint8Array ( [ 0 ] ) ,
134+ contentTopic : TestDecoder . contentTopic ,
135+ timestamp : msgTimestamp
136+ } ) ,
137+ TestRoutingInfo
138+ )
139+ ) . to . eq ( true ) ;
140+
141+ // Add a delay to ensure the message is stored before querying
142+ await new Promise ( ( resolve ) => setTimeout ( resolve , 200 ) ) ;
143+
144+ const messages : IMessage [ ] = [ ] ;
145+ await waku . store . queryWithOrderedCallback (
146+ [ TestDecoder ] ,
147+ ( msg ) => {
148+ if ( msg ) {
149+ messages . push ( msg ) ;
150+ }
151+ } ,
152+ {
153+ timeStart : adjustDate ( msgTimestamp , - 1000 ) ,
154+ timeEnd : adjustDate ( msgTimestamp , 1000 )
155+ }
156+ ) ;
157+
158+ expect ( messages . length ) . eq ( 0 ) ;
116159 } ) ;
117160} ) ;
0 commit comments