| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 | use crate::prelude::*;use flowy_folder::entities::WorkspaceIdPB;use flowy_folder::{  entities::{    app::*,    view::*,    workspace::{CreateWorkspacePayloadPB, WorkspacePB},  },  event_map::FolderEvent::{CreateWorkspace, OpenWorkspace, *},};use flowy_user::{  entities::{SignInPayloadPB, SignUpPayloadPB, UserProfilePB},  errors::FlowyError,  event_map::UserEvent::{InitUser, SignIn, SignOut, SignUp},};use lib_dispatch::prelude::{AFPluginDispatcher, AFPluginRequest, ToBytes};use std::{fs, path::PathBuf, sync::Arc};pub struct ViewTest {  pub sdk: FlowySDKTest,  pub workspace: WorkspacePB,  pub app: AppPB,  pub view: ViewPB,}impl ViewTest {  #[allow(dead_code)]  pub async fn new(sdk: &FlowySDKTest, layout: ViewLayoutTypePB, data: Vec<u8>) -> Self {    let workspace = create_workspace(sdk, "Workspace", "").await;    open_workspace(sdk, &workspace.id).await;    let app = create_app(sdk, "App", "AppFlowy GitHub Project", &workspace.id).await;    let view = create_view(sdk, &app.id, layout, data).await;    Self {      sdk: sdk.clone(),      workspace,      app,      view,    }  }  pub async fn new_grid_view(sdk: &FlowySDKTest, data: Vec<u8>) -> Self {    Self::new(sdk, ViewLayoutTypePB::Grid, data).await  }  pub async fn new_board_view(sdk: &FlowySDKTest, data: Vec<u8>) -> Self {    Self::new(sdk, ViewLayoutTypePB::Board, data).await  }  pub async fn new_calendar_view(sdk: &FlowySDKTest, data: Vec<u8>) -> Self {    Self::new(sdk, ViewLayoutTypePB::Calendar, data).await  }  pub async fn new_document_view(sdk: &FlowySDKTest) -> Self {    Self::new(sdk, ViewLayoutTypePB::Document, vec![]).await  }}async fn create_workspace(sdk: &FlowySDKTest, name: &str, desc: &str) -> WorkspacePB {  let request = CreateWorkspacePayloadPB {    name: name.to_owned(),    desc: desc.to_owned(),  };  FolderEventBuilder::new(sdk.clone())    .event(CreateWorkspace)    .payload(request)    .async_send()    .await    .parse::<WorkspacePB>()}async fn open_workspace(sdk: &FlowySDKTest, workspace_id: &str) {  let payload = WorkspaceIdPB {    value: Some(workspace_id.to_owned()),  };  let _ = FolderEventBuilder::new(sdk.clone())    .event(OpenWorkspace)    .payload(payload)    .async_send()    .await;}async fn create_app(sdk: &FlowySDKTest, name: &str, desc: &str, workspace_id: &str) -> AppPB {  let create_app_request = CreateAppPayloadPB {    workspace_id: workspace_id.to_owned(),    name: name.to_string(),    desc: desc.to_string(),    color_style: Default::default(),  };  FolderEventBuilder::new(sdk.clone())    .event(CreateApp)    .payload(create_app_request)    .async_send()    .await    .parse::<AppPB>()}async fn create_view(  sdk: &FlowySDKTest,  app_id: &str,  layout: ViewLayoutTypePB,  data: Vec<u8>,) -> ViewPB {  let payload = CreateViewPayloadPB {    belong_to_id: app_id.to_string(),    name: "View A".to_string(),    desc: "".to_string(),    thumbnail: Some("http://1.png".to_string()),    layout,    initial_data: data,    ext: Default::default(),  };  FolderEventBuilder::new(sdk.clone())    .event(CreateView)    .payload(payload)    .async_send()    .await    .parse::<ViewPB>()}pub fn root_dir() -> String {  // https://doc.rust-lang.org/cargo/reference/environment-variables.html  let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| "./".to_owned());  let mut path_buf = fs::canonicalize(&PathBuf::from(&manifest_dir)).unwrap();  path_buf.pop(); // rust-lib  path_buf.push("temp");  path_buf.push("flowy");  let root_dir = path_buf.to_str().unwrap().to_string();  if !std::path::Path::new(&root_dir).exists() {    std::fs::create_dir_all(&root_dir).unwrap();  }  root_dir}pub fn random_email() -> String {  format!("{}@appflowy.io", nanoid!(20))}pub fn login_email() -> String {  "[email protected]".to_string()}pub fn login_password() -> String {  "HelloWorld!123".to_string()}pub struct SignUpContext {  pub user_profile: UserProfilePB,  pub password: String,}pub fn sign_up(dispatch: Arc<AFPluginDispatcher>) -> SignUpContext {  let password = login_password();  let payload = SignUpPayloadPB {    email: random_email(),    name: "app flowy".to_string(),    password: password.clone(),  }  .into_bytes()  .unwrap();  let request = AFPluginRequest::new(SignUp).payload(payload);  let user_profile = AFPluginDispatcher::sync_send(dispatch, request)    .parse::<UserProfilePB, FlowyError>()    .unwrap()    .unwrap();  SignUpContext {    user_profile,    password,  }}pub async fn async_sign_up(dispatch: Arc<AFPluginDispatcher>) -> SignUpContext {  let password = login_password();  let email = random_email();  let payload = SignUpPayloadPB {    email,    name: "app flowy".to_string(),    password: password.clone(),  }  .into_bytes()  .unwrap();  let request = AFPluginRequest::new(SignUp).payload(payload);  let user_profile = AFPluginDispatcher::async_send(dispatch.clone(), request)    .await    .parse::<UserProfilePB, FlowyError>()    .unwrap()    .unwrap();  // let _ = create_default_workspace_if_need(dispatch.clone(), &user_profile.id);  SignUpContext {    user_profile,    password,  }}pub async fn init_user_setting(dispatch: Arc<AFPluginDispatcher>) {  let request = AFPluginRequest::new(InitUser);  let _ = AFPluginDispatcher::async_send(dispatch.clone(), request).await;}#[allow(dead_code)]fn sign_in(dispatch: Arc<AFPluginDispatcher>) -> UserProfilePB {  let payload = SignInPayloadPB {    email: login_email(),    password: login_password(),    name: "rust".to_owned(),  }  .into_bytes()  .unwrap();  let request = AFPluginRequest::new(SignIn).payload(payload);  AFPluginDispatcher::sync_send(dispatch, request)    .parse::<UserProfilePB, FlowyError>()    .unwrap()    .unwrap()}#[allow(dead_code)]fn logout(dispatch: Arc<AFPluginDispatcher>) {  let _ = AFPluginDispatcher::sync_send(dispatch, AFPluginRequest::new(SignOut));}
 |