helper.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. use crate::prelude::*;
  2. use flowy_core::{
  3. entities::{
  4. app::*,
  5. view::*,
  6. workspace::{CreateWorkspaceRequest, QueryWorkspaceRequest, Workspace},
  7. },
  8. event::WorkspaceEvent::{CreateWorkspace, OpenWorkspace, *},
  9. };
  10. use flowy_user::{
  11. entities::{SignInRequest, SignUpRequest, UserProfile},
  12. errors::FlowyError,
  13. event::UserEvent::{InitUser, SignIn, SignOut, SignUp},
  14. };
  15. use lib_dispatch::prelude::{EventDispatcher, ModuleRequest, ToBytes};
  16. use lib_infra::uuid_string;
  17. use std::{fs, path::PathBuf, sync::Arc};
  18. pub struct ViewTest {
  19. pub sdk: FlowySDKTest,
  20. pub workspace: Workspace,
  21. pub app: App,
  22. pub view: View,
  23. }
  24. impl ViewTest {
  25. pub async fn new(sdk: &FlowySDKTest) -> Self {
  26. let workspace = create_workspace(&sdk, "Workspace", "").await;
  27. open_workspace(&sdk, &workspace.id).await;
  28. let app = create_app(&sdk, "App", "AppFlowy GitHub Project", &workspace.id).await;
  29. let view = create_view(&sdk, &app.id).await;
  30. Self {
  31. sdk: sdk.clone(),
  32. workspace,
  33. app,
  34. view,
  35. }
  36. }
  37. }
  38. async fn create_workspace(sdk: &FlowySDKTest, name: &str, desc: &str) -> Workspace {
  39. let request = CreateWorkspaceRequest {
  40. name: name.to_owned(),
  41. desc: desc.to_owned(),
  42. };
  43. let workspace = FolderEventBuilder::new(sdk.clone())
  44. .event(CreateWorkspace)
  45. .request(request)
  46. .async_send()
  47. .await
  48. .parse::<Workspace>();
  49. workspace
  50. }
  51. async fn open_workspace(sdk: &FlowySDKTest, workspace_id: &str) {
  52. let request = QueryWorkspaceRequest {
  53. workspace_id: Some(workspace_id.to_owned()),
  54. };
  55. let _ = FolderEventBuilder::new(sdk.clone())
  56. .event(OpenWorkspace)
  57. .request(request)
  58. .async_send()
  59. .await;
  60. }
  61. async fn create_app(sdk: &FlowySDKTest, name: &str, desc: &str, workspace_id: &str) -> App {
  62. let create_app_request = CreateAppRequest {
  63. workspace_id: workspace_id.to_owned(),
  64. name: name.to_string(),
  65. desc: desc.to_string(),
  66. color_style: Default::default(),
  67. };
  68. let app = FolderEventBuilder::new(sdk.clone())
  69. .event(CreateApp)
  70. .request(create_app_request)
  71. .async_send()
  72. .await
  73. .parse::<App>();
  74. app
  75. }
  76. async fn create_view(sdk: &FlowySDKTest, app_id: &str) -> View {
  77. let request = CreateViewRequest {
  78. belong_to_id: app_id.to_string(),
  79. name: "View A".to_string(),
  80. desc: "".to_string(),
  81. thumbnail: Some("http://1.png".to_string()),
  82. view_type: ViewType::Doc,
  83. };
  84. let view = FolderEventBuilder::new(sdk.clone())
  85. .event(CreateView)
  86. .request(request)
  87. .async_send()
  88. .await
  89. .parse::<View>();
  90. view
  91. }
  92. pub fn root_dir() -> String {
  93. // https://doc.rust-lang.org/cargo/reference/environment-variables.html
  94. let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| "./".to_owned());
  95. let mut path_buf = fs::canonicalize(&PathBuf::from(&manifest_dir)).unwrap();
  96. path_buf.pop(); // rust-lib
  97. path_buf.push("temp");
  98. path_buf.push("flowy");
  99. let root_dir = path_buf.to_str().unwrap().to_string();
  100. if !std::path::Path::new(&root_dir).exists() {
  101. std::fs::create_dir_all(&root_dir).unwrap();
  102. }
  103. root_dir
  104. }
  105. pub fn random_email() -> String { format!("{}@appflowy.io", uuid_string()) }
  106. pub fn login_email() -> String { "[email protected]".to_string() }
  107. pub fn login_password() -> String { "HelloWorld!123".to_string() }
  108. pub struct SignUpContext {
  109. pub user_profile: UserProfile,
  110. pub password: String,
  111. }
  112. pub fn sign_up(dispatch: Arc<EventDispatcher>) -> SignUpContext {
  113. let password = login_password();
  114. let payload = SignUpRequest {
  115. email: random_email(),
  116. name: "app flowy".to_string(),
  117. password: password.clone(),
  118. }
  119. .into_bytes()
  120. .unwrap();
  121. let request = ModuleRequest::new(SignUp).payload(payload);
  122. let user_profile = EventDispatcher::sync_send(dispatch, request)
  123. .parse::<UserProfile, FlowyError>()
  124. .unwrap()
  125. .unwrap();
  126. SignUpContext { user_profile, password }
  127. }
  128. pub async fn async_sign_up(dispatch: Arc<EventDispatcher>) -> SignUpContext {
  129. let password = login_password();
  130. let payload = SignUpRequest {
  131. email: random_email(),
  132. name: "app flowy".to_string(),
  133. password: password.clone(),
  134. }
  135. .into_bytes()
  136. .unwrap();
  137. let request = ModuleRequest::new(SignUp).payload(payload);
  138. let user_profile = EventDispatcher::async_send(dispatch.clone(), request)
  139. .await
  140. .parse::<UserProfile, FlowyError>()
  141. .unwrap()
  142. .unwrap();
  143. // let _ = create_default_workspace_if_need(dispatch.clone(), &user_profile.id);
  144. SignUpContext { user_profile, password }
  145. }
  146. pub async fn init_user_setting(dispatch: Arc<EventDispatcher>) {
  147. let request = ModuleRequest::new(InitUser);
  148. let _ = EventDispatcher::async_send(dispatch.clone(), request).await;
  149. }
  150. #[allow(dead_code)]
  151. fn sign_in(dispatch: Arc<EventDispatcher>) -> UserProfile {
  152. let payload = SignInRequest {
  153. email: login_email(),
  154. password: login_password(),
  155. name: "rust".to_owned(),
  156. }
  157. .into_bytes()
  158. .unwrap();
  159. let request = ModuleRequest::new(SignIn).payload(payload);
  160. EventDispatcher::sync_send(dispatch, request)
  161. .parse::<UserProfile, FlowyError>()
  162. .unwrap()
  163. .unwrap()
  164. }
  165. #[allow(dead_code)]
  166. fn logout(dispatch: Arc<EventDispatcher>) { let _ = EventDispatcher::sync_send(dispatch, ModuleRequest::new(SignOut)); }