2020-04-07 00:59:44 +00:00
|
|
|
#![cfg(test)]
|
2020-04-06 11:12:02 +00:00
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
use std::fs;
|
|
|
|
use std::panic;
|
2020-04-07 12:55:42 +00:00
|
|
|
use std::path::Path;
|
2020-04-06 11:12:02 +00:00
|
|
|
|
2020-05-07 14:22:18 +00:00
|
|
|
use crate::api::r0::sync::sync_events::Response as SyncResponse;
|
2020-04-07 00:59:44 +00:00
|
|
|
use crate::events::{
|
2020-04-07 12:55:42 +00:00
|
|
|
collections::{
|
|
|
|
all::{RoomEvent, StateEvent},
|
|
|
|
only::Event,
|
|
|
|
},
|
2020-04-07 00:59:44 +00:00
|
|
|
presence::PresenceEvent,
|
2020-04-23 08:52:47 +00:00
|
|
|
EventJson, TryFromRaw,
|
2020-04-07 00:59:44 +00:00
|
|
|
};
|
2020-05-07 14:22:18 +00:00
|
|
|
use http::Response;
|
|
|
|
use std::convert::TryFrom;
|
2020-04-07 00:59:44 +00:00
|
|
|
|
2020-04-07 20:11:35 +00:00
|
|
|
/// Easily create events to stream into either a Client or a `Room` for testing.
|
2020-04-07 00:59:44 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct EventBuilder {
|
|
|
|
/// The events that determine the state of a `Room`.
|
|
|
|
room_events: Vec<RoomEvent>,
|
|
|
|
/// The presence events that determine the presence state of a `RoomMember`.
|
|
|
|
presence_events: Vec<PresenceEvent>,
|
|
|
|
/// The state events that determine the state of a `Room`.
|
|
|
|
state_events: Vec<StateEvent>,
|
2020-04-07 12:55:42 +00:00
|
|
|
/// The ephemeral room events that determine the state of a `Room`.
|
|
|
|
ephemeral: Vec<Event>,
|
|
|
|
/// The account data events that determine the state of a `Room`.
|
|
|
|
account_data: Vec<Event>,
|
|
|
|
}
|
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
impl EventBuilder {
|
2020-04-07 12:55:42 +00:00
|
|
|
/// Add an event to the room events `Vec`.
|
|
|
|
pub fn add_ephemeral_from_file<Ev: TryFromRaw, P: AsRef<Path>>(
|
|
|
|
mut self,
|
|
|
|
path: P,
|
|
|
|
variant: fn(Ev) -> Event,
|
|
|
|
) -> Self {
|
|
|
|
let val = fs::read_to_string(path.as_ref())
|
2020-04-27 10:12:51 +00:00
|
|
|
.unwrap_or_else(|_| panic!("file not found {:?}", path.as_ref()));
|
2020-04-23 08:52:47 +00:00
|
|
|
let event = serde_json::from_str::<EventJson<Ev>>(&val)
|
2020-04-07 12:55:42 +00:00
|
|
|
.unwrap()
|
2020-04-23 08:52:47 +00:00
|
|
|
.deserialize()
|
2020-04-07 12:55:42 +00:00
|
|
|
.unwrap();
|
|
|
|
self.ephemeral.push(variant(event));
|
2020-04-07 00:59:44 +00:00
|
|
|
self
|
2020-04-06 11:12:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
/// Add an event to the room events `Vec`.
|
2020-04-07 12:55:42 +00:00
|
|
|
pub fn add_account_from_file<Ev: TryFromRaw, P: AsRef<Path>>(
|
|
|
|
mut self,
|
|
|
|
path: P,
|
|
|
|
variant: fn(Ev) -> Event,
|
|
|
|
) -> Self {
|
|
|
|
let val = fs::read_to_string(path.as_ref())
|
2020-04-27 10:12:51 +00:00
|
|
|
.unwrap_or_else(|_| panic!("file not found {:?}", path.as_ref()));
|
2020-04-23 08:52:47 +00:00
|
|
|
let event = serde_json::from_str::<EventJson<Ev>>(&val)
|
2020-04-07 12:55:42 +00:00
|
|
|
.unwrap()
|
2020-04-23 08:52:47 +00:00
|
|
|
.deserialize()
|
2020-04-07 12:55:42 +00:00
|
|
|
.unwrap();
|
|
|
|
self.account_data.push(variant(event));
|
2020-04-07 00:59:44 +00:00
|
|
|
self
|
|
|
|
}
|
2020-04-06 11:12:02 +00:00
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
/// Add an event to the room events `Vec`.
|
2020-04-07 12:55:42 +00:00
|
|
|
pub fn add_room_event_from_file<Ev: TryFromRaw, P: AsRef<Path>>(
|
|
|
|
mut self,
|
|
|
|
path: P,
|
|
|
|
variant: fn(Ev) -> RoomEvent,
|
|
|
|
) -> Self {
|
|
|
|
let val = fs::read_to_string(path.as_ref())
|
2020-04-27 10:12:51 +00:00
|
|
|
.unwrap_or_else(|_| panic!("file not found {:?}", path.as_ref()));
|
2020-04-23 08:52:47 +00:00
|
|
|
let event = serde_json::from_str::<EventJson<Ev>>(&val)
|
2020-04-07 12:55:42 +00:00
|
|
|
.unwrap()
|
2020-04-23 08:52:47 +00:00
|
|
|
.deserialize()
|
2020-04-07 12:55:42 +00:00
|
|
|
.unwrap();
|
2020-04-07 00:59:44 +00:00
|
|
|
self.room_events.push(variant(event));
|
|
|
|
self
|
|
|
|
}
|
2020-04-06 11:12:02 +00:00
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
/// Add a state event to the state events `Vec`.
|
2020-04-07 12:55:42 +00:00
|
|
|
pub fn add_state_event_from_file<Ev: TryFromRaw, P: AsRef<Path>>(
|
|
|
|
mut self,
|
|
|
|
path: P,
|
|
|
|
variant: fn(Ev) -> StateEvent,
|
|
|
|
) -> Self {
|
|
|
|
let val = fs::read_to_string(path.as_ref())
|
2020-04-27 10:12:51 +00:00
|
|
|
.unwrap_or_else(|_| panic!("file not found {:?}", path.as_ref()));
|
2020-04-23 08:52:47 +00:00
|
|
|
let event = serde_json::from_str::<EventJson<Ev>>(&val)
|
2020-04-07 12:55:42 +00:00
|
|
|
.unwrap()
|
2020-04-23 08:52:47 +00:00
|
|
|
.deserialize()
|
2020-04-07 12:55:42 +00:00
|
|
|
.unwrap();
|
2020-04-07 00:59:44 +00:00
|
|
|
self.state_events.push(variant(event));
|
|
|
|
self
|
|
|
|
}
|
2020-04-06 11:12:02 +00:00
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
/// Add a presence event to the presence events `Vec`.
|
|
|
|
pub fn add_presence_event_from_file<P: AsRef<Path>>(mut self, path: P) -> Self {
|
2020-04-07 12:55:42 +00:00
|
|
|
let val = fs::read_to_string(path.as_ref())
|
2020-04-27 10:12:51 +00:00
|
|
|
.unwrap_or_else(|_| panic!("file not found {:?}", path.as_ref()));
|
2020-04-23 08:52:47 +00:00
|
|
|
let event = serde_json::from_str::<EventJson<PresenceEvent>>(&val)
|
2020-04-07 12:55:42 +00:00
|
|
|
.unwrap()
|
2020-04-23 08:52:47 +00:00
|
|
|
.deserialize()
|
2020-04-07 12:55:42 +00:00
|
|
|
.unwrap();
|
2020-04-07 00:59:44 +00:00
|
|
|
self.presence_events.push(event);
|
|
|
|
self
|
|
|
|
}
|
2020-04-06 11:12:02 +00:00
|
|
|
|
2020-05-07 14:22:18 +00:00
|
|
|
/// Consumes `ResponseBuilder and returns SyncResponse.
|
|
|
|
pub fn build_sync_response(self) -> SyncResponse {
|
2020-04-07 12:55:42 +00:00
|
|
|
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": {
|
|
|
|
"!SVkFJHzfwvuaIEawgC:localhost": {
|
|
|
|
"summary": {},
|
|
|
|
"account_data": {
|
|
|
|
"events": self.account_data
|
|
|
|
},
|
|
|
|
"ephemeral": {
|
|
|
|
"events": self.ephemeral
|
|
|
|
},
|
|
|
|
"state": {
|
|
|
|
"events": self.state_events
|
|
|
|
},
|
|
|
|
"timeline": {
|
|
|
|
"events": self.room_events,
|
|
|
|
"limited": true,
|
|
|
|
"prev_batch": "t392-516_47314_0_7_1_1_1_11444_1"
|
|
|
|
},
|
|
|
|
"unread_notifications": {
|
|
|
|
"highlight_count": 0,
|
|
|
|
"notification_count": 11
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"leave": {}
|
|
|
|
},
|
|
|
|
"to_device": {
|
|
|
|
"events": []
|
|
|
|
},
|
|
|
|
"presence": {
|
|
|
|
"events": []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-05-07 14:22:18 +00:00
|
|
|
let response = Response::builder()
|
|
|
|
.body(serde_json::to_vec(&body).unwrap())
|
|
|
|
.unwrap();
|
|
|
|
SyncResponse::try_from(response).unwrap()
|
2020-04-06 13:11:38 +00:00
|
|
|
}
|
2020-04-06 11:12:02 +00:00
|
|
|
}
|