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