tests: streamline the SyncResponseBuilder global account methods

Only keep two: the one that uses the output of the event factory, and
one using custom JSON data.
This commit is contained in:
Benjamin Bouvier
2025-08-21 13:07:51 +02:00
parent ef20342ddf
commit e388fe6522
6 changed files with 20 additions and 59 deletions
+10 -10
View File
@@ -3010,8 +3010,8 @@ pub(crate) mod tests {
RoomState,
};
use matrix_sdk_test::{
async_test, event_factory::EventFactory, GlobalAccountDataTestEvent, JoinedRoomBuilder,
StateTestEvent, SyncResponseBuilder, DEFAULT_TEST_ROOM_ID,
async_test, event_factory::EventFactory, JoinedRoomBuilder, StateTestEvent,
SyncResponseBuilder, DEFAULT_TEST_ROOM_ID,
};
#[cfg(target_family = "wasm")]
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
@@ -3763,13 +3763,13 @@ pub(crate) mod tests {
server
.mock_sync()
.ok_and_run(&client, |builder| {
builder.add_global_account_data_event(GlobalAccountDataTestEvent::Custom(json!({
builder.add_custom_global_account_data(json!({
"content": {
"media_previews": "private",
"invite_avatars": "off"
},
"type": "m.media_preview_config"
})));
}));
})
.await;
@@ -3785,13 +3785,13 @@ pub(crate) mod tests {
server
.mock_sync()
.ok_and_run(&client, |builder| {
builder.add_global_account_data_event(GlobalAccountDataTestEvent::Custom(json!({
builder.add_custom_global_account_data(json!({
"content": {
"media_previews": "off",
"invite_avatars": "on"
},
"type": "m.media_preview_config"
})));
}));
})
.await;
@@ -3814,13 +3814,13 @@ pub(crate) mod tests {
server
.mock_sync()
.ok_and_run(&client, |builder| {
builder.add_global_account_data_event(GlobalAccountDataTestEvent::Custom(json!({
builder.add_custom_global_account_data(json!({
"content": {
"media_previews": "private",
"invite_avatars": "off"
},
"type": "io.element.msc4278.media_preview_config"
})));
}));
})
.await;
@@ -3836,13 +3836,13 @@ pub(crate) mod tests {
server
.mock_sync()
.ok_and_run(&client, |builder| {
builder.add_global_account_data_event(GlobalAccountDataTestEvent::Custom(json!({
builder.add_custom_global_account_data(json!({
"content": {
"media_previews": "off",
"invite_avatars": "on"
},
"type": "io.element.msc4278.media_preview_config"
})));
}));
})
.await;
+1 -3
View File
@@ -1351,7 +1351,7 @@ mod tests {
let mut response_builder = SyncResponseBuilder::new();
let response = response_builder
.add_global_account_data_bulk([Raw::new(&json!({
.add_custom_global_account_data(json!({
"content": {
"algorithm": "m.secret_storage.v1.aes-hmac-sha2",
"iv": "gH2iNpiETFhApvW6/FFEJQ",
@@ -1364,8 +1364,6 @@ mod tests {
},
"type": "m.secret_storage.key.foobar",
}))
.unwrap()
.cast_unchecked()])
.build_sync_response();
client.process_sync(response).await?;
@@ -41,7 +41,7 @@ use ruma::{
int, mxc_uri, owned_event_id, room_id, thirdparty, user_id, OwnedUserId, RoomVersionId,
TransactionId,
};
use serde_json::{from_value, json};
use serde_json::json;
use stream_assert::assert_pending;
use tokio::time::sleep;
use wiremock::{
+3 -3
View File
@@ -81,9 +81,9 @@ mod sync_builder;
pub mod test_json;
pub use self::sync_builder::{
GlobalAccountDataTestEvent, InvitedRoomBuilder, JoinedRoomBuilder, KnockedRoomBuilder,
LeftRoomBuilder, PresenceTestEvent, RoomAccountDataTestEvent, StateTestEvent,
StrippedStateTestEvent, SyncResponseBuilder, bulk_room_members,
InvitedRoomBuilder, JoinedRoomBuilder, KnockedRoomBuilder, LeftRoomBuilder, PresenceTestEvent,
RoomAccountDataTestEvent, StateTestEvent, StrippedStateTestEvent, SyncResponseBuilder,
bulk_room_members,
};
pub static ALICE: Lazy<&UserId> = Lazy::new(|| user_id!("@alice:server.name"));
@@ -31,8 +31,7 @@ pub use joined_room::JoinedRoomBuilder;
pub use knocked_room::KnockedRoomBuilder;
pub use left_room::LeftRoomBuilder;
pub use test_event::{
GlobalAccountDataTestEvent, PresenceTestEvent, RoomAccountDataTestEvent, StateTestEvent,
StrippedStateTestEvent,
PresenceTestEvent, RoomAccountDataTestEvent, StateTestEvent, StrippedStateTestEvent,
};
/// The `SyncResponseBuilder` struct can be used to easily generate valid sync
@@ -145,25 +144,9 @@ impl SyncResponseBuilder {
self
}
/// Add global account data.
pub fn add_global_account_data_event(
&mut self,
event: GlobalAccountDataTestEvent,
) -> &mut Self {
let val = match event {
GlobalAccountDataTestEvent::Custom(json) => json,
};
self.account_data.push(from_json_value(val).unwrap());
self
}
/// Add global account data in bulk.
pub fn add_global_account_data_bulk<I>(&mut self, events: I) -> &mut Self
where
I: IntoIterator<Item = Raw<AnyGlobalAccountDataEvent>>,
{
self.account_data.extend(events);
/// Add custom global account data based on a JSON value.
pub fn add_custom_global_account_data(&mut self, event: serde_json::Value) -> &mut Self {
self.account_data.push(Raw::new(&event).unwrap().cast_unchecked());
self
}
@@ -1,7 +1,6 @@
use ruma::{
events::{
AnyGlobalAccountDataEvent, AnyRoomAccountDataEvent, AnyStrippedStateEvent,
AnySyncStateEvent, presence::PresenceEvent,
AnyRoomAccountDataEvent, AnyStrippedStateEvent, AnySyncStateEvent, presence::PresenceEvent,
},
serde::Raw,
};
@@ -146,22 +145,3 @@ impl From<PresenceTestEvent> for Raw<PresenceEvent> {
from_json_value(val.into()).unwrap()
}
}
/// Test events that can be added to the global account data.
pub enum GlobalAccountDataTestEvent {
Custom(JsonValue),
}
impl From<GlobalAccountDataTestEvent> for JsonValue {
fn from(val: GlobalAccountDataTestEvent) -> Self {
match val {
GlobalAccountDataTestEvent::Custom(json) => json,
}
}
}
impl From<GlobalAccountDataTestEvent> for Raw<AnyGlobalAccountDataEvent> {
fn from(val: GlobalAccountDataTestEvent) -> Self {
from_json_value(val.into()).unwrap()
}
}