@@ -137,7 +137,7 @@ impl RoomIndex {
137
137
self . writer . add_document ( document) ?;
138
138
}
139
139
}
140
- } ;
140
+ }
141
141
Ok ( ( ) )
142
142
}
143
143
@@ -192,12 +192,11 @@ impl RoomIndex {
192
192
}
193
193
194
194
fn contains ( & self , event_id : & EventId ) -> bool {
195
- let search_result = self . search ( format ! ( "event_id:\" {}\" " , event_id ) . as_str ( ) , 5 ) ;
195
+ let search_result = self . search ( format ! ( "event_id:\" {event_id }\" " ) . as_str ( ) , 1 ) ;
196
196
match search_result {
197
- // If there are > 1 entry (which there shouldn't be), still return true
198
- Ok ( results) => results. len ( ) != 0 ,
197
+ Ok ( results) => !results. is_empty ( ) ,
199
198
Err ( err) => {
200
- warn ! ( "Failed to check if event has been indexed, assuming it has: {err:? }" ) ;
199
+ warn ! ( "Failed to check if event has been indexed, assuming it has: {err}" ) ;
201
200
true
202
201
}
203
202
}
@@ -301,4 +300,76 @@ mod tests {
301
300
302
301
Ok ( ( ) )
303
302
}
303
+
304
+ #[ test]
305
+ fn test_index_contains_false ( ) -> Result < ( ) , Box < dyn Error > > {
306
+ let room_id = room_id ! ( "!room_id:localhost" ) ;
307
+ let mut index =
308
+ RoomIndex :: new_in_memory ( room_id) . expect ( "failed to make index in ram: {index:?}" ) ;
309
+
310
+ let event_id = event_id ! ( "$event_id:localhost" ) ;
311
+
312
+ index. commit_and_reload ( ) ?;
313
+
314
+ assert ! ( !index. contains( event_id) , "Index should not contain event" ) ;
315
+
316
+ Ok ( ( ) )
317
+ }
318
+
319
+ #[ test]
320
+ fn test_index_contains_true ( ) -> Result < ( ) , Box < dyn Error > > {
321
+ let room_id = room_id ! ( "!room_id:localhost" ) ;
322
+ let mut index =
323
+ RoomIndex :: new_in_memory ( room_id) . expect ( "failed to make index in ram: {index:?}" ) ;
324
+
325
+ let event_id = event_id ! ( "$event_id:localhost" ) ;
326
+ let event = EventFactory :: new ( )
327
+ . text_msg ( "This is a sentence" )
328
+ . event_id ( event_id)
329
+ . room ( room_id)
330
+ . sender ( user_id ! ( "@user_id:localhost" ) )
331
+ . into_any_sync_message_like_event ( ) ;
332
+
333
+ index. handle_event ( event) ?;
334
+
335
+ index. commit_and_reload ( ) ?;
336
+
337
+ assert ! ( index. contains( event_id) , "Index should contain event" ) ;
338
+
339
+ Ok ( ( ) )
340
+ }
341
+
342
+ #[ test]
343
+ fn test_indexing_idempotency ( ) -> Result < ( ) , Box < dyn Error > > {
344
+ let room_id = room_id ! ( "!room_id:localhost" ) ;
345
+ let mut index =
346
+ RoomIndex :: new_in_memory ( room_id) . expect ( "failed to make index in ram: {index:?}" ) ;
347
+
348
+ let event_id = event_id ! ( "$event_id:localhost" ) ;
349
+ let event = EventFactory :: new ( )
350
+ . text_msg ( "This is a sentence" )
351
+ . event_id ( event_id)
352
+ . room ( room_id)
353
+ . sender ( user_id ! ( "@user_id:localhost" ) )
354
+ . into_any_sync_message_like_event ( ) ;
355
+
356
+ index. handle_event ( event. clone ( ) ) ?;
357
+
358
+ index. commit_and_reload ( ) ?;
359
+
360
+ assert ! ( index. contains( event_id) , "Index should contain event" ) ;
361
+
362
+ // indexing again should do nothing
363
+ index. handle_event ( event) ?;
364
+
365
+ index. commit_and_reload ( ) ?;
366
+
367
+ assert ! ( index. contains( event_id) , "Index should still contain event" ) ;
368
+
369
+ let result = index. search ( "sentence" , 10 ) . expect ( "search failed with: {result:?}" ) ;
370
+
371
+ assert_eq ! ( result. len( ) , 1 , "Index should have ignored second indexing" ) ;
372
+
373
+ Ok ( ( ) )
374
+ }
304
375
}
0 commit comments