AppFlowy/frontend/rust-lib/flowy-test/src/event_builder.rs

156 lines
3.9 KiB
Rust
Raw Normal View History

2021-12-14 10:04:51 +00:00
use crate::FlowySDKTest;
2022-07-19 06:40:56 +00:00
use flowy_user::{entities::UserProfilePB, errors::FlowyError};
use lib_dispatch::prelude::{
2022-12-01 02:59:22 +00:00
AFPluginDispatcher, AFPluginEventResponse, AFPluginFromBytes, AFPluginRequest, StatusCode, ToBytes, *,
};
2021-07-16 15:18:12 +00:00
use std::{
2021-12-14 10:04:51 +00:00
convert::TryFrom,
2021-07-16 15:18:12 +00:00
fmt::{Debug, Display},
hash::Hash,
2021-12-14 10:04:51 +00:00
marker::PhantomData,
sync::Arc,
2021-07-16 15:18:12 +00:00
};
2022-01-22 10:48:43 +00:00
pub type FolderEventBuilder = EventBuilder<FlowyError>;
impl FolderEventBuilder {
2022-01-24 09:35:58 +00:00
pub fn new(sdk: FlowySDKTest) -> Self {
EventBuilder::test(TestContext::new(sdk))
}
2022-07-19 06:40:56 +00:00
pub fn user_profile(&self) -> &Option<UserProfilePB> {
2022-01-24 09:35:58 +00:00
&self.user_profile
}
}
2022-01-22 10:48:43 +00:00
pub type UserModuleEventBuilder = FolderEventBuilder;
2021-12-14 10:04:51 +00:00
2021-09-04 07:12:53 +00:00
#[derive(Clone)]
2021-12-08 09:33:22 +00:00
pub struct EventBuilder<E> {
2021-09-04 07:12:53 +00:00
context: TestContext,
2022-07-19 06:40:56 +00:00
user_profile: Option<UserProfilePB>,
2021-09-04 07:12:53 +00:00
err_phantom: PhantomData<E>,
}
2021-12-08 09:33:22 +00:00
impl<E> EventBuilder<E>
where
E: AFPluginFromBytes + Debug,
{
2021-12-08 09:33:22 +00:00
fn test(context: TestContext) -> Self {
2021-09-04 07:12:53 +00:00
Self {
context,
2021-09-04 08:53:58 +00:00
user_profile: None,
2021-09-04 07:12:53 +00:00
err_phantom: PhantomData,
}
}
2022-02-24 13:49:18 +00:00
pub fn payload<P>(mut self, payload: P) -> Self
2021-07-16 15:18:12 +00:00
where
2021-07-17 00:24:17 +00:00
P: ToBytes,
2021-07-16 15:18:12 +00:00
{
2021-09-04 07:12:53 +00:00
match payload.into_bytes() {
Ok(bytes) => {
let module_request = self.get_request();
self.context.request = Some(module_request.payload(bytes))
2022-01-24 09:35:58 +00:00
}
2021-09-04 07:12:53 +00:00
Err(e) => {
log::error!("Set payload failed: {:?}", e);
2022-01-24 09:35:58 +00:00
}
2021-09-04 07:12:53 +00:00
}
2021-07-16 15:18:12 +00:00
self
}
2021-09-04 07:12:53 +00:00
pub fn event<Event>(mut self, event: Event) -> Self
2021-07-16 15:18:12 +00:00
where
2021-09-04 07:12:53 +00:00
Event: Eq + Hash + Debug + Clone + Display,
2021-07-16 15:18:12 +00:00
{
self.context.request = Some(AFPluginRequest::new(event));
2021-07-16 15:18:12 +00:00
self
}
pub fn sync_send(mut self) -> Self {
2021-09-04 07:12:53 +00:00
let request = self.get_request();
let resp = AFPluginDispatcher::sync_send(self.dispatch(), request);
2021-09-04 07:12:53 +00:00
self.context.response = Some(resp);
2021-07-16 15:18:12 +00:00
self
}
pub async fn async_send(mut self) -> Self {
let request = self.get_request();
let resp = AFPluginDispatcher::async_send(self.dispatch(), request).await;
self.context.response = Some(resp);
self
}
2021-09-04 07:12:53 +00:00
pub fn parse<R>(self) -> R
2021-07-16 15:18:12 +00:00
where
R: AFPluginFromBytes,
2021-07-16 15:18:12 +00:00
{
2021-09-04 07:12:53 +00:00
let response = self.get_response();
2022-08-15 14:40:54 +00:00
match response.clone().parse::<R, E>() {
2021-09-04 07:12:53 +00:00
Ok(Ok(data)) => data,
Ok(Err(e)) => {
2022-08-15 14:40:54 +00:00
panic!(
"Parser {:?} failed: {:?}, response {:?}",
std::any::type_name::<R>(),
e,
response
)
2022-01-24 09:35:58 +00:00
}
2022-08-12 08:06:30 +00:00
Err(e) => panic!(
2022-08-15 14:40:54 +00:00
"Dispatch {:?} failed: {:?}, response {:?}",
2022-08-12 08:06:30 +00:00
std::any::type_name::<R>(),
2022-08-15 14:40:54 +00:00
e,
response
2022-08-12 08:06:30 +00:00
),
2021-09-04 07:12:53 +00:00
}
2021-07-16 15:18:12 +00:00
}
2021-09-04 07:12:53 +00:00
pub fn error(self) -> E {
let response = self.get_response();
assert_eq!(response.status_code, StatusCode::Err);
<AFPluginData<E>>::try_from(response.payload).unwrap().into_inner()
2021-09-04 07:12:53 +00:00
}
2021-07-16 15:18:12 +00:00
2021-09-04 07:12:53 +00:00
pub fn assert_error(self) -> Self {
// self.context.assert_error();
2021-07-16 15:18:12 +00:00
self
}
2021-09-04 07:12:53 +00:00
pub fn assert_success(self) -> Self {
// self.context.assert_success();
2021-07-16 15:18:12 +00:00
self
}
2021-09-04 07:12:53 +00:00
fn dispatch(&self) -> Arc<AFPluginDispatcher> {
2022-01-24 09:35:58 +00:00
self.context.sdk.dispatcher()
}
2021-09-04 07:12:53 +00:00
2022-12-01 02:59:22 +00:00
fn get_response(&self) -> AFPluginEventResponse {
self.context
.response
.as_ref()
.expect("must call sync_send first")
.clone()
}
2021-09-04 07:12:53 +00:00
fn get_request(&mut self) -> AFPluginRequest {
2022-01-24 09:35:58 +00:00
self.context.request.take().expect("must call event first")
}
2021-07-16 15:18:12 +00:00
}
2021-07-17 00:24:17 +00:00
2021-09-04 07:12:53 +00:00
#[derive(Clone)]
pub struct TestContext {
2021-12-08 09:33:22 +00:00
pub sdk: FlowySDKTest,
request: Option<AFPluginRequest>,
2022-12-01 02:59:22 +00:00
response: Option<AFPluginEventResponse>,
2021-07-17 00:24:17 +00:00
}
2021-09-04 07:12:53 +00:00
impl TestContext {
2021-12-08 09:33:22 +00:00
pub fn new(sdk: FlowySDKTest) -> Self {
2021-07-17 00:24:17 +00:00
Self {
2021-09-04 07:12:53 +00:00
sdk,
request: None,
response: None,
2021-07-17 00:24:17 +00:00
}
}
}