Skip to content

Commit

Permalink
chore(test): Don't require two room IDs in the mock_sync_room method
Browse files Browse the repository at this point in the history
  • Loading branch information
poljar committed Nov 12, 2024
1 parent 36b96cc commit 8d6d060
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 11 deletions.
24 changes: 16 additions & 8 deletions crates/matrix-sdk/src/test_utils/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,9 @@ impl MatrixMockServer {
/// Overrides the sync/ endpoint with knowledge that the given
/// invited/joined/knocked/left room exists, runs a sync and returns the
/// given room.
pub async fn sync_room(
&self,
client: &Client,
room_id: &RoomId,
room_data: impl Into<AnyRoomBuilder>,
) -> Room {
pub async fn sync_room(&self, client: &Client, room_data: impl Into<AnyRoomBuilder>) -> Room {
let any_room = room_data.into();
let room_id = any_room.room_id().to_owned();

self.mock_sync()
.ok_and_run(client, move |builder| match any_room {
Expand All @@ -122,13 +118,13 @@ impl MatrixMockServer {
})
.await;

client.get_room(room_id).expect("look at me, the room is known now")
client.get_room(&room_id).expect("look at me, the room is known now")
}

/// Overrides the sync/ endpoint with knowledge that the given room exists
/// in the joined state, runs a sync and returns the given room.
pub async fn sync_joined_room(&self, client: &Client, room_id: &RoomId) -> Room {
self.sync_room(client, room_id, JoinedRoomBuilder::new(room_id)).await
self.sync_room(client, JoinedRoomBuilder::new(room_id)).await
}

/// Verify that the previous mocks expected number of requests match
Expand Down Expand Up @@ -236,6 +232,18 @@ pub enum AnyRoomBuilder {
Knocked(KnockedRoomBuilder),
}

impl AnyRoomBuilder {
/// Get the [`RoomId`] of the room this [`AnyRoomBuilder`] will create.
fn room_id(&self) -> &RoomId {
match self {
AnyRoomBuilder::Invited(r) => r.room_id(),
AnyRoomBuilder::Joined(r) => r.room_id(),
AnyRoomBuilder::Left(r) => r.room_id(),
AnyRoomBuilder::Knocked(r) => r.room_id(),
}
}
}

impl From<InvitedRoomBuilder> for AnyRoomBuilder {
fn from(val: InvitedRoomBuilder) -> AnyRoomBuilder {
AnyRoomBuilder::Invited(val)
Expand Down
6 changes: 3 additions & 3 deletions crates/matrix-sdk/tests/integration/send_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ async fn test_cant_send_invited_room() {
// When I'm invited to a room,
let room_id = room_id!("!a:b.c");
let client = mock.client_builder().build().await;
let room = mock.sync_room(&client, room_id, InvitedRoomBuilder::new(room_id)).await;
let room = mock.sync_room(&client, InvitedRoomBuilder::new(room_id)).await;

// I can't send message to it with the send queue.
assert_matches!(
Expand All @@ -224,7 +224,7 @@ async fn test_cant_send_left_room() {
// When I've left a room,
let room_id = room_id!("!a:b.c");
let client = mock.client_builder().build().await;
let room = mock.sync_room(&client, room_id, LeftRoomBuilder::new(room_id)).await;
let room = mock.sync_room(&client, LeftRoomBuilder::new(room_id)).await;

// I can't send message to it with the send queue.
assert_matches!(
Expand All @@ -242,7 +242,7 @@ async fn test_cant_send_knocked_room() {
// When I've knocked into a room,
let room_id = room_id!("!a:b.c");
let client = mock.client_builder().build().await;
let room = mock.sync_room(&client, room_id, KnockedRoomBuilder::new(room_id)).await;
let room = mock.sync_room(&client, KnockedRoomBuilder::new(room_id)).await;

// I can't send message to it with the send queue.
assert_matches!(
Expand Down
5 changes: 5 additions & 0 deletions testing/matrix-sdk-test/src/sync_builder/invited_room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ impl InvitedRoomBuilder {
Self { room_id: room_id.to_owned(), inner: Default::default() }
}

/// Get the room ID of this [`InvitedRoomBuilder`].
pub fn room_id(&self) -> &RoomId {
&self.room_id
}

/// 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());
Expand Down
5 changes: 5 additions & 0 deletions testing/matrix-sdk-test/src/sync_builder/joined_room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ impl JoinedRoomBuilder {
Self { room_id: room_id.to_owned(), inner: Default::default() }
}

/// Get the room ID of this [`JoinedRoomBuilder`].
pub fn room_id(&self) -> &RoomId {
&self.room_id
}

/// Add an event to the timeline.
///
/// The raw event can be created with the
Expand Down
5 changes: 5 additions & 0 deletions testing/matrix-sdk-test/src/sync_builder/knocked_room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ impl KnockedRoomBuilder {
Self { room_id: room_id.to_owned(), inner: Default::default() }
}

/// Get the room ID of this [`KnockedRoomBuilder`].
pub fn room_id(&self) -> &RoomId {
&self.room_id
}

/// 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());
Expand Down
5 changes: 5 additions & 0 deletions testing/matrix-sdk-test/src/sync_builder/left_room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ impl LeftRoomBuilder {
Self { room_id: room_id.to_owned(), inner: Default::default() }
}

/// Get the room ID of this [`LeftRoomBuilder`].
pub fn room_id(&self) -> &RoomId {
&self.room_id
}

/// Add an event to the timeline.
///
/// The raw event can be created with the
Expand Down

0 comments on commit 8d6d060

Please sign in to comment.