@@ -285,17 +285,42 @@ mod tests {
285
285
}
286
286
287
287
async fn new_room ( room_id : & RoomId , inner : v4:: SlidingSyncRoom ) -> SlidingSyncRoom {
288
+ new_room_with_timeline ( room_id, inner, vec ! [ ] ) . await
289
+ }
290
+
291
+ async fn new_room_with_timeline (
292
+ room_id : & RoomId ,
293
+ inner : v4:: SlidingSyncRoom ,
294
+ timeline : Vec < SyncTimelineEvent > ,
295
+ ) -> SlidingSyncRoom {
288
296
let server = MockServer :: start ( ) . await ;
289
297
let client = logged_in_client ( Some ( server. uri ( ) ) ) . await ;
290
298
291
- SlidingSyncRoom :: new ( client, room_id. to_owned ( ) , inner, vec ! [ ] )
299
+ SlidingSyncRoom :: new ( client, room_id. to_owned ( ) , inner, timeline )
292
300
}
293
301
294
302
#[ tokio:: test]
295
- async fn test_state ( ) {
296
- let room = new_room ( room_id ! ( "!foo:bar.org" ) , room_response ! ( { } ) ) . await ;
303
+ async fn test_state_from_not_loaded ( ) {
304
+ let mut room = new_room ( room_id ! ( "!foo:bar.org" ) , room_response ! ( { } ) ) . await ;
297
305
298
306
assert_eq ! ( room. state, SlidingSyncRoomState :: NotLoaded ) ;
307
+
308
+ // Update with an empty response, but it doesn't matter.
309
+ room. update ( room_response ! ( { } ) , vec ! [ ] ) ;
310
+
311
+ assert_eq ! ( room. state, SlidingSyncRoomState :: Loaded ) ;
312
+ }
313
+
314
+ #[ tokio:: test]
315
+ async fn test_state_from_preloaded ( ) {
316
+ let mut room = new_room ( room_id ! ( "!foo:bar.org" ) , room_response ! ( { } ) ) . await ;
317
+
318
+ room. state = SlidingSyncRoomState :: Preloaded ;
319
+
320
+ // Update with an empty response, but it doesn't matter.
321
+ room. update ( room_response ! ( { } ) , vec ! [ ] ) ;
322
+
323
+ assert_eq ! ( room. state, SlidingSyncRoomState :: Loaded ) ;
299
324
}
300
325
301
326
#[ tokio:: test]
@@ -456,13 +481,269 @@ mod tests {
456
481
}
457
482
}
458
483
484
+ #[ tokio:: test]
485
+ async fn test_timeline_initially_empty ( ) {
486
+ let room = new_room ( room_id ! ( "!foo:bar.org" ) , room_response ! ( { } ) ) . await ;
487
+
488
+ assert ! ( room. timeline_queue. is_empty( ) ) ;
489
+ }
490
+
491
+ macro_rules! timeline_event {
492
+ ( from $sender: literal with id $event_id: literal at $ts: literal: $message: literal) => {
493
+ TimelineEvent :: new(
494
+ Raw :: new( & json!( {
495
+ "content" : RoomMessageEventContent :: text_plain( $message) ,
496
+ "type" : "m.room.message" ,
497
+ "event_id" : $event_id,
498
+ "room_id" : "!foo:bar.org" ,
499
+ "origin_server_ts" : $ts,
500
+ "sender" : $sender,
501
+ } ) )
502
+ . unwrap( )
503
+ . cast( )
504
+ ) . into( )
505
+ } ;
506
+ }
507
+
508
+ macro_rules! assert_timeline_queue_event_ids {
509
+ (
510
+ with $( $timeline_queue: ident ) .* {
511
+ $(
512
+ $nth: literal => $event_id: literal
513
+ ) ,*
514
+ $( , ) *
515
+ }
516
+ ) => {
517
+ let timeline = & $( $timeline_queue ) .* ;
518
+
519
+ $(
520
+ assert_eq!( timeline[ $nth ] . event. deserialize( ) . unwrap( ) . event_id( ) , $event_id) ;
521
+ ) *
522
+ } ;
523
+ }
524
+
525
+ #[ tokio:: test]
526
+ async fn test_timeline_queue_initially_not_empty ( ) {
527
+ let room = new_room_with_timeline (
528
+ room_id ! ( "!foo:bar.org" ) ,
529
+ room_response ! ( { } ) ,
530
+ vec ! [
531
+ timeline_event!( from "@alice:baz.org" with id "$x0:baz.org" at 0 : "message 0" ) ,
532
+ timeline_event!( from "@alice:baz.org" with id "$x1:baz.org" at 1 : "message 1" ) ,
533
+ ] ,
534
+ )
535
+ . await ;
536
+
537
+ assert_eq ! ( room. state, SlidingSyncRoomState :: NotLoaded ) ;
538
+ assert_eq ! ( room. timeline_queue. len( ) , 2 ) ;
539
+ assert_timeline_queue_event_ids ! (
540
+ with room. timeline_queue {
541
+ 0 => "$x0:baz.org" ,
542
+ 1 => "$x1:baz.org" ,
543
+ }
544
+ ) ;
545
+ }
546
+
547
+ #[ tokio:: test]
548
+ async fn test_timeline_queue_update_with_empty_timeline ( ) {
549
+ let mut room = new_room_with_timeline (
550
+ room_id ! ( "!foo:bar.org" ) ,
551
+ room_response ! ( { } ) ,
552
+ vec ! [
553
+ timeline_event!( from "@alice:baz.org" with id "$x0:baz.org" at 0 : "message 0" ) ,
554
+ timeline_event!( from "@alice:baz.org" with id "$x1:baz.org" at 1 : "message 1" ) ,
555
+ ] ,
556
+ )
557
+ . await ;
558
+
559
+ assert_eq ! ( room. state, SlidingSyncRoomState :: NotLoaded ) ;
560
+ assert_eq ! ( room. timeline_queue. len( ) , 2 ) ;
561
+ assert_timeline_queue_event_ids ! (
562
+ with room. timeline_queue {
563
+ 0 => "$x0:baz.org" ,
564
+ 1 => "$x1:baz.org" ,
565
+ }
566
+ ) ;
567
+
568
+ room. update ( room_response ! ( { } ) , vec ! [ ] ) ;
569
+
570
+ // The queue is unmodified.
571
+ assert_eq ! ( room. state, SlidingSyncRoomState :: Loaded ) ;
572
+ assert_eq ! ( room. timeline_queue. len( ) , 2 ) ;
573
+ assert_timeline_queue_event_ids ! (
574
+ with room. timeline_queue {
575
+ 0 => "$x0:baz.org" ,
576
+ 1 => "$x1:baz.org" ,
577
+ }
578
+ ) ;
579
+ }
580
+
581
+ #[ tokio:: test]
582
+ async fn test_timeline_queue_update_with_empty_timeline_and_with_limited ( ) {
583
+ let mut room = new_room_with_timeline (
584
+ room_id ! ( "!foo:bar.org" ) ,
585
+ room_response ! ( { } ) ,
586
+ vec ! [
587
+ timeline_event!( from "@alice:baz.org" with id "$x0:baz.org" at 0 : "message 0" ) ,
588
+ timeline_event!( from "@alice:baz.org" with id "$x1:baz.org" at 1 : "message 1" ) ,
589
+ ] ,
590
+ )
591
+ . await ;
592
+
593
+ assert_eq ! ( room. state, SlidingSyncRoomState :: NotLoaded ) ;
594
+ assert_eq ! ( room. timeline_queue. len( ) , 2 ) ;
595
+ assert_timeline_queue_event_ids ! (
596
+ with room. timeline_queue {
597
+ 0 => "$x0:baz.org" ,
598
+ 1 => "$x1:baz.org" ,
599
+ }
600
+ ) ;
601
+
602
+ room. update (
603
+ room_response ! ( {
604
+ "limited" : true
605
+ } ) ,
606
+ vec ! [ ] ,
607
+ ) ;
608
+
609
+ // The queue has been emptied.
610
+ assert_eq ! ( room. state, SlidingSyncRoomState :: Loaded ) ;
611
+ assert_eq ! ( room. timeline_queue. len( ) , 0 ) ;
612
+ }
613
+
614
+ #[ tokio:: test]
615
+ async fn test_timeline_queue_update_from_preloaded ( ) {
616
+ let mut room = new_room_with_timeline (
617
+ room_id ! ( "!foo:bar.org" ) ,
618
+ room_response ! ( { } ) ,
619
+ vec ! [
620
+ timeline_event!( from "@alice:baz.org" with id "$x0:baz.org" at 0 : "message 0" ) ,
621
+ timeline_event!( from "@alice:baz.org" with id "$x1:baz.org" at 1 : "message 1" ) ,
622
+ ] ,
623
+ )
624
+ . await ;
625
+
626
+ room. state = SlidingSyncRoomState :: Preloaded ;
627
+
628
+ assert_eq ! ( room. state, SlidingSyncRoomState :: Preloaded ) ;
629
+ assert_eq ! ( room. timeline_queue. len( ) , 2 ) ;
630
+ assert_timeline_queue_event_ids ! (
631
+ with room. timeline_queue {
632
+ 0 => "$x0:baz.org" ,
633
+ 1 => "$x1:baz.org" ,
634
+ }
635
+ ) ;
636
+
637
+ room. update (
638
+ room_response ! ( { } ) ,
639
+ vec ! [
640
+ timeline_event!( from "@alice:baz.org" with id "$x2:baz.org" at 2 : "message 2" ) ,
641
+ timeline_event!( from "@alice:baz.org" with id "$x3:baz.org" at 3 : "message 3" ) ,
642
+ ] ,
643
+ ) ;
644
+
645
+ // The queue is emptied, and new events are appended.
646
+ assert_eq ! ( room. state, SlidingSyncRoomState :: Loaded ) ;
647
+ assert_eq ! ( room. timeline_queue. len( ) , 2 ) ;
648
+ assert_timeline_queue_event_ids ! (
649
+ with room. timeline_queue {
650
+ 0 => "$x2:baz.org" ,
651
+ 1 => "$x3:baz.org" ,
652
+ }
653
+ ) ;
654
+ }
655
+
656
+ #[ tokio:: test]
657
+ async fn test_timeline_queue_update_from_not_loaded ( ) {
658
+ let mut room = new_room_with_timeline (
659
+ room_id ! ( "!foo:bar.org" ) ,
660
+ room_response ! ( { } ) ,
661
+ vec ! [
662
+ timeline_event!( from "@alice:baz.org" with id "$x0:baz.org" at 0 : "message 0" ) ,
663
+ timeline_event!( from "@alice:baz.org" with id "$x1:baz.org" at 1 : "message 1" ) ,
664
+ ] ,
665
+ )
666
+ . await ;
667
+
668
+ assert_eq ! ( room. state, SlidingSyncRoomState :: NotLoaded ) ;
669
+ assert_eq ! ( room. timeline_queue. len( ) , 2 ) ;
670
+ assert_timeline_queue_event_ids ! (
671
+ with room. timeline_queue {
672
+ 0 => "$x0:baz.org" ,
673
+ 1 => "$x1:baz.org" ,
674
+ }
675
+ ) ;
676
+
677
+ room. update (
678
+ room_response ! ( { } ) ,
679
+ vec ! [
680
+ timeline_event!( from "@alice:baz.org" with id "$x2:baz.org" at 2 : "message 2" ) ,
681
+ timeline_event!( from "@alice:baz.org" with id "$x3:baz.org" at 3 : "message 3" ) ,
682
+ ] ,
683
+ ) ;
684
+
685
+ // New events are appended to the queue.
686
+ assert_eq ! ( room. state, SlidingSyncRoomState :: Loaded ) ;
687
+ assert_eq ! ( room. timeline_queue. len( ) , 4 ) ;
688
+ assert_timeline_queue_event_ids ! (
689
+ with room. timeline_queue {
690
+ 0 => "$x0:baz.org" ,
691
+ 1 => "$x1:baz.org" ,
692
+ 2 => "$x2:baz.org" ,
693
+ 3 => "$x3:baz.org" ,
694
+ }
695
+ ) ;
696
+ }
697
+
698
+ #[ tokio:: test]
699
+ async fn test_timeline_queue_update_from_not_loaded_with_limited ( ) {
700
+ let mut room = new_room_with_timeline (
701
+ room_id ! ( "!foo:bar.org" ) ,
702
+ room_response ! ( { } ) ,
703
+ vec ! [
704
+ timeline_event!( from "@alice:baz.org" with id "$x0:baz.org" at 0 : "message 0" ) ,
705
+ timeline_event!( from "@alice:baz.org" with id "$x1:baz.org" at 1 : "message 1" ) ,
706
+ ] ,
707
+ )
708
+ . await ;
709
+
710
+ assert_eq ! ( room. state, SlidingSyncRoomState :: NotLoaded ) ;
711
+ assert_eq ! ( room. timeline_queue. len( ) , 2 ) ;
712
+ assert_timeline_queue_event_ids ! (
713
+ with room. timeline_queue {
714
+ 0 => "$x0:baz.org" ,
715
+ 1 => "$x1:baz.org" ,
716
+ }
717
+ ) ;
718
+
719
+ room. update (
720
+ room_response ! ( {
721
+ "limited" : true ,
722
+ } ) ,
723
+ vec ! [
724
+ timeline_event!( from "@alice:baz.org" with id "$x2:baz.org" at 2 : "message 2" ) ,
725
+ timeline_event!( from "@alice:baz.org" with id "$x3:baz.org" at 3 : "message 3" ) ,
726
+ ] ,
727
+ ) ;
728
+
729
+ // The queue is emptied, and new events are appended.
730
+ assert_eq ! ( room. state, SlidingSyncRoomState :: Loaded ) ;
731
+ assert_eq ! ( room. timeline_queue. len( ) , 2 ) ;
732
+ assert_timeline_queue_event_ids ! (
733
+ with room. timeline_queue {
734
+ 0 => "$x2:baz.org" ,
735
+ 1 => "$x3:baz.org" ,
736
+ }
737
+ ) ;
738
+ }
739
+
459
740
#[ test]
460
741
fn test_frozen_sliding_sync_room_serialization ( ) {
461
742
let frozen_sliding_sync_room = FrozenSlidingSyncRoom {
462
743
room_id : room_id ! ( "!29fhd83h92h0:example.com" ) . to_owned ( ) ,
463
744
inner : v4:: SlidingSyncRoom :: default ( ) ,
464
745
timeline_queue : vector ! [ TimelineEvent :: new(
465
- Raw :: new( & json! ( {
746
+ Raw :: new( & json!( {
466
747
"content" : RoomMessageEventContent :: text_plain( "let it gooo!" ) ,
467
748
"type" : "m.room.message" ,
468
749
"event_id" : "$xxxxx:example.org" ,
0 commit comments