| 
									
										
										
										
											2022-10-20 11:35:11 +08:00
										 |  |  | #![allow(clippy::all)]
 | 
					
						
							| 
									
										
										
										
											2022-10-29 20:54:11 +08:00
										 |  |  | use lib_ot::core::{NodeTreeContext, OperationTransform, Transaction};
 | 
					
						
							|  |  |  | use lib_ot::text_delta::DeltaTextOperationBuilder;
 | 
					
						
							| 
									
										
										
										
											2022-09-11 18:16:19 +08:00
										 |  |  | use lib_ot::{
 | 
					
						
							| 
									
										
										
										
											2022-10-10 11:56:48 +08:00
										 |  |  |     core::attributes::AttributeHashMap,
 | 
					
						
							| 
									
										
										
										
											2022-10-20 11:35:11 +08:00
										 |  |  |     core::{Body, Changeset, NodeData, NodeTree, Path, TransactionBuilder},
 | 
					
						
							| 
									
										
										
										
											2022-10-22 21:57:44 +08:00
										 |  |  |     text_delta::DeltaTextOperations,
 | 
					
						
							| 
									
										
										
										
											2022-09-11 12:59:01 +08:00
										 |  |  | };
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  | use std::collections::HashMap;
 | 
					
						
							| 
									
										
										
										
											2022-09-09 15:05:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | pub enum NodeScript {
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |     InsertNode {
 | 
					
						
							|  |  |  |         path: Path,
 | 
					
						
							|  |  |  |         node_data: NodeData,
 | 
					
						
							|  |  |  |         rev_id: usize,
 | 
					
						
							|  |  |  |     },
 | 
					
						
							| 
									
										
										
										
											2022-10-20 11:35:11 +08:00
										 |  |  |     InsertNodes {
 | 
					
						
							|  |  |  |         path: Path,
 | 
					
						
							|  |  |  |         node_data_list: Vec<NodeData>,
 | 
					
						
							|  |  |  |         rev_id: usize,
 | 
					
						
							|  |  |  |     },
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |     UpdateAttributes {
 | 
					
						
							|  |  |  |         path: Path,
 | 
					
						
							| 
									
										
										
										
											2022-10-10 11:56:48 +08:00
										 |  |  |         attributes: AttributeHashMap,
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |     },
 | 
					
						
							|  |  |  |     UpdateBody {
 | 
					
						
							|  |  |  |         path: Path,
 | 
					
						
							| 
									
										
										
										
											2022-10-20 11:35:11 +08:00
										 |  |  |         changeset: Changeset,
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |     },
 | 
					
						
							|  |  |  |     DeleteNode {
 | 
					
						
							|  |  |  |         path: Path,
 | 
					
						
							| 
									
										
										
										
											2022-09-14 09:23:33 +08:00
										 |  |  |         rev_id: usize,
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |     },
 | 
					
						
							| 
									
										
										
										
											2022-10-20 11:35:11 +08:00
										 |  |  |     AssertNumberOfChildrenAtPath {
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |         path: Option<Path>,
 | 
					
						
							| 
									
										
										
										
											2022-10-20 11:35:11 +08:00
										 |  |  |         expected: usize,
 | 
					
						
							|  |  |  |     },
 | 
					
						
							|  |  |  |     AssertNodesAtRoot {
 | 
					
						
							|  |  |  |         expected: Vec<NodeData>,
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |     },
 | 
					
						
							| 
									
										
										
										
											2022-10-20 11:35:11 +08:00
										 |  |  |     #[allow(dead_code)]
 | 
					
						
							|  |  |  |     AssertNodesAtPath {
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |         path: Path,
 | 
					
						
							| 
									
										
										
										
											2022-10-20 11:35:11 +08:00
										 |  |  |         expected: Vec<NodeData>,
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |     },
 | 
					
						
							|  |  |  |     AssertNode {
 | 
					
						
							|  |  |  |         path: Path,
 | 
					
						
							| 
									
										
										
										
											2022-10-20 11:35:11 +08:00
										 |  |  |         expected: Option<NodeData>,
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |     },
 | 
					
						
							| 
									
										
										
										
											2022-10-30 09:35:15 +08:00
										 |  |  |     AssertNodeAttributes {
 | 
					
						
							|  |  |  |         path: Path,
 | 
					
						
							|  |  |  |         expected: &'static str,
 | 
					
						
							|  |  |  |     },
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |     AssertNodeDelta {
 | 
					
						
							|  |  |  |         path: Path,
 | 
					
						
							| 
									
										
										
										
											2022-10-22 21:57:44 +08:00
										 |  |  |         expected: DeltaTextOperations,
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |     },
 | 
					
						
							| 
									
										
										
										
											2022-10-30 12:54:07 +08:00
										 |  |  |     AssertNodeDeltaContent {
 | 
					
						
							|  |  |  |         path: Path,
 | 
					
						
							|  |  |  |         expected: &'static str,
 | 
					
						
							|  |  |  |     },
 | 
					
						
							| 
									
										
										
										
											2022-10-20 11:35:11 +08:00
										 |  |  |     #[allow(dead_code)]
 | 
					
						
							|  |  |  |     AssertTreeJSON {
 | 
					
						
							|  |  |  |         expected: String,
 | 
					
						
							|  |  |  |     },
 | 
					
						
							| 
									
										
										
										
											2022-09-09 15:05:41 +08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | pub struct NodeTest {
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |     rev_id: usize,
 | 
					
						
							|  |  |  |     rev_operations: HashMap<usize, Transaction>,
 | 
					
						
							| 
									
										
										
										
											2022-09-10 08:58:08 +08:00
										 |  |  |     node_tree: NodeTree,
 | 
					
						
							| 
									
										
										
										
											2022-09-09 15:05:41 +08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | impl NodeTest {
 | 
					
						
							|  |  |  |     pub fn new() -> Self {
 | 
					
						
							|  |  |  |         Self {
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |             rev_id: 0,
 | 
					
						
							|  |  |  |             rev_operations: HashMap::new(),
 | 
					
						
							| 
									
										
										
										
											2022-10-20 11:35:11 +08:00
										 |  |  |             node_tree: NodeTree::new(NodeTreeContext::default()),
 | 
					
						
							| 
									
										
										
										
											2022-09-09 15:05:41 +08:00
										 |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pub fn run_scripts(&mut self, scripts: Vec<NodeScript>) {
 | 
					
						
							|  |  |  |         for script in scripts {
 | 
					
						
							|  |  |  |             self.run_script(script);
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pub fn run_script(&mut self, script: NodeScript) {
 | 
					
						
							|  |  |  |         match script {
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |             NodeScript::InsertNode {
 | 
					
						
							|  |  |  |                 path,
 | 
					
						
							|  |  |  |                 node_data: node,
 | 
					
						
							|  |  |  |                 rev_id,
 | 
					
						
							|  |  |  |             } => {
 | 
					
						
							| 
									
										
										
										
											2022-10-29 20:54:11 +08:00
										 |  |  |                 let mut transaction = TransactionBuilder::new().insert_node_at_path(path, node).build();
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |                 self.transform_transaction_if_need(&mut transaction, rev_id);
 | 
					
						
							|  |  |  |                 self.apply_transaction(transaction);
 | 
					
						
							| 
									
										
										
										
											2022-09-09 15:05:41 +08:00
										 |  |  |             }
 | 
					
						
							| 
									
										
										
										
											2022-10-20 11:35:11 +08:00
										 |  |  |             NodeScript::InsertNodes {
 | 
					
						
							|  |  |  |                 path,
 | 
					
						
							|  |  |  |                 node_data_list,
 | 
					
						
							|  |  |  |                 rev_id,
 | 
					
						
							|  |  |  |             } => {
 | 
					
						
							| 
									
										
										
										
											2022-10-29 20:54:11 +08:00
										 |  |  |                 let mut transaction = TransactionBuilder::new()
 | 
					
						
							| 
									
										
										
										
											2022-10-20 11:35:11 +08:00
										 |  |  |                     .insert_nodes_at_path(path, node_data_list)
 | 
					
						
							| 
									
										
										
										
											2022-10-29 20:54:11 +08:00
										 |  |  |                     .build();
 | 
					
						
							| 
									
										
										
										
											2022-10-20 11:35:11 +08:00
										 |  |  |                 self.transform_transaction_if_need(&mut transaction, rev_id);
 | 
					
						
							|  |  |  |                 self.apply_transaction(transaction);
 | 
					
						
							|  |  |  |             }
 | 
					
						
							| 
									
										
										
										
											2022-09-11 12:59:01 +08:00
										 |  |  |             NodeScript::UpdateAttributes { path, attributes } => {
 | 
					
						
							| 
									
										
										
										
											2022-10-29 20:54:11 +08:00
										 |  |  |                 let node = self.node_tree.get_node_data_at_path(&path).unwrap();
 | 
					
						
							|  |  |  |                 let transaction = TransactionBuilder::new()
 | 
					
						
							|  |  |  |                     .update_node_at_path(
 | 
					
						
							|  |  |  |                         &path,
 | 
					
						
							|  |  |  |                         Changeset::Attributes {
 | 
					
						
							|  |  |  |                             new: attributes,
 | 
					
						
							|  |  |  |                             old: node.attributes,
 | 
					
						
							|  |  |  |                         },
 | 
					
						
							|  |  |  |                     )
 | 
					
						
							|  |  |  |                     .build();
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |                 self.apply_transaction(transaction);
 | 
					
						
							| 
									
										
										
										
											2022-09-09 15:05:41 +08:00
										 |  |  |             }
 | 
					
						
							| 
									
										
										
										
											2022-09-11 12:59:01 +08:00
										 |  |  |             NodeScript::UpdateBody { path, changeset } => {
 | 
					
						
							|  |  |  |                 //
 | 
					
						
							| 
									
										
										
										
											2022-10-29 20:54:11 +08:00
										 |  |  |                 let transaction = TransactionBuilder::new().update_node_at_path(&path, changeset).build();
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |                 self.apply_transaction(transaction);
 | 
					
						
							| 
									
										
										
										
											2022-09-11 12:59:01 +08:00
										 |  |  |             }
 | 
					
						
							| 
									
										
										
										
											2022-09-14 09:23:33 +08:00
										 |  |  |             NodeScript::DeleteNode { path, rev_id } => {
 | 
					
						
							| 
									
										
										
										
											2022-10-29 20:54:11 +08:00
										 |  |  |                 let mut transaction = TransactionBuilder::new()
 | 
					
						
							|  |  |  |                     .delete_node_at_path(&self.node_tree, &path)
 | 
					
						
							|  |  |  |                     .build();
 | 
					
						
							| 
									
										
										
										
											2022-09-14 09:23:33 +08:00
										 |  |  |                 self.transform_transaction_if_need(&mut transaction, rev_id);
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |                 self.apply_transaction(transaction);
 | 
					
						
							| 
									
										
										
										
											2022-09-09 15:05:41 +08:00
										 |  |  |             }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-20 11:35:11 +08:00
										 |  |  |             NodeScript::AssertNode { path, expected } => {
 | 
					
						
							|  |  |  |                 let node = self.node_tree.get_node_data_at_path(&path);
 | 
					
						
							|  |  |  |                 assert_eq!(node, expected.map(|e| e.into()));
 | 
					
						
							| 
									
										
										
										
											2022-09-09 15:05:41 +08:00
										 |  |  |             }
 | 
					
						
							| 
									
										
										
										
											2022-10-30 09:35:15 +08:00
										 |  |  |             NodeScript::AssertNodeAttributes { path, expected } => {
 | 
					
						
							|  |  |  |                 let node = self.node_tree.get_node_data_at_path(&path).unwrap();
 | 
					
						
							|  |  |  |                 assert_eq!(node.attributes.to_json().unwrap(), expected);
 | 
					
						
							|  |  |  |             }
 | 
					
						
							| 
									
										
										
										
											2022-10-20 11:35:11 +08:00
										 |  |  |             NodeScript::AssertNumberOfChildrenAtPath { path, expected } => match path {
 | 
					
						
							| 
									
										
										
										
											2022-09-09 15:05:41 +08:00
										 |  |  |                 None => {
 | 
					
						
							|  |  |  |                     let len = self.node_tree.number_of_children(None);
 | 
					
						
							| 
									
										
										
										
											2022-10-20 11:35:11 +08:00
										 |  |  |                     assert_eq!(len, expected)
 | 
					
						
							| 
									
										
										
										
											2022-09-09 15:05:41 +08:00
										 |  |  |                 }
 | 
					
						
							|  |  |  |                 Some(path) => {
 | 
					
						
							| 
									
										
										
										
											2022-09-11 12:59:01 +08:00
										 |  |  |                     let node_id = self.node_tree.node_id_at_path(path).unwrap();
 | 
					
						
							| 
									
										
										
										
											2022-09-09 15:05:41 +08:00
										 |  |  |                     let len = self.node_tree.number_of_children(Some(node_id));
 | 
					
						
							| 
									
										
										
										
											2022-10-20 11:35:11 +08:00
										 |  |  |                     assert_eq!(len, expected)
 | 
					
						
							| 
									
										
										
										
											2022-09-09 15:05:41 +08:00
										 |  |  |                 }
 | 
					
						
							|  |  |  |             },
 | 
					
						
							| 
									
										
										
										
											2022-10-20 11:35:11 +08:00
										 |  |  |             NodeScript::AssertNodesAtRoot { expected } => {
 | 
					
						
							|  |  |  |                 let nodes = self.node_tree.get_node_data_at_root().unwrap().children;
 | 
					
						
							|  |  |  |                 assert_eq!(nodes, expected)
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |             NodeScript::AssertNodesAtPath { path, expected } => {
 | 
					
						
							|  |  |  |                 let nodes = self.node_tree.get_node_data_at_path(&path).unwrap().children;
 | 
					
						
							|  |  |  |                 assert_eq!(nodes, expected)
 | 
					
						
							|  |  |  |             }
 | 
					
						
							| 
									
										
										
										
											2022-09-11 12:59:01 +08:00
										 |  |  |             NodeScript::AssertNodeDelta { path, expected } => {
 | 
					
						
							|  |  |  |                 let node = self.node_tree.get_node_at_path(&path).unwrap();
 | 
					
						
							| 
									
										
										
										
											2022-10-20 11:35:11 +08:00
										 |  |  |                 if let Body::Delta(delta) = node.body.clone() {
 | 
					
						
							| 
									
										
										
										
											2022-09-11 12:59:01 +08:00
										 |  |  |                     debug_assert_eq!(delta, expected);
 | 
					
						
							|  |  |  |                 } else {
 | 
					
						
							|  |  |  |                     panic!("Node body type not match, expect Delta");
 | 
					
						
							|  |  |  |                 }
 | 
					
						
							|  |  |  |             }
 | 
					
						
							| 
									
										
										
										
											2022-10-30 12:54:07 +08:00
										 |  |  |             NodeScript::AssertNodeDeltaContent { path, expected } => {
 | 
					
						
							|  |  |  |                 let node = self.node_tree.get_node_at_path(&path).unwrap();
 | 
					
						
							|  |  |  |                 if let Body::Delta(delta) = node.body.clone() {
 | 
					
						
							|  |  |  |                     debug_assert_eq!(delta.content().unwrap(), expected);
 | 
					
						
							|  |  |  |                 } else {
 | 
					
						
							|  |  |  |                     panic!("Node body type not match, expect Delta");
 | 
					
						
							|  |  |  |                 }
 | 
					
						
							|  |  |  |             }
 | 
					
						
							| 
									
										
										
										
											2022-10-20 11:35:11 +08:00
										 |  |  |             NodeScript::AssertTreeJSON { expected } => {
 | 
					
						
							|  |  |  |                 let json = serde_json::to_string(&self.node_tree).unwrap();
 | 
					
						
							|  |  |  |                 assert_eq!(json, expected)
 | 
					
						
							|  |  |  |             }
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fn apply_transaction(&mut self, transaction: Transaction) {
 | 
					
						
							|  |  |  |         self.rev_id += 1;
 | 
					
						
							|  |  |  |         self.rev_operations.insert(self.rev_id, transaction.clone());
 | 
					
						
							| 
									
										
										
										
											2022-09-14 21:04:12 +08:00
										 |  |  |         self.node_tree.apply_transaction(transaction).unwrap();
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fn transform_transaction_if_need(&mut self, transaction: &mut Transaction, rev_id: usize) {
 | 
					
						
							|  |  |  |         if self.rev_id >= rev_id {
 | 
					
						
							| 
									
										
										
										
											2022-09-14 12:03:52 +08:00
										 |  |  |             for rev_id in rev_id..=self.rev_id {
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |                 let old_transaction = self.rev_operations.get(&rev_id).unwrap();
 | 
					
						
							| 
									
										
										
										
											2022-09-14 21:04:12 +08:00
										 |  |  |                 *transaction = old_transaction.transform(transaction).unwrap();
 | 
					
						
							| 
									
										
										
										
											2022-09-13 20:23:56 +08:00
										 |  |  |             }
 | 
					
						
							| 
									
										
										
										
											2022-09-09 15:05:41 +08:00
										 |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2022-10-29 20:54:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | pub fn edit_node_delta(
 | 
					
						
							|  |  |  |     delta: &DeltaTextOperations,
 | 
					
						
							|  |  |  |     new_delta: DeltaTextOperations,
 | 
					
						
							|  |  |  | ) -> (Changeset, DeltaTextOperations) {
 | 
					
						
							|  |  |  |     let inverted = new_delta.invert(&delta);
 | 
					
						
							|  |  |  |     let expected = delta.compose(&new_delta).unwrap();
 | 
					
						
							|  |  |  |     let changeset = Changeset::Delta {
 | 
					
						
							|  |  |  |         delta: new_delta.clone(),
 | 
					
						
							|  |  |  |         inverted: inverted.clone(),
 | 
					
						
							|  |  |  |     };
 | 
					
						
							|  |  |  |     (changeset, expected)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | pub fn make_node_delta_changeset(
 | 
					
						
							|  |  |  |     initial_content: &str,
 | 
					
						
							|  |  |  |     insert_str: &str,
 | 
					
						
							|  |  |  | ) -> (DeltaTextOperations, Changeset, DeltaTextOperations) {
 | 
					
						
							|  |  |  |     let initial_content = initial_content.to_owned();
 | 
					
						
							|  |  |  |     let initial_delta = DeltaTextOperationBuilder::new().insert(&initial_content).build();
 | 
					
						
							|  |  |  |     let delta = DeltaTextOperationBuilder::new()
 | 
					
						
							|  |  |  |         .retain(initial_content.len())
 | 
					
						
							|  |  |  |         .insert(insert_str)
 | 
					
						
							|  |  |  |         .build();
 | 
					
						
							|  |  |  |     let inverted = delta.invert(&initial_delta);
 | 
					
						
							|  |  |  |     let expected = initial_delta.compose(&delta).unwrap();
 | 
					
						
							|  |  |  |     let changeset = Changeset::Delta {
 | 
					
						
							|  |  |  |         delta: delta.clone(),
 | 
					
						
							|  |  |  |         inverted: inverted.clone(),
 | 
					
						
							|  |  |  |     };
 | 
					
						
							|  |  |  |     (initial_delta, changeset, expected)
 | 
					
						
							|  |  |  | }
 |