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

406 lines
12 KiB
Rust
Raw Normal View History

2021-12-08 09:33:22 +00:00
use crate::prelude::*;
2021-08-29 14:00:42 +00:00
use bytes::Bytes;
2021-12-06 06:41:09 +00:00
use flowy_core::{
2021-12-08 09:33:22 +00:00
entities::{
app::*,
trash::{RepeatedTrash, TrashIdentifier},
view::*,
workspace::{CreateWorkspaceRequest, QueryWorkspaceRequest, Workspace, *},
},
errors::{ErrorCode, WorkspaceError},
event::WorkspaceEvent::{CreateWorkspace, OpenWorkspace, *},
2021-12-06 06:41:09 +00:00
};
2021-12-08 09:33:22 +00:00
use flowy_document_infra::entities::doc::Doc;
use flowy_user::{
2021-09-04 08:53:58 +00:00
entities::{SignInRequest, SignUpRequest, UserProfile},
2021-09-16 04:35:55 +00:00
errors::UserError,
event::UserEvent::{SignIn, SignOut, SignUp},
};
2021-12-08 09:33:22 +00:00
use lib_dispatch::prelude::{EventDispatcher, ModuleRequest, ToBytes};
use lib_infra::{kv::KV, uuid};
2021-09-04 07:12:53 +00:00
use std::{fs, path::PathBuf, sync::Arc};
2021-07-16 15:18:12 +00:00
2021-12-08 09:33:22 +00:00
pub struct WorkspaceTest {
pub sdk: FlowySDKTest,
pub workspace: Workspace,
}
impl WorkspaceTest {
pub async fn new() -> Self {
let sdk = FlowySDKTest::setup();
let _ = sdk.init_user().await;
let workspace = create_workspace(&sdk, "Workspace", "").await;
open_workspace(&sdk, &workspace.id).await;
Self { sdk, workspace }
}
}
pub struct AppTest {
pub sdk: FlowySDKTest,
pub workspace: Workspace,
pub app: App,
}
impl AppTest {
pub async fn new() -> Self {
let sdk = FlowySDKTest::setup();
let _ = sdk.init_user().await;
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;
Self { sdk, workspace, app }
}
pub async fn move_app_to_trash(&self) {
let request = UpdateAppRequest {
app_id: self.app.id.clone(),
name: None,
desc: None,
color_style: None,
is_trash: Some(true),
};
update_app(&self.sdk, request).await;
}
}
pub struct ViewTest {
pub sdk: FlowySDKTest,
pub workspace: Workspace,
pub app: App,
pub view: View,
}
impl ViewTest {
pub async fn new(sdk: &FlowySDKTest) -> 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).await;
Self {
sdk: sdk.clone(),
workspace,
app,
view,
}
}
pub async fn delete_views(&self, view_ids: Vec<String>) {
let request = QueryViewRequest { view_ids };
delete_view(&self.sdk, request).await;
}
pub async fn delete_views_permanent(&self, view_ids: Vec<String>) {
let request = QueryViewRequest { view_ids };
delete_view(&self.sdk, request).await;
CoreModuleEventBuilder::new(self.sdk.clone())
.event(DeleteAll)
.async_send()
.await;
}
}
pub fn invalid_workspace_name_test_case() -> Vec<(String, ErrorCode)> {
vec![
("".to_owned(), ErrorCode::WorkspaceNameInvalid),
("1234".repeat(100), ErrorCode::WorkspaceNameTooLong),
]
}
pub async fn create_workspace(sdk: &FlowySDKTest, name: &str, desc: &str) -> Workspace {
let request = CreateWorkspaceRequest {
name: name.to_owned(),
desc: desc.to_owned(),
};
let workspace = CoreModuleEventBuilder::new(sdk.clone())
.event(CreateWorkspace)
.request(request)
.async_send()
.await
.parse::<Workspace>();
workspace
}
async fn open_workspace(sdk: &FlowySDKTest, workspace_id: &str) {
let request = QueryWorkspaceRequest {
workspace_id: Some(workspace_id.to_owned()),
};
let _ = CoreModuleEventBuilder::new(sdk.clone())
.event(OpenWorkspace)
.request(request)
.async_send()
.await;
}
pub async fn read_workspace(sdk: &FlowySDKTest, request: QueryWorkspaceRequest) -> Vec<Workspace> {
let repeated_workspace = CoreModuleEventBuilder::new(sdk.clone())
.event(ReadWorkspaces)
.request(request.clone())
.async_send()
.await
.parse::<RepeatedWorkspace>();
let workspaces;
if let Some(workspace_id) = &request.workspace_id {
workspaces = repeated_workspace
.into_inner()
.into_iter()
.filter(|workspace| &workspace.id == workspace_id)
.collect::<Vec<Workspace>>();
debug_assert_eq!(workspaces.len(), 1);
} else {
workspaces = repeated_workspace.items;
}
workspaces
}
pub async fn create_app(sdk: &FlowySDKTest, name: &str, desc: &str, workspace_id: &str) -> App {
let create_app_request = CreateAppRequest {
workspace_id: workspace_id.to_owned(),
name: name.to_string(),
desc: desc.to_string(),
color_style: Default::default(),
};
let app = CoreModuleEventBuilder::new(sdk.clone())
.event(CreateApp)
.request(create_app_request)
.async_send()
.await
.parse::<App>();
app
}
pub async fn delete_app(sdk: &FlowySDKTest, app_id: &str) {
let delete_app_request = AppIdentifier {
app_id: app_id.to_string(),
};
CoreModuleEventBuilder::new(sdk.clone())
.event(DeleteApp)
.request(delete_app_request)
.async_send()
.await;
}
pub async fn update_app(sdk: &FlowySDKTest, request: UpdateAppRequest) {
CoreModuleEventBuilder::new(sdk.clone())
.event(UpdateApp)
.request(request)
.async_send()
.await;
}
pub async fn read_app(sdk: &FlowySDKTest, request: QueryAppRequest) -> App {
let app = CoreModuleEventBuilder::new(sdk.clone())
.event(ReadApp)
.request(request)
.async_send()
.await
.parse::<App>();
app
}
pub async fn create_view_with_request(sdk: &FlowySDKTest, request: CreateViewRequest) -> View {
let view = CoreModuleEventBuilder::new(sdk.clone())
.event(CreateView)
.request(request)
.async_send()
.await
.parse::<View>();
view
}
pub async fn create_view(sdk: &FlowySDKTest, app_id: &str) -> View {
let request = CreateViewRequest {
belong_to_id: app_id.to_string(),
name: "View A".to_string(),
desc: "".to_string(),
thumbnail: Some("http://1.png".to_string()),
view_type: ViewType::Doc,
};
create_view_with_request(sdk, request).await
}
pub async fn update_view(sdk: &FlowySDKTest, request: UpdateViewRequest) {
CoreModuleEventBuilder::new(sdk.clone())
.event(UpdateView)
.request(request)
.async_send()
.await;
}
pub async fn read_view(sdk: &FlowySDKTest, request: QueryViewRequest) -> View {
CoreModuleEventBuilder::new(sdk.clone())
.event(ReadView)
.request(request)
.async_send()
.await
.parse::<View>()
}
pub async fn delete_view(sdk: &FlowySDKTest, request: QueryViewRequest) {
CoreModuleEventBuilder::new(sdk.clone())
.event(DeleteView)
.request(request)
.async_send()
.await;
}
pub async fn read_trash(sdk: &FlowySDKTest) -> RepeatedTrash {
CoreModuleEventBuilder::new(sdk.clone())
.event(ReadTrash)
.async_send()
.await
.parse::<RepeatedTrash>()
}
pub async fn putback_trash(sdk: &FlowySDKTest, id: TrashIdentifier) {
CoreModuleEventBuilder::new(sdk.clone())
.event(PutbackTrash)
.request(id)
.async_send()
.await;
}
pub async fn open_view(sdk: &FlowySDKTest, request: QueryViewRequest) -> Doc {
CoreModuleEventBuilder::new(sdk.clone())
.event(OpenView)
.request(request)
.async_send()
.await
.parse::<Doc>()
}
2021-07-16 15:18:12 +00:00
pub fn root_dir() -> String {
// https://doc.rust-lang.org/cargo/reference/environment-variables.html
2021-11-27 11:19:41 +00:00
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| "./".to_owned());
2021-07-16 15:18:12 +00:00
let mut path_buf = fs::canonicalize(&PathBuf::from(&manifest_dir)).unwrap();
path_buf.pop(); // rust-lib
path_buf.push("flowy-test");
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
}
2021-08-31 15:01:46 +00:00
pub fn random_email() -> String { format!("{}@appflowy.io", uuid()) }
2021-07-16 15:18:12 +00:00
2021-09-04 01:00:15 +00:00
pub fn login_email() -> String { "annie2@appflowy.io".to_string() }
2021-07-17 02:26:05 +00:00
pub fn login_password() -> String { "HelloWorld!123".to_string() }
2021-08-29 14:00:42 +00:00
2021-11-27 11:19:41 +00:00
const DEFAULT_WORKSPACE_NAME: &str = "My workspace";
const DEFAULT_WORKSPACE_DESC: &str = "This is your first workspace";
const DEFAULT_WORKSPACE: &str = "Default_Workspace";
2021-08-29 14:00:42 +00:00
2021-09-11 06:26:30 +00:00
#[allow(dead_code)]
2021-12-04 03:26:17 +00:00
pub(crate) fn create_default_workspace_if_need(dispatch: Arc<EventDispatcher>, user_id: &str) -> Result<(), UserError> {
2021-08-29 14:00:42 +00:00
let key = format!("{}{}", user_id, DEFAULT_WORKSPACE);
2021-09-03 04:44:48 +00:00
if KV::get_bool(&key).unwrap_or(false) {
2021-09-16 04:35:55 +00:00
return Err(UserError::internal());
2021-08-29 14:00:42 +00:00
}
2021-09-03 04:44:48 +00:00
KV::set_bool(&key, true);
2021-08-29 14:00:42 +00:00
let payload: Bytes = CreateWorkspaceRequest {
name: DEFAULT_WORKSPACE_NAME.to_string(),
desc: DEFAULT_WORKSPACE_DESC.to_string(),
}
.into_bytes()
.unwrap();
let request = ModuleRequest::new(CreateWorkspace).payload(payload);
2021-12-04 03:26:17 +00:00
let result = EventDispatcher::sync_send(dispatch.clone(), request)
.parse::<Workspace, WorkspaceError>()
2021-09-16 04:35:55 +00:00
.map_err(|e| UserError::internal().context(e))?;
2021-08-29 14:00:42 +00:00
2021-09-16 04:35:55 +00:00
let workspace = result.map_err(|e| UserError::internal().context(e))?;
2021-08-29 14:00:42 +00:00
let query: Bytes = QueryWorkspaceRequest {
2021-11-27 11:19:41 +00:00
workspace_id: Some(workspace.id),
2021-08-29 14:00:42 +00:00
}
.into_bytes()
.unwrap();
let request = ModuleRequest::new(OpenWorkspace).payload(query);
2021-12-04 03:26:17 +00:00
let _result = EventDispatcher::sync_send(dispatch, request)
.parse::<Workspace, WorkspaceError>()
2021-08-29 14:00:42 +00:00
.unwrap()
.unwrap();
Ok(())
}
pub struct SignUpContext {
2021-09-04 08:53:58 +00:00
pub user_profile: UserProfile,
pub password: String,
}
2021-12-04 03:26:17 +00:00
pub fn sign_up(dispatch: Arc<EventDispatcher>) -> SignUpContext {
let password = login_password();
let payload = SignUpRequest {
email: random_email(),
name: "app flowy".to_string(),
password: password.clone(),
}
.into_bytes()
.unwrap();
let request = ModuleRequest::new(SignUp).payload(payload);
2021-12-04 03:26:17 +00:00
let user_profile = EventDispatcher::sync_send(dispatch, request)
2021-09-04 08:53:58 +00:00
.parse::<UserProfile, UserError>()
.unwrap()
.unwrap();
SignUpContext { user_profile, password }
}
2021-12-04 03:26:17 +00:00
pub async fn async_sign_up(dispatch: Arc<EventDispatcher>) -> SignUpContext {
let password = login_password();
let payload = SignUpRequest {
email: random_email(),
name: "app flowy".to_string(),
password: password.clone(),
}
.into_bytes()
.unwrap();
let request = ModuleRequest::new(SignUp).payload(payload);
2021-12-04 03:26:17 +00:00
let user_profile = EventDispatcher::async_send(dispatch.clone(), request)
.await
.parse::<UserProfile, UserError>()
.unwrap()
.unwrap();
2021-09-11 06:26:30 +00:00
// let _ = create_default_workspace_if_need(dispatch.clone(), &user_profile.id);
2021-09-04 08:53:58 +00:00
SignUpContext { user_profile, password }
}
#[allow(dead_code)]
2021-12-04 03:26:17 +00:00
fn sign_in(dispatch: Arc<EventDispatcher>) -> UserProfile {
let payload = SignInRequest {
email: login_email(),
password: login_password(),
2021-11-07 08:58:06 +00:00
name: "rust".to_owned(),
}
.into_bytes()
.unwrap();
let request = ModuleRequest::new(SignIn).payload(payload);
2021-12-04 03:26:17 +00:00
EventDispatcher::sync_send(dispatch, request)
2021-09-04 08:53:58 +00:00
.parse::<UserProfile, UserError>()
.unwrap()
2021-11-27 11:19:41 +00:00
.unwrap()
}
#[allow(dead_code)]
2021-12-04 03:26:17 +00:00
fn logout(dispatch: Arc<EventDispatcher>) { let _ = EventDispatcher::sync_send(dispatch, ModuleRequest::new(SignOut)); }