Skip to main content

matrix_sdk_test/
lib.rs

1use std::{fmt, sync::LazyLock};
2
3use http::Response;
4pub use matrix_sdk_test_macros::async_test;
5use ruma::{
6    RoomId, UserId,
7    api::{IncomingResponse, IncomingResponseExt as _, OutgoingResponse, OutgoingResponseExt as _},
8    room_id,
9    serde::{Base64, base64::UrlSafe},
10    user_id,
11};
12use sha2::{Digest, Sha256};
13
14/// Create a `Raw<AnyMessageLikeEventContent>` from arbitrary JSON.
15///
16/// Forwards all arguments to [`serde_json::json`].
17#[macro_export]
18macro_rules! message_like_event_content {
19    ($( $tt:tt )*) => {
20        ::ruma::serde::Raw::new(&::serde_json::json!( $($tt)* ))
21            .unwrap()
22            .cast_unchecked::<::ruma::events::AnyMessageLikeEventContent>()
23    }
24}
25
26/// Create a `Raw<AnyTimelineEvent>` from arbitrary JSON.
27///
28/// Forwards all arguments to [`serde_json::json`].
29#[macro_export]
30macro_rules! timeline_event {
31    ($( $tt:tt )*) => {
32        ::ruma::serde::Raw::new(&::serde_json::json!( $($tt)* ))
33            .unwrap()
34            .cast_unchecked::<::ruma::events::AnyTimelineEvent>()
35    }
36}
37
38/// Create a `Raw<AnySyncTimelineEvent>` from arbitrary JSON.
39///
40/// Forwards all arguments to [`serde_json::json`].
41#[macro_export]
42macro_rules! sync_timeline_event {
43    ($( $tt:tt )*) => {
44        ::ruma::serde::Raw::new(&::serde_json::json!( $($tt)* ))
45            .unwrap()
46            .cast_unchecked::<::ruma::events::AnySyncTimelineEvent>()
47    }
48}
49
50/// Create a `Raw<AnySyncStateEvent>` from arbitrary JSON.
51///
52/// Forwards all arguments to [`serde_json::json`].
53#[macro_export]
54macro_rules! sync_state_event {
55    ($( $tt:tt )*) => {
56        ::ruma::serde::Raw::new(&::serde_json::json!( $($tt)* ))
57            .unwrap()
58            .cast_unchecked::<::ruma::events::AnySyncStateEvent>()
59    }
60}
61
62/// Create a `Raw<AnyStrippedStateEvent>` from arbitrary JSON.
63///
64/// Forwards all arguments to [`serde_json::json`].
65#[macro_export]
66macro_rules! stripped_state_event {
67    ($( $tt:tt )*) => {
68        ::ruma::serde::Raw::new(&::serde_json::json!( $($tt)* ))
69            .unwrap()
70            .cast_unchecked::<::ruma::events::AnyStrippedStateEvent>()
71    }
72}
73
74#[cfg(not(target_family = "wasm"))]
75pub mod mocks;
76
77pub mod event_factory;
78pub mod notification_settings;
79mod sync_builder;
80pub mod test_json;
81
82pub use self::sync_builder::{
83    InvitedRoomBuilder, JoinedRoomBuilder, KnockedRoomBuilder, LeftRoomBuilder,
84    SyncResponseBuilder, bulk_room_members,
85};
86
87pub static ALICE: LazyLock<&UserId> = LazyLock::new(|| user_id!("@alice:server.name"));
88pub static BOB: LazyLock<&UserId> = LazyLock::new(|| user_id!("@bob:other.server"));
89pub static CAROL: LazyLock<&UserId> = LazyLock::new(|| user_id!("@carol:other.server"));
90
91/// The default room ID for tests.
92pub static DEFAULT_TEST_ROOM_ID: LazyLock<&RoomId> =
93    LazyLock::new(|| room_id!("!SVkFJHzfwvuaIEawgC:localhost"));
94
95/// Build a typed Ruma [`IncomingResponse`] object from a json body.
96pub fn ruma_response_from_json<ResponseType: IncomingResponse>(
97    json: &serde_json::Value,
98) -> ResponseType {
99    let json_bytes = serde_json::to_vec(json).expect("JSON-serialization of response value failed");
100    let http_response = Response::builder()
101        .status(200)
102        .body(json_bytes.as_slice())
103        .expect("Failed to build HTTP response");
104    ResponseType::try_from_http_response(http_response).expect("Can't parse the response json")
105}
106
107/// Serialise a typed Ruma [`OutgoingResponse`] object to JSON.
108pub fn ruma_response_to_json<ResponseType: OutgoingResponse>(
109    response: ResponseType,
110) -> serde_json::Value {
111    let http_response: Response<Vec<u8>> =
112        response.try_into_http_response().expect("Failed to build HTTP response");
113    let json_bytes = http_response.into_body();
114    serde_json::from_slice(&json_bytes).expect("Can't parse the response JSON")
115}
116
117#[derive(Debug)] // required to be able to return TestResult from #[test] fns
118pub enum TestError {}
119
120// If this was just `T: Debug`, it would conflict with
121// the `impl From<T> for T` in `std`.
122//
123// Adding a dummy `Display` bound works around this.
124impl<T: fmt::Display + fmt::Debug> From<T> for TestError {
125    #[track_caller]
126    fn from(value: T) -> Self {
127        panic!("error: {value:?}")
128    }
129}
130
131pub type TestResult = Result<(), TestError>;
132
133/// Compute the sha256 hash of the given bytes and convert it to unpadded
134/// URL-safe base64.
135///
136/// This matches the format of event IDs since room version 4, and of room IDs
137/// since room version 12.
138pub fn base64_sha256_hash(bytes: &[u8]) -> String {
139    Base64::<UrlSafe, _>::new(Sha256::digest(bytes)).encode()
140}