From 6f683d3cdeeb011d121e66737fde9ac3c869ae88 Mon Sep 17 00:00:00 2001 From: Robin Date: Wed, 16 Apr 2025 17:29:01 +0200 Subject: [PATCH] refactor(tests): Accept more types for sync builder state events Refactoring the test event implementation to use the From trait rather than ad-hoc methods along the way. --- .../tests/integration/room/joined.rs | 2 +- .../src/sync_builder/invited_room.rs | 2 +- .../src/sync_builder/joined_room.rs | 8 +- .../src/sync_builder/knocked_room.rs | 2 +- .../src/sync_builder/left_room.rs | 4 +- .../src/sync_builder/test_event.rs | 142 +++++++++--------- 6 files changed, 84 insertions(+), 76 deletions(-) diff --git a/crates/matrix-sdk/tests/integration/room/joined.rs b/crates/matrix-sdk/tests/integration/room/joined.rs index 7cd783726..d0235f886 100644 --- a/crates/matrix-sdk/tests/integration/room/joined.rs +++ b/crates/matrix-sdk/tests/integration/room/joined.rs @@ -878,7 +878,7 @@ async fn test_call_notifications_dont_notify_room_without_mention_powerlevel() { let (client, server) = logged_in_client_with_server().await; let mut sync_builder = SyncResponseBuilder::new(); - let mut power_level_event = StateTestEvent::PowerLevels.into_json_value(); + let mut power_level_event: Value = StateTestEvent::PowerLevels.into(); // Allow noone to send room notify events. *power_level_event.get_mut("content").unwrap().get_mut("notifications").unwrap() = json!({"room": 101}); diff --git a/testing/matrix-sdk-test/src/sync_builder/invited_room.rs b/testing/matrix-sdk-test/src/sync_builder/invited_room.rs index 7af107446..16a821eba 100644 --- a/testing/matrix-sdk-test/src/sync_builder/invited_room.rs +++ b/testing/matrix-sdk-test/src/sync_builder/invited_room.rs @@ -27,7 +27,7 @@ impl InvitedRoomBuilder { /// Add an event to the state. pub fn add_state_event(mut self, event: StrippedStateTestEvent) -> Self { - self.inner.invite_state.events.push(event.into_raw_event()); + self.inner.invite_state.events.push(event.into()); self } diff --git a/testing/matrix-sdk-test/src/sync_builder/joined_room.rs b/testing/matrix-sdk-test/src/sync_builder/joined_room.rs index 74b223edb..ca98fcff3 100644 --- a/testing/matrix-sdk-test/src/sync_builder/joined_room.rs +++ b/testing/matrix-sdk-test/src/sync_builder/joined_room.rs @@ -9,7 +9,7 @@ use ruma::{ }; use serde_json::{from_value as from_json_value, Value as JsonValue}; -use super::{RoomAccountDataTestEvent, StateTestEvent}; +use super::RoomAccountDataTestEvent; use crate::{event_factory::EventBuilder, DEFAULT_TEST_ROOM_ID}; pub struct JoinedRoomBuilder { @@ -74,8 +74,8 @@ impl JoinedRoomBuilder { } /// Add an event to the state. - pub fn add_state_event(mut self, event: StateTestEvent) -> Self { - self.inner.state.events.push(event.into_raw_event()); + pub fn add_state_event(mut self, event: impl Into>) -> Self { + self.inner.state.events.push(event.into()); self } @@ -102,7 +102,7 @@ impl JoinedRoomBuilder { /// Add room account data. pub fn add_account_data(mut self, event: RoomAccountDataTestEvent) -> Self { - self.inner.account_data.events.push(event.into_raw_event()); + self.inner.account_data.events.push(event.into()); self } diff --git a/testing/matrix-sdk-test/src/sync_builder/knocked_room.rs b/testing/matrix-sdk-test/src/sync_builder/knocked_room.rs index 5838298ef..2d488eb13 100644 --- a/testing/matrix-sdk-test/src/sync_builder/knocked_room.rs +++ b/testing/matrix-sdk-test/src/sync_builder/knocked_room.rs @@ -27,7 +27,7 @@ impl KnockedRoomBuilder { /// Add an event to the state. pub fn add_state_event(mut self, event: StrippedStateTestEvent) -> Self { - self.inner.knock_state.events.push(event.into_raw_event()); + self.inner.knock_state.events.push(event.into()); self } diff --git a/testing/matrix-sdk-test/src/sync_builder/left_room.rs b/testing/matrix-sdk-test/src/sync_builder/left_room.rs index acf271d66..b4d8f0e41 100644 --- a/testing/matrix-sdk-test/src/sync_builder/left_room.rs +++ b/testing/matrix-sdk-test/src/sync_builder/left_room.rs @@ -71,7 +71,7 @@ impl LeftRoomBuilder { /// Add an event to the state. pub fn add_state_event(mut self, event: StateTestEvent) -> Self { - self.inner.state.events.push(event.into_raw_event()); + self.inner.state.events.push(event.into()); self } @@ -86,7 +86,7 @@ impl LeftRoomBuilder { /// Add room account data. pub fn add_account_data(mut self, event: RoomAccountDataTestEvent) -> Self { - self.inner.account_data.events.push(event.into_raw_event()); + self.inner.account_data.events.push(event.into()); self } diff --git a/testing/matrix-sdk-test/src/sync_builder/test_event.rs b/testing/matrix-sdk-test/src/sync_builder/test_event.rs index 41beec05e..ae52e9968 100644 --- a/testing/matrix-sdk-test/src/sync_builder/test_event.rs +++ b/testing/matrix-sdk-test/src/sync_builder/test_event.rs @@ -33,36 +33,42 @@ pub enum StateTestEvent { Custom(JsonValue), } -impl StateTestEvent { - /// Get the JSON representation of this test event. - pub fn into_json_value(self) -> JsonValue { - match self { - Self::Alias => test_json::sync_events::ALIAS.to_owned(), - Self::Aliases => test_json::sync_events::ALIASES.to_owned(), - Self::Create => test_json::sync_events::CREATE.to_owned(), - Self::Encryption => test_json::sync_events::ENCRYPTION.to_owned(), - Self::HistoryVisibility => test_json::sync_events::HISTORY_VISIBILITY.to_owned(), - Self::JoinRules => test_json::sync_events::JOIN_RULES.to_owned(), - Self::Member => test_json::sync_events::MEMBER.to_owned(), - Self::MemberAdditional => test_json::sync_events::MEMBER_ADDITIONAL.to_owned(), - Self::MemberBan => test_json::sync_events::MEMBER_BAN.to_owned(), - Self::MemberInvite => test_json::sync_events::MEMBER_INVITE.to_owned(), - Self::MemberLeave => test_json::sync_events::MEMBER_LEAVE.to_owned(), - Self::MemberNameChange => test_json::sync_events::MEMBER_NAME_CHANGE.to_owned(), - Self::PowerLevels => test_json::sync_events::POWER_LEVELS.to_owned(), - Self::RedactedInvalid => test_json::sync_events::REDACTED_INVALID.to_owned(), - Self::RedactedState => test_json::sync_events::REDACTED_STATE.to_owned(), - Self::RoomAvatar => test_json::sync_events::ROOM_AVATAR.to_owned(), - Self::RoomName => test_json::sync_events::NAME.to_owned(), - Self::RoomPinnedEvents => test_json::sync_events::PINNED_EVENTS.to_owned(), - Self::RoomTopic => test_json::sync_events::TOPIC.to_owned(), - Self::Custom(json) => json, +impl From for JsonValue { + fn from(val: StateTestEvent) -> Self { + match val { + StateTestEvent::Alias => test_json::sync_events::ALIAS.to_owned(), + StateTestEvent::Aliases => test_json::sync_events::ALIASES.to_owned(), + StateTestEvent::Create => test_json::sync_events::CREATE.to_owned(), + StateTestEvent::Encryption => test_json::sync_events::ENCRYPTION.to_owned(), + StateTestEvent::HistoryVisibility => { + test_json::sync_events::HISTORY_VISIBILITY.to_owned() + } + StateTestEvent::JoinRules => test_json::sync_events::JOIN_RULES.to_owned(), + StateTestEvent::Member => test_json::sync_events::MEMBER.to_owned(), + StateTestEvent::MemberAdditional => { + test_json::sync_events::MEMBER_ADDITIONAL.to_owned() + } + StateTestEvent::MemberBan => test_json::sync_events::MEMBER_BAN.to_owned(), + StateTestEvent::MemberInvite => test_json::sync_events::MEMBER_INVITE.to_owned(), + StateTestEvent::MemberLeave => test_json::sync_events::MEMBER_LEAVE.to_owned(), + StateTestEvent::MemberNameChange => { + test_json::sync_events::MEMBER_NAME_CHANGE.to_owned() + } + StateTestEvent::PowerLevels => test_json::sync_events::POWER_LEVELS.to_owned(), + StateTestEvent::RedactedInvalid => test_json::sync_events::REDACTED_INVALID.to_owned(), + StateTestEvent::RedactedState => test_json::sync_events::REDACTED_STATE.to_owned(), + StateTestEvent::RoomAvatar => test_json::sync_events::ROOM_AVATAR.to_owned(), + StateTestEvent::RoomName => test_json::sync_events::NAME.to_owned(), + StateTestEvent::RoomPinnedEvents => test_json::sync_events::PINNED_EVENTS.to_owned(), + StateTestEvent::RoomTopic => test_json::sync_events::TOPIC.to_owned(), + StateTestEvent::Custom(json) => json, } } +} - /// Get the typed JSON representation of this test event. - pub fn into_raw_event(self) -> Raw { - from_json_value(self.into_json_value()).unwrap() +impl From for Raw { + fn from(val: StateTestEvent) -> Self { + from_json_value(val.into()).unwrap() } } @@ -73,19 +79,19 @@ pub enum StrippedStateTestEvent { Custom(JsonValue), } -impl StrippedStateTestEvent { - /// Get the JSON representation of this test event. - pub fn into_json_value(self) -> JsonValue { - match self { - Self::Member => test_json::sync_events::MEMBER_STRIPPED.to_owned(), - Self::RoomName => test_json::sync_events::NAME_STRIPPED.to_owned(), - Self::Custom(json) => json, +impl From for JsonValue { + fn from(val: StrippedStateTestEvent) -> Self { + match val { + StrippedStateTestEvent::Member => test_json::sync_events::MEMBER_STRIPPED.to_owned(), + StrippedStateTestEvent::RoomName => test_json::sync_events::NAME_STRIPPED.to_owned(), + StrippedStateTestEvent::Custom(json) => json, } } +} - /// Get the typed JSON representation of this test event. - pub fn into_raw_event(self) -> Raw { - from_json_value(self.into_json_value()).unwrap() +impl From for Raw { + fn from(val: StrippedStateTestEvent) -> Self { + from_json_value(val.into()).unwrap() } } @@ -97,20 +103,22 @@ pub enum RoomAccountDataTestEvent { Custom(JsonValue), } -impl RoomAccountDataTestEvent { - /// Get the JSON representation of this test event. - pub fn into_json_value(self) -> JsonValue { - match self { - Self::FullyRead => test_json::sync_events::FULLY_READ.to_owned(), - Self::Tags => test_json::sync_events::TAG.to_owned(), - Self::MarkedUnread => test_json::sync_events::MARKED_UNREAD.to_owned(), - Self::Custom(json) => json, +impl From for JsonValue { + fn from(val: RoomAccountDataTestEvent) -> Self { + match val { + RoomAccountDataTestEvent::FullyRead => test_json::sync_events::FULLY_READ.to_owned(), + RoomAccountDataTestEvent::Tags => test_json::sync_events::TAG.to_owned(), + RoomAccountDataTestEvent::MarkedUnread => { + test_json::sync_events::MARKED_UNREAD.to_owned() + } + RoomAccountDataTestEvent::Custom(json) => json, } } +} - /// Get the typed JSON representation of this test event. - pub fn into_raw_event(self) -> Raw { - from_json_value(self.into_json_value()).unwrap() +impl From for Raw { + fn from(val: RoomAccountDataTestEvent) -> Self { + from_json_value(val.into()).unwrap() } } @@ -120,18 +128,18 @@ pub enum PresenceTestEvent { Custom(JsonValue), } -impl PresenceTestEvent { - /// Get the JSON representation of this test event. - pub fn into_json_value(self) -> JsonValue { - match self { - Self::Presence => test_json::sync_events::PRESENCE.to_owned(), - Self::Custom(json) => json, +impl From for JsonValue { + fn from(val: PresenceTestEvent) -> Self { + match val { + PresenceTestEvent::Presence => test_json::sync_events::PRESENCE.to_owned(), + PresenceTestEvent::Custom(json) => json, } } +} - /// Get the typed JSON representation of this test event. - pub fn into_raw_event(self) -> Raw { - from_json_value(self.into_json_value()).unwrap() +impl From for Raw { + fn from(val: PresenceTestEvent) -> Self { + from_json_value(val.into()).unwrap() } } @@ -142,18 +150,18 @@ pub enum GlobalAccountDataTestEvent { Custom(JsonValue), } -impl GlobalAccountDataTestEvent { - /// Get the JSON representation of this test event. - pub fn into_json_value(self) -> JsonValue { - match self { - Self::Direct => test_json::sync_events::DIRECT.to_owned(), - Self::PushRules => test_json::sync_events::PUSH_RULES.to_owned(), - Self::Custom(json) => json, +impl From for JsonValue { + fn from(val: GlobalAccountDataTestEvent) -> Self { + match val { + GlobalAccountDataTestEvent::Direct => test_json::sync_events::DIRECT.to_owned(), + GlobalAccountDataTestEvent::PushRules => test_json::sync_events::PUSH_RULES.to_owned(), + GlobalAccountDataTestEvent::Custom(json) => json, } } +} - /// Get the typed JSON representation of this test event. - pub fn into_raw_event(self) -> Raw { - from_json_value(self.into_json_value()).unwrap() +impl From for Raw { + fn from(val: GlobalAccountDataTestEvent) -> Self { + from_json_value(val.into()).unwrap() } }