@@ -84,6 +84,7 @@ impl TestContextManager {
8484 pub async fn alice ( & mut self ) -> TestContext {
8585 TestContext :: builder ( )
8686 . configure_alice ( )
87+ . with_id_offset ( 1000 )
8788 . with_log_sink ( self . log_sink . clone ( ) )
8889 . build ( Some ( & mut self . used_names ) )
8990 . await
@@ -92,6 +93,7 @@ impl TestContextManager {
9293 pub async fn bob ( & mut self ) -> TestContext {
9394 TestContext :: builder ( )
9495 . configure_bob ( )
96+ . with_id_offset ( 2000 )
9597 . with_log_sink ( self . log_sink . clone ( ) )
9698 . build ( Some ( & mut self . used_names ) )
9799 . await
@@ -100,6 +102,7 @@ impl TestContextManager {
100102 pub async fn charlie ( & mut self ) -> TestContext {
101103 TestContext :: builder ( )
102104 . configure_charlie ( )
105+ . with_id_offset ( 3000 )
103106 . with_log_sink ( self . log_sink . clone ( ) )
104107 . build ( Some ( & mut self . used_names ) )
105108 . await
@@ -108,6 +111,7 @@ impl TestContextManager {
108111 pub async fn dom ( & mut self ) -> TestContext {
109112 TestContext :: builder ( )
110113 . configure_dom ( )
114+ . with_id_offset ( 4000 )
111115 . with_log_sink ( self . log_sink . clone ( ) )
112116 . build ( Some ( & mut self . used_names ) )
113117 . await
@@ -116,6 +120,7 @@ impl TestContextManager {
116120 pub async fn elena ( & mut self ) -> TestContext {
117121 TestContext :: builder ( )
118122 . configure_elena ( )
123+ . with_id_offset ( 5000 )
119124 . with_log_sink ( self . log_sink . clone ( ) )
120125 . build ( Some ( & mut self . used_names ) )
121126 . await
@@ -124,6 +129,7 @@ impl TestContextManager {
124129 pub async fn fiona ( & mut self ) -> TestContext {
125130 TestContext :: builder ( )
126131 . configure_fiona ( )
132+ . with_id_offset ( 6000 )
127133 . with_log_sink ( self . log_sink . clone ( ) )
128134 . build ( Some ( & mut self . used_names ) )
129135 . await
@@ -263,6 +269,11 @@ pub struct TestContextBuilder {
263269 /// so the caller should store the LogSink elsewhere to
264270 /// prevent it from being dropped immediately.
265271 log_sink : Option < LogSink > ,
272+
273+ /// Offset for chat-,message-,contact ids.
274+ ///
275+ /// This makes tests fail where ids from different accounts were mixed up.
276+ id_offset : Option < u32 > ,
266277}
267278
268279impl TestContextBuilder {
@@ -328,6 +339,14 @@ impl TestContextBuilder {
328339 self
329340 }
330341
342+ /// Adds an offset for chat-, message-, contact IDs.
343+ ///
344+ /// This makes it harder to accidentally mix up IDs from different accounts.
345+ pub fn with_id_offset ( mut self , offset : u32 ) -> Self {
346+ self . id_offset = Some ( offset) ;
347+ self
348+ }
349+
331350 /// Builds the [`TestContext`].
332351 pub async fn build ( self , used_names : Option < & mut BTreeSet < String > > ) -> TestContext {
333352 if let Some ( key_pair) = self . key_pair {
@@ -360,6 +379,22 @@ impl TestContextBuilder {
360379 key:: store_self_keypair ( & test_context, & key_pair)
361380 . await
362381 . expect ( "Failed to save key" ) ;
382+
383+ if let Some ( offset) = self . id_offset {
384+ test_context
385+ . ctx
386+ . sql
387+ . execute (
388+ "UPDATE sqlite_sequence SET seq = ?
389+ WHERE name = 'contacts'
390+ OR name = 'chats'
391+ OR name = 'msgs'" ,
392+ ( offset, ) ,
393+ )
394+ . await
395+ . expect ( "Failed set id offset" ) ;
396+ }
397+
363398 test_context
364399 } else {
365400 TestContext :: new_internal ( None , self . log_sink ) . await
@@ -409,21 +444,33 @@ impl TestContext {
409444 ///
410445 /// This is a shortcut which configures alice@example.org with a fixed key.
411446 pub async fn new_alice ( ) -> Self {
412- Self :: builder ( ) . configure_alice ( ) . build ( None ) . await
447+ Self :: builder ( )
448+ . configure_alice ( )
449+ . with_id_offset ( 11000 )
450+ . build ( None )
451+ . await
413452 }
414453
415454 /// Creates a new configured [`TestContext`].
416455 ///
417456 /// This is a shortcut which configures bob@example.net with a fixed key.
418457 pub async fn new_bob ( ) -> Self {
419- Self :: builder ( ) . configure_bob ( ) . build ( None ) . await
458+ Self :: builder ( )
459+ . configure_bob ( )
460+ . with_id_offset ( 12000 )
461+ . build ( None )
462+ . await
420463 }
421464
422465 /// Creates a new configured [`TestContext`].
423466 ///
424467 /// This is a shortcut which configures fiona@example.net with a fixed key.
425468 pub async fn new_fiona ( ) -> Self {
426- Self :: builder ( ) . configure_fiona ( ) . build ( None ) . await
469+ Self :: builder ( )
470+ . configure_fiona ( )
471+ . with_id_offset ( 13000 )
472+ . build ( None )
473+ . await
427474 }
428475
429476 /// Print current chat state.
@@ -1624,4 +1671,32 @@ mod tests {
16241671 let runtime = tokio:: runtime:: Runtime :: new ( ) . expect ( "unable to create tokio runtime" ) ;
16251672 runtime. block_on ( TestContext :: new ( ) ) ;
16261673 }
1674+
1675+ #[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
1676+ async fn test_id_offset ( ) {
1677+ let mut tcm = TestContextManager :: new ( ) ;
1678+ let alice = tcm. alice ( ) . await ;
1679+ let bob = tcm. bob ( ) . await ;
1680+ let fiona = tcm. fiona ( ) . await ;
1681+
1682+ // chat ids
1683+ let alice_bob_chat = alice. create_chat ( & bob) . await ;
1684+ let bob_alice_chat = bob. create_chat ( & alice) . await ;
1685+ assert_ne ! ( alice_bob_chat. id, bob_alice_chat. id) ;
1686+
1687+ // contact ids
1688+ let alice_fiona_contact_id = alice. add_or_lookup_contact_id ( & fiona) . await ;
1689+ let fiona_fiona_contact_id = bob. add_or_lookup_contact_id ( & fiona) . await ;
1690+ assert_ne ! ( alice_fiona_contact_id, fiona_fiona_contact_id) ;
1691+
1692+ // message ids
1693+ let alice_group_id = alice
1694+ . create_group_with_members ( "test group" , & [ & bob, & fiona] )
1695+ . await ;
1696+ let alice_sent_msg = alice. send_text ( alice_group_id, "testing" ) . await ;
1697+ let bob_received_id = bob. recv_msg ( & alice_sent_msg) . await ;
1698+ assert_ne ! ( alice_sent_msg. sender_msg_id, bob_received_id. id) ;
1699+ let fiona_received_id = fiona. recv_msg ( & alice_sent_msg) . await ;
1700+ assert_ne ! ( bob_received_id. id, fiona_received_id. id) ;
1701+ }
16271702}
0 commit comments