1use std::fmt;
2
3use http::Response;
4pub use matrix_sdk_test_macros::async_test;
5use once_cell::sync::Lazy;
6use ruma::{
7 RoomId, UserId,
8 api::{
9 IncomingResponse, OutgoingResponse, client::sync::sync_events::v3::Response as SyncResponse,
10 },
11 room_id, user_id,
12};
13use serde_json::Value as JsonValue;
14
15#[macro_export]
19macro_rules! message_like_event_content {
20 ($( $tt:tt )*) => {
21 ::ruma::serde::Raw::new(&::serde_json::json!( $($tt)* ))
22 .unwrap()
23 .cast_unchecked::<::ruma::events::AnyMessageLikeEventContent>()
24 }
25}
26
27#[macro_export]
31macro_rules! timeline_event {
32 ($( $tt:tt )*) => {
33 ::ruma::serde::Raw::new(&::serde_json::json!( $($tt)* ))
34 .unwrap()
35 .cast_unchecked::<::ruma::events::AnyTimelineEvent>()
36 }
37}
38
39#[macro_export]
43macro_rules! sync_timeline_event {
44 ($( $tt:tt )*) => {
45 ::ruma::serde::Raw::new(&::serde_json::json!( $($tt)* ))
46 .unwrap()
47 .cast_unchecked::<::ruma::events::AnySyncTimelineEvent>()
48 }
49}
50
51#[macro_export]
55macro_rules! sync_state_event {
56 ($( $tt:tt )*) => {
57 ::ruma::serde::Raw::new(&::serde_json::json!( $($tt)* ))
58 .unwrap()
59 .cast_unchecked::<::ruma::events::AnySyncStateEvent>()
60 }
61}
62
63#[macro_export]
67macro_rules! stripped_state_event {
68 ($( $tt:tt )*) => {
69 ::ruma::serde::Raw::new(&::serde_json::json!( $($tt)* ))
70 .unwrap()
71 .cast_unchecked::<::ruma::events::AnyStrippedStateEvent>()
72 }
73}
74
75#[cfg(not(target_family = "wasm"))]
76pub mod mocks;
77
78pub mod event_factory;
79pub mod notification_settings;
80mod sync_builder;
81pub mod test_json;
82
83pub use self::sync_builder::{
84 InvitedRoomBuilder, JoinedRoomBuilder, KnockedRoomBuilder, LeftRoomBuilder, PresenceTestEvent,
85 RoomAccountDataTestEvent, StateTestEvent, StrippedStateTestEvent, SyncResponseBuilder,
86 bulk_room_members,
87};
88
89pub static ALICE: Lazy<&UserId> = Lazy::new(|| user_id!("@alice:server.name"));
90pub static BOB: Lazy<&UserId> = Lazy::new(|| user_id!("@bob:other.server"));
91pub static CAROL: Lazy<&UserId> = Lazy::new(|| user_id!("@carol:other.server"));
92
93pub static DEFAULT_TEST_ROOM_ID: Lazy<&RoomId> =
95 Lazy::new(|| room_id!("!SVkFJHzfwvuaIEawgC:localhost"));
96
97pub enum SyncResponseFile {
99 All,
100 Default,
101 DefaultWithSummary,
102 Invite,
103 Leave,
104 Voip,
105}
106
107pub fn sync_response(kind: SyncResponseFile) -> SyncResponse {
109 let data: &JsonValue = match kind {
110 SyncResponseFile::All => &test_json::MORE_SYNC,
111 SyncResponseFile::Default => &test_json::SYNC,
112 SyncResponseFile::DefaultWithSummary => &test_json::DEFAULT_SYNC_SUMMARY,
113 SyncResponseFile::Invite => &test_json::INVITE_SYNC,
114 SyncResponseFile::Leave => &test_json::LEAVE_SYNC,
115 SyncResponseFile::Voip => &test_json::VOIP_SYNC,
116 };
117
118 ruma_response_from_json(data)
119}
120
121pub fn ruma_response_from_json<ResponseType: IncomingResponse>(
123 json: &serde_json::Value,
124) -> ResponseType {
125 let json_bytes = serde_json::to_vec(json).expect("JSON-serialization of response value failed");
126 let http_response =
127 Response::builder().status(200).body(json_bytes).expect("Failed to build HTTP response");
128 ResponseType::try_from_http_response(http_response).expect("Can't parse the response json")
129}
130
131pub fn ruma_response_to_json<ResponseType: OutgoingResponse>(
133 response: ResponseType,
134) -> serde_json::Value {
135 let http_response: Response<Vec<u8>> =
136 response.try_into_http_response().expect("Failed to build HTTP response");
137 let json_bytes = http_response.into_body();
138 serde_json::from_slice(&json_bytes).expect("Can't parse the response JSON")
139}
140
141#[derive(Debug)] pub enum TestError {}
143
144impl<T: fmt::Display + fmt::Debug> From<T> for TestError {
149 #[track_caller]
150 fn from(value: T) -> Self {
151 panic!("error: {value:?}")
152 }
153}
154
155pub type TestResult = Result<(), TestError>;