test: Add unique offsets to ids generated by TestContext to increase test correctness (#7297)

and fix the mistakes in tests that get discovered by this.

closes #6799
This commit is contained in:
Simon Laux
2025-10-19 19:08:23 +02:00
committed by GitHub
parent 65b61efb31
commit 59fac54f7b
14 changed files with 137 additions and 60 deletions

View File

@@ -483,7 +483,7 @@ async fn test_msg_with_implicit_member_removed() -> Result<()> {
// If Bob sends a message to Alice now, Fiona is removed.
assert_eq!(get_chat_contacts(&alice, alice_chat_id).await?.len(), 3);
let sent_msg = bob
.send_text(alice_chat_id, "I have removed Fiona some time ago.")
.send_text(bob_chat_id, "I have removed Fiona some time ago.")
.await;
alice.recv_msg(&sent_msg).await;
assert_eq!(get_chat_contacts(&alice, alice_chat_id).await?.len(), 2);
@@ -2412,14 +2412,14 @@ async fn test_forward_from_saved_to_saved() -> Result<()> {
let bob = TestContext::new_bob().await;
let sent = alice.send_text(alice.create_chat(&bob).await.id, "k").await;
bob.recv_msg(&sent).await;
let received_message = bob.recv_msg(&sent).await;
let orig = bob.get_last_msg().await;
let self_chat = bob.get_self_chat().await;
save_msgs(&bob, &[orig.id]).await?;
let saved1 = bob.get_last_msg().await;
assert_eq!(
saved1.get_original_msg_id(&bob).await?.unwrap(),
sent.sender_msg_id
received_message.id
);
assert_ne!(saved1.from_id, ContactId::SELF);
@@ -2644,7 +2644,7 @@ async fn test_broadcast() -> Result<()> {
add_contact_to_chat(
&alice,
broadcast_id,
get_chat_contacts(&alice, chat_bob.id).await?.pop().unwrap(),
get_chat_contacts(&alice, msg.chat_id).await?.pop().unwrap(),
)
.await?;
let fiona_contact_id = alice.add_or_lookup_contact_id(&fiona).await;
@@ -4013,26 +4013,27 @@ async fn test_info_contact_id() -> Result<()> {
)
.await?;
let fiona_id = alice.add_or_lookup_contact_id(&tcm.fiona().await).await; // contexts are in sync, fiona_id is same everywhere
add_contact_to_chat(alice, alice_chat_id, fiona_id).await?;
let alice_fiona_id = alice.add_or_lookup_contact_id(&tcm.fiona().await).await;
let bob_fiona_id = bob.add_or_lookup_contact_id(&tcm.fiona().await).await;
add_contact_to_chat(alice, alice_chat_id, alice_fiona_id).await?;
pop_recv_and_check(
alice,
alice2,
bob,
SystemMessage::MemberAddedToGroup,
fiona_id,
fiona_id,
alice_fiona_id,
bob_fiona_id,
)
.await?;
remove_contact_from_chat(alice, alice_chat_id, fiona_id).await?;
remove_contact_from_chat(alice, alice_chat_id, alice_fiona_id).await?;
pop_recv_and_check(
alice,
alice2,
bob,
SystemMessage::MemberRemovedFromGroup,
fiona_id,
fiona_id,
alice_fiona_id,
bob_fiona_id,
)
.await?;
@@ -4040,11 +4041,12 @@ async fn test_info_contact_id() -> Result<()> {
// We raw delete in db as Contact::delete() leaves a tombstone (which is great as the tap works longer then)
alice
.sql
.execute("DELETE FROM contacts WHERE id=?", (fiona_id,))
.execute("DELETE FROM contacts WHERE id=?", (alice_fiona_id,))
.await?;
let msg = alice.get_last_msg().await;
assert_eq!(msg.get_info_type(), SystemMessage::MemberRemovedFromGroup);
assert!(msg.get_info_contact_id(alice).await?.is_none());
assert!(msg.get_info_contact_id(bob).await?.is_none());
Ok(())
}

View File

@@ -616,7 +616,7 @@ mod tests {
loop {
let event = bob.evtracker.recv().await.unwrap();
if let EventType::WebxdcRealtimeAdvertisementReceived { msg_id } = event.typ {
assert!(msg_id == alice_webxdc.id);
assert!(msg_id == bob_webxdc.id);
break;
}
}

View File

@@ -2901,9 +2901,9 @@ async fn test_accept_outgoing() -> Result<()> {
let bob1_chat = bob1.create_chat(&alice1).await;
let sent = bob1.send_text(bob1_chat.id, "Hello!").await;
alice1.recv_msg(&sent).await;
let alice1_msg = alice1.recv_msg(&sent).await;
alice2.recv_msg(&sent).await;
let alice1_msg = bob2.recv_msg(&sent).await;
bob2.recv_msg(&sent).await;
assert_eq!(alice1_msg.text, "Hello!");
let alice1_chat = chat::Chat::load_from_db(&alice1, alice1_msg.chat_id).await?;
assert!(alice1_chat.is_contact_request());

View File

@@ -122,7 +122,7 @@ async fn test_key_contacts_migration_email2() -> Result<()> {
.await?
.is_empty()
);
let pgp_bob = Contact::get_by_id(&t, ContactId::new(11)).await?;
let pgp_bob = Contact::get_by_id(&t, ContactId::new(11001)).await?;
assert_eq!(pgp_bob.is_key_contact(), true);
assert_eq!(pgp_bob.origin, Origin::Hidden);

View File

@@ -84,6 +84,7 @@ impl TestContextManager {
pub async fn alice(&mut self) -> TestContext {
TestContext::builder()
.configure_alice()
.with_id_offset(1000)
.with_log_sink(self.log_sink.clone())
.build(Some(&mut self.used_names))
.await
@@ -92,6 +93,7 @@ impl TestContextManager {
pub async fn bob(&mut self) -> TestContext {
TestContext::builder()
.configure_bob()
.with_id_offset(2000)
.with_log_sink(self.log_sink.clone())
.build(Some(&mut self.used_names))
.await
@@ -100,6 +102,7 @@ impl TestContextManager {
pub async fn charlie(&mut self) -> TestContext {
TestContext::builder()
.configure_charlie()
.with_id_offset(3000)
.with_log_sink(self.log_sink.clone())
.build(Some(&mut self.used_names))
.await
@@ -108,6 +111,7 @@ impl TestContextManager {
pub async fn dom(&mut self) -> TestContext {
TestContext::builder()
.configure_dom()
.with_id_offset(4000)
.with_log_sink(self.log_sink.clone())
.build(Some(&mut self.used_names))
.await
@@ -116,6 +120,7 @@ impl TestContextManager {
pub async fn elena(&mut self) -> TestContext {
TestContext::builder()
.configure_elena()
.with_id_offset(5000)
.with_log_sink(self.log_sink.clone())
.build(Some(&mut self.used_names))
.await
@@ -124,6 +129,7 @@ impl TestContextManager {
pub async fn fiona(&mut self) -> TestContext {
TestContext::builder()
.configure_fiona()
.with_id_offset(6000)
.with_log_sink(self.log_sink.clone())
.build(Some(&mut self.used_names))
.await
@@ -263,6 +269,11 @@ pub struct TestContextBuilder {
/// so the caller should store the LogSink elsewhere to
/// prevent it from being dropped immediately.
log_sink: Option<LogSink>,
/// Offset for chat-,message-,contact ids.
///
/// This makes tests fail where ids from different accounts were mixed up.
id_offset: Option<u32>,
}
impl TestContextBuilder {
@@ -328,6 +339,14 @@ impl TestContextBuilder {
self
}
/// Adds an offset for chat-, message-, contact IDs.
///
/// This makes it harder to accidentally mix up IDs from different accounts.
pub fn with_id_offset(mut self, offset: u32) -> Self {
self.id_offset = Some(offset);
self
}
/// Builds the [`TestContext`].
pub async fn build(self, used_names: Option<&mut BTreeSet<String>>) -> TestContext {
if let Some(key_pair) = self.key_pair {
@@ -360,6 +379,22 @@ impl TestContextBuilder {
key::store_self_keypair(&test_context, &key_pair)
.await
.expect("Failed to save key");
if let Some(offset) = self.id_offset {
test_context
.ctx
.sql
.execute(
"UPDATE sqlite_sequence SET seq = ?
WHERE name = 'contacts'
OR name = 'chats'
OR name = 'msgs'",
(offset,),
)
.await
.expect("Failed set id offset");
}
test_context
} else {
TestContext::new_internal(None, self.log_sink).await
@@ -409,21 +444,33 @@ impl TestContext {
///
/// This is a shortcut which configures alice@example.org with a fixed key.
pub async fn new_alice() -> Self {
Self::builder().configure_alice().build(None).await
Self::builder()
.configure_alice()
.with_id_offset(11000)
.build(None)
.await
}
/// Creates a new configured [`TestContext`].
///
/// This is a shortcut which configures bob@example.net with a fixed key.
pub async fn new_bob() -> Self {
Self::builder().configure_bob().build(None).await
Self::builder()
.configure_bob()
.with_id_offset(12000)
.build(None)
.await
}
/// Creates a new configured [`TestContext`].
///
/// This is a shortcut which configures fiona@example.net with a fixed key.
pub async fn new_fiona() -> Self {
Self::builder().configure_fiona().build(None).await
Self::builder()
.configure_fiona()
.with_id_offset(13000)
.build(None)
.await
}
/// Print current chat state.
@@ -1624,4 +1671,32 @@ mod tests {
let runtime = tokio::runtime::Runtime::new().expect("unable to create tokio runtime");
runtime.block_on(TestContext::new());
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_id_offset() {
let mut tcm = TestContextManager::new();
let alice = tcm.alice().await;
let bob = tcm.bob().await;
let fiona = tcm.fiona().await;
// chat ids
let alice_bob_chat = alice.create_chat(&bob).await;
let bob_alice_chat = bob.create_chat(&alice).await;
assert_ne!(alice_bob_chat.id, bob_alice_chat.id);
// contact ids
let alice_fiona_contact_id = alice.add_or_lookup_contact_id(&fiona).await;
let fiona_fiona_contact_id = bob.add_or_lookup_contact_id(&fiona).await;
assert_ne!(alice_fiona_contact_id, fiona_fiona_contact_id);
// message ids
let alice_group_id = alice
.create_group_with_members("test group", &[&bob, &fiona])
.await;
let alice_sent_msg = alice.send_text(alice_group_id, "testing").await;
let bob_received_id = bob.recv_msg(&alice_sent_msg).await;
assert_ne!(alice_sent_msg.sender_msg_id, bob_received_id.id);
let fiona_received_id = fiona.recv_msg(&alice_sent_msg).await;
assert_ne!(bob_received_id.id, fiona_received_id.id);
}
}