Skip to content

Commit c8992a2

Browse files
committed
test: Check that IncomingMsg isn't emitted for reactions
1 parent a6db7ba commit c8992a2

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

src/reaction.rs

Lines changed: 41 additions & 11 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 {
@@ -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 {
@@ -600,11 +612,21 @@ Here's my footer -- bob@example.net"
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+
error!(t, "Unwanted event: {ev:?}.");
628+
unreachable!()
629+
}
608630
}
609631

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

636658
bob_msg.chat_id.accept(&bob).await?;
637659

660+
bob.evtracker.clear_events();
638661
send_reaction(&bob, bob_msg.id, "👍").await.unwrap();
639662
expect_reactions_changed_event(&bob, bob_msg.chat_id, bob_msg.id, ContactId::SELF).await?;
640-
assert!(!has_incoming_reactions_event(&bob).await);
663+
expect_no_unwanted_events(&bob).await;
641664
assert_eq!(get_chat_msgs(&bob, bob_msg.chat_id).await?.len(), 2);
642665

643666
let bob_reaction_msg = bob.pop_sent_msg().await;
@@ -656,6 +679,7 @@ Here's my footer -- bob@example.net"
656679
expect_reactions_changed_event(&alice, chat_alice.id, alice_msg.sender_msg_id, *bob_id)
657680
.await?;
658681
expect_incoming_reactions_event(&alice, alice_msg.sender_msg_id, *bob_id, "👍").await?;
682+
expect_no_unwanted_events(&alice).await;
659683

660684
// Alice reacts to own message.
661685
send_reaction(&alice, alice_msg.sender_msg_id, "👍 😀")
@@ -684,6 +708,7 @@ Here's my footer -- bob@example.net"
684708
let bob = TestContext::new_bob().await;
685709
alice.set_config(Config::Displayname, Some("ALICE")).await?;
686710
bob.set_config(Config::Displayname, Some("BOB")).await?;
711+
let alice_bob_id = alice.add_or_lookup_contact_id(&bob).await;
687712

688713
// Alice sends message to Bob
689714
let alice_chat = alice.create_chat(&bob).await;
@@ -696,7 +721,9 @@ Here's my footer -- bob@example.net"
696721
send_reaction(&bob, bob_msg1.id, "👍").await?;
697722
let bob_send_reaction = bob.pop_sent_msg().await;
698723
alice.recv_msg_trash(&bob_send_reaction).await;
699-
assert!(has_incoming_reactions_event(&alice).await);
724+
expect_incoming_reactions_event(&alice, alice_msg1.sender_msg_id, alice_bob_id, "👍")
725+
.await?;
726+
expect_no_unwanted_events(&alice).await;
700727

701728
let chatlist = Chatlist::try_load(&bob, 0, None, None).await?;
702729
let summary = chatlist.get_summary(&bob, 0, None).await?;
@@ -711,8 +738,9 @@ Here's my footer -- bob@example.net"
711738
SystemTime::shift(Duration::from_secs(10));
712739
send_reaction(&alice, alice_msg1.sender_msg_id, "🍿").await?;
713740
let alice_send_reaction = alice.pop_sent_msg().await;
741+
bob.evtracker.clear_events();
714742
bob.recv_msg_opt(&alice_send_reaction).await;
715-
assert!(!has_incoming_reactions_event(&bob).await);
743+
expect_no_unwanted_events(&bob).await;
716744

717745
assert_summary(&alice, "You reacted 🍿 to \"Party?\"").await;
718746
assert_summary(&bob, "ALICE reacted 🍿 to \"Party?\"").await;
@@ -934,7 +962,9 @@ Here's my footer -- bob@example.net"
934962
expect_reactions_changed_event(&alice0, chat_id, alice0_msg_id, ContactId::SELF).await?;
935963
expect_reactions_changed_event(&alice1, alice1_msg.chat_id, alice1_msg.id, ContactId::SELF)
936964
.await?;
937-
965+
for a in [&alice0, &alice1] {
966+
expect_no_unwanted_events(a).await;
967+
}
938968
Ok(())
939969
}
940970
}

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)