use flowy_dispatch::prelude::{FromBytes, ToBytes}; use flowy_user::entities::UserDetail; use std::{ fmt::{Debug, Display}, hash::Hash, }; use crate::{ helper::valid_email, tester::{TesterContext, TesterTrait}, }; use flowy_user::errors::UserError; use flowy_workspace::errors::WorkspaceError; use std::marker::PhantomData; pub type WorkspaceTestBuilder = TestBuilder>; impl WorkspaceTestBuilder { pub fn new() -> Self { let builder = Self { tester: Box::new(FixedUserTester::::new()), user_detail: None, }; builder.login() } } pub type UserTestBuilder = TestBuilder>; impl UserTestBuilder { pub fn new() -> Self { Self { tester: Box::new(RandomUserTester::::new()), user_detail: None, } } } pub struct TestBuilder { pub tester: Box, pub user_detail: Option, } impl TestBuilder where T: TesterTrait, { pub fn login(mut self) -> Self { let user_detail = self.tester.login(); self.user_detail = Some(user_detail); self } pub fn logout(self) -> Self { self.tester.logout(); self } pub fn request

(mut self, request: P) -> Self where P: ToBytes, { self.tester.set_payload(request); self } pub fn event(mut self, event: E) -> Self where E: Eq + Hash + Debug + Clone + Display, { self.tester.set_event(event); self } pub fn sync_send(mut self) -> Self { self.tester.sync_send(); self } pub fn parse(mut self) -> R where R: FromBytes, { self.tester.parse::() } pub fn error(mut self) -> ::Error { self.tester.error() } pub fn assert_error(mut self) -> Self { self.tester.assert_error(); self } pub fn assert_success(mut self) -> Self { self.tester.assert_success(); self } } pub struct RandomUserTester { context: TesterContext, err_phantom: PhantomData, } impl RandomUserTester where Error: FromBytes + Debug, { pub fn new() -> Self { Self { context: TesterContext::default(), err_phantom: PhantomData, } } } impl TesterTrait for RandomUserTester where Error: FromBytes + Debug, { type Error = Error; fn mut_context(&mut self) -> &mut TesterContext { &mut self.context } fn context(&self) -> &TesterContext { &self.context } } pub struct FixedUserTester { context: TesterContext, err_phantom: PhantomData, } impl FixedUserTester where Error: FromBytes + Debug, { pub fn new() -> Self { Self { context: TesterContext::new(valid_email()), err_phantom: PhantomData, } } } impl TesterTrait for FixedUserTester where Error: FromBytes + Debug, { type Error = Error; fn mut_context(&mut self) -> &mut TesterContext { &mut self.context } fn context(&self) -> &TesterContext { &self.context } }