project_note
Clone
https://git.tahinli.com/Tahinli/project_note.git
0 watching·0 forks·Rust·3.6 MiB·c49f5e7·Tahinli·1 month ago·2 commits
| 1 | use loro_common::{LoroResult, PeerID}; |
| 2 | use loro_internal::{handler::TreeHandler, LoroDoc, ToJson, TreeID, TreeParentId}; |
| 3 | use pretty_assertions::assert_eq; |
| 4 | use serde_json::{json, Value}; |
| 5 | |
| 6 | fn deep_json(doc: &LoroDoc) -> Value { |
| 7 | doc.get_deep_value().to_json_value() |
| 8 | } |
| 9 | |
| 10 | #[test] |
| 11 | fn detached_tree_handler_queries_and_clear_follow_contracts() -> LoroResult<()> { |
| 12 | let tree = TreeHandler::new_detached(); |
| 13 | assert_eq!(format!("{tree:?}"), "TreeHandler Detached"); |
| 14 | assert!(tree.is_fractional_index_enabled()); |
| 15 | tree.enable_fractional_index(2); |
| 16 | tree.disable_fractional_index(); |
| 17 | assert!(!tree.is_deleted()); |
| 18 | assert!(tree.is_empty()); |
| 19 | assert_eq!(tree.__internal__next_tree_id(), TreeID::new(PeerID::MAX, 0)); |
| 20 | |
| 21 | let root = tree.create(TreeParentId::Root)?; |
| 22 | let first = tree.create_at(root.into(), 0)?; |
| 23 | let second = tree.create_at(root.into(), 1)?; |
| 24 | let third = tree.create_at(root.into(), 2)?; |
| 25 | |
| 26 | assert_eq!(tree.__internal__next_tree_id(), TreeID::new(PeerID::MAX, 4)); |
| 27 | assert_eq!(tree.get_child_at(&TreeParentId::Root, 0), Some(root)); |
| 28 | assert_eq!(tree.get_child_at(&root.into(), 0), Some(first)); |
| 29 | assert_eq!(tree.get_child_at(&root.into(), 1), Some(second)); |
| 30 | assert_eq!(tree.get_child_at(&root.into(), 2), Some(third)); |
| 31 | assert_eq!(tree.get_child_at(&root.into(), 3), None); |
| 32 | assert_eq!(tree.get_index_by_tree_id(&second), Some(1)); |
| 33 | assert!(tree.get_position_by_tree_id(&second).is_some()); |
| 34 | assert!(tree |
| 35 | .get_position_by_tree_id(&TreeID::new(PeerID::MAX, 99)) |
| 36 | .is_none()); |
| 37 | assert!(tree.is_parent(&second, &root.into())); |
| 38 | assert_eq!(tree.get_nodes_under(TreeParentId::Root).len(), 4); |
| 39 | assert_eq!(tree.get_nodes_under(root.into()).len(), 3); |
| 40 | assert_eq!(tree.nodes().len(), 4); |
| 41 | |
| 42 | tree.mov_before(third, first)?; |
| 43 | assert_eq!( |
| 44 | tree.children(&root.into()), |
| 45 | Some(vec![third, first, second]) |
| 46 | ); |
| 47 | tree.mov_after(third, second)?; |
| 48 | assert_eq!( |
| 49 | tree.children(&root.into()), |
| 50 | Some(vec![first, second, third]) |
| 51 | ); |
| 52 | tree.move_to(third, TreeParentId::Root, 0)?; |
| 53 | assert_eq!(tree.children(&TreeParentId::Root), Some(vec![third, root])); |
| 54 | |
| 55 | tree.delete(third)?; |
| 56 | assert!(!tree.contains(third)); |
| 57 | assert_eq!(tree.get_node_parent(&third), None); |
| 58 | |
| 59 | tree.clear()?; |
| 60 | assert!(tree.is_empty()); |
| 61 | assert_eq!(tree.roots(), Vec::<TreeID>::new()); |
| 62 | assert_eq!(tree.nodes(), Vec::<TreeID>::new()); |
| 63 | |
| 64 | Ok(()) |
| 65 | } |
| 66 | |
| 67 | #[test] |
| 68 | fn attached_tree_hierarchy_positions_and_diff_apply_follow_contracts() -> LoroResult<()> { |
| 69 | let doc = LoroDoc::new_auto_commit(); |
| 70 | doc.set_peer_id(77)?; |
| 71 | let tree = doc.get_tree("outline"); |
| 72 | tree.enable_fractional_index(2); |
| 73 | |
| 74 | let root = tree.create(TreeParentId::Root)?; |
| 75 | let first = tree.create_at(root.into(), 0)?; |
| 76 | let second = tree.create_at(root.into(), 1)?; |
| 77 | tree.get_meta(root)?.insert("name", "root")?; |
| 78 | tree.get_meta(first)?.insert("name", "first")?; |
| 79 | tree.get_meta(second)?.insert("name", "second")?; |
| 80 | |
| 81 | assert!(format!("{tree:?}").contains("TreeHandler")); |
| 82 | assert_eq!(tree.get_child_at(&TreeParentId::Root, 0), Some(root)); |
| 83 | assert_eq!(tree.get_child_at(&root.into(), 0), Some(first)); |
| 84 | assert_eq!(tree.get_child_at(&root.into(), 1), Some(second)); |
| 85 | assert_eq!(tree.get_index_by_tree_id(&second), Some(1)); |
| 86 | assert!(tree.get_position_by_tree_id(&second).is_some()); |
| 87 | assert!(tree.get_last_move_id(&root).is_some()); |
| 88 | |
| 89 | let hierarchy = tree.get_all_hierarchy_nodes_under(TreeParentId::Root); |
| 90 | assert_eq!(hierarchy.len(), 1); |
| 91 | assert_eq!(hierarchy[0].id, root); |
| 92 | assert_eq!(hierarchy[0].children.len(), 2); |
| 93 | assert_eq!(hierarchy[0].children[0].id, first); |
| 94 | assert_eq!(hierarchy[0].children[1].id, second); |
| 95 | |
| 96 | doc.commit_then_renew(); |
| 97 | let v1 = doc.state_frontiers(); |
| 98 | let before = deep_json(&doc); |
| 99 | |
| 100 | tree.move_to(second, TreeParentId::Root, 0)?; |
| 101 | tree.delete(first)?; |
| 102 | tree.get_meta(second)?.insert("status", "promoted")?; |
| 103 | doc.commit_then_renew(); |
| 104 | let v2 = doc.state_frontiers(); |
| 105 | let after = deep_json(&doc); |
| 106 | assert_ne!(before, after); |
| 107 | |
| 108 | let fork = doc.fork_at(&v1)?; |
| 109 | assert_eq!(deep_json(&fork), before); |
| 110 | fork.apply_diff(doc.diff(&v1, &v2)?)?; |
| 111 | assert_eq!(deep_json(&fork), after); |
| 112 | |
| 113 | tree.clear()?; |
| 114 | doc.commit_then_renew(); |
| 115 | assert!(tree.is_empty()); |
| 116 | assert_eq!(deep_json(&doc), json!({"outline": []})); |
| 117 | |
| 118 | Ok(()) |
| 119 | } |