matrix_sdk_test/
lib.rs

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/// Create a `Raw<AnyMessageLikeEventContent>` from arbitrary JSON.
16///
17/// Forwards all arguments to [`serde_json::json`].
18#[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/// Create a `Raw<AnyTimelineEvent>` from arbitrary JSON.
28///
29/// Forwards all arguments to [`serde_json::json`].
30#[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/// Create a `Raw<AnySyncTimelineEvent>` from arbitrary JSON.
40///
41/// Forwards all arguments to [`serde_json::json`].
42#[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/// Create a `Raw<AnySyncStateEvent>` from arbitrary JSON.
52///
53/// Forwards all arguments to [`serde_json::json`].
54#[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/// Create a `Raw<AnyStrippedStateEvent>` from arbitrary JSON.
64///
65/// Forwards all arguments to [`serde_json::json`].
66#[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
93/// The default room ID for tests.
94pub static DEFAULT_TEST_ROOM_ID: Lazy<&RoomId> =
95    Lazy::new(|| room_id!("!SVkFJHzfwvuaIEawgC:localhost"));
96
97/// Embedded sync response files
98pub enum SyncResponseFile {
99    All,
100    Default,
101    DefaultWithSummary,
102    Invite,
103    Leave,
104    Voip,
105}
106
107/// Get specific API responses for testing
108pub 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
121/// Build a typed Ruma [`IncomingResponse`] object from a json body.
122pub 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
131/// Serialise a typed Ruma [`OutgoingResponse`] object to JSON.
132pub 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)] // required to be able to return TestResult from #[test] fns
142pub enum TestError {}
143
144// If this was just `T: Debug`, it would conflict with
145// the `impl From<T> for T` in `std`.
146//
147// Adding a dummy `Display` bound works around this.
148impl<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>;