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.
This commit is contained in:
Robin
2025-04-16 17:29:01 +02:00
committed by Damir Jelić
parent 9242d1869a
commit 6f683d3cde
6 changed files with 84 additions and 76 deletions

View File

@@ -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});

View File

@@ -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
}

View File

@@ -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<Raw<AnySyncStateEvent>>) -> 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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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<StateTestEvent> 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<AnySyncStateEvent> {
from_json_value(self.into_json_value()).unwrap()
impl From<StateTestEvent> for Raw<AnySyncStateEvent> {
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<StrippedStateTestEvent> 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<AnyStrippedStateEvent> {
from_json_value(self.into_json_value()).unwrap()
impl From<StrippedStateTestEvent> for Raw<AnyStrippedStateEvent> {
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<RoomAccountDataTestEvent> 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<AnyRoomAccountDataEvent> {
from_json_value(self.into_json_value()).unwrap()
impl From<RoomAccountDataTestEvent> for Raw<AnyRoomAccountDataEvent> {
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<PresenceTestEvent> 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<PresenceEvent> {
from_json_value(self.into_json_value()).unwrap()
impl From<PresenceTestEvent> for Raw<PresenceEvent> {
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<GlobalAccountDataTestEvent> 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<AnyGlobalAccountDataEvent> {
from_json_value(self.into_json_value()).unwrap()
impl From<GlobalAccountDataTestEvent> for Raw<AnyGlobalAccountDataEvent> {
fn from(val: GlobalAccountDataTestEvent) -> Self {
from_json_value(val.into()).unwrap()
}
}