Skip to content

Commit 717c18e

Browse files
committed
test: Check that IncomingMsg isn't emitted for reactions
1 parent 4026c82 commit 717c18e

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

src/reaction.rs

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,12 @@ Here's my footer -- bob@example.net"
558558
) -> Result<()> {
559559
let event = t
560560
.evtracker
561-
.get_matching(|evt| matches!(evt, EventType::ReactionsChanged { .. }))
561+
.get_matching(|evt| {
562+
matches!(
563+
evt,
564+
EventType::ReactionsChanged { .. } | EventType::IncomingMsg { .. }
565+
)
566+
})
562567
.await;
563568
match event {
564569
EventType::ReactionsChanged {
@@ -570,7 +575,7 @@ Here's my footer -- bob@example.net"
570575
assert_eq!(msg_id, expected_msg_id);
571576
assert_eq!(contact_id, expected_contact_id);
572577
}
573-
_ => unreachable!(),
578+
_ => panic!("Unexpected event {event:?}."),
574579
}
575580
Ok(())
576581
}
@@ -583,7 +588,14 @@ Here's my footer -- bob@example.net"
583588
) -> Result<()> {
584589
let event = t
585590
.evtracker
586-
.get_matching(|evt| matches!(evt, EventType::IncomingReaction { .. }))
591+
// Check for absence of `IncomingMsg` events -- it appeared that it's quite easy to make
592+
// bugs when `IncomingMsg` is issued for reactions.
593+
.get_matching(|evt| {
594+
matches!(
595+
evt,
596+
EventType::IncomingReaction { .. } | EventType::IncomingMsg { .. }
597+
)
598+
})
587599
.await;
588600
match event {
589601
EventType::IncomingReaction {
@@ -595,16 +607,25 @@ Here's my footer -- bob@example.net"
595607
assert_eq!(contact_id, expected_contact_id);
596608
assert_eq!(reaction, Reaction::from(expected_reaction));
597609
}
598-
_ => unreachable!(),
610+
_ => panic!("Unexpected event {event:?}."),
599611
}
600612
Ok(())
601613
}
602614

603-
async fn has_incoming_reactions_event(t: &TestContext) -> bool {
604-
t.evtracker
605-
.get_matching_opt(t, |evt| matches!(evt, EventType::IncomingReaction { .. }))
606-
.await
607-
.is_some()
615+
/// Checks that no unwanted events remain after expecting "wanted" reaction events.
616+
async fn expect_no_unwanted_events(t: &TestContext) {
617+
let ev = t
618+
.evtracker
619+
.get_matching_opt(t, |evt| {
620+
matches!(
621+
evt,
622+
EventType::IncomingReaction { .. } | EventType::IncomingMsg { .. }
623+
)
624+
})
625+
.await;
626+
if let Some(ev) = ev {
627+
panic!("Unwanted event {ev:?}.")
628+
}
608629
}
609630

610631
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
@@ -635,9 +656,10 @@ Here's my footer -- bob@example.net"
635656

636657
bob_msg.chat_id.accept(&bob).await?;
637658

659+
bob.evtracker.clear_events();
638660
send_reaction(&bob, bob_msg.id, "👍").await.unwrap();
639661
expect_reactions_changed_event(&bob, bob_msg.chat_id, bob_msg.id, ContactId::SELF).await?;
640-
assert!(!has_incoming_reactions_event(&bob).await);
662+
expect_no_unwanted_events(&bob).await;
641663
assert_eq!(get_chat_msgs(&bob, bob_msg.chat_id).await?.len(), 2);
642664

643665
let bob_reaction_msg = bob.pop_sent_msg().await;
@@ -656,6 +678,7 @@ Here's my footer -- bob@example.net"
656678
expect_reactions_changed_event(&alice, chat_alice.id, alice_msg.sender_msg_id, *bob_id)
657679
.await?;
658680
expect_incoming_reactions_event(&alice, alice_msg.sender_msg_id, *bob_id, "👍").await?;
681+
expect_no_unwanted_events(&alice).await;
659682

660683
// Alice reacts to own message.
661684
send_reaction(&alice, alice_msg.sender_msg_id, "👍 😀")
@@ -684,6 +707,7 @@ Here's my footer -- bob@example.net"
684707
let bob = TestContext::new_bob().await;
685708
alice.set_config(Config::Displayname, Some("ALICE")).await?;
686709
bob.set_config(Config::Displayname, Some("BOB")).await?;
710+
let alice_bob_id = alice.add_or_lookup_contact_id(&bob).await;
687711

688712
// Alice sends message to Bob
689713
let alice_chat = alice.create_chat(&bob).await;
@@ -696,7 +720,9 @@ Here's my footer -- bob@example.net"
696720
send_reaction(&bob, bob_msg1.id, "👍").await?;
697721
let bob_send_reaction = bob.pop_sent_msg().await;
698722
alice.recv_msg_trash(&bob_send_reaction).await;
699-
assert!(has_incoming_reactions_event(&alice).await);
723+
expect_incoming_reactions_event(&alice, alice_msg1.sender_msg_id, alice_bob_id, "👍")
724+
.await?;
725+
expect_no_unwanted_events(&alice).await;
700726

701727
let chatlist = Chatlist::try_load(&bob, 0, None, None).await?;
702728
let summary = chatlist.get_summary(&bob, 0, None).await?;
@@ -711,8 +737,9 @@ Here's my footer -- bob@example.net"
711737
SystemTime::shift(Duration::from_secs(10));
712738
send_reaction(&alice, alice_msg1.sender_msg_id, "🍿").await?;
713739
let alice_send_reaction = alice.pop_sent_msg().await;
740+
bob.evtracker.clear_events();
714741
bob.recv_msg_opt(&alice_send_reaction).await;
715-
assert!(!has_incoming_reactions_event(&bob).await);
742+
expect_no_unwanted_events(&bob).await;
716743

717744
assert_summary(&alice, "You reacted 🍿 to \"Party?\"").await;
718745
assert_summary(&bob, "ALICE reacted 🍿 to \"Party?\"").await;
@@ -934,7 +961,9 @@ Here's my footer -- bob@example.net"
934961
expect_reactions_changed_event(&alice0, chat_id, alice0_msg_id, ContactId::SELF).await?;
935962
expect_reactions_changed_event(&alice1, alice1_msg.chat_id, alice1_msg.id, ContactId::SELF)
936963
.await?;
937-
964+
for a in [&alice0, &alice1] {
965+
expect_no_unwanted_events(a).await;
966+
}
938967
Ok(())
939968
}
940969
}

src/test_utils.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,8 @@ impl TestContext {
655655
.expect("failed to load msg")
656656
}
657657

658-
/// Returns the [`Contact`] for the other [`TestContext`], creating it if necessary.
659-
pub async fn add_or_lookup_contact(&self, other: &TestContext) -> Contact {
658+
/// Returns the [`ContactId`] for the other [`TestContext`], creating a contact if necessary.
659+
pub async fn add_or_lookup_contact_id(&self, other: &TestContext) -> ContactId {
660660
let primary_self_addr = other.ctx.get_primary_self_addr().await.unwrap();
661661
let addr = ContactAddress::new(&primary_self_addr).unwrap();
662662
// MailinglistAddress is the lowest allowed origin, we'd prefer to not modify the
@@ -670,6 +670,12 @@ impl TestContext {
670670
Modifier::Modified => warn!(&self.ctx, "Contact {} modified by TestContext", &addr),
671671
Modifier::Created => warn!(&self.ctx, "Contact {} created by TestContext", &addr),
672672
}
673+
contact_id
674+
}
675+
676+
/// Returns the [`Contact`] for the other [`TestContext`], creating it if necessary.
677+
pub async fn add_or_lookup_contact(&self, other: &TestContext) -> Contact {
678+
let contact_id = self.add_or_lookup_contact_id(other).await;
673679
Contact::get_by_id(&self.ctx, contact_id).await.unwrap()
674680
}
675681

0 commit comments

Comments
 (0)