Skip to content

Commit

Permalink
xmtp_mls tests compile for wasm32
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Sep 5, 2024
1 parent 5da8805 commit 66008a4
Show file tree
Hide file tree
Showing 29 changed files with 118 additions and 59 deletions.
2 changes: 1 addition & 1 deletion xmtp_api_grpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub const DEV_ADDRESS: &str = "https://grpc.dev.xmtp.network:443";
pub use grpc_api_helper::Client;

#[cfg(test)]
mod tests {
pub mod tests {
use std::time::{SystemTime, UNIX_EPOCH};

use self::auth_token::Authenticator;
Expand Down
4 changes: 2 additions & 2 deletions xmtp_mls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,8 @@ gloo-timers = { workspace = true, features = ["futures"] }


[dev-dependencies]
ctor.workspace = true
ethers.workspace = true
mockall = "0.13.0"
mockito = "1.4.0"
tracing-log = "0.2.0"
tracing-test = "0.2.4"
xmtp_id = { path = "../xmtp_id", features = ["test-utils"] }
Expand All @@ -102,6 +100,8 @@ anyhow.workspace = true
xmtp_api_grpc = { path = "../xmtp_api_grpc" }
tracing-subscriber.workspace = true
tempfile = "3.5.0"
mockito = "1.4.0"
ctor.workspace = true

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
xmtp_api_http = { path = "../xmtp_api_http" }
Expand Down
2 changes: 1 addition & 1 deletion xmtp_mls/src/api/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ where
}

#[cfg(test)]
mod tests {
pub(crate) mod tests {
use super::super::test_utils::*;
use super::GetIdentityUpdatesV2Filter;
use crate::{api::ApiClientWrapper, retry::Retry};
Expand Down
40 changes: 40 additions & 0 deletions xmtp_mls/src/api/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub fn build_group_messages(num_messages: usize, group_id: Vec<u8>) -> Vec<Group
}

// Create a mock XmtpClient for testing the client wrapper
// need separate defs for wasm and not wasm, b/c `cfg_attr` not supportd in macro! block
#[cfg(not(target_arch = "wasm32"))]
mock! {
pub ApiClient {}

Expand Down Expand Up @@ -74,3 +76,41 @@ mock! {
async fn create_dev() -> Self { ApiClient }
}
}

#[cfg(target_arch = "wasm32")]
mock! {
pub ApiClient {}

impl ClientWithMetadata for ApiClient {
fn set_libxmtp_version(&mut self, version: String) -> Result<(), Error>;
fn set_app_version(&mut self, version: String) -> Result<(), Error>;
}

#[async_trait::async_trait(?Send)]
impl XmtpMlsClient for ApiClient {
async fn upload_key_package(&self, request: UploadKeyPackageRequest) -> Result<(), Error>;
async fn fetch_key_packages(
&self,
request: FetchKeyPackagesRequest,
) -> Result<FetchKeyPackagesResponse, Error>;
async fn send_group_messages(&self, request: SendGroupMessagesRequest) -> Result<(), Error>;
async fn send_welcome_messages(&self, request: SendWelcomeMessagesRequest) -> Result<(), Error>;
async fn query_group_messages(&self, request: QueryGroupMessagesRequest) -> Result<QueryGroupMessagesResponse, Error>;
async fn query_welcome_messages(&self, request: QueryWelcomeMessagesRequest) -> Result<QueryWelcomeMessagesResponse, Error>;
async fn subscribe_group_messages(&self, request: SubscribeGroupMessagesRequest) -> Result<GroupMessageStream, Error>;
async fn subscribe_welcome_messages(&self, request: SubscribeWelcomeMessagesRequest) -> Result<WelcomeMessageStream, Error>;
}

#[async_trait::async_trait(?Send)]
impl XmtpIdentityClient for ApiClient {
async fn publish_identity_update(&self, request: PublishIdentityUpdateRequest) -> Result<PublishIdentityUpdateResponse, Error>;
async fn get_identity_updates_v2(&self, request: GetIdentityUpdatesV2Request) -> Result<GetIdentityUpdatesV2Response, Error>;
async fn get_inbox_ids(&self, request: GetInboxIdsRequest) -> Result<GetInboxIdsResponse, Error>;
}

#[async_trait::async_trait(?Send)]
impl XmtpTestClient for ApiClient {
async fn create_local() -> Self { ApiClient }
async fn create_dev() -> Self { ApiClient }
}
}
2 changes: 1 addition & 1 deletion xmtp_mls/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ where
}

#[cfg(test)]
mod tests {
pub(crate) mod tests {
use crate::api::ApiClientWrapper;
use crate::builder::ClientBuilderError;
use crate::identity::IdentityError;
Expand Down
2 changes: 1 addition & 1 deletion xmtp_mls/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ pub fn deserialize_welcome(welcome_bytes: &Vec<u8>) -> Result<Welcome, ClientErr
}

#[cfg(test)]
mod tests {
pub(crate) mod tests {
use xmtp_cryptography::utils::generate_local_wallet;
use xmtp_id::InboxOwner;

Expand Down
2 changes: 1 addition & 1 deletion xmtp_mls/src/codecs/group_updated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl ContentCodec<GroupUpdated> for GroupUpdatedCodec {
}

#[cfg(test)]
mod tests {
pub(crate) mod tests {
use xmtp_proto::xmtp::mls::message_contents::{group_updated::Inbox, GroupUpdated};

use crate::utils::test::rand_string;
Expand Down
2 changes: 1 addition & 1 deletion xmtp_mls/src/codecs/membership_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl ContentCodec<GroupMembershipChanges> for GroupMembershipChangeCodec {
}

#[cfg(test)]
mod tests {
pub(crate) mod tests {
use xmtp_proto::xmtp::mls::message_contents::MembershipChange;

use crate::utils::test::{rand_string, rand_vec};
Expand Down
2 changes: 1 addition & 1 deletion xmtp_mls/src/codecs/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl ContentCodec<String> for TextCodec {
}

#[cfg(test)]
mod tests {
pub(crate) mod tests {
use crate::codecs::{text::TextCodec, ContentCodec};

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
Expand Down
2 changes: 1 addition & 1 deletion xmtp_mls/src/groups/group_membership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub struct MembershipDiff<'inbox_id> {
}

#[cfg(test)]
mod tests {
pub(crate) mod tests {
use super::GroupMembership;

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
Expand Down
2 changes: 1 addition & 1 deletion xmtp_mls/src/groups/group_permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ impl std::fmt::Display for PreconfiguredPolicies {
}

#[cfg(test)]
mod tests {
pub(crate) mod tests {
use crate::{
groups::{group_mutable_metadata::MetadataField, validated_commit::MutableMetadataChanges},
utils::test::{rand_string, rand_vec},
Expand Down
2 changes: 1 addition & 1 deletion xmtp_mls/src/groups/intents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ impl TryFrom<Vec<u8>> for PostCommitAction {
}

#[cfg(test)]
mod tests {
pub(crate) mod tests {
use super::*;

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
Expand Down
4 changes: 2 additions & 2 deletions xmtp_mls/src/groups/message_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,8 +779,8 @@ fn new_pin() -> String {
format!("{:04}", pin)
}

#[cfg(test)]
mod tests {
#[cfg(all(not(target_arch = "wasm32"), test))]
pub(crate) mod tests {

const HISTORY_SERVER_HOST: &str = "0.0.0.0";
const HISTORY_SERVER_PORT: u16 = 5558;
Expand Down
38 changes: 26 additions & 12 deletions xmtp_mls/src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ pub mod group_metadata;
pub mod group_mutable_metadata;
pub mod group_permissions;
pub mod intents;
pub mod members;
pub(super) mod members;
#[allow(dead_code)]
pub mod message_history;
mod subscriptions;
mod sync;
pub(super) mod subscriptions;
pub(super) mod sync;
pub mod validated_commit;

use intents::SendMessageIntentData;
Expand Down Expand Up @@ -1273,13 +1273,12 @@ fn build_group_join_config() -> MlsGroupJoinConfig {
}

#[cfg(test)]
mod tests {
pub(crate) mod tests {
use diesel::connection::SimpleConnection;
use futures::future::join_all;
use openmls::prelude::{tls_codec::Serialize, Member, MlsGroup as OpenMlsGroup};
use prost::Message;
use std::sync::Arc;
use tracing_test::traced_test;
use xmtp_cryptography::utils::generate_local_wallet;
use xmtp_proto::xmtp::mls::message_contents::EncodedContent;

Expand Down Expand Up @@ -1607,8 +1606,11 @@ mod tests {
not(target_arch = "wasm32"),
tokio::test(flavor = "multi_thread", worker_threads = 1)
)]
#[traced_test]
#[tracing_test::traced_test]
async fn test_create_from_welcome_validation() {
if cfg!(target_arch = "wasm32") {
let _ = tracing_log::LogTracer::init_with_filter(log::LevelFilter::Debug);
}
let alix = ClientBuilder::new_test_client(&generate_local_wallet()).await;
let bo = ClientBuilder::new_test_client(&generate_local_wallet()).await;

Expand Down Expand Up @@ -1950,7 +1952,10 @@ mod tests {
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test(flavor = "multi_thread", worker_threads = 10))]
#[cfg_attr(
not(target_arch = "wasm32"),
tokio::test(flavor = "multi_thread", worker_threads = 10)
)]
async fn test_self_resolve_epoch_mismatch() {
let amal = ClientBuilder::new_test_client(&generate_local_wallet()).await;
let bola = ClientBuilder::new_test_client(&generate_local_wallet()).await;
Expand Down Expand Up @@ -2070,7 +2075,6 @@ mod tests {
assert_eq!(amal_group_pinned_frame_url, "pinned frame");
}


#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test(flavor = "current_thread"))]
#[ignore]
Expand Down Expand Up @@ -3009,7 +3013,10 @@ mod tests {
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test(flavor = "multi_thread", worker_threads = 5))]
#[cfg_attr(
not(target_arch = "wasm32"),
tokio::test(flavor = "multi_thread", worker_threads = 5)
)]
async fn test_parallel_syncs() {
let wallet = generate_local_wallet();
let alix1 = Arc::new(ClientBuilder::new_test_client(&wallet).await);
Expand All @@ -3025,7 +3032,7 @@ mod tests {
let client_clone = alix1.clone();
// Each of these syncs is going to trigger the client to invite alix2 to the group
// because of the race
tokio::spawn(async move { group_clone.sync(&client_clone).await })
crate::spawn(async move { group_clone.sync(&client_clone).await })
})
.collect();

Expand Down Expand Up @@ -3119,7 +3126,11 @@ mod tests {
* We need to be safe even in situations where there are multiple
* intents that do the same thing, leading to conflicts
*/
#[tokio::test(flavor = "multi_thread", worker_threads = 5)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(
not(target_arch = "wasm32"),
tokio::test(flavor = "multi_thread", worker_threads = 5)
)]
async fn add_missing_installs_reentrancy() {
let wallet = generate_local_wallet();
let alix1 = ClientBuilder::new_test_client(&wallet).await;
Expand Down Expand Up @@ -3207,7 +3218,10 @@ mod tests {
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test(flavor = "multi_thread", worker_threads = 5))]
#[cfg_attr(
not(target_arch = "wasm32"),
tokio::test(flavor = "multi_thread", worker_threads = 5)
)]
async fn respect_allow_epoch_increment() {
let wallet = generate_local_wallet();
let client = ClientBuilder::new_test_client(&wallet).await;
Expand Down
8 changes: 4 additions & 4 deletions xmtp_mls/src/groups/subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl MlsGroup {
}

#[cfg(test)]
mod tests {
pub(crate) mod tests {
use super::*;
use std::time::Duration;
use tokio_stream::wrappers::UnboundedReceiverStream;
Expand Down Expand Up @@ -243,7 +243,7 @@ mod tests {
let notify_ptr = notify.clone();
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
let mut stream = UnboundedReceiverStream::new(rx);
tokio::spawn(async move {
crate::spawn(async move {
let stream = bola_group_ptr.stream(bola_ptr).await.unwrap();
futures::pin_mut!(stream);
while let Some(item) = stream.next().await {
Expand Down Expand Up @@ -292,7 +292,7 @@ mod tests {
let stream = tokio_stream::wrappers::UnboundedReceiverStream::new(rx);
let amal_ptr = amal.clone();
let group_ptr = group.clone();
tokio::spawn(async move {
crate::spawn(async move {
let stream = group_ptr.stream(amal_ptr).await.unwrap();
futures::pin_mut!(stream);
while let Some(item) = stream.next().await {
Expand Down Expand Up @@ -339,7 +339,7 @@ mod tests {
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
let (start_tx, start_rx) = tokio::sync::oneshot::channel();
let mut stream = UnboundedReceiverStream::new(rx);
tokio::spawn(async move {
crate::spawn(async move {
let stream = amal_group_ptr.stream(amal_ptr).await.unwrap();
let _ = start_tx.send(());
futures::pin_mut!(stream);
Expand Down
2 changes: 1 addition & 1 deletion xmtp_mls/src/groups/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,7 @@ fn decode_staged_commit(data: Vec<u8>) -> Result<StagedCommit, MessageProcessing
}

#[cfg(test)]
mod tests {
pub(crate) mod tests {
use super::*;
use crate::builder::ClientBuilder;
use futures::future;
Expand Down
6 changes: 4 additions & 2 deletions xmtp_mls/src/identity_updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,6 @@ pub async fn load_identity_updates<ApiClient: XmtpApi>(
#[cfg(test)]
pub(crate) mod tests {
use ethers::signers::LocalWallet;
use tracing_test::traced_test;
use xmtp_cryptography::utils::generate_local_wallet;
use xmtp_id::{
associations::{builder::SignatureRequest, AssociationState, RecoverableEcdsaSignature},
Expand Down Expand Up @@ -565,8 +564,11 @@ pub(crate) mod tests {

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
#[traced_test]
#[tracing_test::traced_test]
async fn cache_association_state() {
if cfg!(target_arch = "wasm32") {
let _ = tracing_log::LogTracer::init_with_filter(log::LevelFilter::Debug);
}
let wallet = generate_local_wallet();
let wallet_2 = generate_local_wallet();
let wallet_address = wallet.get_address();
Expand Down
15 changes: 8 additions & 7 deletions xmtp_mls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ impl<T> XmtpApi for T where
}

#[cfg(any(test, feature = "test-utils", feature = "bench"))]
#[async_trait::async_trait]
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
pub trait XmtpTestClient {
async fn create_local() -> Self;
async fn create_dev() -> Self;
Expand Down Expand Up @@ -125,12 +126,13 @@ mod wasm_test {
pub use crate::groups::group_membership::tests::*;
pub use crate::groups::group_permissions::tests::*;
pub use crate::groups::intents::tests::*;
pub use crate::groups::members::tests::*;
// message history test use `mockito` and `tempfile`
// which are incompatible with wasm since they use
// system sockets/files
// pub use crate::groups::message_history::tests::*;
pub use crate::groups::subscriptions::tests::*;
pub use crate::groups::sync::tests::*;
pub use crate::groups::tests::*;
pub use crate::groups::validated_commit::tests::*;
pub use crate::identity_updates::tests::*;
pub use crate::retry::tests::*;
pub use crate::storage::encrypted_store::association_state::tests::*;
Expand All @@ -146,14 +148,13 @@ mod wasm_test {
}

#[cfg(test)]
mod tests {
pub(crate) mod tests {
use log::LevelFilter;
use tracing_test::traced_test;

// Execute once before any tests are run
#[ctor::ctor]
#[cfg_attr(not(target_arch = "wasm32"), ctor::ctor)]
// Capture traces in a variable that can be checked in tests, as well as outputting them to stdout on test failure
#[traced_test]
#[cfg_attr(not(target_arch = "wasm32"), tracing_test::traced_test)]
fn setup() {
// Capture logs (e.g. log::info!()) as traces too
let _ = tracing_log::LogTracer::init_with_filter(LevelFilter::Debug);
Expand Down
2 changes: 1 addition & 1 deletion xmtp_mls/src/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl RetryableError for xmtp_proto::api_client::Error {
}

#[cfg(test)]
mod tests {
pub(crate) mod tests {
use super::*;
use thiserror::Error;
use tokio::sync::mpsc;
Expand Down
Loading

0 comments on commit 66008a4

Please sign in to comment.