276 lines
7.3 KiB
Rust
Raw Normal View History

use crate::prelude::*;
2021-09-11 14:26:30 +08:00
use flowy_document::entities::doc::Doc;
2021-07-20 14:03:21 +08:00
use flowy_workspace::{
2021-10-16 21:38:50 +08:00
entities::{
app::*,
trash::{RepeatedTrash, TrashIdentifier},
view::*,
workspace::*,
},
2021-07-20 15:51:49 +08:00
event::WorkspaceEvent::*,
2021-07-20 14:03:21 +08:00
};
2021-09-11 14:26:30 +08:00
pub struct WorkspaceTest {
pub sdk: FlowyTestSDK,
pub workspace: Workspace,
}
impl WorkspaceTest {
pub async fn new() -> Self {
let test = FlowyTest::setup();
let _ = test.init_user().await;
let workspace = create_workspace(&test.sdk, "Workspace", "").await;
open_workspace(&test.sdk, &workspace.id).await;
Self {
sdk: test.sdk,
workspace,
}
2021-09-11 14:26:30 +08:00
}
}
pub struct AppTest {
pub sdk: FlowyTestSDK,
pub workspace: Workspace,
pub app: App,
}
impl AppTest {
pub async fn new() -> Self {
let test = FlowyTest::setup();
let _ = test.init_user().await;
let workspace = create_workspace(&test.sdk, "Workspace", "").await;
open_workspace(&test.sdk, &workspace.id).await;
let app = create_app(&test.sdk, "App", "AppFlowy Github Project", &workspace.id).await;
Self {
sdk: test.sdk,
workspace,
app,
}
2021-09-11 14:26:30 +08:00
}
pub async fn move_app_to_trash(&self) {
2021-09-11 14:26:30 +08:00
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;
2021-09-11 14:26:30 +08:00
}
}
pub struct ViewTest {
2021-09-11 14:26:30 +08:00
pub sdk: FlowyTestSDK,
pub workspace: Workspace,
pub app: App,
pub view: View,
}
impl ViewTest {
pub async fn new(test: &FlowyTest) -> Self {
let workspace = create_workspace(&test.sdk, "Workspace", "").await;
open_workspace(&test.sdk, &workspace.id).await;
let app = create_app(&test.sdk, "App", "AppFlowy Github Project", &workspace.id).await;
let view = create_view(&test.sdk, &app.id).await;
Self {
sdk: test.sdk.clone(),
workspace,
app,
view,
}
2021-09-11 14:26:30 +08:00
}
pub async fn delete_views(&self, view_ids: Vec<String>) {
let request = QueryViewRequest { view_ids };
delete_view(&self.sdk, request).await;
2021-09-11 14:26:30 +08:00
}
2021-10-18 22:46:05 +08:00
pub async fn delete_views_permanent(&self, view_ids: Vec<String>) {
let request = QueryViewRequest { view_ids };
delete_view(&self.sdk, request).await;
2021-10-18 22:46:05 +08:00
FlowyWorkspaceTest::new(self.sdk.clone())
.event(DeleteAll)
.async_send()
.await;
}
2021-09-11 14:26:30 +08:00
}
pub fn invalid_workspace_name_test_case() -> Vec<String> {
vec!["", "1234".repeat(100).as_str()]
.iter()
.map(|s| s.to_string())
.collect::<Vec<_>>()
}
2021-07-20 14:03:21 +08:00
pub async fn create_workspace(sdk: &FlowyTestSDK, name: &str, desc: &str) -> Workspace {
2021-07-20 14:03:21 +08:00
let request = CreateWorkspaceRequest {
name: name.to_owned(),
desc: desc.to_owned(),
};
2021-09-11 14:26:30 +08:00
let workspace = FlowyWorkspaceTest::new(sdk.clone())
2021-09-03 12:44:48 +08:00
.event(CreateWorkspace)
.request(request)
.async_send()
.await
2021-09-03 12:44:48 +08:00
.parse::<Workspace>();
2021-09-02 23:32:37 +08:00
workspace
2021-07-20 14:03:21 +08:00
}
async fn open_workspace(sdk: &FlowyTestSDK, workspace_id: &str) {
2021-09-11 14:26:30 +08:00
let request = QueryWorkspaceRequest {
workspace_id: Some(workspace_id.to_owned()),
};
let _ = FlowyWorkspaceTest::new(sdk.clone())
.event(OpenWorkspace)
.request(request)
.async_send()
.await;
2021-09-11 14:26:30 +08:00
}
2021-10-18 22:46:05 +08:00
pub async fn read_workspace(sdk: &FlowyTestSDK, request: QueryWorkspaceRequest) -> Option<Workspace> {
2021-09-11 14:26:30 +08:00
let mut repeated_workspace = FlowyWorkspaceTest::new(sdk.clone())
.event(ReadWorkspaces)
.request(request.clone())
2021-10-18 22:46:05 +08:00
.async_send()
.await
2021-08-29 22:00:42 +08:00
.parse::<RepeatedWorkspace>();
2021-09-13 15:51:13 +08:00
let mut workspaces;
2021-09-11 14:26:30 +08:00
if let Some(workspace_id) = &request.workspace_id {
workspaces = repeated_workspace
.into_inner()
2021-09-11 14:26:30 +08:00
.into_iter()
.filter(|workspace| &workspace.id == workspace_id)
.collect::<Vec<Workspace>>();
debug_assert_eq!(workspaces.len(), 1);
} else {
workspaces = repeated_workspace.items;
}
workspaces.drain(..1).collect::<Vec<Workspace>>().pop()
}
pub async fn create_app(sdk: &FlowyTestSDK, name: &str, desc: &str, workspace_id: &str) -> App {
2021-07-20 14:03:21 +08:00
let create_app_request = CreateAppRequest {
2021-08-29 22:00:42 +08:00
workspace_id: workspace_id.to_owned(),
2021-07-20 14:03:21 +08:00
name: name.to_string(),
desc: desc.to_string(),
color_style: Default::default(),
};
2021-09-11 14:26:30 +08:00
let app = FlowyWorkspaceTest::new(sdk.clone())
2021-07-20 14:03:21 +08:00
.event(CreateApp)
.request(create_app_request)
.async_send()
.await
2021-07-20 14:03:21 +08:00
.parse::<App>();
app
}
2021-10-18 22:46:05 +08:00
pub async fn delete_app(sdk: &FlowyTestSDK, app_id: &str) {
let delete_app_request = DeleteAppRequest {
app_id: app_id.to_string(),
};
2021-07-20 14:03:21 +08:00
2021-09-11 14:26:30 +08:00
FlowyWorkspaceTest::new(sdk.clone())
2021-09-04 15:12:53 +08:00
.event(DeleteApp)
.request(delete_app_request)
2021-10-18 22:46:05 +08:00
.async_send()
.await;
2021-07-20 14:03:21 +08:00
}
2021-07-20 15:51:49 +08:00
pub async fn update_app(sdk: &FlowyTestSDK, request: UpdateAppRequest) {
FlowyWorkspaceTest::new(sdk.clone())
.event(UpdateApp)
.request(request)
.async_send()
.await;
2021-09-04 15:12:53 +08:00
}
2021-10-18 22:46:05 +08:00
pub async fn read_app(sdk: &FlowyTestSDK, request: QueryAppRequest) -> App {
2021-09-11 14:26:30 +08:00
let app = FlowyWorkspaceTest::new(sdk.clone())
.event(ReadApp)
.request(request)
2021-10-18 22:46:05 +08:00
.async_send()
.await
.parse::<App>();
2021-07-20 15:51:49 +08:00
app
}
pub async fn create_view_with_request(sdk: &FlowyTestSDK, request: CreateViewRequest) -> View {
2021-09-11 14:26:30 +08:00
let view = FlowyWorkspaceTest::new(sdk.clone())
2021-07-20 15:51:49 +08:00
.event(CreateView)
.request(request)
.async_send()
.await
2021-07-20 15:51:49 +08:00
.parse::<View>();
view
}
pub async fn create_view(sdk: &FlowyTestSDK, app_id: &str) -> View {
let request = CreateViewRequest {
2021-09-11 14:26:30 +08:00
belong_to_id: app_id.to_string(),
name: "View A".to_string(),
desc: "".to_string(),
2021-09-02 23:32:37 +08:00
thumbnail: Some("http://1.png".to_string()),
view_type: ViewType::Doc,
};
create_view_with_request(sdk, request).await
}
pub async fn update_view(sdk: &FlowyTestSDK, request: UpdateViewRequest) {
FlowyWorkspaceTest::new(sdk.clone())
.event(UpdateView)
.request(request)
.async_send()
.await;
2021-09-04 15:12:53 +08:00
}
pub async fn read_view(sdk: &FlowyTestSDK, request: QueryViewRequest) -> View {
2021-09-11 14:26:30 +08:00
FlowyWorkspaceTest::new(sdk.clone())
.event(ReadView)
.request(request)
.async_send()
.await
.parse::<View>()
}
2021-09-11 14:26:30 +08:00
pub async fn delete_view(sdk: &FlowyTestSDK, request: QueryViewRequest) {
FlowyWorkspaceTest::new(sdk.clone())
.event(DeleteView)
.request(request)
.async_send()
.await;
}
2021-10-16 21:38:50 +08:00
pub async fn read_trash(sdk: &FlowyTestSDK) -> RepeatedTrash {
FlowyWorkspaceTest::new(sdk.clone())
.event(ReadTrash)
.async_send()
.await
.parse::<RepeatedTrash>()
}
pub async fn putback_trash(sdk: &FlowyTestSDK, id: TrashIdentifier) {
FlowyWorkspaceTest::new(sdk.clone())
.event(PutbackTrash)
.request(id)
.async_send()
.await;
}
pub async fn open_view(sdk: &FlowyTestSDK, request: QueryViewRequest) -> Doc {
2021-09-11 14:26:30 +08:00
FlowyWorkspaceTest::new(sdk.clone())
.event(OpenView)
.request(request)
.async_send()
.await
2021-09-11 14:26:30 +08:00
.parse::<Doc>()
}