From d10573bb578ce907dda153cf4d17d636f6124f82 Mon Sep 17 00:00:00 2001 From: Nicholas Molnar <65710+neekolas@users.noreply.github.com> Date: Tue, 7 Nov 2023 09:08:58 -0800 Subject: [PATCH] Add tests for intent data --- xmtp_mls/src/groups/intents.rs | 48 ++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/xmtp_mls/src/groups/intents.rs b/xmtp_mls/src/groups/intents.rs index 37bcefa44..be7b704d0 100644 --- a/xmtp_mls/src/groups/intents.rs +++ b/xmtp_mls/src/groups/intents.rs @@ -26,6 +26,7 @@ pub enum IntentError { Generic(String), } +#[derive(Debug, Clone)] pub struct SendMessageIntentData { pub message: Vec, } @@ -65,6 +66,7 @@ impl From for Vec { } } +#[derive(Debug, Clone)] pub struct AddMembersIntentData { pub key_packages: Vec, } @@ -119,10 +121,12 @@ impl From for Vec { } } +#[derive(Debug, Clone)] pub enum PostCommitAction { SendWelcomes(SendWelcomesAction), } +#[derive(Debug, Clone)] pub struct SendWelcomesAction { pub installation_ids: Vec>, pub welcome_message: Vec, @@ -188,3 +192,47 @@ impl From> for PostCommitAction { PostCommitAction::from_bytes(data.as_slice()).unwrap() } } + +#[cfg(test)] +mod tests { + use xmtp_cryptography::utils::generate_local_wallet; + + use crate::{builder::ClientBuilder, InboxOwner}; + + use super::*; + + #[test] + fn test_serialize_send_message() { + let message = vec![1, 2, 3]; + let intent = SendMessageIntentData::new(message.clone()); + let as_bytes: Vec = intent.into(); + let restored_intent = SendMessageIntentData::from_bytes(as_bytes.as_slice()).unwrap(); + + assert_eq!(restored_intent.message, message); + } + + #[tokio::test] + async fn test_serialize_add_members() { + let wallet = generate_local_wallet(); + let wallet_address = wallet.get_address(); + let client = ClientBuilder::new_test_client(wallet.into()).await; + let key_package = client + .identity + .new_key_package(&client.mls_provider()) + .unwrap(); + let verified_key_package = VerifiedKeyPackage::new(key_package, wallet_address.clone()); + + let intent = AddMembersIntentData::new(vec![verified_key_package.clone()]); + let as_bytes: Vec = intent.clone().into(); + let restored_intent = + AddMembersIntentData::from_bytes(as_bytes.as_slice(), &client.mls_provider()).unwrap(); + + assert!(intent.key_packages[0] + .inner + .eq(&restored_intent.key_packages[0].inner)); + assert_eq!( + intent.key_packages[0].wallet_address, + restored_intent.key_packages[0].wallet_address + ); + } +}