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.

feat: test insert sub trees

Vincent Chan096544d6a314
diff --git a/shared-lib/lib-ot/src/core/document/document_operation.rs b/shared-lib/lib-ot/src/core/document/document_operation.rs
index 77c74127bd..caca29e110 100644
--- a/shared-lib/lib-ot/src/core/document/document_operation.rs
+++ b/shared-lib/lib-ot/src/core/document/document_operation.rs
@@ -5,7 +5,10 @@ use crate::core::{NodeAttributes, NodeSubTree, TextDelta};
#[serde(tag = "type")]
pub enum DocumentOperation {
#[serde(rename = "insert-operation")]
- Insert { path: Position, nodes: Vec<Box<NodeSubTree>> },
+ Insert {
+ path: Position,
+ nodes: Vec<Box<NodeSubTree>>,
+ },
#[serde(rename = "update-operation")]
Update {
path: Position,
@@ -14,7 +17,10 @@ pub enum DocumentOperation {
old_attributes: NodeAttributes,
},
#[serde(rename = "delete-operation")]
- Delete { path: Position, nodes: Vec<Box<NodeSubTree>> },
+ Delete {
+ path: Position,
+ nodes: Vec<Box<NodeSubTree>>,
+ },
#[serde(rename = "text-edit-operation")]
TextEdit {
path: Position,
@@ -160,7 +166,25 @@ mod tests {
let result = serde_json::to_string(&insert).unwrap();
assert_eq!(
result,
- r#"{"type":"insert-operation","path":[0,1],"nodes":[{"node_type":"text","attributes":{}}]}"#
+ r#"{"type":"insert-operation","path":[0,1],"nodes":[{"type":"text","attributes":{}}]}"#
+ );
+ }
+
+ #[test]
+ fn test_serialize_insert_sub_trees() {
+ let insert = DocumentOperation::Insert {
+ path: Position(vec![0, 1]),
+ nodes: vec![Box::new(NodeSubTree {
+ node_type: "text".into(),
+ attributes: NodeAttributes::new(),
+ delta: None,
+ children: vec![Box::new(NodeSubTree::new("text".into()))],
+ })],
+ };
+ let result = serde_json::to_string(&insert).unwrap();
+ assert_eq!(
+ result,
+ r#"{"type":"insert-operation","path":[0,1],"nodes":[{"type":"text","attributes":{},"children":[{"type":"text","attributes":{}}]}]}"#
);
}
diff --git a/shared-lib/lib-ot/src/core/document/node.rs b/shared-lib/lib-ot/src/core/document/node.rs
index 1e298bc72e..e74c7d4918 100644
--- a/shared-lib/lib-ot/src/core/document/node.rs
+++ b/shared-lib/lib-ot/src/core/document/node.rs
@@ -19,6 +19,7 @@ impl NodeData {
#[derive(Clone, serde::Serialize, serde::Deserialize)]
pub struct NodeSubTree {
+ #[serde(rename = "type")]
pub node_type: String,
pub attributes: NodeAttributes,
#[serde(skip_serializing_if = "Option::is_none")]
diff --git a/shared-lib/lib-ot/src/core/document/transaction.rs b/shared-lib/lib-ot/src/core/document/transaction.rs
index e22ba21c3a..73fce7d8ad 100644
--- a/shared-lib/lib-ot/src/core/document/transaction.rs
+++ b/shared-lib/lib-ot/src/core/document/transaction.rs
@@ -1,7 +1,7 @@
use crate::core::document::position::Position;
use crate::core::{DocumentOperation, DocumentTree, NodeAttributes, NodeSubTree};
-use std::collections::HashMap;
use indextree::NodeId;
+use std::collections::HashMap;
pub struct Transaction {
pub operations: Vec<DocumentOperation>,
@@ -86,7 +86,7 @@ impl<'a> TransactionBuilder<'a> {
} else {
break;
}
- };
+ }
Box::new(NodeSubTree {
node_type: node_data.node_type.clone(),
diff --git a/shared-lib/lib-ot/tests/main.rs b/shared-lib/lib-ot/tests/main.rs
index 7daf74e957..31e7748b3a 100644
--- a/shared-lib/lib-ot/tests/main.rs
+++ b/shared-lib/lib-ot/tests/main.rs
@@ -1,4 +1,4 @@
-use lib_ot::core::{DocumentTree, NodeData, Position, TransactionBuilder};
+use lib_ot::core::{DocumentTree, NodeAttributes, NodeSubTree, Position, TransactionBuilder};
use lib_ot::errors::OTErrorCode;
use std::collections::HashMap;
@@ -13,7 +13,7 @@ fn test_documents() {
let mut document = DocumentTree::new();
let transaction = {
let mut tb = TransactionBuilder::new(&document);
- tb.insert_nodes_at_path(&vec![0].into(), &vec![NodeData::new("text")]);
+ tb.insert_nodes_at_path(&vec![0].into(), &vec![Box::new(NodeSubTree::new("text"))]);
tb.finalize()
};
document.apply(transaction).unwrap();
@@ -47,29 +47,52 @@ fn test_inserts_nodes() {
let mut document = DocumentTree::new();
let transaction = {
let mut tb = TransactionBuilder::new(&document);
- tb.insert_nodes_at_path(&vec![0].into(), &vec![NodeData::new("text")]);
- tb.insert_nodes_at_path(&vec![1].into(), &vec![NodeData::new("text")]);
- tb.insert_nodes_at_path(&vec![2].into(), &vec![NodeData::new("text")]);
+ tb.insert_nodes_at_path(&vec![0].into(), &vec![Box::new(NodeSubTree::new("text"))]);
+ tb.insert_nodes_at_path(&vec![1].into(), &vec![Box::new(NodeSubTree::new("text"))]);
+ tb.insert_nodes_at_path(&vec![2].into(), &vec![Box::new(NodeSubTree::new("text"))]);
tb.finalize()
};
document.apply(transaction).unwrap();
let transaction = {
let mut tb = TransactionBuilder::new(&document);
- tb.insert_nodes_at_path(&vec![1].into(), &vec![NodeData::new("text")]);
+ tb.insert_nodes_at_path(&vec![1].into(), &vec![Box::new(NodeSubTree::new("text"))]);
tb.finalize()
};
document.apply(transaction).unwrap();
}
+#[test]
+fn test_inserts_subtrees() {
+ let mut document = DocumentTree::new();
+ let transaction = {
+ let mut tb = TransactionBuilder::new(&document);
+ tb.insert_nodes_at_path(
+ &vec![0].into(),
+ &vec![Box::new(NodeSubTree {
+ node_type: "text".into(),
+ attributes: NodeAttributes::new(),
+ delta: None,
+ children: vec![Box::new(NodeSubTree::new("image".into()))],
+ })],
+ );
+ tb.finalize()
+ };
+ document.apply(transaction).unwrap();
+
+ let node = document.node_at_path(&Position(vec![0, 0])).unwrap();
+ let data = document.arena.get(node).unwrap().get();
+ assert_eq!(data.node_type, "image");
+}
+
#[test]
fn test_update_nodes() {
let mut document = DocumentTree::new();
let transaction = {
let mut tb = TransactionBuilder::new(&document);
- tb.insert_nodes_at_path(&vec![0].into(), &vec![NodeData::new("text")]);
- tb.insert_nodes_at_path(&vec![1].into(), &vec![NodeData::new("text")]);
- tb.insert_nodes_at_path(&vec![2].into(), &vec![NodeData::new("text")]);
+ tb.insert_nodes_at_path(&vec![0].into(), &vec![Box::new(NodeSubTree::new("text"))]);
+ tb.insert_nodes_at_path(&vec![1].into(), &vec![Box::new(NodeSubTree::new("text"))]);
+ tb.insert_nodes_at_path(&vec![2].into(), &vec![Box::new(NodeSubTree::new("text"))]);
tb.finalize()
};
document.apply(transaction).unwrap();
@@ -92,9 +115,9 @@ fn test_delete_nodes() {
let mut document = DocumentTree::new();
let transaction = {
let mut tb = TransactionBuilder::new(&document);
- tb.insert_nodes_at_path(&vec![0].into(), &vec![NodeData::new("text")]);
- tb.insert_nodes_at_path(&vec![1].into(), &vec![NodeData::new("text")]);
- tb.insert_nodes_at_path(&vec![2].into(), &vec![NodeData::new("text")]);
+ tb.insert_nodes_at_path(&vec![0].into(), &vec![Box::new(NodeSubTree::new("text"))]);
+ tb.insert_nodes_at_path(&vec![1].into(), &vec![Box::new(NodeSubTree::new("text"))]);
+ tb.insert_nodes_at_path(&vec![2].into(), &vec![Box::new(NodeSubTree::new("text"))]);
tb.finalize()
};
document.apply(transaction).unwrap();
@@ -115,8 +138,8 @@ fn test_errors() {
let mut document = DocumentTree::new();
let transaction = {
let mut tb = TransactionBuilder::new(&document);
- tb.insert_nodes_at_path(&vec![0].into(), &vec![NodeData::new("text")]);
- tb.insert_nodes_at_path(&vec![100].into(), &vec![NodeData::new("text")]);
+ tb.insert_nodes_at_path(&vec![0].into(), &vec![Box::new(NodeSubTree::new("text"))]);
+ tb.insert_nodes_at_path(&vec![100].into(), &vec![Box::new(NodeSubTree::new("text"))]);
tb.finalize()
};
let result = document.apply(transaction);