diff --git a/src/ephemeral.rs b/src/ephemeral.rs index 1bfac1f30..8cf58c9da 100644 --- a/src/ephemeral.rs +++ b/src/ephemeral.rs @@ -460,6 +460,8 @@ pub(crate) async fn start_ephemeral_timers(context: &Context) -> sql::Result<()> #[cfg(test)] mod tests { use super::*; + use crate::chat; + use crate::contact::{Contact, Origin}; use crate::test_utils::*; #[async_std::test] @@ -525,4 +527,81 @@ mod tests { "Message deletion timer is set to 4 weeks." ); } + + #[async_std::test] + async fn test_ephemeral_timer() -> crate::error::Result<()> { + let alice = TestContext::new_alice().await; + let bob = TestContext::new_bob().await; + + let (contact_alice_id, _modified) = Contact::add_or_lookup( + &bob.ctx, + "Alice", + "alice@example.com", + Origin::ManuallyCreated, + ) + .await?; + let (contact_bob_id, _modified) = Contact::add_or_lookup( + &alice.ctx, + "Bob", + "bob@example.net", + Origin::ManuallyCreated, + ) + .await?; + + let chat_alice = chat::create_by_contact_id(&alice.ctx, contact_bob_id).await?; + let chat_bob = chat::create_by_contact_id(&bob.ctx, contact_alice_id).await?; + + // Alice sends message to Bob + let mut msg = Message::new(Viewtype::Text); + chat::prepare_msg(&alice.ctx, chat_alice, &mut msg).await?; + chat::send_msg(&alice.ctx, chat_alice, &mut msg).await?; + let sent = alice.pop_sent_msg().await; + bob.recv_msg(&sent).await; + + // Alice sends second message to Bob, with no timer + let mut msg = Message::new(Viewtype::Text); + chat::prepare_msg(&alice.ctx, chat_alice, &mut msg).await?; + chat::send_msg(&alice.ctx, chat_alice, &mut msg).await?; + let sent = alice.pop_sent_msg().await; + + assert_eq!( + chat_bob.get_ephemeral_timer(&bob.ctx).await?, + Timer::Disabled + ); + + // Bob sets ephemeral timer and sends a message about timer change + chat_bob + .set_ephemeral_timer(&bob.ctx, Timer::Enabled { duration: 60 }) + .await?; + let sent_timer_change = bob.pop_sent_msg().await; + + assert_eq!( + chat_bob.get_ephemeral_timer(&bob.ctx).await?, + Timer::Enabled { duration: 60 } + ); + + // Bob receives message from Alice. + // Alice message has no timer. However, Bob should not disable timer, + // because Alice replies to old message. + bob.recv_msg(&sent).await; + + assert_eq!( + chat_alice.get_ephemeral_timer(&alice.ctx).await?, + Timer::Disabled + ); + assert_eq!( + chat_bob.get_ephemeral_timer(&bob.ctx).await?, + Timer::Enabled { duration: 60 } + ); + + // Alice receives message from Bob + alice.recv_msg(&sent_timer_change).await; + + assert_eq!( + chat_alice.get_ephemeral_timer(&alice.ctx).await?, + Timer::Enabled { duration: 60 } + ); + + Ok(()) + } }