AppFlowy/frontend/rust-lib/flowy-folder/tests/workspace/script.rs

463 lines
14 KiB
Rust
Raw Normal View History

2022-07-19 06:11:29 +00:00
use flowy_folder::entities::view::{RepeatedViewIdPB, ViewIdPB};
use flowy_folder::entities::workspace::WorkspaceIdPB;
use flowy_folder::entities::{
2022-07-19 06:11:29 +00:00
app::{AppIdPB, CreateAppPayloadPB, UpdateAppPayloadPB},
trash::{RepeatedTrashPB, TrashIdPB, TrashType},
view::{CreateViewPayloadPB, UpdateViewPayloadPB},
workspace::{CreateWorkspacePayloadPB, RepeatedWorkspacePB},
2022-08-11 13:18:27 +00:00
SubViewDataTypePB,
2022-01-22 10:48:43 +00:00
};
use flowy_folder::entities::{
2022-07-19 06:11:29 +00:00
app::{AppPB, RepeatedAppPB},
trash::TrashPB,
2022-08-10 09:59:28 +00:00
view::{RepeatedViewPB, ViewDataTypePB, ViewPB},
2022-07-19 06:11:29 +00:00
workspace::WorkspacePB,
2022-03-12 13:06:15 +00:00
};
use flowy_folder::event_map::FolderEvent::*;
use flowy_folder::{errors::ErrorCode, services::folder_editor::FolderEditor};
2022-03-19 08:52:28 +00:00
use flowy_revision::disk::RevisionState;
use flowy_revision::REVISION_WRITE_INTERVAL_IN_MILLIS;
2022-07-19 03:31:04 +00:00
use flowy_sync::entities::text_block::DocumentPB;
2022-03-12 13:06:15 +00:00
use flowy_test::{event_builder::*, FlowySDKTest};
2022-01-22 10:48:43 +00:00
use std::{sync::Arc, time::Duration};
use tokio::time::sleep;
pub enum FolderScript {
// Workspace
ReadAllWorkspaces,
2022-03-12 13:06:15 +00:00
CreateWorkspace {
name: String,
desc: String,
},
// AssertWorkspaceRevisionJson(String),
2022-07-19 06:11:29 +00:00
AssertWorkspace(WorkspacePB),
2022-01-22 10:48:43 +00:00
ReadWorkspace(Option<String>),
// App
2022-03-12 13:06:15 +00:00
CreateApp {
name: String,
desc: String,
},
// AssertAppRevisionJson(String),
2022-07-19 06:11:29 +00:00
AssertApp(AppPB),
2022-01-22 10:48:43 +00:00
ReadApp(String),
2022-03-12 13:06:15 +00:00
UpdateApp {
name: Option<String>,
desc: Option<String>,
},
2022-01-22 10:48:43 +00:00
DeleteApp,
// View
2022-03-12 13:06:15 +00:00
CreateView {
name: String,
desc: String,
2022-08-10 09:59:28 +00:00
data_type: ViewDataTypePB,
2022-03-12 13:06:15 +00:00
},
2022-07-19 06:11:29 +00:00
AssertView(ViewPB),
2022-01-22 10:48:43 +00:00
ReadView(String),
2022-03-12 13:06:15 +00:00
UpdateView {
name: Option<String>,
desc: Option<String>,
},
2022-01-22 10:48:43 +00:00
DeleteView,
DeleteViews(Vec<String>),
// Trash
RestoreAppFromTrash,
RestoreViewFromTrash,
ReadTrash,
DeleteAllTrash,
// Sync
AssertCurrentRevId(i64),
AssertNextSyncRevId(Option<i64>),
2022-03-12 13:06:15 +00:00
AssertRevisionState {
rev_id: i64,
state: RevisionState,
},
2022-01-22 10:48:43 +00:00
}
pub struct FolderTest {
pub sdk: FlowySDKTest,
2022-07-19 06:11:29 +00:00
pub all_workspace: Vec<WorkspacePB>,
pub workspace: WorkspacePB,
pub app: AppPB,
pub view: ViewPB,
pub trash: Vec<TrashPB>,
2022-01-22 10:48:43 +00:00
// pub folder_editor:
}
impl FolderTest {
pub async fn new() -> Self {
let sdk = FlowySDKTest::default();
let _ = sdk.init_user().await;
let mut workspace = create_workspace(&sdk, "FolderWorkspace", "Folder test workspace").await;
let mut app = create_app(&sdk, &workspace.id, "Folder App", "Folder test app").await;
2022-03-12 01:30:13 +00:00
let view = create_view(
&sdk,
&app.id,
"Folder View",
"Folder test view",
2022-08-10 09:59:28 +00:00
ViewDataTypePB::TextBlock,
2022-03-12 01:30:13 +00:00
)
.await;
2022-07-19 06:11:29 +00:00
app.belongings = RepeatedViewPB {
2022-01-22 10:48:43 +00:00
items: vec![view.clone()],
};
2022-07-19 06:11:29 +00:00
workspace.apps = RepeatedAppPB {
2022-01-22 10:48:43 +00:00
items: vec![app.clone()],
};
Self {
sdk,
all_workspace: vec![],
workspace,
app,
view,
trash: vec![],
}
}
pub async fn run_scripts(&mut self, scripts: Vec<FolderScript>) {
for script in scripts {
self.run_script(script).await;
}
}
pub async fn run_script(&mut self, script: FolderScript) {
let sdk = &self.sdk;
2022-05-26 09:28:44 +00:00
let folder_editor: Arc<FolderEditor> = sdk.folder_manager.folder_editor().await;
2022-01-22 10:48:43 +00:00
let rev_manager = folder_editor.rev_manager();
2022-01-25 12:37:48 +00:00
let cache = rev_manager.revision_cache().await;
2022-01-22 10:48:43 +00:00
match script {
FolderScript::ReadAllWorkspaces => {
let all_workspace = read_workspace(sdk, None).await;
self.all_workspace = all_workspace;
2022-01-24 09:35:58 +00:00
}
2022-01-22 10:48:43 +00:00
FolderScript::CreateWorkspace { name, desc } => {
let workspace = create_workspace(sdk, &name, &desc).await;
self.workspace = workspace;
2022-01-24 09:35:58 +00:00
}
// FolderScript::AssertWorkspaceRevisionJson(expected_json) => {
// let workspace = read_workspace(sdk, Some(self.workspace.id.clone()))
// .await
// .pop()
// .unwrap();
// let workspace_revision: WorkspaceRevision = workspace.into();
// let json = serde_json::to_string(&workspace_revision).unwrap();
// assert_eq!(json, expected_json);
// }
2022-01-22 10:48:43 +00:00
FolderScript::AssertWorkspace(workspace) => {
2022-08-12 12:10:56 +00:00
assert_eq!(self.workspace, workspace, "Workspace not equal");
2022-01-24 09:35:58 +00:00
}
2022-01-22 10:48:43 +00:00
FolderScript::ReadWorkspace(workspace_id) => {
let workspace = read_workspace(sdk, workspace_id).await.pop().unwrap();
self.workspace = workspace;
2022-01-24 09:35:58 +00:00
}
2022-01-22 10:48:43 +00:00
FolderScript::CreateApp { name, desc } => {
2022-01-26 15:29:18 +00:00
let app = create_app(sdk, &self.workspace.id, &name, &desc).await;
2022-01-22 10:48:43 +00:00
self.app = app;
2022-01-24 09:35:58 +00:00
}
// FolderScript::AssertAppRevisionJson(expected_json) => {
// let app_revision: AppRevision = self.app.clone().into();
// let json = serde_json::to_string(&app_revision).unwrap();
// assert_eq!(json, expected_json);
// }
2022-01-22 10:48:43 +00:00
FolderScript::AssertApp(app) => {
2022-08-12 12:10:56 +00:00
assert_eq!(self.app, app, "App not equal");
2022-01-24 09:35:58 +00:00
}
2022-01-22 10:48:43 +00:00
FolderScript::ReadApp(app_id) => {
2022-01-24 09:35:58 +00:00
let app = read_app(sdk, &app_id).await;
2022-01-22 10:48:43 +00:00
self.app = app;
2022-01-24 09:35:58 +00:00
}
2022-01-22 10:48:43 +00:00
FolderScript::UpdateApp { name, desc } => {
2022-01-24 09:35:58 +00:00
update_app(sdk, &self.app.id, name, desc).await;
}
2022-01-22 10:48:43 +00:00
FolderScript::DeleteApp => {
2022-01-24 09:35:58 +00:00
delete_app(sdk, &self.app.id).await;
}
2022-01-22 10:48:43 +00:00
2022-03-12 13:06:15 +00:00
FolderScript::CreateView { name, desc, data_type } => {
let view = create_view(sdk, &self.app.id, &name, &desc, data_type).await;
2022-01-22 10:48:43 +00:00
self.view = view;
2022-01-24 09:35:58 +00:00
}
2022-01-22 10:48:43 +00:00
FolderScript::AssertView(view) => {
2022-08-12 12:10:56 +00:00
assert_eq!(self.view, view, "View not equal");
2022-01-24 09:35:58 +00:00
}
2022-01-22 10:48:43 +00:00
FolderScript::ReadView(view_id) => {
2022-02-24 12:36:24 +00:00
let view = read_view(sdk, &view_id).await;
2022-01-22 10:48:43 +00:00
self.view = view;
2022-01-24 09:35:58 +00:00
}
2022-01-22 10:48:43 +00:00
FolderScript::UpdateView { name, desc } => {
2022-01-24 09:35:58 +00:00
update_view(sdk, &self.view.id, name, desc).await;
}
2022-01-22 10:48:43 +00:00
FolderScript::DeleteView => {
2022-01-24 09:35:58 +00:00
delete_view(sdk, vec![self.view.id.clone()]).await;
}
2022-01-22 10:48:43 +00:00
FolderScript::DeleteViews(view_ids) => {
2022-01-24 09:35:58 +00:00
delete_view(sdk, view_ids).await;
}
2022-01-22 10:48:43 +00:00
FolderScript::RestoreAppFromTrash => {
2022-01-24 09:35:58 +00:00
restore_app_from_trash(sdk, &self.app.id).await;
}
2022-01-22 10:48:43 +00:00
FolderScript::RestoreViewFromTrash => {
2022-01-24 09:35:58 +00:00
restore_view_from_trash(sdk, &self.view.id).await;
}
2022-01-22 10:48:43 +00:00
FolderScript::ReadTrash => {
let mut trash = read_trash(sdk).await;
2022-01-22 10:48:43 +00:00
self.trash = trash.into_inner();
2022-01-24 09:35:58 +00:00
}
2022-01-22 10:48:43 +00:00
FolderScript::DeleteAllTrash => {
2022-01-24 09:35:58 +00:00
delete_all_trash(sdk).await;
2022-01-22 10:48:43 +00:00
self.trash = vec![];
2022-01-24 09:35:58 +00:00
}
2022-01-22 10:48:43 +00:00
FolderScript::AssertRevisionState { rev_id, state } => {
let record = cache.get(rev_id).await.unwrap();
2022-08-12 12:10:56 +00:00
assert_eq!(record.state, state, "Revision state is not match");
2022-01-22 10:48:43 +00:00
if let RevisionState::Ack = state {
// There is a defer action that writes the revisions to disk, so we wait here.
// Make sure everything is written.
sleep(Duration::from_millis(2 * REVISION_WRITE_INTERVAL_IN_MILLIS)).await;
}
2022-01-24 09:35:58 +00:00
}
2022-01-22 10:48:43 +00:00
FolderScript::AssertCurrentRevId(rev_id) => {
2022-01-26 15:29:18 +00:00
assert_eq!(rev_manager.rev_id(), rev_id, "Current rev_id is not match");
2022-01-24 09:35:58 +00:00
}
2022-01-22 10:48:43 +00:00
FolderScript::AssertNextSyncRevId(rev_id) => {
let next_revision = rev_manager.next_sync_revision().await.unwrap();
if rev_id.is_none() {
2022-01-24 09:35:58 +00:00
assert!(next_revision.is_none(), "Next revision should be None");
2022-01-22 10:48:43 +00:00
return;
}
2022-01-26 15:29:18 +00:00
let next_revision = next_revision
.unwrap_or_else(|| panic!("Expected Next revision is {}, but receive None", rev_id.unwrap()));
2022-02-18 15:04:55 +00:00
let mut notify = rev_manager.ack_notify();
let _ = notify.recv().await;
2022-08-12 12:10:56 +00:00
assert_eq!(next_revision.rev_id, rev_id.unwrap(), "Revision id not match");
2022-01-24 09:35:58 +00:00
}
2022-01-22 10:48:43 +00:00
}
}
}
pub fn invalid_workspace_name_test_case() -> Vec<(String, ErrorCode)> {
vec![
("".to_owned(), ErrorCode::WorkspaceNameInvalid),
("1234".repeat(100), ErrorCode::WorkspaceNameTooLong),
]
}
2022-03-12 13:06:15 +00:00
2022-07-19 06:11:29 +00:00
pub async fn create_workspace(sdk: &FlowySDKTest, name: &str, desc: &str) -> WorkspacePB {
let request = CreateWorkspacePayloadPB {
2022-03-12 13:06:15 +00:00
name: name.to_owned(),
desc: desc.to_owned(),
};
let workspace = FolderEventBuilder::new(sdk.clone())
.event(CreateWorkspace)
.payload(request)
.async_send()
.await
2022-07-19 06:11:29 +00:00
.parse::<WorkspacePB>();
2022-03-12 13:06:15 +00:00
workspace
}
2022-07-19 06:11:29 +00:00
pub async fn read_workspace(sdk: &FlowySDKTest, workspace_id: Option<String>) -> Vec<WorkspacePB> {
let request = WorkspaceIdPB { value: workspace_id };
let mut repeated_workspace = FolderEventBuilder::new(sdk.clone())
2022-03-12 13:06:15 +00:00
.event(ReadWorkspaces)
.payload(request.clone())
.async_send()
.await
2022-07-19 06:11:29 +00:00
.parse::<RepeatedWorkspacePB>();
2022-03-12 13:06:15 +00:00
let workspaces;
if let Some(workspace_id) = &request.value {
workspaces = repeated_workspace
.into_inner()
.into_iter()
.filter(|workspace| &workspace.id == workspace_id)
2022-07-19 06:11:29 +00:00
.collect::<Vec<WorkspacePB>>();
2022-03-12 13:06:15 +00:00
debug_assert_eq!(workspaces.len(), 1);
} else {
workspaces = repeated_workspace.items;
}
workspaces
}
2022-07-19 06:11:29 +00:00
pub async fn create_app(sdk: &FlowySDKTest, workspace_id: &str, name: &str, desc: &str) -> AppPB {
let create_app_request = CreateAppPayloadPB {
2022-03-12 13:06:15 +00:00
workspace_id: workspace_id.to_owned(),
name: name.to_string(),
desc: desc.to_string(),
color_style: Default::default(),
};
let app = FolderEventBuilder::new(sdk.clone())
.event(CreateApp)
.payload(create_app_request)
.async_send()
.await
2022-07-19 06:11:29 +00:00
.parse::<AppPB>();
2022-03-12 13:06:15 +00:00
app
}
2022-07-19 06:11:29 +00:00
pub async fn read_app(sdk: &FlowySDKTest, app_id: &str) -> AppPB {
let request = AppIdPB {
2022-03-12 13:06:15 +00:00
value: app_id.to_owned(),
};
let app = FolderEventBuilder::new(sdk.clone())
.event(ReadApp)
.payload(request)
.async_send()
.await
2022-07-19 06:11:29 +00:00
.parse::<AppPB>();
2022-03-12 13:06:15 +00:00
app
}
pub async fn update_app(sdk: &FlowySDKTest, app_id: &str, name: Option<String>, desc: Option<String>) {
2022-07-19 06:11:29 +00:00
let request = UpdateAppPayloadPB {
2022-03-12 13:06:15 +00:00
app_id: app_id.to_string(),
name,
desc,
color_style: None,
is_trash: None,
};
FolderEventBuilder::new(sdk.clone())
.event(UpdateApp)
.payload(request)
.async_send()
.await;
}
pub async fn delete_app(sdk: &FlowySDKTest, app_id: &str) {
2022-07-19 06:11:29 +00:00
let request = AppIdPB {
2022-03-12 13:06:15 +00:00
value: app_id.to_string(),
};
FolderEventBuilder::new(sdk.clone())
.event(DeleteApp)
.payload(request)
.async_send()
.await;
}
2022-08-10 09:59:28 +00:00
pub async fn create_view(
sdk: &FlowySDKTest,
app_id: &str,
name: &str,
desc: &str,
data_type: ViewDataTypePB,
) -> ViewPB {
2022-08-11 13:18:27 +00:00
let sub_data_type = match data_type {
ViewDataTypePB::TextBlock => None,
ViewDataTypePB::Database => Some(SubViewDataTypePB::Grid),
};
2022-07-19 06:11:29 +00:00
let request = CreateViewPayloadPB {
2022-03-12 13:06:15 +00:00
belong_to_id: app_id.to_string(),
name: name.to_string(),
desc: desc.to_string(),
thumbnail: None,
data_type,
2022-08-11 13:18:27 +00:00
sub_data_type,
2022-03-12 13:06:15 +00:00
plugin_type: 0,
2022-03-16 02:02:37 +00:00
data: vec![],
2022-03-12 13:06:15 +00:00
};
let view = FolderEventBuilder::new(sdk.clone())
.event(CreateView)
.payload(request)
.async_send()
.await
2022-07-19 06:11:29 +00:00
.parse::<ViewPB>();
2022-03-12 13:06:15 +00:00
view
}
2022-07-19 06:11:29 +00:00
pub async fn read_view(sdk: &FlowySDKTest, view_id: &str) -> ViewPB {
let view_id: ViewIdPB = view_id.into();
2022-03-12 13:06:15 +00:00
FolderEventBuilder::new(sdk.clone())
.event(ReadView)
.payload(view_id)
.async_send()
.await
2022-07-19 06:11:29 +00:00
.parse::<ViewPB>()
2022-03-12 13:06:15 +00:00
}
pub async fn update_view(sdk: &FlowySDKTest, view_id: &str, name: Option<String>, desc: Option<String>) {
2022-07-19 06:11:29 +00:00
let request = UpdateViewPayloadPB {
2022-03-12 13:06:15 +00:00
view_id: view_id.to_string(),
name,
desc,
thumbnail: None,
};
FolderEventBuilder::new(sdk.clone())
.event(UpdateView)
.payload(request)
.async_send()
.await;
}
pub async fn delete_view(sdk: &FlowySDKTest, view_ids: Vec<String>) {
2022-07-19 06:11:29 +00:00
let request = RepeatedViewIdPB { items: view_ids };
2022-03-12 13:06:15 +00:00
FolderEventBuilder::new(sdk.clone())
.event(DeleteView)
.payload(request)
.async_send()
.await;
}
#[allow(dead_code)]
2022-07-19 03:31:04 +00:00
pub async fn set_latest_view(sdk: &FlowySDKTest, view_id: &str) -> DocumentPB {
2022-07-19 06:11:29 +00:00
let view_id: ViewIdPB = view_id.into();
2022-03-12 13:06:15 +00:00
FolderEventBuilder::new(sdk.clone())
.event(SetLatestView)
.payload(view_id)
.async_send()
.await
2022-07-19 03:31:04 +00:00
.parse::<DocumentPB>()
2022-03-12 13:06:15 +00:00
}
2022-07-19 06:11:29 +00:00
pub async fn read_trash(sdk: &FlowySDKTest) -> RepeatedTrashPB {
2022-03-12 13:06:15 +00:00
FolderEventBuilder::new(sdk.clone())
.event(ReadTrash)
.async_send()
.await
2022-07-19 06:11:29 +00:00
.parse::<RepeatedTrashPB>()
2022-03-12 13:06:15 +00:00
}
pub async fn restore_app_from_trash(sdk: &FlowySDKTest, app_id: &str) {
2022-07-19 06:11:29 +00:00
let id = TrashIdPB {
2022-03-12 13:06:15 +00:00
id: app_id.to_owned(),
ty: TrashType::TrashApp,
};
FolderEventBuilder::new(sdk.clone())
.event(PutbackTrash)
.payload(id)
.async_send()
.await;
}
pub async fn restore_view_from_trash(sdk: &FlowySDKTest, view_id: &str) {
2022-07-19 06:11:29 +00:00
let id = TrashIdPB {
2022-03-12 13:06:15 +00:00
id: view_id.to_owned(),
ty: TrashType::TrashView,
};
FolderEventBuilder::new(sdk.clone())
.event(PutbackTrash)
.payload(id)
.async_send()
.await;
}
pub async fn delete_all_trash(sdk: &FlowySDKTest) {
FolderEventBuilder::new(sdk.clone())
.event(DeleteAllTrash)
.async_send()
.await;
}