2022-09-13 12:23:56 +00:00
|
|
|
use crate::node::script::NodeScript::*;
|
|
|
|
use crate::node::script::NodeTest;
|
|
|
|
use lib_ot::core::{AttributeBuilder, Node, NodeTree, Transaction, TransactionBuilder};
|
2022-09-11 10:16:19 +00:00
|
|
|
use lib_ot::{
|
2022-09-12 03:30:02 +00:00
|
|
|
core::{NodeBodyChangeset, NodeData, NodeDataBuilder, NodeOperation, Path},
|
2022-09-12 02:44:33 +00:00
|
|
|
text_delta::TextDeltaBuilder,
|
2022-09-11 04:59:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn operation_insert_node_serde_test() {
|
|
|
|
let insert = NodeOperation::Insert {
|
|
|
|
path: Path(vec![0, 1]),
|
|
|
|
nodes: vec![NodeData::new("text".to_owned())],
|
|
|
|
};
|
|
|
|
let result = serde_json::to_string(&insert).unwrap();
|
|
|
|
assert_eq!(result, r#"{"op":"insert","path":[0,1],"nodes":[{"type":"text"}]}"#);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn operation_insert_node_with_children_serde_test() {
|
2022-09-11 10:16:19 +00:00
|
|
|
let node = NodeDataBuilder::new("text")
|
2022-09-11 04:59:01 +00:00
|
|
|
.add_node(NodeData::new("sub_text".to_owned()))
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let insert = NodeOperation::Insert {
|
|
|
|
path: Path(vec![0, 1]),
|
|
|
|
nodes: vec![node],
|
|
|
|
};
|
|
|
|
assert_eq!(
|
|
|
|
serde_json::to_string(&insert).unwrap(),
|
|
|
|
r#"{"op":"insert","path":[0,1],"nodes":[{"type":"text","children":[{"type":"sub_text"}]}]}"#
|
|
|
|
);
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn operation_update_node_attributes_serde_test() {
|
|
|
|
let operation = NodeOperation::UpdateAttributes {
|
|
|
|
path: Path(vec![0, 1]),
|
2022-09-12 03:30:02 +00:00
|
|
|
attributes: AttributeBuilder::new().insert("bold", true).build(),
|
|
|
|
old_attributes: AttributeBuilder::new().insert("bold", false).build(),
|
2022-09-11 04:59:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let result = serde_json::to_string(&operation).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
result,
|
2022-09-13 03:38:19 +00:00
|
|
|
r#"{"op":"update","path":[0,1],"attributes":{"bold":true},"oldAttributes":{"bold":null}}"#
|
2022-09-11 04:59:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-09-11 10:16:19 +00:00
|
|
|
fn operation_update_node_body_serialize_test() {
|
2022-09-12 02:44:33 +00:00
|
|
|
let delta = TextDeltaBuilder::new().insert("AppFlowy...").build();
|
2022-09-11 04:59:01 +00:00
|
|
|
let inverted = delta.invert_str("");
|
|
|
|
let changeset = NodeBodyChangeset::Delta { delta, inverted };
|
|
|
|
let insert = NodeOperation::UpdateBody {
|
|
|
|
path: Path(vec![0, 1]),
|
|
|
|
changeset,
|
|
|
|
};
|
|
|
|
let result = serde_json::to_string(&insert).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
result,
|
2022-09-11 10:16:19 +00:00
|
|
|
r#"{"op":"update-body","path":[0,1],"changeset":{"delta":{"delta":[{"insert":"AppFlowy..."}],"inverted":[{"delete":11}]}}}"#
|
2022-09-11 04:59:01 +00:00
|
|
|
);
|
|
|
|
//
|
|
|
|
}
|
2022-09-11 10:16:19 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn operation_update_node_body_deserialize_test() {
|
|
|
|
let json_1 = r#"{"op":"update-body","path":[0,1],"changeset":{"delta":{"delta":[{"insert":"AppFlowy..."}],"inverted":[{"delete":11}]}}}"#;
|
|
|
|
let operation: NodeOperation = serde_json::from_str(json_1).unwrap();
|
|
|
|
let json_2 = serde_json::to_string(&operation).unwrap();
|
|
|
|
assert_eq!(json_1, json_2);
|
|
|
|
}
|
2022-09-13 12:23:56 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn operation_insert_transform_test() {
|
|
|
|
let node_1 = NodeDataBuilder::new("text_1").build();
|
|
|
|
let node_2 = NodeDataBuilder::new("text_2").build();
|
|
|
|
let op_1 = NodeOperation::Insert {
|
|
|
|
path: Path(vec![0, 1]),
|
|
|
|
nodes: vec![node_1],
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut insert_2 = NodeOperation::Insert {
|
|
|
|
path: Path(vec![0, 1]),
|
|
|
|
nodes: vec![node_2],
|
|
|
|
};
|
|
|
|
|
|
|
|
// let mut node_tree = NodeTree::new("root");
|
|
|
|
// node_tree.apply_op(insert_1.clone()).unwrap();
|
|
|
|
|
|
|
|
let new_op = op_1.transform(&insert_2);
|
|
|
|
let json = serde_json::to_string(&new_op).unwrap();
|
|
|
|
assert_eq!(json, r#"{"op":"insert","path":[0,2],"nodes":[{"type":"text_2"}]}"#);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn operation_insert_transform_test2() {
|
|
|
|
let mut test = NodeTest::new();
|
|
|
|
let node_data_1 = NodeDataBuilder::new("text_1").build();
|
|
|
|
let node_data_2 = NodeDataBuilder::new("text_2").build();
|
|
|
|
let node_2: Node = node_data_2.clone().into();
|
|
|
|
let node_data_3 = NodeDataBuilder::new("text_3").build();
|
|
|
|
let node_3: Node = node_data_3.clone().into();
|
|
|
|
|
|
|
|
let scripts = vec![
|
|
|
|
InsertNode {
|
|
|
|
path: 0.into(),
|
|
|
|
node_data: node_data_1.clone(),
|
|
|
|
rev_id: 1,
|
|
|
|
},
|
|
|
|
InsertNode {
|
|
|
|
path: 1.into(),
|
|
|
|
node_data: node_data_2.clone(),
|
|
|
|
rev_id: 2,
|
|
|
|
},
|
|
|
|
InsertNode {
|
|
|
|
path: 1.into(),
|
|
|
|
node_data: node_data_3.clone(),
|
|
|
|
rev_id: 1,
|
|
|
|
},
|
|
|
|
// AssertNode {
|
|
|
|
// path: 2.into(),
|
|
|
|
// expected: node_2,
|
|
|
|
// },
|
|
|
|
AssertNode {
|
|
|
|
path: 1.into(),
|
|
|
|
expected: node_3,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
test.run_scripts(scripts);
|
|
|
|
}
|