| 1 | |
| 2 | |
| 3 | #[test] |
| 4 | #[cfg(feature = "counter")] |
| 5 | fn mergeable_container_id_roundtrips_parent_key_and_type() { |
| 6 | use loro_internal::ContainerType; |
| 7 | |
| 8 | let parent = loro_common::ContainerID::new_root("state", ContainerType::Map); |
| 9 | let key = "field\u{1}with/slash:and:semicolon"; |
| 10 | let cid = loro_common::ContainerID::new_mergeable(&parent, key, ContainerType::Counter); |
| 11 | |
| 12 | assert!(cid.is_mergeable()); |
| 13 | let (decoded_parent, decoded_key, decoded_type) = cid.parse_mergeable().unwrap(); |
| 14 | assert_eq!(decoded_parent, parent); |
| 15 | assert_eq!(decoded_key, key); |
| 16 | assert_eq!(decoded_type, ContainerType::Counter); |
| 17 | } |
| 18 | |
| 19 | #[test] |
| 20 | fn user_root_names_cannot_use_mergeable_namespace() { |
| 21 | assert!(!loro_common::check_root_container_name( |
| 22 | loro_common::MERGEABLE_NAMESPACE_PREFIX |
| 23 | )); |
| 24 | } |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | #[test] |
| 34 | #[cfg(feature = "counter")] |
| 35 | fn parse_mergeable_rejects_malformed_payloads() { |
| 36 | use loro_common::{ContainerID, ContainerType}; |
| 37 | |
| 38 | |
| 39 | let plain_root = ContainerID::new_root("ordinary", ContainerType::Map); |
| 40 | assert!(plain_root.parse_mergeable().is_none()); |
| 41 | |
| 42 | |
| 43 | let no_key = ContainerID::Root { |
| 44 | name: "🤝:$state".into(), |
| 45 | container_type: ContainerType::Counter, |
| 46 | }; |
| 47 | assert!( |
| 48 | no_key.parse_mergeable().is_none(), |
| 49 | "payload without a key segment must reject" |
| 50 | ); |
| 51 | |
| 52 | |
| 53 | let truncated = ContainerID::Root { |
| 54 | name: "🤝:".into(), |
| 55 | container_type: ContainerType::Counter, |
| 56 | }; |
| 57 | assert!( |
| 58 | truncated.parse_mergeable().is_none(), |
| 59 | "empty payload must reject" |
| 60 | ); |
| 61 | |
| 62 | |
| 63 | let malformed = ContainerID::Root { |
| 64 | name: "🤝:$state>bad\\xescape".into(), |
| 65 | container_type: ContainerType::Counter, |
| 66 | }; |
| 67 | assert!( |
| 68 | malformed.parse_mergeable().is_none(), |
| 69 | "unknown escape in payload must reject" |
| 70 | ); |
| 71 | |
| 72 | let raw_slash = ContainerID::Root { |
| 73 | name: "🤝:$state>raw/slash".into(), |
| 74 | container_type: ContainerType::Counter, |
| 75 | }; |
| 76 | assert!(raw_slash.parse_mergeable().is_none()); |
| 77 | |
| 78 | let dangling = ContainerID::Root { |
| 79 | name: "🤝:$state>dangling\\".into(), |
| 80 | container_type: ContainerType::Counter, |
| 81 | }; |
| 82 | assert!(dangling.parse_mergeable().is_none()); |
| 83 | |
| 84 | for name in ["🤝:$root\\sname>key", "🤝:$root\\0name>key"] { |
| 85 | let invalid_root_name = ContainerID::Root { |
| 86 | name: name.into(), |
| 87 | container_type: ContainerType::Counter, |
| 88 | }; |
| 89 | assert!( |
| 90 | invalid_root_name.parse_mergeable().is_none(), |
| 91 | "root base parent must reject decoded slash/NUL: {name:?}" |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | for name in [ |
| 96 | "🤝:@Z:0>key", |
| 97 | "🤝:@001:0>key", |
| 98 | "🤝:@1:00>key", |
| 99 | "🤝:@1:-0>key", |
| 100 | ] { |
| 101 | let non_canonical = ContainerID::Root { |
| 102 | name: name.into(), |
| 103 | container_type: ContainerType::Counter, |
| 104 | }; |
| 105 | assert!( |
| 106 | non_canonical.parse_mergeable().is_none(), |
| 107 | "non-canonical base36 payload must reject: {name:?}" |
| 108 | ); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | #[test] |
| 124 | fn root_name_validator_rejects_mergeable_namespace_inputs() { |
| 125 | use loro_common::{check_root_container_name, MERGEABLE_NAMESPACE_PREFIX}; |
| 126 | |
| 127 | |
| 128 | assert!(!check_root_container_name(MERGEABLE_NAMESPACE_PREFIX)); |
| 129 | |
| 130 | assert!(!check_root_container_name("🤝:deadbeef")); |
| 131 | assert!(!check_root_container_name("🤝:")); |
| 132 | |
| 133 | assert!(check_root_container_name("foo🤝:bar")); |
| 134 | |
| 135 | assert!(check_root_container_name("\u{200B}🤝:abc")); |
| 136 | |
| 137 | assert!(check_root_container_name("state")); |
| 138 | assert!(check_root_container_name("ordinary-name_with-symbols")); |
| 139 | |
| 140 | assert!(!check_root_container_name("")); |
| 141 | |
| 142 | assert!(!check_root_container_name("a/b")); |
| 143 | assert!(!check_root_container_name("a\0b")); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | #[test] |
| 150 | fn mergeable_cid_roundtrips_for_every_container_kind() { |
| 151 | use loro_common::{ContainerID, ContainerType}; |
| 152 | let parent = ContainerID::new_root("state", ContainerType::Map); |
| 153 | |
| 154 | for kind in ContainerType::ALL_TYPES { |
| 155 | let cid = ContainerID::new_mergeable(&parent, "field", kind); |
| 156 | assert!(cid.is_mergeable(), "kind {kind:?}: must be mergeable"); |
| 157 | let again = ContainerID::new_mergeable(&parent, "field", kind); |
| 158 | assert_eq!(cid, again, "kind {kind:?}: cid must be deterministic"); |
| 159 | let (decoded_parent, decoded_key, decoded_kind) = cid |
| 160 | .parse_mergeable() |
| 161 | .unwrap_or_else(|| panic!("kind {kind:?}: parse_mergeable returned None")); |
| 162 | assert_eq!(decoded_parent, parent, "kind {kind:?}: parent roundtrip"); |
| 163 | assert_eq!(decoded_key, "field", "kind {kind:?}: key roundtrip"); |
| 164 | assert_eq!(decoded_kind, kind, "kind {kind:?}: kind roundtrip"); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | #[test] |
| 173 | fn mergeable_cid_roundtrips_for_degenerate_keys() { |
| 174 | use loro_common::{ContainerID, ContainerType}; |
| 175 | let parent = ContainerID::new_root("state", ContainerType::Map); |
| 176 | |
| 177 | let long_key: String = std::iter::repeat('k').take(2048).collect(); |
| 178 | let cases: Vec<&str> = vec![ |
| 179 | "", |
| 180 | long_key.as_str(), |
| 181 | "with\0nul\0bytes", |
| 182 | "embedded 🤝: substring in the middle", |
| 183 | "starts_with_🤝:_prefix", |
| 184 | "trailing_emoji_🤝:", |
| 185 | "ascii/slash/looking", |
| 186 | "contains>separator", |
| 187 | "contains\\backslash", |
| 188 | "mixed>\\/\0chars", |
| 189 | ]; |
| 190 | |
| 191 | for key in cases { |
| 192 | let cid = ContainerID::new_mergeable(&parent, key, ContainerType::Map); |
| 193 | assert!(cid.is_mergeable(), "key {key:?}: must be mergeable"); |
| 194 | let (decoded_parent, decoded_key, decoded_kind) = cid |
| 195 | .parse_mergeable() |
| 196 | .unwrap_or_else(|| panic!("key {key:?}: parse_mergeable returned None")); |
| 197 | assert_eq!(decoded_parent, parent); |
| 198 | assert_eq!(decoded_key, key, "key {key:?}: round-trip mismatch"); |
| 199 | assert_eq!(decoded_kind, ContainerType::Map); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | #[test] |
| 204 | fn mergeable_cid_uses_flattened_path_payload() { |
| 205 | use loro_common::{ContainerID, ContainerType, MERGEABLE_NAMESPACE_PREFIX}; |
| 206 | |
| 207 | let parent = ContainerID::new_root("state", ContainerType::Map); |
| 208 | let child = ContainerID::new_mergeable(&parent, "note>1", ContainerType::Map); |
| 209 | let grandchild = ContainerID::new_mergeable(&child, "body/slash\\nul\0", ContainerType::Text); |
| 210 | |
| 211 | let name = match &grandchild { |
| 212 | ContainerID::Root { name, .. } => name.as_str(), |
| 213 | _ => panic!("expected Root"), |
| 214 | }; |
| 215 | assert!( |
| 216 | name.starts_with(MERGEABLE_NAMESPACE_PREFIX), |
| 217 | "mergeable cid must use the reserved namespace" |
| 218 | ); |
| 219 | assert_eq!( |
| 220 | name, "🤝:$state>note\\>1>body\\sslash\\\\nul\\0", |
| 221 | "nested mergeable ids must extend the flattened path, not embed parent cid bytes" |
| 222 | ); |
| 223 | assert!( |
| 224 | !name.contains('/'), |
| 225 | "synthetic root names must not contain raw slash" |
| 226 | ); |
| 227 | assert!( |
| 228 | !name.contains('\0'), |
| 229 | "synthetic root names must not contain raw NUL" |
| 230 | ); |
| 231 | |
| 232 | let (decoded_parent, decoded_key, decoded_kind) = grandchild.parse_mergeable().unwrap(); |
| 233 | assert_eq!(decoded_parent, child); |
| 234 | assert_eq!(decoded_key, "body/slash\\nul\0"); |
| 235 | assert_eq!(decoded_kind, ContainerType::Text); |
| 236 | } |
| 237 | |
| 238 | #[test] |
| 239 | fn mergeable_cid_roundtrips_normal_base_parent() { |
| 240 | use loro_common::{ContainerID, ContainerType, ID}; |
| 241 | |
| 242 | let parent = ContainerID::new_normal( |
| 243 | ID { |
| 244 | peer: u64::MAX, |
| 245 | counter: i32::MIN, |
| 246 | }, |
| 247 | ContainerType::Map, |
| 248 | ); |
| 249 | let cid = ContainerID::new_mergeable(&parent, "field", ContainerType::List); |
| 250 | let name = match &cid { |
| 251 | ContainerID::Root { name, .. } => name.as_str(), |
| 252 | _ => panic!("expected Root"), |
| 253 | }; |
| 254 | assert!( |
| 255 | name.starts_with("🤝:@3w5e11264sgsf:-zik0zk>"), |
| 256 | "normal parent ids should use compact base36 encoding; got {name:?}" |
| 257 | ); |
| 258 | |
| 259 | let (decoded_parent, decoded_key, decoded_kind) = cid.parse_mergeable().unwrap(); |
| 260 | assert_eq!(decoded_parent, parent); |
| 261 | assert_eq!(decoded_key, "field"); |
| 262 | assert_eq!(decoded_kind, ContainerType::List); |
| 263 | } |
| 264 | |
| 265 | #[test] |
| 266 | fn mergeable_cid_roundtrips_escaped_root_base_parent() { |
| 267 | use loro_common::{ContainerID, ContainerType}; |
| 268 | |
| 269 | let parent = ContainerID::new_root("root>\\name", ContainerType::Map); |
| 270 | let cid = ContainerID::new_mergeable(&parent, "field", ContainerType::Tree); |
| 271 | let name = match &cid { |
| 272 | ContainerID::Root { name, .. } => name.as_str(), |
| 273 | _ => panic!("expected Root"), |
| 274 | }; |
| 275 | assert_eq!(name, "🤝:$root\\>\\\\name>field"); |
| 276 | |
| 277 | let (decoded_parent, decoded_key, decoded_kind) = cid.parse_mergeable().unwrap(); |
| 278 | assert_eq!(decoded_parent, parent); |
| 279 | assert_eq!(decoded_key, "field"); |
| 280 | assert_eq!(decoded_kind, ContainerType::Tree); |
| 281 | } |
| 282 | |
| 283 | #[test] |
| 284 | #[should_panic(expected = "mergeable child parent must be a map")] |
| 285 | fn mergeable_cid_rejects_non_map_parent() { |
| 286 | use loro_common::{ContainerID, ContainerType}; |
| 287 | |
| 288 | let parent = ContainerID::new_root("content", ContainerType::Text); |
| 289 | let _ = ContainerID::new_mergeable(&parent, "field", ContainerType::Map); |
| 290 | } |
| 291 | |
| 292 | #[test] |
| 293 | fn nested_mergeable_cid_size_grows_linearly() { |
| 294 | use loro_common::{ContainerID, ContainerType}; |
| 295 | |
| 296 | let mut cid = ContainerID::new_root("state", ContainerType::Map); |
| 297 | let mut sizes = Vec::new(); |
| 298 | for _ in 0..=8 { |
| 299 | cid = ContainerID::new_mergeable(&cid, "k", ContainerType::Map); |
| 300 | sizes.push(cid.to_bytes().len()); |
| 301 | } |
| 302 | |
| 303 | for window in sizes.windows(2) { |
| 304 | assert_eq!( |
| 305 | window[1] - window[0], |
| 306 | 2, |
| 307 | "each additional one-byte key should add only separator + key bytes; sizes={sizes:?}" |
| 308 | ); |
| 309 | } |
| 310 | assert!( |
| 311 | sizes[8] < 64, |
| 312 | "depth 8 should stay compact with flattened encoding; sizes={sizes:?}" |
| 313 | ); |
| 314 | } |