use crate::{ helper::{valid_email, valid_password}, init_sdk, }; use flowy_dispatch::prelude::*; pub use flowy_sdk::*; use flowy_user::{ errors::UserError, event::UserEvent::{SignIn, SignOut}, prelude::*, }; use std::{ convert::TryFrom, fmt::{Debug, Display}, hash::Hash, marker::PhantomData, thread, }; pub struct Tester { inner_request: Option, assert_status_code: Option, response: Option, err_phantom: PhantomData, user_detail: Option, } impl Tester where Error: FromBytes + Debug, { pub fn new(event: E) -> Self where E: Eq + Hash + Debug + Clone + Display, { init_sdk(); log::trace!( "{:?} thread started: thread_id= {}", thread::current(), thread_id::get() ); Self { inner_request: Some(ModuleRequest::new(event)), assert_status_code: None, response: None, err_phantom: PhantomData, user_detail: None, } } pub fn set_request

(&mut self, request: P) where P: ToBytes, { let mut inner_request = self.inner_request.take().unwrap(); let bytes = request.into_bytes().unwrap(); inner_request = inner_request.payload(bytes); self.inner_request = Some(inner_request); } pub fn assert_error(&mut self) { self.assert_status_code = Some(StatusCode::Err); } pub fn assert_success(&mut self) { self.assert_status_code = Some(StatusCode::Ok); } pub async fn async_send(&mut self) { assert_eq!(self.inner_request.is_some(), true, "must set event"); let resp = EventDispatch::async_send(self.inner_request.take().unwrap()).await; self.response = Some(resp); } pub fn sync_send(&mut self) { let resp = EventDispatch::sync_send(self.inner_request.take().unwrap()); self.response = Some(resp); } pub fn parse(self) -> R where R: FromBytes, { let response = self.response.unwrap(); match response.parse::() { Ok(Ok(data)) => data, Ok(Err(e)) => panic!("parse failed: {:?}", e), Err(e) => panic!("Internal error: {:?}", e), } } pub fn error(self) -> Error { let response = self.response.unwrap(); assert_eq!(response.status_code, StatusCode::Err); >::try_from(response.payload) .unwrap() .into_inner() } } pub struct TesterContext { request: Option, status_code: StatusCode, response: Option, } impl std::default::Default for TesterContext { fn default() -> Self { Self { request: None, status_code: StatusCode::Ok, response: None, } } } pub trait TesterTrait { type Error: FromBytes + Debug; fn context(&mut self) -> &mut TesterContext; fn assert_error(&mut self) { self.context().status_code = StatusCode::Err; } fn assert_success(&mut self) { self.context().status_code = StatusCode::Ok; } fn set_event(&mut self, event: E) where E: Eq + Hash + Debug + Clone + Display, { init_sdk(); self.context().request = Some(ModuleRequest::new(event)); } fn set_payload

(&mut self, payload: P) where P: ToBytes, { let bytes = payload.into_bytes().unwrap(); let module_request = self.context().request.take().unwrap(); self.context().request = Some(module_request.payload(bytes)); } fn sync_send(&mut self) { let resp = EventDispatch::sync_send(self.context().request.take().unwrap()); self.context().response = Some(resp); } fn parse(&mut self) -> R where R: FromBytes, { let response = self.context().response.clone().unwrap(); match response.parse::() { Ok(Ok(data)) => data, Ok(Err(e)) => panic!("parse failed: {:?}", e), Err(e) => panic!("Internal error: {:?}", e), } } fn error(&mut self) -> Self::Error { let response = self.context().response.clone().unwrap(); assert_eq!(response.status_code, StatusCode::Err); >::try_from(response.payload) .unwrap() .into_inner() } fn login(&self) -> UserDetail { init_sdk(); let _ = EventDispatch::sync_send(ModuleRequest::new(SignOut)); let request = SignInRequest { email: valid_email(), password: valid_password(), }; let mut tester = Tester::::new(SignIn); tester.set_request(request); tester.sync_send(); tester.parse::() } fn logout(&self) { init_sdk(); let _ = EventDispatch::sync_send(ModuleRequest::new(SignOut)); } }