@@ -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,95 @@ 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+ // Check if the message timestamp falls within the time range
79+ const messageTime = msgTimestamp . getTime ( ) ;
80+ const startTimeMs = timeStart . getTime ( ) ;
81+ const endTimeMs = timeEnd . getTime ( ) ;
82+
83+ if ( startTime > endTime ) {
84+ // Invalid range: startTime is after endTime
7885 expect ( messages . length ) . eq ( 0 ) ;
79- } else {
86+ } else if ( messageTime >= startTimeMs && messageTime <= endTimeMs ) {
87+ // Message timestamp is within the range
8088 expect ( messages . length ) . eq ( 1 ) ;
8189 expect ( messages [ 0 ] . payload ! [ 0 ] ! ) . eq ( 0 ) ;
90+ } else {
91+ // Message timestamp is outside the range
92+ expect ( messages . length ) . eq ( 0 ) ;
8293 }
8394 } ) ;
8495 } ) ;
8596
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 ) ;
97+ // Test case for messages with timestamps too far in the past
98+ it ( " Timestamp too far from node time: -20000 ms from now" , async function ( ) {
99+ const msgTimestamp = adjustDate ( new Date ( ) , - 20000 ) ;
100+ expect (
101+ await nwaku . sendMessage (
102+ ServiceNode . toMessageRpcQuery ( {
103+ payload : new Uint8Array ( [ 0 ] ) ,
104+ contentTopic : TestDecoder . contentTopic ,
105+ timestamp : msgTimestamp
106+ } ) ,
107+ TestRoutingInfo
108+ )
109+ ) . to . eq ( true ) ;
99110
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 )
111+ // Add a delay to ensure the message is stored before querying
112+ await new Promise ( ( resolve ) => setTimeout ( resolve , 200 ) ) ;
113+
114+ const messages : IMessage [ ] = [ ] ;
115+ await waku . store . queryWithOrderedCallback (
116+ [ TestDecoder ] ,
117+ ( msg ) => {
118+ if ( msg ) {
119+ messages . push ( msg ) ;
111120 }
112- ) ;
121+ } ,
122+ {
123+ timeStart : adjustDate ( msgTimestamp , - 1000 ) ,
124+ timeEnd : adjustDate ( msgTimestamp , 1000 )
125+ }
126+ ) ;
113127
114- expect ( messages . length ) . eq ( 0 ) ;
115- } ) ;
128+ expect ( messages . length ) . eq ( 0 ) ;
129+ } ) ;
130+
131+ // Test case for messages with timestamps too far in the future
132+ it ( "Timestamp too far from node time: 40000 ms from now" , async function ( ) {
133+ const msgTimestamp = adjustDate ( new Date ( ) , 40000 ) ;
134+ expect (
135+ await nwaku . sendMessage (
136+ ServiceNode . toMessageRpcQuery ( {
137+ payload : new Uint8Array ( [ 0 ] ) ,
138+ contentTopic : TestDecoder . contentTopic ,
139+ timestamp : msgTimestamp
140+ } ) ,
141+ TestRoutingInfo
142+ )
143+ ) . to . eq ( true ) ;
144+
145+ // Add a delay to ensure the message is stored before querying
146+ await new Promise ( ( resolve ) => setTimeout ( resolve , 200 ) ) ;
147+
148+ const messages : IMessage [ ] = [ ] ;
149+ await waku . store . queryWithOrderedCallback (
150+ [ TestDecoder ] ,
151+ ( msg ) => {
152+ if ( msg ) {
153+ messages . push ( msg ) ;
154+ }
155+ } ,
156+ {
157+ timeStart : adjustDate ( msgTimestamp , - 1000 ) ,
158+ timeEnd : adjustDate ( msgTimestamp , 1000 )
159+ }
160+ ) ;
161+
162+ expect ( messages . length ) . eq ( 0 ) ;
116163 } ) ;
117164} ) ;
0 commit comments