use http::Response;
pub use matrix_sdk_test_macros::async_test;
use once_cell::sync::Lazy;
use ruma::{
api::{client::sync::sync_events::v3::Response as SyncResponse, IncomingResponse},
room_id, user_id, RoomId, UserId,
};
use serde_json::Value as JsonValue;
#[macro_export]
macro_rules! message_like_event_content {
($( $tt:tt )*) => {
::ruma::serde::Raw::new(&::serde_json::json!( $($tt)* ))
.unwrap()
.cast::<::ruma::events::AnyMessageLikeEventContent>()
}
}
#[macro_export]
macro_rules! timeline_event {
($( $tt:tt )*) => {
::ruma::serde::Raw::new(&::serde_json::json!( $($tt)* ))
.unwrap()
.cast::<::ruma::events::AnyTimelineEvent>()
}
}
#[macro_export]
macro_rules! sync_timeline_event {
($( $tt:tt )*) => {
::ruma::serde::Raw::new(&::serde_json::json!( $($tt)* ))
.unwrap()
.cast::<::ruma::events::AnySyncTimelineEvent>()
}
}
#[macro_export]
macro_rules! sync_state_event {
($( $tt:tt )*) => {
::ruma::serde::Raw::new(&::serde_json::json!( $($tt)* ))
.unwrap()
.cast::<::ruma::events::AnySyncStateEvent>()
}
}
#[macro_export]
macro_rules! stripped_state_event {
($( $tt:tt )*) => {
::ruma::serde::Raw::new(&::serde_json::json!( $($tt)* ))
.unwrap()
.cast::<::ruma::events::AnyStrippedStateEvent>()
}
}
#[macro_export]
macro_rules! init_tracing_for_tests {
() => {
#[cfg(not(target_arch = "wasm32"))]
#[$crate::__macro_support::ctor]
fn init_logging() {
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use $crate::__macro_support::tracing_subscriber;
tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
"info,matrix_sdk=debug".into()
}))
.with(tracing_subscriber::fmt::layer().with_test_writer())
.init();
}
};
}
#[doc(hidden)]
pub mod __macro_support {
#[cfg(not(target_arch = "wasm32"))]
pub use ctor::ctor;
#[cfg(not(target_arch = "wasm32"))]
pub use tracing_subscriber;
}
mod event_builder;
#[cfg(not(target_arch = "wasm32"))]
pub mod mocks;
pub mod event_factory;
pub mod notification_settings;
mod sync_builder;
pub mod test_json;
pub use self::{
event_builder::EventBuilder,
sync_builder::{
bulk_room_members, EphemeralTestEvent, GlobalAccountDataTestEvent, InvitedRoomBuilder,
JoinedRoomBuilder, KnockedRoomBuilder, LeftRoomBuilder, PresenceTestEvent,
RoomAccountDataTestEvent, StateTestEvent, StrippedStateTestEvent, SyncResponseBuilder,
},
};
pub static ALICE: Lazy<&UserId> = Lazy::new(|| user_id!("@alice:server.name"));
pub static BOB: Lazy<&UserId> = Lazy::new(|| user_id!("@bob:other.server"));
pub static CAROL: Lazy<&UserId> = Lazy::new(|| user_id!("@carol:other.server"));
pub static DEFAULT_TEST_ROOM_ID: Lazy<&RoomId> =
Lazy::new(|| room_id!("!SVkFJHzfwvuaIEawgC:localhost"));
pub enum SyncResponseFile {
All,
Default,
DefaultWithSummary,
Invite,
Leave,
Voip,
}
pub fn sync_response(kind: SyncResponseFile) -> SyncResponse {
let data: &JsonValue = match kind {
SyncResponseFile::All => &test_json::MORE_SYNC,
SyncResponseFile::Default => &test_json::SYNC,
SyncResponseFile::DefaultWithSummary => &test_json::DEFAULT_SYNC_SUMMARY,
SyncResponseFile::Invite => &test_json::INVITE_SYNC,
SyncResponseFile::Leave => &test_json::LEAVE_SYNC,
SyncResponseFile::Voip => &test_json::VOIP_SYNC,
};
ruma_response_from_json(data)
}
pub fn ruma_response_from_json<ResponseType: IncomingResponse>(
json: &serde_json::Value,
) -> ResponseType {
let json_bytes = serde_json::to_vec(json).expect("JSON-serialization of response value failed");
let http_response =
Response::builder().status(200).body(json_bytes).expect("Failed to build HTTP response");
ResponseType::try_from_http_response(http_response).expect("Can't parse the response json")
}