Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: minimize usage of EventFactory::state_key #4562

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions crates/matrix-sdk/src/room/knock_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ impl KnockRequestMemberInfo {
mod tests {
use matrix_sdk_test::{async_test, event_factory::EventFactory, JoinedRoomBuilder};
use ruma::{
event_id,
events::room::member::{MembershipState, RoomMemberEventContent},
owned_user_id, room_id, user_id, EventId,
event_id, events::room::member::MembershipState, owned_user_id, room_id, user_id, EventId,
};

use crate::{
Expand All @@ -128,10 +126,9 @@ mod tests {

let f = EventFactory::new().room(room_id);
let joined_room_builder = JoinedRoomBuilder::new(room_id).add_state_bulk(vec![f
.event(RoomMemberEventContent::new(MembershipState::Knock))
.member(user_id)
.membership(MembershipState::Knock)
.event_id(event_id)
.sender(user_id)
.state_key(user_id)
.into_raw_timeline()
.cast()]);
let room = server.sync_room(&client, joined_room_builder).await;
Expand Down
11 changes: 3 additions & 8 deletions crates/matrix-sdk/src/room/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3694,11 +3694,7 @@ mod tests {
async_test, event_factory::EventFactory, test_json, JoinedRoomBuilder, StateTestEvent,
SyncResponseBuilder,
};
use ruma::{
device_id, event_id,
events::room::member::{MembershipState, RoomMemberEventContent},
int, room_id, user_id,
};
use ruma::{device_id, event_id, events::room::member::MembershipState, int, room_id, user_id};
use wiremock::{
matchers::{header, method, path_regex},
Mock, MockServer, ResponseTemplate,
Expand Down Expand Up @@ -3894,10 +3890,9 @@ mod tests {

let f = EventFactory::new().room(room_id);
let joined_room_builder = JoinedRoomBuilder::new(room_id).add_state_bulk(vec![f
.event(RoomMemberEventContent::new(MembershipState::Knock))
.member(user_id)
.membership(MembershipState::Knock)
.event_id(event_id)
.sender(user_id)
.state_key(user_id)
.into_raw_timeline()
.cast()]);
let room = server.sync_room(&client, joined_room_builder).await;
Expand Down
15 changes: 3 additions & 12 deletions crates/matrix-sdk/tests/integration/room/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ use ruma::{
event_id,
events::{
direct::DirectUserIdentifier,
room::{
avatar::{self, RoomAvatarEventContent},
member::MembershipState,
message::RoomMessageEventContent,
},
room::{avatar, member::MembershipState, message::RoomMessageEventContent},
AnySyncStateEvent, AnySyncTimelineEvent, StateEventType,
},
mxc_uri, room_id,
Expand Down Expand Up @@ -881,9 +877,7 @@ async fn test_room_avatar() {
// Set the avatar, but not the info.
let avatar_url_1 = mxc_uri!("mxc://server.local/abcdef");

let mut content = RoomAvatarEventContent::new();
content.url = Some(avatar_url_1.to_owned());
let event = factory.event(content).state_key("").into_raw_sync();
let event = factory.room_avatar().url(avatar_url_1).into_raw_sync();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So smooth.


let mut sync_builder = SyncResponseBuilder::new();
sync_builder.add_joined_room(JoinedRoomBuilder::new(room_id).add_timeline_event(event));
Expand All @@ -903,10 +897,7 @@ async fn test_room_avatar() {
avatar_info_2.mimetype = Some("image/png".to_owned());
avatar_info_2.size = Some(uint!(5243));

let mut content = RoomAvatarEventContent::new();
content.url = Some(avatar_url_2.to_owned());
content.info = Some(avatar_info_2.into());
let event = factory.event(content).state_key("").into_raw_sync();
let event = factory.room_avatar().url(avatar_url_2).info(avatar_info_2).into_raw_sync();

sync_builder.add_joined_room(JoinedRoomBuilder::new(room_id).add_timeline_event(event));
mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
Expand Down
66 changes: 23 additions & 43 deletions crates/matrix-sdk/tests/integration/room/joined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use ruma::{
direct::DirectUserIdentifier,
receipt::ReceiptThread,
room::{
member::{MembershipState, RoomMemberEventContent},
member::MembershipState,
message::{RoomMessageEventContent, RoomMessageEventContentWithoutRelation},
},
TimelineEventType,
Expand Down Expand Up @@ -843,10 +843,9 @@ async fn test_subscribe_to_knock_requests() {
let user_id = user_id!("@alice:b.c");
let knock_event_id = event_id!("$alice-knock:b.c");
let knock_event = f
.event(RoomMemberEventContent::new(MembershipState::Knock))
.member(user_id)
.membership(MembershipState::Knock)
.event_id(knock_event_id)
.sender(user_id)
.state_key(user_id)
.into_raw_timeline()
.cast();

Expand Down Expand Up @@ -877,9 +876,8 @@ async fn test_subscribe_to_knock_requests() {

// If we then receive a new member event for Alice that's not 'knock'
let joined_room_builder = JoinedRoomBuilder::new(room_id).add_state_bulk(vec![f
.event(RoomMemberEventContent::new(MembershipState::Invite))
.sender(user_id)
.state_key(user_id)
.member(user_id)
.membership(MembershipState::Invite)
.into_raw_timeline()
.cast()]);
server.sync_room(&client, joined_room_builder).await;
Expand Down Expand Up @@ -917,12 +915,8 @@ async fn test_subscribe_to_knock_requests_reloads_members_on_limited_sync() {
let f = EventFactory::new().room(room_id);

let user_id = user_id!("@alice:b.c");
let knock_event = f
.event(RoomMemberEventContent::new(MembershipState::Knock))
.sender(user_id)
.state_key(user_id)
.into_raw_timeline()
.cast();
let knock_event =
f.member(user_id).membership(MembershipState::Knock).into_raw_timeline().cast();

server
.mock_get_members()
Expand Down Expand Up @@ -969,10 +963,9 @@ async fn test_remove_outdated_seen_knock_requests_ids_when_membership_changed()
let user_id = user_id!("@alice:b.c");
let knock_event_id = event_id!("$alice-knock:b.c");
let knock_event = f
.event(RoomMemberEventContent::new(MembershipState::Knock))
.member(user_id)
.membership(MembershipState::Knock)
.event_id(knock_event_id)
.sender(user_id)
.state_key(user_id)
.into_raw_timeline()
.cast();

Expand All @@ -990,12 +983,8 @@ async fn test_remove_outdated_seen_knock_requests_ids_when_membership_changed()

// If we then load the members again and the previously knocking member is in
// another state now
let joined_event = f
.event(RoomMemberEventContent::new(MembershipState::Join))
.sender(user_id)
.state_key(user_id)
.into_raw_timeline()
.cast();
let joined_event =
f.member(user_id).membership(MembershipState::Join).into_raw_timeline().cast();

server.mock_get_members().ok(vec![joined_event]).mock_once().mount().await;

Expand Down Expand Up @@ -1024,10 +1013,9 @@ async fn test_remove_outdated_seen_knock_requests_ids_when_we_have_an_outdated_k
let user_id = user_id!("@alice:b.c");
let knock_event_id = event_id!("$alice-knock:b.c");
let knock_event = f
.event(RoomMemberEventContent::new(MembershipState::Knock))
.member(user_id)
.membership(MembershipState::Knock)
.event_id(knock_event_id)
.sender(user_id)
.state_key(user_id)
.into_raw_timeline()
.cast();

Expand All @@ -1046,10 +1034,9 @@ async fn test_remove_outdated_seen_knock_requests_ids_when_we_have_an_outdated_k
// If we then load the members again and the previously knocking member has a
// different event id
let knock_event = f
.event(RoomMemberEventContent::new(MembershipState::Knock))
.member(user_id)
.membership(MembershipState::Knock)
.event_id(event_id!("$knock-2:b.c"))
.sender(user_id)
.state_key(user_id)
.into_raw_timeline()
.cast();

Expand Down Expand Up @@ -1080,10 +1067,9 @@ async fn test_subscribe_to_knock_requests_clears_seen_ids_on_member_reload() {
let user_id = user_id!("@alice:b.c");
let knock_event_id = event_id!("$alice-knock:b.c");
let knock_event = f
.event(RoomMemberEventContent::new(MembershipState::Knock))
.member(user_id)
.membership(MembershipState::Knock)
.event_id(knock_event_id)
.sender(user_id)
.state_key(user_id)
.into_raw_timeline()
.cast();

Expand Down Expand Up @@ -1114,12 +1100,8 @@ async fn test_subscribe_to_knock_requests_clears_seen_ids_on_member_reload() {

// If we then load the members again and the previously knocking member is in
// another state now
let joined_event = f
.event(RoomMemberEventContent::new(MembershipState::Join))
.sender(user_id)
.state_key(user_id)
.into_raw_timeline()
.cast();
let joined_event =
f.member(user_id).membership(MembershipState::Join).into_raw_timeline().cast();

server.mock_get_members().ok(vec![joined_event]).mock_once().mount().await;

Expand Down Expand Up @@ -1163,9 +1145,8 @@ async fn test_room_member_updates_sender_on_full_member_reload() {
let user_id = user_id!("@alice:b.c");
let joined_event = EventFactory::new()
.room(room_id)
.event(RoomMemberEventContent::new(MembershipState::Join))
.sender(user_id)
.state_key(user_id)
.member(user_id)
.membership(MembershipState::Join)
.into_raw_timeline()
.cast();
server.mock_get_members().ok(vec![joined_event]).mock_once().mount().await;
Expand All @@ -1191,9 +1172,8 @@ async fn test_room_member_updates_sender_on_partial_members_update() {
let user_id = user_id!("@alice:b.c");
let joined_event = EventFactory::new()
.room(room_id)
.event(RoomMemberEventContent::new(MembershipState::Join))
.sender(user_id)
.state_key(user_id)
.member(user_id)
.membership(MembershipState::Join)
.into_raw_sync()
.cast();
server
Expand Down
27 changes: 27 additions & 0 deletions testing/matrix-sdk-test/src/event_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use ruma::{
receipt::{Receipt, ReceiptEventContent, ReceiptThread, ReceiptType},
relation::{Annotation, InReplyTo, Replacement, Thread},
room::{
avatar::{self, RoomAvatarEventContent},
encrypted::{EncryptedEventScheme, RoomEncryptedEventContent},
member::{MembershipState, RoomMemberEventContent},
message::{
Expand Down Expand Up @@ -170,6 +171,10 @@ where
self
}

/// For state events manually created, define the state key.
///
/// For other state events created in the [`EventFactory`], this is
/// automatically filled upon creation or update of the events.
pub fn state_key(mut self, state_key: impl Into<String>) -> Self {
self.state_key = Some(state_key.into());
self
Expand Down Expand Up @@ -465,6 +470,14 @@ impl EventFactory {
event
}

/// Create an empty state event for the room avatar.
pub fn room_avatar(&self) -> EventBuilder<RoomAvatarEventContent> {
let mut event = self.event(RoomAvatarEventContent::new());
// The state key is empty for a room avatar state event.
event.state_key = Some("".to_owned());
event
}

/// Create a new `m.member_hints` event with the given service members.
///
/// ```
Expand Down Expand Up @@ -749,6 +762,20 @@ impl EventBuilder<RoomMemberEventContent> {
}
}

impl EventBuilder<RoomAvatarEventContent> {
/// Defines the URL for the room avatar.
pub fn url(mut self, url: &MxcUri) -> Self {
self.content.url = Some(url.to_owned());
self
}

/// Defines the image info for the avatar.
pub fn info(mut self, image: avatar::ImageInfo) -> Self {
self.content.info = Some(Box::new(image));
self
}
}

pub struct ReadReceiptBuilder<'a> {
factory: &'a EventFactory,
content: ReceiptEventContent,
Expand Down
Loading