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);
}
}

View File

@@ -1,8 +1,8 @@
Group#Chat#10: Group chat [3 member(s)]
Group#Chat#2001: Group chat [3 member(s)]
--------------------------------------------------------------------------------
Msg#10: info (Contact#Contact#Info): Messages are end-to-end encrypted. [NOTICED][INFO]
Msg#11🔒: (Contact#Contact#10): Hi! I created a group. [FRESH]
Msg#12🔒: Me (Contact#Contact#Self): You left the group. [INFO] √
Msg#13🔒: (Contact#Contact#10): Member charlie@example.net added by alice@example.org. [FRESH][INFO]
Msg#14🔒: (Contact#Contact#10): What a silence! [FRESH]
Msg#2001: info (Contact#Contact#Info): Messages are end-to-end encrypted. [NOTICED][INFO]
Msg#2002🔒: (Contact#Contact#2001): Hi! I created a group. [FRESH]
Msg#2003🔒: Me (Contact#Contact#Self): You left the group. [INFO] √
Msg#2004🔒: (Contact#Contact#2001): Member charlie@example.net added by alice@example.org. [FRESH][INFO]
Msg#2005🔒: (Contact#Contact#2001): What a silence! [FRESH]
--------------------------------------------------------------------------------

View File

@@ -1,10 +1,10 @@
Group#Chat#10: Group [5 member(s)]
Group#Chat#1001: Group [5 member(s)]
--------------------------------------------------------------------------------
Msg#10: info (Contact#Contact#Info): Messages are end-to-end encrypted. [NOTICED][INFO]
Msg#11🔒: Me (Contact#Contact#Self): populate √
Msg#12: info (Contact#Contact#Info): Member dom@example.net added. [NOTICED][INFO]
Msg#13: info (Contact#Contact#Info): Member fiona@example.net removed. [NOTICED][INFO]
Msg#14🔒: (Contact#Contact#10): Member elena@example.net added by bob@example.net. [FRESH][INFO]
Msg#15🔒: Me (Contact#Contact#Self): You added member fiona@example.net. [INFO] o
Msg#16🔒: (Contact#Contact#10): Member fiona@example.net removed by bob@example.net. [FRESH][INFO]
Msg#1001: info (Contact#Contact#Info): Messages are end-to-end encrypted. [NOTICED][INFO]
Msg#1002🔒: Me (Contact#Contact#Self): populate √
Msg#1003: info (Contact#Contact#Info): Member dom@example.net added. [NOTICED][INFO]
Msg#1004: info (Contact#Contact#Info): Member fiona@example.net removed. [NOTICED][INFO]
Msg#1005🔒: (Contact#Contact#1001): Member elena@example.net added by bob@example.net. [FRESH][INFO]
Msg#1006🔒: Me (Contact#Contact#Self): You added member fiona@example.net. [INFO] o
Msg#1007🔒: (Contact#Contact#1001): Member fiona@example.net removed by bob@example.net. [FRESH][INFO]
--------------------------------------------------------------------------------

View File

@@ -1,5 +1,5 @@
Single#Chat#10: bob@example.net [bob@example.net] Icon: 4138c52e5bc1c576cda7dd44d088c07.png
Single#Chat#1001: bob@example.net [bob@example.net] Icon: 4138c52e5bc1c576cda7dd44d088c07.png
--------------------------------------------------------------------------------
Msg#10: Me (Contact#Contact#Self): We share this account √
Msg#11: Me (Contact#Contact#Self): I'm Alice too √
Msg#1001: Me (Contact#Contact#Self): We share this account √
Msg#1002: Me (Contact#Contact#Self): I'm Alice too √
--------------------------------------------------------------------------------

View File

@@ -1,5 +1,5 @@
Single#Chat#10: Bob [bob@example.net] Icon: 4138c52e5bc1c576cda7dd44d088c07.png
Single#Chat#11001: Bob [bob@example.net] Icon: 4138c52e5bc1c576cda7dd44d088c07.png
--------------------------------------------------------------------------------
Msg#10: Me (Contact#Contact#Self): Happy birthday, Bob! √
Msg#11: (Contact#Contact#10): Happy birthday to me, Alice! [FRESH]
Msg#11001: Me (Contact#Contact#Self): Happy birthday, Bob! √
Msg#11002: (Contact#Contact#11001): Happy birthday to me, Alice! [FRESH]
--------------------------------------------------------------------------------

View File

@@ -1,5 +1,5 @@
Single#Chat#10: bob@example.net [KEY bob@example.net]
Single#Chat#1001: bob@example.net [KEY bob@example.net]
--------------------------------------------------------------------------------
Msg#10: info (Contact#Contact#Info): Messages are end-to-end encrypted. [NOTICED][INFO]
Msg#11🔒: Me (Contact#Contact#Self): Test This is encrypted, signed, and has an Autocrypt Header without prefer-encrypt=mutual. √
Msg#1001: info (Contact#Contact#Info): Messages are end-to-end encrypted. [NOTICED][INFO]
Msg#1002🔒: Me (Contact#Contact#Self): Test This is encrypted, signed, and has an Autocrypt Header without prefer-encrypt=mutual. √
--------------------------------------------------------------------------------

View File

@@ -1,4 +1,4 @@
Single#Chat#11: bob@example.net [bob@example.net] Icon: 4138c52e5bc1c576cda7dd44d088c07.png
Single#Chat#1002: bob@example.net [bob@example.net] Icon: 4138c52e5bc1c576cda7dd44d088c07.png
--------------------------------------------------------------------------------
Msg#12: Me (Contact#Contact#Self): One classical MUA message √
Msg#1003: Me (Contact#Contact#Self): One classical MUA message √
--------------------------------------------------------------------------------

View File

@@ -1,6 +1,6 @@
Single#Chat#10: bob@example.net [KEY bob@example.net]
Single#Chat#1001: bob@example.net [KEY bob@example.net]
--------------------------------------------------------------------------------
Msg#10: info (Contact#Contact#Info): Messages are end-to-end encrypted. [NOTICED][INFO]
Msg#11🔒: (Contact#Contact#10): Heyho from DC [FRESH]
Msg#13🔒: Me (Contact#Contact#Self): Sending with DC again √
Msg#1001: info (Contact#Contact#Info): Messages are end-to-end encrypted. [NOTICED][INFO]
Msg#1002🔒: (Contact#Contact#1001): Heyho from DC [FRESH]
Msg#1004🔒: Me (Contact#Contact#Self): Sending with DC again √
--------------------------------------------------------------------------------

View File

@@ -1,9 +1,9 @@
Group#Chat#11: Group [3 member(s)]
Group#Chat#6002: Group [3 member(s)]
--------------------------------------------------------------------------------
Msg#13: info (Contact#Contact#Info): alice@example.org invited you to join this group.
Msg#6004: info (Contact#Contact#Info): alice@example.org invited you to join this group.
Waiting for the device of alice@example.org to reply… [NOTICED][INFO]
Msg#15: info (Contact#Contact#Info): alice@example.org replied, waiting for being added to the group… [NOTICED][INFO]
Msg#12: info (Contact#Contact#Info): Messages are end-to-end encrypted. [NOTICED][INFO]
Msg#17🔒: (Contact#Contact#10): Member Me added by alice@example.org. [FRESH][INFO]
Msg#6006: info (Contact#Contact#Info): alice@example.org replied, waiting for being added to the group… [NOTICED][INFO]
Msg#6003: info (Contact#Contact#Info): Messages are end-to-end encrypted. [NOTICED][INFO]
Msg#6008🔒: (Contact#Contact#6001): Member Me added by alice@example.org. [FRESH][INFO]
--------------------------------------------------------------------------------

View File

@@ -1,11 +1,11 @@
Group#Chat#11: Group [3 member(s)]
Group#Chat#3002: Group [3 member(s)]
--------------------------------------------------------------------------------
Msg#13: info (Contact#Contact#Info): alice@example.org invited you to join this group.
Msg#3004: info (Contact#Contact#Info): alice@example.org invited you to join this group.
Waiting for the device of alice@example.org to reply… [NOTICED][INFO]
Msg#15: info (Contact#Contact#Info): alice@example.org replied, waiting for being added to the group… [NOTICED][INFO]
Msg#12: info (Contact#Contact#Info): Messages are end-to-end encrypted. [NOTICED][INFO]
Msg#17🔒: (Contact#Contact#11): [FRESH]
Msg#18: info (Contact#Contact#Info): Member bob@example.net added. [NOTICED][INFO]
Msg#19🔒: (Contact#Contact#10): Member Me added by alice@example.org. [FRESH][INFO]
Msg#3006: info (Contact#Contact#Info): alice@example.org replied, waiting for being added to the group… [NOTICED][INFO]
Msg#3003: info (Contact#Contact#Info): Messages are end-to-end encrypted. [NOTICED][INFO]
Msg#3008🔒: (Contact#Contact#3002): [FRESH]
Msg#3009: info (Contact#Contact#Info): Member bob@example.net added. [NOTICED][INFO]
Msg#3010🔒: (Contact#Contact#3001): Member Me added by alice@example.org. [FRESH][INFO]
--------------------------------------------------------------------------------