base: Add some initial test for room creation.

master
Damir Jelić 2020-05-13 15:57:09 +02:00
parent 51f0a487ce
commit e32af74be5
2 changed files with 155 additions and 26 deletions

View File

@ -1428,3 +1428,41 @@ impl BaseClient {
} }
} }
} }
#[cfg(test)]
mod test {
use crate::identifiers::{RoomId, UserId};
use crate::{events::collections::all::RoomEvent, BaseClient, Session};
use matrix_sdk_test::{async_test, EventBuilder, EventsFile};
use std::convert::TryFrom;
fn get_client() -> BaseClient {
let session = Session {
access_token: "1234".to_owned(),
user_id: UserId::try_from("@example:localhost").unwrap(),
device_id: "DEVICEID".to_owned(),
};
BaseClient::new(Some(session)).unwrap()
}
fn get_room_id() -> RoomId {
RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap()
}
#[async_test]
async fn test_joined_room_creation() {
let mut sync_response = EventBuilder::default()
.add_room_event(EventsFile::Member, RoomEvent::RoomMember)
.build_sync_response();
let client = get_client();
let room_id = get_room_id();
client
.receive_sync_response(&mut sync_response)
.await
.unwrap();
let room = client.get_joined_room(&room_id).await;
assert!(room.is_some());
}
}

View File

@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::panic; use std::panic;
@ -10,8 +11,10 @@ use matrix_sdk_common::events::{
only::Event, only::Event,
}, },
presence::PresenceEvent, presence::PresenceEvent,
stripped::AnyStrippedStateEvent,
EventJson, TryFromRaw, EventJson, TryFromRaw,
}; };
use matrix_sdk_common::identifiers::RoomId;
pub use matrix_sdk_test_macros::async_test; pub use matrix_sdk_test_macros::async_test;
@ -45,7 +48,11 @@ pub enum EventsFile {
#[derive(Default)] #[derive(Default)]
pub struct EventBuilder { pub struct EventBuilder {
/// The events that determine the state of a `Room`. /// The events that determine the state of a `Room`.
room_events: Vec<RoomEvent>, joined_room_events: HashMap<RoomId, Vec<RoomEvent>>,
/// The events that determine the state of a `Room`.
invited_room_events: Vec<AnyStrippedStateEvent>,
/// The events that determine the state of a `Room`.
left_room_events: Vec<RoomEvent>,
/// The presence events that determine the presence state of a `RoomMember`. /// The presence events that determine the presence state of a `RoomMember`.
presence_events: Vec<PresenceEvent>, presence_events: Vec<PresenceEvent>,
/// The state events that determine the state of a `Room`. /// The state events that determine the state of a `Room`.
@ -111,7 +118,59 @@ impl EventBuilder {
.unwrap() .unwrap()
.deserialize() .deserialize()
.unwrap(); .unwrap();
self.room_events.push(variant(event)); self.add_joined_event(
&RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap(),
variant(event),
);
self
}
pub fn add_custom_joined_event<Ev: TryFromRaw>(
mut self,
event: serde_json::Value,
variant: fn(Ev) -> RoomEvent,
) -> Self {
let event = serde_json::from_value::<EventJson<Ev>>(event)
.unwrap()
.deserialize()
.unwrap();
self.add_joined_event(
&RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap(),
variant(event),
);
self
}
fn add_joined_event(&mut self, room_id: &RoomId, event: RoomEvent) {
self.joined_room_events
.entry(room_id.clone())
.or_insert_with(Vec::new)
.push(event);
}
pub fn add_custom_invited_event<Ev: TryFromRaw>(
mut self,
event: serde_json::Value,
variant: fn(Ev) -> AnyStrippedStateEvent,
) -> Self {
let event = serde_json::from_value::<EventJson<Ev>>(event)
.unwrap()
.deserialize()
.unwrap();
self.invited_room_events.push(variant(event));
self
}
pub fn add_custom_left_event<Ev: TryFromRaw>(
mut self,
event: serde_json::Value,
variant: fn(Ev) -> RoomEvent,
) -> Self {
let event = serde_json::from_value::<EventJson<Ev>>(event)
.unwrap()
.deserialize()
.unwrap();
self.left_room_events.push(variant(event));
self self
} }
@ -152,19 +211,11 @@ impl EventBuilder {
} }
/// Consumes `ResponseBuilder and returns SyncResponse. /// Consumes `ResponseBuilder and returns SyncResponse.
pub fn build_sync_response(self) -> SyncResponse { pub fn build_sync_response(mut self) -> SyncResponse {
let body = serde_json::json! { let main_room_id = RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap();
{
"device_one_time_keys_count": {}, // TODO generalize this.
"next_batch": "s526_47314_0_7_1_1_1_11444_1", let joined_room = serde_json::json!({
"device_lists": {
"changed": [],
"left": []
},
"rooms": {
"invite": {},
"join": {
"!SVkFJHzfwvuaIEawgC:localhost": {
"summary": {}, "summary": {},
"account_data": { "account_data": {
"events": self.account_data "events": self.account_data
@ -176,7 +227,7 @@ impl EventBuilder {
"events": self.state_events "events": self.state_events
}, },
"timeline": { "timeline": {
"events": self.room_events, "events": self.joined_room_events.remove(&main_room_id).unwrap_or_default(),
"limited": true, "limited": true,
"prev_batch": "t392-516_47314_0_7_1_1_1_11444_1" "prev_batch": "t392-516_47314_0_7_1_1_1_11444_1"
}, },
@ -184,8 +235,48 @@ impl EventBuilder {
"highlight_count": 0, "highlight_count": 0,
"notification_count": 11 "notification_count": 11
} }
} });
let mut rooms: HashMap<RoomId, serde_json::Value> = HashMap::new();
rooms.insert(main_room_id, joined_room);
for (room_id, events) in self.joined_room_events.drain() {
let joined_room = serde_json::json!({
"summary": {},
"account_data": {
"events": [],
}, },
"ephemeral": {
"events": [],
},
"state": {
"events": [],
},
"timeline": {
"events": events,
"limited": true,
"prev_batch": "t392-516_47314_0_7_1_1_1_11444_1"
},
"unread_notifications": {
"highlight_count": 0,
"notification_count": 11
}
});
rooms.insert(room_id, joined_room);
}
let body = serde_json::json! {
{
"device_one_time_keys_count": {},
"next_batch": "s526_47314_0_7_1_1_1_11444_1",
"device_lists": {
"changed": [],
"left": []
},
"rooms": {
"invite": {},
"join": rooms,
"leave": {} "leave": {}
}, },
"to_device": { "to_device": {