Skip to content

Commit

Permalink
test: add default workspace test
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 committed Aug 27, 2024
1 parent a67310a commit 8c27ee2
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 50 deletions.
2 changes: 0 additions & 2 deletions libs/workspace-template/src/document/getting_started.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ use crate::{gen_view_id, TemplateData, TemplateObjectId, WorkspaceTemplate};
// |-- Desktop guide (document)
// |-- Mobile guide (document)
// |-- To-Dos (board)
// |-- Inbox (grid)
// |-- Ask AI (ai chat)
// |-- Shared (space)
// |-- ... (empty)
// Note: Update the folder structure above if you changed the code below
Expand Down
8 changes: 4 additions & 4 deletions libs/workspace-template/src/tests/getting_started_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::document::getting_started::*;
use crate::TemplateData;
use crate::TemplateObjectId;
use crate::{hierarchy_builder::WorkspaceViewBuilder, WorkspaceTemplate};
use collab::preclude::uuid_v4;
use collab_database::database::DatabaseData;
use collab_database::entity::CreateDatabaseParams;
use collab_document::document_data::generate_id;
Expand All @@ -13,8 +15,6 @@ use tokio::sync::RwLock;
#[cfg(test)]
mod tests {

use crate::TemplateObjectId;

use super::*;

#[tokio::test]
Expand Down Expand Up @@ -45,7 +45,7 @@ mod tests {

async fn test_document_json(json_str: &str) {
let template = GettingStartedTemplate;
let object_id = generate_id();
let object_id = uuid_v4().to_string();
let result = template
.create_document_from_json(object_id.clone(), json_str)
.await;
Expand All @@ -65,7 +65,7 @@ mod tests {

async fn test_database_json(json_str: &str) -> Vec<TemplateData> {
let template = GettingStartedTemplate;
let object_id = generate_id();
let object_id = uuid_v4().to_string();
let database_data = serde_json::from_str::<DatabaseData>(json_str).unwrap();
let create_database_params =
CreateDatabaseParams::from_database_data(database_data, Some(object_id.clone()));
Expand Down
3 changes: 2 additions & 1 deletion tests/collab/storage_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,8 @@ async fn insert_folder_data_success_test() {
.await
.unwrap();

assert_eq!(templates.len(), 2);
// 2 spaces + 3 documents + 2 databases + 5rows
assert_eq!(templates.len(), 12);
for (index, template) in templates.into_iter().enumerate() {
if index == 0 {
assert_eq!(template.collab_type, CollabType::Document);
Expand Down
127 changes: 127 additions & 0 deletions tests/workspace/default_user_workspace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
use std::collections::HashSet;

use app_error::ErrorCode;
use client_api::entity::{
AccountLink, CreateTemplateCategoryParams, CreateTemplateParams, PublishCollabItem,
PublishCollabMetadata, TemplateCategoryType, UpdateTemplateCategoryParams, UpdateTemplateParams,
};
use client_api_test::*;
use collab::core::collab::DataSource;
use collab::core::origin::CollabOrigin;
use collab_document::document::Document;
use collab_entity::CollabType;
use database_entity::dto::{QueryCollab, QueryCollabParams};
use sqlx::Database;
use uuid::Uuid;

// |-- General (space)
// |-- Getting started (document)
// |-- Desktop guide (document)
// |-- Mobile guide (document)
// |-- To-Dos (board)
// |-- Shared (space)
// |-- ... (empty)
#[tokio::test]
async fn get_user_default_workspace_test() {
let email = generate_unique_email();
let password = "Hello!123#";
let c = localhost_client();
c.sign_up(&email, password).await.unwrap();
let mut test_client = TestClient::new_user().await;
let folder = test_client.get_user_folder().await;

let workspace_id = test_client.workspace_id().await;
let views = folder.get_views_belong_to(&workspace_id);

// 2 spaces
assert_eq!(views.len(), 2);

// the first view is the general space
assert_eq!(views[0].name, "General");

// and it contains 1 document and 1 board
let general_space_views = folder.get_views_belong_to(&views[0].id);
assert_eq!(general_space_views.len(), 2);

// the first view is the getting started document, and contains 2 sub views
{
let getting_started_view = general_space_views[0];
assert_eq!(getting_started_view.name, "Getting started");
assert_eq!(getting_started_view.layout, ViewLayout::Document);
assert_eq!(getting_started_view.icon, "⭐️");

let getting_started_document =
get_document_collab_from_remote(&mut test_client, workspace_id, &getting_started_view.id)
.await;
let document_data = getting_started_document.get_document_data().unwrap();
print!(
"getting_started blocks len {:?}",
document_data.blocks.len()
);

let getting_started_sub_views = folder.get_views_belong_to(&getting_started_view.id);
assert_eq!(getting_started_sub_views.len(), 2);

let desktop_guide_view = getting_started_sub_views[0];
assert_eq!(desktop_guide_view.name, "Desktop guide");
assert_eq!(desktop_guide_view.layout, ViewLayout::Document);
assert_eq!(desktop_guide_view.icon, "📎");
let desktop_guide_document =
get_document_collab_from_remote(&mut test_client, workspace_id, &desktop_guide_view.id).await;
let desktop_guide_document_data = desktop_guide_document.get_document_data().unwrap();
print!(
"desktop_guide blocks len {:?}",
desktop_guide_document_data.blocks.len()
);

let mobile_guide_view = getting_started_sub_views[1];
assert_eq!(mobile_guide_view.name, "Mobile guide");
assert_eq!(mobile_guide_view.layout, ViewLayout::Document);
assert_eq!(mobile_guide_view.icon, "");
let mobile_guide_document =
get_document_collab_from_remote(&mut test_client, workspace_id, &mobile_guide_view.id).await;
let mobile_guide_document_data = mobile_guide_document.get_document_data().unwrap();
print!(
"mobile_guide blocks len {:?}",
mobile_guide_document_data.blocks.len()
);
}

// the second view is the to-dos board, and contains 0 sub views
{
let to_dos_view = general_space_views[1];
assert_eq!(to_dos_view.name, "To-Dos");
assert_eq!(to_dos_view.layout, ViewLayout::Board);
assert_eq!(to_dos_view.icon, "✅");

let to_dos_sub_views = folder.get_views_belong_to(&to_dos_view.id);
assert_eq!(to_dos_sub_views.len(), 0);
}

// shared space is empty
assert_eq!(views[1].name, "Shared");
let shared_space_views = folder.get_views_belong_to(&views[1].id);
assert_eq!(shared_space_views.len(), 0);
}

async fn get_document_collab_from_remote(
test_client: &mut TestClient,
workspace_id: String,
document_id: &str,
) -> Document {
let params = QueryCollabParams {
workspace_id,
inner: QueryCollab {
object_id: document_id.to_string(),
collab_type: CollabType::Document,
},
};
let resp = test_client.get_collab(params).await.unwrap();
Document::open_with_options(
CollabOrigin::Empty,
DataSource::DocStateV1(resp.encode_collab.doc_state.to_vec()),
document_id,
vec![],
)
.unwrap()
}
1 change: 1 addition & 0 deletions tests/workspace/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod default_user_workspace;
mod edit_workspace;
mod invitation_crud;
mod member_crud;
Expand Down
43 changes: 0 additions & 43 deletions tests/workspace/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,49 +23,6 @@ async fn get_first_workspace_string(c: &client_api::Client) -> String {
.to_string()
}

#[tokio::test]
async fn get_user_default_workspace_test() {
let email = generate_unique_email();
let password = "Hello!123#";
let c = localhost_client();
c.sign_up(&email, password).await.unwrap();
let mut test_client = TestClient::new_user().await;
let folder = test_client.get_user_folder().await;

let workspace_id = test_client.workspace_id().await;
let views = folder.get_views_belong_to(&workspace_id);
assert_eq!(views.len(), 1);
assert_eq!(views[0].name, "Getting started");

let document_id = views[0].id.clone();
let document =
get_document_collab_from_remote(&mut test_client, workspace_id, &document_id).await;
let document_data = document.get_document_data().unwrap();
assert_eq!(document_data.blocks.len(), 25);
}

async fn get_document_collab_from_remote(
test_client: &mut TestClient,
workspace_id: String,
document_id: &str,
) -> Document {
let params = QueryCollabParams {
workspace_id,
inner: QueryCollab {
object_id: document_id.to_string(),
collab_type: CollabType::Document,
},
};
let resp = test_client.get_collab(params).await.unwrap();
Document::open_with_options(
CollabOrigin::Empty,
DataSource::DocStateV1(resp.encode_collab.doc_state.to_vec()),
document_id,
vec![],
)
.unwrap()
}

#[tokio::test]
async fn test_template_category_crud() {
let (authorized_client, _) = generate_unique_registered_user_client().await;
Expand Down

0 comments on commit 8c27ee2

Please sign in to comment.