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, OutgoingResponse},
8 room_id,
9 serde::{Base64, base64::UrlSafe},
10 user_id,
11};
12use sha2::{Digest, Sha256};
13
14#[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#[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#[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#[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#[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
91pub static DEFAULT_TEST_ROOM_ID: LazyLock<&RoomId> =
93 LazyLock::new(|| room_id!("!SVkFJHzfwvuaIEawgC:localhost"));
94
95pub 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 =
101 Response::builder().status(200).body(json_bytes).expect("Failed to build HTTP response");
102 ResponseType::try_from_http_response(http_response).expect("Can't parse the response json")
103}
104
105pub fn ruma_response_to_json<ResponseType: OutgoingResponse>(
107 response: ResponseType,
108) -> serde_json::Value {
109 let http_response: Response<Vec<u8>> =
110 response.try_into_http_response().expect("Failed to build HTTP response");
111 let json_bytes = http_response.into_body();
112 serde_json::from_slice(&json_bytes).expect("Can't parse the response JSON")
113}
114
115#[derive(Debug)] pub enum TestError {}
117
118impl<T: fmt::Display + fmt::Debug> From<T> for TestError {
123 #[track_caller]
124 fn from(value: T) -> Self {
125 panic!("error: {value:?}")
126 }
127}
128
129pub type TestResult = Result<(), TestError>;
130
131pub fn base64_sha256_hash(bytes: &[u8]) -> String {
137 Base64::<UrlSafe, _>::new(Sha256::digest(bytes)).encode()
138}