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

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