Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

chore: add document test (#2932)

Kilu.He9fb8f221cfee
diff --git a/frontend/rust-lib/flowy-document2/src/entities.rs b/frontend/rust-lib/flowy-document2/src/entities.rs
index 97d397dce2..715bc7165f 100644
--- a/frontend/rust-lib/flowy-document2/src/entities.rs
+++ b/frontend/rust-lib/flowy-document2/src/entities.rs
@@ -147,7 +147,7 @@ pub struct DocumentDataPB {
pub meta: MetaPB,
}
-#[derive(Default, ProtoBuf, Debug)]
+#[derive(Default, ProtoBuf, Debug, Clone)]
pub struct BlockPB {
#[pb(index = 1)]
pub id: String,
diff --git a/frontend/rust-lib/flowy-test/src/document_event.rs b/frontend/rust-lib/flowy-test/src/document/document_event.rs
similarity index 76%
rename from frontend/rust-lib/flowy-test/src/document_event.rs
rename to frontend/rust-lib/flowy-test/src/document/document_event.rs
index e332515891..121517a129 100644
--- a/frontend/rust-lib/flowy-test/src/document_event.rs
+++ b/frontend/rust-lib/flowy-test/src/document/document_event.rs
@@ -57,6 +57,30 @@ impl DocumentEventTest {
OpenDocumentData { id: doc_id, data }
}
+ pub async fn get_block(&self, doc_id: &str, block_id: &str) -> Option<BlockPB> {
+ let document = self.open_document(doc_id.to_string()).await;
+ document.data.blocks.get(block_id).cloned()
+ }
+
+ pub async fn get_page_id(&self, doc_id: &str) -> String {
+ let data = self.get_document_data(doc_id).await;
+ data.page_id
+ }
+
+ pub async fn get_document_data(&self, doc_id: &str) -> DocumentDataPB {
+ let document = self.open_document(doc_id.to_string()).await;
+ document.data
+ }
+
+ pub async fn get_block_children(&self, doc_id: &str, block_id: &str) -> Option<Vec<String>> {
+ let block = self.get_block(doc_id, block_id).await;
+ block.as_ref()?;
+ let document_data = self.get_document_data(doc_id).await;
+ let children_map = document_data.meta.children_map;
+ let children_id = block.unwrap().children_id;
+ children_map.get(&children_id).map(|c| c.children.clone())
+ }
+
pub async fn apply_actions(&self, payload: ApplyActionPayloadPB) {
let core = &self.inner;
EventBuilder::new(core.clone())
diff --git a/frontend/rust-lib/flowy-test/src/document/mod.rs b/frontend/rust-lib/flowy-test/src/document/mod.rs
new file mode 100644
index 0000000000..89fffd26bb
--- /dev/null
+++ b/frontend/rust-lib/flowy-test/src/document/mod.rs
@@ -0,0 +1,3 @@
+pub mod document_event;
+pub mod text_block_event;
+pub mod utils;
diff --git a/frontend/rust-lib/flowy-test/src/document/text_block_event.rs b/frontend/rust-lib/flowy-test/src/document/text_block_event.rs
new file mode 100644
index 0000000000..dba583957b
--- /dev/null
+++ b/frontend/rust-lib/flowy-test/src/document/text_block_event.rs
@@ -0,0 +1,125 @@
+use crate::document::document_event::DocumentEventTest;
+use crate::document::utils::{gen_id, gen_text_block_data};
+use flowy_document2::entities::*;
+use std::sync::Arc;
+
+const TEXT_BLOCK_TY: &str = "paragraph";
+
+pub struct TextBlockEventTest {
+ doc: Arc<DocumentEventTest>,
+ doc_id: String,
+}
+
+impl TextBlockEventTest {
+ pub async fn new() -> Self {
+ let doc = DocumentEventTest::new().await;
+ let doc_id = doc.create_document().await.id;
+ Self {
+ doc: Arc::new(doc),
+ doc_id,
+ }
+ }
+
+ pub async fn get(&self, block_id: &str) -> Option<BlockPB> {
+ let doc = self.doc.clone();
+ let doc_id = self.doc_id.clone();
+ doc.get_block(&doc_id, block_id).await
+ }
+
+ /// Insert a new text block at the index of parent's children.
+ pub async fn insert_index(&self, text: String, index: usize, parent_id: Option<&str>) -> String {
+ let doc = self.doc.clone();
+ let doc_id = self.doc_id.clone();
+ let page_id = self.doc.get_page_id(&doc_id).await;
+ let parent_id = parent_id
+ .map(|id| id.to_string())
+ .unwrap_or_else(|| page_id);
+ let parent_children = self.doc.get_block_children(&doc_id, &parent_id).await;
+
+ let prev_id = {
+ // If index is 0, then the new block will be the first child of parent.
+ if index == 0 {
+ None
+ } else {
+ parent_children.and_then(|children| {
+ // If index is greater than the length of children, then the new block will be the last child of parent.
+ if index >= children.len() {
+ children.last().cloned()
+ } else {
+ children.get(index - 1).cloned()
+ }
+ })
+ }
+ };
+
+ let new_block_id = gen_id();
+ let data = gen_text_block_data(text);
+
+ let new_block = BlockPB {
+ id: new_block_id.clone(),
+ ty: TEXT_BLOCK_TY.to_string(),
+ data,
+ parent_id: parent_id.clone(),
+ children_id: gen_id(),
+ };
+ let action = BlockActionPB {
+ action: BlockActionTypePB::Insert,
+ payload: BlockActionPayloadPB {
+ block: new_block,
+ prev_id,
+ parent_id: Some(parent_id),
+ },
+ };
+ let payload = ApplyActionPayloadPB {
+ document_id: doc_id,
+ actions: vec![action],
+ };
+ doc.apply_actions(payload).await;
+ new_block_id
+ }
+
+ pub async fn update(&self, block_id: &str, text: String) {
+ let doc = self.doc.clone();
+ let doc_id = self.doc_id.clone();
+ let block = self.get(block_id).await.unwrap();
+ let data = gen_text_block_data(text);
+ let new_block = {
+ let mut new_block = block.clone();
+ new_block.data = data;
+ new_block
+ };
+ let action = BlockActionPB {
+ action: BlockActionTypePB::Update,
+ payload: BlockActionPayloadPB {
+ block: new_block,
+ prev_id: None,
+ parent_id: Some(block.parent_id.clone()),
+ },
+ };
+ let payload = ApplyActionPayloadPB {
+ document_id: doc_id,
+ actions: vec![action],
+ };
+ doc.apply_actions(payload).await;
+ }
+
+ pub async fn delete(&self, block_id: &str) {
+ let doc = self.doc.clone();
+ let doc_id = self.doc_id.clone();
+ let block = self.get(block_id).await.unwrap();
+ let parent_id = block.parent_id.clone();
+ let action = BlockActionPB {
+ action: BlockActionTypePB::Delete,
+ payload: BlockActionPayloadPB {
+ block,
+ prev_id: None,
+ parent_id: Some(parent_id),
+ },
+ };
+ let payload = ApplyActionPayloadPB {
+ document_id: doc_id,
+ actions: vec![action],
+ };
+ doc.apply_actions(payload).await;
+ }
+}
diff --git a/frontend/rust-lib/flowy-test/tests/document/utils.rs b/frontend/rust-lib/flowy-test/src/document/utils.rs
similarity index 89%
rename from frontend/rust-lib/flowy-test/tests/document/utils.rs
rename to frontend/rust-lib/flowy-test/src/document/utils.rs
index 3c7adc4646..b94be77fc5 100644
--- a/frontend/rust-lib/flowy-test/tests/document/utils.rs
+++ b/frontend/rust-lib/flowy-test/src/document/utils.rs
@@ -1,12 +1,22 @@
+use crate::document::document_event::*;
use flowy_document2::entities::*;
-use flowy_test::document_event::OpenDocumentData;
use nanoid::nanoid;
+use serde_json::json;
use std::collections::HashMap;
pub fn gen_id() -> String {
nanoid!(10)
}
+pub fn gen_text_block_data(text: String) -> String {
+ json!({
+ "delta": [{
+ "insert": text
+ }]
+ })
+ .to_string()
+}
+
pub struct ParseDocumentData {
pub doc_id: String,
pub page_id: String,
diff --git a/frontend/rust-lib/flowy-test/src/lib.rs b/frontend/rust-lib/flowy-test/src/lib.rs
index fd60017728..cfd967ba77 100644
--- a/frontend/rust-lib/flowy-test/src/lib.rs
+++ b/frontend/rust-lib/flowy-test/src/lib.rs
@@ -15,7 +15,7 @@ use flowy_user::errors::FlowyError;
use crate::event_builder::EventBuilder;
use crate::user_event::{async_sign_up, init_user_setting, SignUpContext};
-pub mod document_event;
+pub mod document;
pub mod event_builder;
pub mod folder_event;
pub mod user_event;
diff --git a/frontend/rust-lib/flowy-test/tests/document/block_test.rs b/frontend/rust-lib/flowy-test/tests/document/block_test.rs
new file mode 100644
index 0000000000..dad47ee58f
--- /dev/null
+++ b/frontend/rust-lib/flowy-test/tests/document/block_test.rs
@@ -0,0 +1,28 @@
+use flowy_test::document::text_block_event::TextBlockEventTest;
+use flowy_test::document::utils::gen_text_block_data;
+
+#[tokio::test]
+async fn insert_text_block_test() {
+ let test = TextBlockEventTest::new().await;
+ let text = "Hello World".to_string();
+ let block_id = test.insert_index(text.clone(), 1, None).await;
+ let block = test.get(&block_id).await;
+ assert!(block.is_some());
+ let block = block.unwrap();
+ let data = gen_text_block_data(text);
+ assert_eq!(block.data, data);
+}
+
+#[tokio::test]
+async fn update_text_block_test() {
+ let test = TextBlockEventTest::new().await;
+ let insert_text = "Hello World".to_string();
+ let block_id = test.insert_index(insert_text.clone(), 1, None).await;
+ let update_text = "Hello World 2".to_string();
+ test.update(&block_id, update_text.clone()).await;
+ let block = test.get(&block_id).await;
+ assert!(block.is_some());
+ let block = block.unwrap();
+ let update_data = gen_text_block_data(update_text);
+ assert_eq!(block.data, update_data);
+}
diff --git a/frontend/rust-lib/flowy-test/tests/document/test.rs b/frontend/rust-lib/flowy-test/tests/document/document_test.rs
similarity index 95%
rename from frontend/rust-lib/flowy-test/tests/document/test.rs
rename to frontend/rust-lib/flowy-test/tests/document/document_test.rs
index a7a7944fdb..8bbf812c7e 100644
--- a/frontend/rust-lib/flowy-test/tests/document/test.rs
+++ b/frontend/rust-lib/flowy-test/tests/document/document_test.rs
@@ -1,6 +1,6 @@
-use crate::document::utils::*;
use flowy_document2::entities::*;
-use flowy_test::document_event::DocumentEventTest;
+use flowy_test::document::document_event::DocumentEventTest;
+use flowy_test::document::utils::*;
#[tokio::test]
async fn get_document_event_test() {
diff --git a/frontend/rust-lib/flowy-test/tests/document/mod.rs b/frontend/rust-lib/flowy-test/tests/document/mod.rs
index 60914e9678..9907638bcc 100644
--- a/frontend/rust-lib/flowy-test/tests/document/mod.rs
+++ b/frontend/rust-lib/flowy-test/tests/document/mod.rs
@@ -1,2 +1,2 @@
-mod test;
-mod utils;
+mod block_test;
+mod document_test;