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::{ |
| 2 | loro_value, ContainerID, ContainerType, IdSpan, LoroError, LoroResult, LoroValue, PeerID, ID, |
| 3 | }; |
| 4 | use loro_internal::awareness::EphemeralStore; |
| 5 | use loro_internal::sync::{AtomicBool, Mutex}; |
| 6 | use loro_internal::{ |
| 7 | cursor::PosType, |
| 8 | delta::ResolvedMapValue, |
| 9 | encoding::ImportStatus, |
| 10 | event::{Diff, EventTriggerKind}, |
| 11 | fx_map, |
| 12 | handler::{Handler, TextDelta, ValueOrHandler}, |
| 13 | loro::{CommitOptions, ExportMode}, |
| 14 | version::{Frontiers, VersionRange}, |
| 15 | ApplyDiff, HandlerTrait, ListHandler, LoroDoc, MapHandler, TextHandler, ToJson, TreeHandler, |
| 16 | TreeParentId, |
| 17 | }; |
| 18 | use rustc_hash::FxHashMap; |
| 19 | use serde_json::json; |
| 20 | use std::sync::Arc; |
| 21 | |
| 22 | #[test] |
| 23 | fn issue_502() -> LoroResult<()> { |
| 24 | let doc = LoroDoc::new_auto_commit(); |
| 25 | doc.get_map("map").insert("stringA", "Original data")?; |
| 26 | doc.commit_then_renew(); |
| 27 | doc.get_map("map").insert("stringA", "Updated data")?; |
| 28 | doc.attach(); |
| 29 | doc.get_map("map").insert("stringB", "Something else")?; |
| 30 | doc.commit_then_renew(); |
| 31 | Ok(()) |
| 32 | } |
| 33 | |
| 34 | #[test] |
| 35 | fn issue_225() -> LoroResult<()> { |
| 36 | let doc = LoroDoc::new_auto_commit(); |
| 37 | let text = doc.get_text("text"); |
| 38 | text.insert_unicode(0, "123")?; |
| 39 | text.mark(0, 3, "bold", true.into(), PosType::Event)?; |
| 40 | // when apply_delta, the attributes of insert should override the current styles |
| 41 | text.apply_delta(&[ |
| 42 | TextDelta::Retain { |
| 43 | retain: 3, |
| 44 | attributes: None, |
| 45 | }, |
| 46 | TextDelta::Insert { |
| 47 | insert: "new".into(), |
| 48 | attributes: None, |
| 49 | }, |
| 50 | ])?; |
| 51 | assert_eq!( |
| 52 | text.get_richtext_value().to_json_value(), |
| 53 | json!([{ "insert": "123", "attributes": { "bold": true } }, { "insert": "new" }]) |
| 54 | ); |
| 55 | |
| 56 | Ok(()) |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn issue_211() -> LoroResult<()> { |
| 61 | let doc1 = LoroDoc::new_auto_commit(); |
| 62 | let doc2 = LoroDoc::new_auto_commit(); |
| 63 | doc1.get_text("text").insert_unicode(0, "T")?; |
| 64 | doc2.merge(&doc1)?; |
| 65 | let v0 = doc1.oplog_frontiers(); |
| 66 | doc1.get_text("text").insert_unicode(1, "A")?; |
| 67 | doc2.get_text("text").insert_unicode(1, "B")?; |
| 68 | doc1.checkout(&v0)?; |
| 69 | doc2.checkout(&v0)?; |
| 70 | doc1.checkout_to_latest(); |
| 71 | doc2.checkout_to_latest(); |
| 72 | // let v1_of_doc1 = doc1.oplog_frontiers(); |
| 73 | let v1_of_doc2 = doc2.oplog_frontiers(); |
| 74 | doc2.get_text("text").insert_unicode(2, "B")?; |
| 75 | doc2.checkout(&v1_of_doc2)?; |
| 76 | doc2.checkout(&v0)?; |
| 77 | assert_eq!( |
| 78 | doc2.get_deep_value().to_json_value(), |
| 79 | json!({ |
| 80 | "text": "T" |
| 81 | }) |
| 82 | ); |
| 83 | Ok(()) |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn mark_with_the_same_key_value_should_be_skipped() { |
| 88 | let a = LoroDoc::new_auto_commit(); |
| 89 | let text = a.get_text("text"); |
| 90 | text.insert_unicode(0, "Hello world!").unwrap(); |
| 91 | text.mark(0, 11, "bold", "value".into(), PosType::Event) |
| 92 | .unwrap(); |
| 93 | a.commit_then_renew(); |
| 94 | let v = a.oplog_vv(); |
| 95 | text.mark(0, 5, "bold", "value".into(), PosType::Event) |
| 96 | .unwrap(); |
| 97 | a.commit_then_renew(); |
| 98 | let new_v = a.oplog_vv(); |
| 99 | // new mark should be ignored, so vv should be the same |
| 100 | assert_eq!(v, new_v); |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn event_from_checkout() { |
| 105 | let a = LoroDoc::new_auto_commit(); |
| 106 | let sub = a.subscribe_root(Arc::new(|event| { |
| 107 | assert!(matches!( |
| 108 | event.event_meta.by, |
| 109 | EventTriggerKind::Checkout | EventTriggerKind::Local |
| 110 | )); |
| 111 | })); |
| 112 | a.get_text("text").insert_unicode(0, "hello").unwrap(); |
| 113 | a.commit_then_renew(); |
| 114 | let version = a.oplog_frontiers(); |
| 115 | a.get_text("text").insert_unicode(0, "hello").unwrap(); |
| 116 | a.commit_then_renew(); |
| 117 | sub.unsubscribe(); |
| 118 | let ran = Arc::new(AtomicBool::new(false)); |
| 119 | let ran_cloned = ran.clone(); |
| 120 | let _g = a.subscribe_root(Arc::new(move |event| { |
| 121 | assert!(event.event_meta.by.is_checkout()); |
| 122 | ran.store(true, std::sync::atomic::Ordering::Relaxed); |
| 123 | })); |
| 124 | a.checkout(&version).unwrap(); |
| 125 | assert!(ran_cloned.load(std::sync::atomic::Ordering::Relaxed)); |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn handler_in_event() { |
| 130 | let doc = LoroDoc::new_auto_commit(); |
| 131 | let _g = doc.subscribe_root(Arc::new(|e| { |
| 132 | dbg!(&e); |
| 133 | let value = e.events[0] |
| 134 | .diff |
| 135 | .as_list() |
| 136 | .unwrap() |
| 137 | .iter() |
| 138 | .next() |
| 139 | .unwrap() |
| 140 | .as_replace() |
| 141 | .unwrap() |
| 142 | .0 |
| 143 | .iter() |
| 144 | .next() |
| 145 | .unwrap(); |
| 146 | assert!(matches!(value, ValueOrHandler::Handler(Handler::Text(_)))); |
| 147 | })); |
| 148 | let list = doc.get_list("list"); |
| 149 | list.insert_container(0, TextHandler::new_detached()) |
| 150 | .unwrap(); |
| 151 | doc.commit_then_renew(); |
| 152 | } |
| 153 | |
| 154 | #[test] |
| 155 | fn out_of_bound_test() { |
| 156 | let a = LoroDoc::new_auto_commit(); |
| 157 | a.get_text("text").insert_unicode(0, "Hello").unwrap(); |
| 158 | a.get_list("list").insert(0, "Hello").unwrap(); |
| 159 | a.get_list("list").insert(1, "Hello").unwrap(); |
| 160 | // expect out of bound err |
| 161 | let err = a.get_text("text").insert_unicode(6, "Hello").unwrap_err(); |
| 162 | assert!(matches!(err, loro_common::LoroError::OutOfBound { .. })); |
| 163 | let err = a.get_text("text").delete_unicode(3, 5).unwrap_err(); |
| 164 | assert!(matches!(err, loro_common::LoroError::OutOfBound { .. })); |
| 165 | let err = a |
| 166 | .get_text("text") |
| 167 | .mark(0, 8, "h", 5.into(), PosType::Event) |
| 168 | .unwrap_err(); |
| 169 | assert!(matches!(err, loro_common::LoroError::OutOfBound { .. })); |
| 170 | let _err = a |
| 171 | .get_text("text") |
| 172 | .mark(3, 0, "h", 5.into(), PosType::Event) |
| 173 | .unwrap_err(); |
| 174 | let err = a.get_list("list").insert(6, "Hello").unwrap_err(); |
| 175 | assert!(matches!(err, loro_common::LoroError::OutOfBound { .. })); |
| 176 | let err = a.get_list("list").delete(3, 2).unwrap_err(); |
| 177 | assert!(matches!(err, loro_common::LoroError::OutOfBound { .. })); |
| 178 | let err = a |
| 179 | .get_list("list") |
| 180 | .insert_container(3, MapHandler::new_detached()) |
| 181 | .unwrap_err(); |
| 182 | assert!(matches!(err, loro_common::LoroError::OutOfBound { .. })); |
| 183 | } |
| 184 | |
| 185 | #[test] |
| 186 | fn list() { |
| 187 | let a = LoroDoc::new_auto_commit(); |
| 188 | a.get_list("list").insert(0, "Hello").unwrap(); |
| 189 | assert_eq!(a.get_list("list").get(0).unwrap(), LoroValue::from("Hello")); |
| 190 | let map = a |
| 191 | .get_list("list") |
| 192 | .insert_container(1, MapHandler::new_detached()) |
| 193 | .unwrap(); |
| 194 | map.insert("Hello", LoroValue::from("u")).unwrap(); |
| 195 | let pos = map |
| 196 | .insert_container("pos", MapHandler::new_detached()) |
| 197 | .unwrap(); |
| 198 | pos.insert("x", 0).unwrap(); |
| 199 | pos.insert("y", 100).unwrap(); |
| 200 | |
| 201 | let cid = map.id(); |
| 202 | let id = a.get_list("list").get(1); |
| 203 | assert_eq!(id.as_ref().unwrap().as_container().unwrap(), &cid); |
| 204 | let map = a.get_map(id.unwrap().into_container().unwrap()); |
| 205 | let new_pos = a.get_map(map.get("pos").unwrap().into_container().unwrap()); |
| 206 | assert_eq!( |
| 207 | new_pos.get_deep_value().to_json_value(), |
| 208 | json!({ |
| 209 | "x": 0, |
| 210 | "y": 100, |
| 211 | }) |
| 212 | ); |
| 213 | } |
| 214 | |
| 215 | #[test] |
| 216 | fn richtext_mark_event() { |
| 217 | let a = LoroDoc::new_auto_commit(); |
| 218 | let _g = a.subscribe( |
| 219 | &a.get_text("text").id(), |
| 220 | Arc::new(|e| { |
| 221 | let delta = e.events[0].diff.as_text().unwrap(); |
| 222 | assert_eq!( |
| 223 | delta.to_json_value(), |
| 224 | json!([ |
| 225 | {"insert": "He", "attributes": {"bold": true}}, |
| 226 | {"insert": "ll", "attributes": {"bold": null}}, |
| 227 | {"insert": "o", "attributes": {"bold": true}} |
| 228 | ]) |
| 229 | ) |
| 230 | }), |
| 231 | ); |
| 232 | a.get_text("text").insert_unicode(0, "Hello").unwrap(); |
| 233 | a.get_text("text") |
| 234 | .mark(0, 5, "bold", true.into(), PosType::Event) |
| 235 | .unwrap(); |
| 236 | a.get_text("text") |
| 237 | .mark(2, 4, "bold", LoroValue::Null, PosType::Event) |
| 238 | .unwrap(); |
| 239 | let _ = a.implicit_commit_then_stop(); |
| 240 | let b = LoroDoc::new_auto_commit(); |
| 241 | let _g = b.subscribe( |
| 242 | &a.get_text("text").id(), |
| 243 | Arc::new(|e| { |
| 244 | let delta = e.events[0].diff.as_text().unwrap(); |
| 245 | assert_eq!( |
| 246 | delta.to_json_value(), |
| 247 | json!([ |
| 248 | {"insert": "He", "attributes": {"bold": true}}, |
| 249 | {"insert": "ll", "attributes": {"bold": null}}, |
| 250 | {"insert": "o", "attributes": {"bold": true}} |
| 251 | ]) |
| 252 | ) |
| 253 | }), |
| 254 | ); |
| 255 | b.merge(&a).unwrap(); |
| 256 | } |
| 257 | |
| 258 | #[test] |
| 259 | fn concurrent_richtext_mark_event() { |
| 260 | let a = LoroDoc::new_auto_commit(); |
| 261 | let b = LoroDoc::new_auto_commit(); |
| 262 | let c = LoroDoc::new_auto_commit(); |
| 263 | a.get_text("text").insert_unicode(0, "Hello").unwrap(); |
| 264 | b.merge(&a).unwrap(); |
| 265 | c.merge(&a).unwrap(); |
| 266 | b.get_text("text") |
| 267 | .mark(0, 3, "bold", true.into(), PosType::Event) |
| 268 | .unwrap(); |
| 269 | c.get_text("text") |
| 270 | .mark(1, 4, "link", true.into(), PosType::Event) |
| 271 | .unwrap(); |
| 272 | b.merge(&c).unwrap(); |
| 273 | let sub_id = a.subscribe( |
| 274 | &a.get_text("text").id(), |
| 275 | Arc::new(|e| { |
| 276 | let delta = e.events[0].diff.as_text().unwrap(); |
| 277 | assert_eq!( |
| 278 | delta.to_json_value(), |
| 279 | json!([ |
| 280 | {"retain": 1, "attributes": {"bold": true, }}, |
| 281 | {"retain": 2, "attributes": {"bold": true, "link": true}}, |
| 282 | {"retain": 1, "attributes": {"link": true}}, |
| 283 | ]) |
| 284 | ) |
| 285 | }), |
| 286 | ); |
| 287 | |
| 288 | a.merge(&b).unwrap(); |
| 289 | sub_id.unsubscribe(); |
| 290 | |
| 291 | let sub_id = a.subscribe( |
| 292 | &a.get_text("text").id(), |
| 293 | Arc::new(|e| { |
| 294 | let delta = e.events[0].diff.as_text().unwrap(); |
| 295 | assert_eq!( |
| 296 | delta.to_json_value(), |
| 297 | json!([ |
| 298 | { |
| 299 | "retain": 2, |
| 300 | }, |
| 301 | { |
| 302 | "retain": 1, |
| 303 | "attributes": {"bold": null, "link": true} |
| 304 | } |
| 305 | ]) |
| 306 | ) |
| 307 | }), |
| 308 | ); |
| 309 | |
| 310 | b.get_text("text") |
| 311 | .mark(2, 3, "bold", LoroValue::Null, PosType::Event) |
| 312 | .unwrap(); |
| 313 | a.merge(&b).unwrap(); |
| 314 | sub_id.unsubscribe(); |
| 315 | let _g = a.subscribe( |
| 316 | &a.get_text("text").id(), |
| 317 | Arc::new(|e| { |
| 318 | for container_diff in e.events { |
| 319 | let delta = container_diff.diff.as_text().unwrap(); |
| 320 | assert_eq!( |
| 321 | delta.to_json_value(), |
| 322 | json!([ |
| 323 | { |
| 324 | "retain": 2, |
| 325 | }, |
| 326 | { |
| 327 | "insert": "A", |
| 328 | "attributes": {"bold": true, "link": true} |
| 329 | } |
| 330 | ]) |
| 331 | ) |
| 332 | } |
| 333 | }), |
| 334 | ); |
| 335 | a.get_text("text").insert_unicode(2, "A").unwrap(); |
| 336 | let _ = a.implicit_commit_then_stop(); |
| 337 | } |
| 338 | |
| 339 | #[test] |
| 340 | fn insert_richtext_event() { |
| 341 | let a = LoroDoc::new_auto_commit(); |
| 342 | a.get_text("text").insert_unicode(0, "Hello").unwrap(); |
| 343 | a.get_text("text") |
| 344 | .mark(0, 5, "bold", true.into(), PosType::Event) |
| 345 | .unwrap(); |
| 346 | a.commit_then_renew(); |
| 347 | let text = a.get_text("text"); |
| 348 | let _g = a.subscribe( |
| 349 | &text.id(), |
| 350 | Arc::new(|e| { |
| 351 | let delta = e.events[0].diff.as_text().unwrap(); |
| 352 | assert_eq!( |
| 353 | delta.to_json_value(), |
| 354 | json!([ |
| 355 | {"retain": 5,}, |
| 356 | {"insert": " World!", "attributes": {"bold": true}} |
| 357 | ]) |
| 358 | ) |
| 359 | }), |
| 360 | ); |
| 361 | |
| 362 | text.insert_unicode(5, " World!").unwrap(); |
| 363 | } |
| 364 | |
| 365 | #[test] |
| 366 | fn import_after_init_handlers() { |
| 367 | let a = LoroDoc::new_auto_commit(); |
| 368 | let _g = a.subscribe( |
| 369 | &ContainerID::new_root("text", ContainerType::Text), |
| 370 | Arc::new(|event| { |
| 371 | assert!(matches!( |
| 372 | event.events[0].diff, |
| 373 | loro_internal::event::Diff::Text(_) |
| 374 | )) |
| 375 | }), |
| 376 | ); |
| 377 | let _g = a.subscribe( |
| 378 | &ContainerID::new_root("map", ContainerType::Map), |
| 379 | Arc::new(|event| { |
| 380 | assert!(matches!( |
| 381 | event.events[0].diff, |
| 382 | loro_internal::event::Diff::Map(_) |
| 383 | )) |
| 384 | }), |
| 385 | ); |
| 386 | let _g = a.subscribe( |
| 387 | &ContainerID::new_root("list", ContainerType::List), |
| 388 | Arc::new(|event| { |
| 389 | assert!(matches!( |
| 390 | event.events[0].diff, |
| 391 | loro_internal::event::Diff::List(_) |
| 392 | )) |
| 393 | }), |
| 394 | ); |
| 395 | |
| 396 | let b = LoroDoc::new_auto_commit(); |
| 397 | b.get_list("list").insert(0, "list").unwrap(); |
| 398 | b.get_list("list_a").insert(0, "list_a").unwrap(); |
| 399 | b.get_text("text").insert_unicode(0, "text").unwrap(); |
| 400 | b.get_map("map").insert("m", "map").unwrap(); |
| 401 | a.import(&b.export(ExportMode::Snapshot).unwrap()).unwrap(); |
| 402 | a.commit_then_renew(); |
| 403 | } |
| 404 | |
| 405 | #[test] |
| 406 | fn test_from_snapshot() { |
| 407 | let a = LoroDoc::new_auto_commit(); |
| 408 | a.get_text("text").insert_unicode(0, "0").unwrap(); |
| 409 | let snapshot = a.export(ExportMode::Snapshot).unwrap(); |
| 410 | let c = LoroDoc::from_snapshot(&snapshot).unwrap(); |
| 411 | assert_eq!(a.get_deep_value(), c.get_deep_value()); |
| 412 | assert_eq!(a.oplog_frontiers(), c.oplog_frontiers()); |
| 413 | assert_eq!(a.state_frontiers(), c.state_frontiers()); |
| 414 | let updates = a.export(ExportMode::all_updates()).unwrap(); |
| 415 | let d = match LoroDoc::from_snapshot(&updates) { |
| 416 | Ok(_) => panic!(), |
| 417 | Err(e) => e, |
| 418 | }; |
| 419 | assert!(matches!(d, loro_common::LoroError::DecodeError(..))); |
| 420 | } |
| 421 | |
| 422 | #[test] |
| 423 | fn test_pending() { |
| 424 | let a = LoroDoc::new_auto_commit(); |
| 425 | a.set_peer_id(0).unwrap(); |
| 426 | a.get_text("text").insert_unicode(0, "0").unwrap(); |
| 427 | let b = LoroDoc::new_auto_commit(); |
| 428 | b.set_peer_id(1).unwrap(); |
| 429 | b.import(&a.export(ExportMode::all_updates()).unwrap()) |
| 430 | .unwrap(); |
| 431 | b.get_text("text").insert_unicode(0, "1").unwrap(); |
| 432 | let c = LoroDoc::new_auto_commit(); |
| 433 | b.set_peer_id(2).unwrap(); |
| 434 | c.import(&b.export(ExportMode::all_updates()).unwrap()) |
| 435 | .unwrap(); |
| 436 | c.get_text("text").insert_unicode(0, "2").unwrap(); |
| 437 | |
| 438 | // c creates a pending change for a, insert "2" cannot be merged into a yet |
| 439 | a.import(&c.export(ExportMode::updates(&b.oplog_vv())).unwrap()) |
| 440 | .unwrap(); |
| 441 | assert_eq!(a.get_deep_value().to_json_value(), json!({"text": "0"})); |
| 442 | |
| 443 | // b does not has c's change |
| 444 | a.import(&b.export(ExportMode::updates(&a.oplog_vv())).unwrap()) |
| 445 | .unwrap(); |
| 446 | dbg!(&a.oplog().lock()); |
| 447 | assert_eq!(a.get_deep_value().to_json_value(), json!({"text": "210"})); |
| 448 | } |
| 449 | |
| 450 | #[test] |
| 451 | fn test_checkout() { |
| 452 | let doc_0 = LoroDoc::new_auto_commit(); |
| 453 | doc_0.set_peer_id(0).unwrap(); |
| 454 | let doc_1 = LoroDoc::new_auto_commit(); |
| 455 | doc_1.set_peer_id(1).unwrap(); |
| 456 | |
| 457 | let value: Arc<Mutex<LoroValue>> = Arc::new(Mutex::new(LoroValue::Map(Default::default()))); |
| 458 | let root_value = value.clone(); |
| 459 | let _g = doc_0.subscribe_root(Arc::new(move |event| { |
| 460 | dbg!(&event); |
| 461 | let mut root_value = root_value.lock(); |
| 462 | for container_diff in event.events { |
| 463 | root_value.apply( |
| 464 | &container_diff.path.iter().map(|x| x.1.clone()).collect(), |
| 465 | std::slice::from_ref(&container_diff.diff), |
| 466 | ); |
| 467 | } |
| 468 | })); |
| 469 | |
| 470 | let map = doc_0.get_map("map"); |
| 471 | let handler = map |
| 472 | .insert_container("text", TextHandler::new_detached()) |
| 473 | .unwrap(); |
| 474 | let text = handler; |
| 475 | text.insert_unicode(0, "123").unwrap(); |
| 476 | |
| 477 | let map = doc_1.get_map("map"); |
| 478 | |
| 479 | let handler = map |
| 480 | .insert_container("text", TextHandler::new_detached()) |
| 481 | .unwrap(); |
| 482 | let text = handler; |
| 483 | text.insert_unicode(0, "123").unwrap(); |
| 484 | |
| 485 | doc_0 |
| 486 | .import(&doc_1.export(ExportMode::all_updates()).unwrap()) |
| 487 | .unwrap(); |
| 488 | |
| 489 | doc_0 |
| 490 | .checkout(&Frontiers::from(vec![ID::new(0, 2)])) |
| 491 | .unwrap(); |
| 492 | |
| 493 | assert_eq!(&doc_0.get_deep_value(), &*value.lock()); |
| 494 | assert_eq!( |
| 495 | value.lock().to_json_value(), |
| 496 | json!({ |
| 497 | "map": { |
| 498 | "text": "12" |
| 499 | } |
| 500 | }) |
| 501 | ); |
| 502 | } |
| 503 | |
| 504 | #[test] |
| 505 | fn test_timestamp() { |
| 506 | let doc = LoroDoc::new(); |
| 507 | doc.set_record_timestamp(true); |
| 508 | let text = doc.get_text("text"); |
| 509 | let mut txn = doc.txn().unwrap(); |
| 510 | text.insert_with_txn(&mut txn, 0, "123", PosType::Unicode) |
| 511 | .unwrap(); |
| 512 | txn.commit().unwrap(); |
| 513 | let op_log = &doc.oplog().lock(); |
| 514 | let change = op_log.get_change_at(ID::new(doc.peer_id(), 0)).unwrap(); |
| 515 | assert!(change.timestamp() > 1690966970); |
| 516 | } |
| 517 | |
| 518 | #[test] |
| 519 | fn test_text_checkout() { |
| 520 | let doc = LoroDoc::new_auto_commit(); |
| 521 | doc.set_peer_id(1).unwrap(); |
| 522 | let text = doc.get_text("text"); |
| 523 | text.insert_unicode(0, "你界").unwrap(); |
| 524 | text.insert_unicode(1, "好世").unwrap(); |
| 525 | doc.commit_then_renew(); |
| 526 | { |
| 527 | doc.checkout(&Frontiers::from([ID::new(doc.peer_id(), 0)].as_slice())) |
| 528 | .unwrap(); |
| 529 | assert_eq!(text.get_value().as_string().unwrap().as_str(), "你"); |
| 530 | } |
| 531 | { |
| 532 | doc.checkout(&Frontiers::from([ID::new(doc.peer_id(), 1)].as_slice())) |
| 533 | .unwrap(); |
| 534 | assert_eq!(text.get_value().as_string().unwrap().as_str(), "你界"); |
| 535 | } |
| 536 | { |
| 537 | doc.checkout(&Frontiers::from([ID::new(doc.peer_id(), 2)].as_slice())) |
| 538 | .unwrap(); |
| 539 | assert_eq!(text.get_value().as_string().unwrap().as_str(), "你好界"); |
| 540 | } |
| 541 | { |
| 542 | doc.checkout(&Frontiers::from([ID::new(doc.peer_id(), 3)].as_slice())) |
| 543 | .unwrap(); |
| 544 | assert_eq!(text.get_value().as_string().unwrap().as_str(), "你好世界"); |
| 545 | } |
| 546 | assert_eq!(text.len_unicode(), 4); |
| 547 | assert_eq!(text.len_utf8(), 12); |
| 548 | assert_eq!(text.len_unicode(), 4); |
| 549 | |
| 550 | doc.checkout_to_latest(); |
| 551 | text.delete_unicode(3, 1).unwrap(); |
| 552 | assert_eq!(text.get_value().as_string().unwrap().as_str(), "你好世"); |
| 553 | text.delete_unicode(2, 1).unwrap(); |
| 554 | assert_eq!(text.get_value().as_string().unwrap().as_str(), "你好"); |
| 555 | doc.checkout(&Frontiers::from([ID::new(doc.peer_id(), 3)].as_slice())) |
| 556 | .unwrap(); |
| 557 | assert_eq!(text.get_value().as_string().unwrap().as_str(), "你好世界"); |
| 558 | doc.checkout(&Frontiers::from([ID::new(doc.peer_id(), 4)].as_slice())) |
| 559 | .unwrap(); |
| 560 | assert_eq!(text.get_value().as_string().unwrap().as_str(), "你好世"); |
| 561 | doc.checkout(&Frontiers::from([ID::new(doc.peer_id(), 5)].as_slice())) |
| 562 | .unwrap(); |
| 563 | assert_eq!(text.get_value().as_string().unwrap().as_str(), "你好"); |
| 564 | { |
| 565 | doc.checkout(&Frontiers::from([ID::new(doc.peer_id(), 0)].as_slice())) |
| 566 | .unwrap(); |
| 567 | assert_eq!(text.get_value().as_string().unwrap().as_str(), "你"); |
| 568 | } |
| 569 | { |
| 570 | doc.checkout(&Frontiers::from([ID::new(doc.peer_id(), 1)].as_slice())) |
| 571 | .unwrap(); |
| 572 | assert_eq!(text.get_value().as_string().unwrap().as_str(), "你界"); |
| 573 | } |
| 574 | { |
| 575 | doc.checkout(&Frontiers::from([ID::new(doc.peer_id(), 2)].as_slice())) |
| 576 | .unwrap(); |
| 577 | assert_eq!(text.get_value().as_string().unwrap().as_str(), "你好界"); |
| 578 | } |
| 579 | { |
| 580 | doc.checkout(&Frontiers::from([ID::new(doc.peer_id(), 3)].as_slice())) |
| 581 | .unwrap(); |
| 582 | assert_eq!(text.get_value().as_string().unwrap().as_str(), "你好世界"); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | #[test] |
| 587 | fn map_checkout() { |
| 588 | let doc = LoroDoc::new_auto_commit(); |
| 589 | let meta = doc.get_map("meta"); |
| 590 | let v_empty = doc.oplog_frontiers(); |
| 591 | meta.insert("key", 0).unwrap(); |
| 592 | let v0 = doc.oplog_frontiers(); |
| 593 | meta.insert("key", 1).unwrap(); |
| 594 | let v1 = doc.oplog_frontiers(); |
| 595 | assert_eq!(meta.get_deep_value().to_json(), r#"{"key":1}"#); |
| 596 | doc.checkout(&v0).unwrap(); |
| 597 | assert_eq!(meta.get_deep_value().to_json(), r#"{"key":0}"#); |
| 598 | doc.checkout(&v_empty).unwrap(); |
| 599 | assert_eq!(meta.get_deep_value().to_json(), r#"{}"#); |
| 600 | doc.checkout(&v1).unwrap(); |
| 601 | assert_eq!(meta.get_deep_value().to_json(), r#"{"key":1}"#); |
| 602 | } |
| 603 | |
| 604 | #[test] |
| 605 | fn a_list_of_map_checkout() { |
| 606 | let doc = LoroDoc::new_auto_commit(); |
| 607 | let entry = doc.get_map("entry"); |
| 608 | let (list, sub) = { |
| 609 | let list = entry |
| 610 | .insert_container("list", ListHandler::new_detached()) |
| 611 | .unwrap(); |
| 612 | let sub_map = list |
| 613 | .insert_container(0, MapHandler::new_detached()) |
| 614 | .unwrap(); |
| 615 | sub_map.insert("x", 100).unwrap(); |
| 616 | sub_map.insert("y", 1000).unwrap(); |
| 617 | (list, sub_map) |
| 618 | }; |
| 619 | let v0 = doc.oplog_frontiers(); |
| 620 | let d0 = doc.get_deep_value().to_json(); |
| 621 | |
| 622 | list.insert(0, 3).unwrap(); |
| 623 | list.push(4).unwrap(); |
| 624 | list.insert_container(2, MapHandler::new_detached()) |
| 625 | .unwrap(); |
| 626 | list.insert_container(3, TextHandler::new_detached()) |
| 627 | .unwrap(); |
| 628 | |
| 629 | list.delete(2, 1).unwrap(); |
| 630 | sub.insert("x", 9).unwrap(); |
| 631 | sub.insert("y", 9).unwrap(); |
| 632 | sub.insert("z", 9).unwrap(); |
| 633 | let v1 = doc.oplog_frontiers(); |
| 634 | let d1 = doc.get_deep_value().to_json(); |
| 635 | sub.insert("x", 77).unwrap(); |
| 636 | sub.insert("y", 88).unwrap(); |
| 637 | list.delete(0, 1).unwrap(); |
| 638 | list.insert(0, 123).unwrap(); |
| 639 | list.push(99).unwrap(); |
| 640 | let v2 = doc.oplog_frontiers(); |
| 641 | let d2 = doc.get_deep_value().to_json(); |
| 642 | |
| 643 | doc.checkout(&v0).unwrap(); |
| 644 | assert_eq!(doc.get_deep_value().to_json(), d0); |
| 645 | doc.checkout(&v1).unwrap(); |
| 646 | assert_eq!(doc.get_deep_value().to_json(), d1); |
| 647 | doc.checkout(&v2).unwrap(); |
| 648 | println!("{}", doc.get_deep_value_with_id().to_json_pretty()); |
| 649 | assert_eq!(doc.get_deep_value().to_json(), d2); |
| 650 | doc.checkout(&v1).unwrap(); |
| 651 | |
| 652 | println!("{}", doc.get_deep_value_with_id().to_json_pretty()); |
| 653 | assert_eq!(doc.get_deep_value().to_json(), d1); |
| 654 | doc.checkout(&v0).unwrap(); |
| 655 | assert_eq!(doc.get_deep_value().to_json(), d0); |
| 656 | } |
| 657 | |
| 658 | #[test] |
| 659 | fn map_concurrent_checkout() { |
| 660 | let doc_a = LoroDoc::new_auto_commit(); |
| 661 | let meta_a = doc_a.get_map("meta"); |
| 662 | let doc_b = LoroDoc::new_auto_commit(); |
| 663 | let meta_b = doc_b.get_map("meta"); |
| 664 | |
| 665 | meta_a.insert("key", 0).unwrap(); |
| 666 | let va = doc_a.oplog_frontiers(); |
| 667 | meta_b.insert("s", 1).unwrap(); |
| 668 | let vb_0 = doc_b.oplog_frontiers(); |
| 669 | meta_b.insert("key", 1).unwrap(); |
| 670 | let vb_1 = doc_b.oplog_frontiers(); |
| 671 | doc_a |
| 672 | .import(&doc_b.export(ExportMode::Snapshot).unwrap()) |
| 673 | .unwrap(); |
| 674 | meta_a.insert("key", 2).unwrap(); |
| 675 | |
| 676 | let v_merged = doc_a.oplog_frontiers(); |
| 677 | |
| 678 | doc_a.checkout(&va).unwrap(); |
| 679 | assert_eq!(meta_a.get_deep_value().to_json_value(), json!({"key":0})); |
| 680 | doc_a.checkout(&vb_0).unwrap(); |
| 681 | assert_eq!(meta_a.get_deep_value().to_json_value(), json!({"s":1})); |
| 682 | doc_a.checkout(&vb_1).unwrap(); |
| 683 | assert_eq!( |
| 684 | meta_a.get_deep_value().to_json_value(), |
| 685 | json!({"s":1,"key":1}) |
| 686 | ); |
| 687 | doc_a.checkout(&v_merged).unwrap(); |
| 688 | assert_eq!( |
| 689 | meta_a.get_deep_value().to_json_value(), |
| 690 | json!({"s":1,"key":2}) |
| 691 | ); |
| 692 | } |
| 693 | |
| 694 | #[test] |
| 695 | fn tree_checkout() { |
| 696 | let doc_a = LoroDoc::new_auto_commit(); |
| 697 | let _g = doc_a.subscribe_root(Arc::new(|_e| {})); |
| 698 | doc_a.set_peer_id(1).unwrap(); |
| 699 | let tree = doc_a.get_tree("root"); |
| 700 | let id1 = tree.create(TreeParentId::Root).unwrap(); |
| 701 | let id2 = tree.create(TreeParentId::Node(id1)).unwrap(); |
| 702 | let v1_state = tree.get_deep_value(); |
| 703 | let v1 = doc_a.oplog_frontiers(); |
| 704 | let _id3 = tree.create(TreeParentId::Node(id2)).unwrap(); |
| 705 | let v2_state = tree.get_deep_value(); |
| 706 | let v2 = doc_a.oplog_frontiers(); |
| 707 | tree.delete(id2).unwrap(); |
| 708 | let v3_state = tree.get_deep_value(); |
| 709 | let v3 = doc_a.oplog_frontiers(); |
| 710 | doc_a.checkout(&v1).unwrap(); |
| 711 | assert_eq!( |
| 712 | serde_json::to_value(tree.get_deep_value()) |
| 713 | .unwrap() |
| 714 | .get("roots"), |
| 715 | serde_json::to_value(v1_state).unwrap().get("roots") |
| 716 | ); |
| 717 | doc_a.checkout(&v2).unwrap(); |
| 718 | assert_eq!( |
| 719 | serde_json::to_value(tree.get_deep_value()) |
| 720 | .unwrap() |
| 721 | .get("roots"), |
| 722 | serde_json::to_value(v2_state).unwrap().get("roots") |
| 723 | ); |
| 724 | doc_a.checkout(&v3).unwrap(); |
| 725 | assert_eq!( |
| 726 | serde_json::to_value(tree.get_deep_value()) |
| 727 | .unwrap() |
| 728 | .get("roots"), |
| 729 | serde_json::to_value(v3_state).unwrap().get("roots") |
| 730 | ); |
| 731 | |
| 732 | doc_a.attach(); |
| 733 | tree.create(TreeParentId::Root).unwrap(); |
| 734 | } |
| 735 | |
| 736 | #[test] |
| 737 | fn issue_batch_import_snapshot() { |
| 738 | let doc = LoroDoc::new_auto_commit(); |
| 739 | doc.set_peer_id(123).unwrap(); |
| 740 | let doc2 = LoroDoc::new_auto_commit(); |
| 741 | doc2.set_peer_id(456).unwrap(); |
| 742 | doc.get_map("map").insert("s", "hello world!").unwrap(); |
| 743 | doc2.get_map("map").insert("s", "hello?").unwrap(); |
| 744 | |
| 745 | let data1 = doc.export(ExportMode::Snapshot).unwrap(); |
| 746 | let data2 = doc2.export(ExportMode::Snapshot).unwrap(); |
| 747 | let doc3 = LoroDoc::new(); |
| 748 | doc3.import_batch(&[data1, data2]).unwrap(); |
| 749 | } |
| 750 | |
| 751 | #[test] |
| 752 | fn state_may_deadlock_when_import() { |
| 753 | // helper function ref: https://github.com/rust-lang/rfcs/issues/2798#issuecomment-552949300 |
| 754 | use std::time::Duration; |
| 755 | use std::{sync::mpsc, thread}; |
| 756 | fn panic_after<T, F>(d: Duration, f: F) -> T |
| 757 | where |
| 758 | T: Send + 'static, |
| 759 | F: FnOnce() -> T, |
| 760 | F: Send + 'static, |
| 761 | { |
| 762 | let (done_tx, done_rx) = mpsc::channel(); |
| 763 | let handle = thread::spawn(move || { |
| 764 | let val = f(); |
| 765 | done_tx.send(()).expect("Unable to send completion signal"); |
| 766 | val |
| 767 | }); |
| 768 | |
| 769 | match done_rx.recv_timeout(d) { |
| 770 | Ok(_) => handle.join().expect("Thread panicked"), |
| 771 | Err(_) => panic!("Thread took too long"), |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | panic_after(Duration::from_millis(100), || { |
| 776 | let doc = LoroDoc::new_auto_commit(); |
| 777 | let map = doc.get_map("map"); |
| 778 | let _g = doc.subscribe_root(Arc::new(move |_e| { |
| 779 | map.id(); |
| 780 | })); |
| 781 | |
| 782 | let doc2 = LoroDoc::new_auto_commit(); |
| 783 | doc2.get_map("map").insert("foo", 123).unwrap(); |
| 784 | doc.import(&doc.export(ExportMode::Snapshot).unwrap()) |
| 785 | .unwrap(); |
| 786 | }) |
| 787 | } |
| 788 | |
| 789 | #[ctor::ctor] |
| 790 | fn init() { |
| 791 | dev_utils::setup_test_log(); |
| 792 | } |
| 793 | |
| 794 | #[test] |
| 795 | fn missing_event_when_checkout() { |
| 796 | let doc = LoroDoc::new_auto_commit(); |
| 797 | doc.checkout(&doc.oplog_frontiers()).unwrap(); |
| 798 | let value = Arc::new(Mutex::new(FxHashMap::default())); |
| 799 | let map = value.clone(); |
| 800 | let _g = doc.subscribe( |
| 801 | &ContainerID::new_root("tree", ContainerType::Tree), |
| 802 | Arc::new(move |e| { |
| 803 | let mut v = map.lock(); |
| 804 | for container_diff in e.events.iter() { |
| 805 | let from_children = |
| 806 | container_diff.id != ContainerID::new_root("tree", ContainerType::Tree); |
| 807 | if from_children { |
| 808 | if let Diff::Map(map) = &container_diff.diff { |
| 809 | for (k, ResolvedMapValue { value, .. }) in map.updated.iter() { |
| 810 | match value { |
| 811 | Some(value) => { |
| 812 | v.insert( |
| 813 | k.to_string(), |
| 814 | *value.as_value().unwrap().as_i64().unwrap(), |
| 815 | ); |
| 816 | } |
| 817 | None => { |
| 818 | v.remove(&k.to_string()); |
| 819 | } |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | }), |
| 826 | ); |
| 827 | |
| 828 | let doc2 = LoroDoc::new_auto_commit(); |
| 829 | let tree = doc2.get_tree("tree"); |
| 830 | let node = tree.create_at(TreeParentId::Root, 0).unwrap(); |
| 831 | let _ = tree.create_at(TreeParentId::Root, 0).unwrap(); |
| 832 | let meta = tree.get_meta(node).unwrap(); |
| 833 | meta.insert("a", 0).unwrap(); |
| 834 | doc.import(&doc2.export(ExportMode::updates(&doc.oplog_vv())).unwrap()) |
| 835 | .unwrap(); |
| 836 | doc.attach(); |
| 837 | meta.insert("b", 1).unwrap(); |
| 838 | doc.checkout(&doc.oplog_frontiers()).unwrap(); |
| 839 | doc.import(&doc2.export(ExportMode::updates(&doc.oplog_vv())).unwrap()) |
| 840 | .unwrap(); |
| 841 | // checkout use the same diff_calculator, the depth of calculator is not updated |
| 842 | doc.attach(); |
| 843 | assert!(value.lock().contains_key("b")); |
| 844 | } |
| 845 | |
| 846 | #[test] |
| 847 | fn empty_event() { |
| 848 | let doc = LoroDoc::new_auto_commit(); |
| 849 | doc.get_map("map").insert("key", 123).unwrap(); |
| 850 | doc.commit_then_renew(); |
| 851 | let fire = Arc::new(AtomicBool::new(false)); |
| 852 | let fire_clone = Arc::clone(&fire); |
| 853 | let _g = doc.subscribe_root(Arc::new(move |_e| { |
| 854 | fire_clone.store(true, std::sync::atomic::Ordering::Relaxed); |
| 855 | })); |
| 856 | doc.import(&doc.export(ExportMode::Snapshot).unwrap()) |
| 857 | .unwrap(); |
| 858 | assert!(!fire.load(std::sync::atomic::Ordering::Relaxed)); |
| 859 | } |
| 860 | |
| 861 | #[test] |
| 862 | fn insert_attach_container() -> LoroResult<()> { |
| 863 | let doc = LoroDoc::new_auto_commit(); |
| 864 | let list = doc.get_list("list"); |
| 865 | list.insert_container(0, MapHandler::new_detached())? |
| 866 | .insert("key", 1)?; |
| 867 | list.insert_container(1, MapHandler::new_detached())? |
| 868 | .insert("key", 2)?; |
| 869 | let elem = list.insert_container(2, MapHandler::new_detached())?; |
| 870 | elem.insert("key", 3)?; |
| 871 | let new_map = list.insert_container(0, elem)?; |
| 872 | assert_eq!(new_map.get_value().to_json_value(), json!({"key": 3})); |
| 873 | list.delete(3, 1)?; |
| 874 | |
| 875 | let elem = list.insert_container(0, TextHandler::new_detached())?; |
| 876 | elem.insert(0, "abc", PosType::Unicode)?; |
| 877 | elem.mark(0, 2, "bold", true.into(), PosType::Event)?; |
| 878 | let new_text = list.insert_container(0, elem)?; |
| 879 | assert_eq!( |
| 880 | new_text.get_richtext_value().to_json_value(), |
| 881 | json!([{"insert":"ab", "attributes": {"bold": true}}, {"insert":"c"}]) |
| 882 | ); |
| 883 | |
| 884 | let elem = list.insert_container(0, ListHandler::new_detached())?; |
| 885 | elem.insert(0, "list")?; |
| 886 | let new_list = list.insert_container(0, elem)?; |
| 887 | new_list.insert(0, "new_list")?; |
| 888 | assert_eq!( |
| 889 | new_list.get_value().to_json_value(), |
| 890 | json!(["new_list", "list"]) |
| 891 | ); |
| 892 | |
| 893 | // let elem = list.insert_container(2, TreeHandler::new_detached())?; |
| 894 | // let p = elem.create(None)?; |
| 895 | // elem.create(p)?; |
| 896 | // list.insert_container(0, elem)?; |
| 897 | |
| 898 | assert_eq!( |
| 899 | doc.get_deep_value().to_json_value(), |
| 900 | json!({ |
| 901 | "list": [["new_list", "list"], ["list"],"abc", "abc", {"key": 3}, {"key": 1}, {"key": 2}] |
| 902 | }) |
| 903 | ); |
| 904 | Ok(()) |
| 905 | } |
| 906 | |
| 907 | #[test] |
| 908 | fn tree_attach() { |
| 909 | let tree = TreeHandler::new_detached(); |
| 910 | let id = tree.create(TreeParentId::Root).unwrap(); |
| 911 | tree.get_meta(id).unwrap().insert("key", "value").unwrap(); |
| 912 | let doc = LoroDoc::new_auto_commit(); |
| 913 | doc.get_list("list").insert_container(0, tree).unwrap(); |
| 914 | let v = doc.get_deep_value(); |
| 915 | assert_eq!( |
| 916 | v.as_map().unwrap().get("list").unwrap().as_list().unwrap()[0] |
| 917 | .as_list() |
| 918 | .unwrap()[0] |
| 919 | .as_map() |
| 920 | .unwrap() |
| 921 | .get("meta") |
| 922 | .unwrap() |
| 923 | .to_json_value(), |
| 924 | json!({"key":"value"}) |
| 925 | ) |
| 926 | } |
| 927 | |
| 928 | #[test] |
| 929 | #[cfg(feature = "counter")] |
| 930 | fn counter() { |
| 931 | let doc = LoroDoc::new_auto_commit(); |
| 932 | let counter = doc.get_counter("counter"); |
| 933 | counter.increment(1.).unwrap(); |
| 934 | counter.increment(2.).unwrap(); |
| 935 | counter.decrement(1.).unwrap(); |
| 936 | let json = doc |
| 937 | .export_json_updates(&Default::default(), &doc.oplog_vv(), true) |
| 938 | .unwrap(); |
| 939 | let doc2 = LoroDoc::new_auto_commit(); |
| 940 | doc2.import_json_updates(json).unwrap(); |
| 941 | } |
| 942 | |
| 943 | #[test] |
| 944 | fn test_insert_utf8() { |
| 945 | let doc = LoroDoc::new_auto_commit(); |
| 946 | let text = doc.get_text("text"); |
| 947 | text.insert_utf8(0, "Hello ").unwrap(); |
| 948 | text.insert_utf8(6, "World").unwrap(); |
| 949 | assert_eq!( |
| 950 | text.get_richtext_value().to_json_value(), |
| 951 | json!([{"insert":"Hello World"}]) |
| 952 | ) |
| 953 | } |
| 954 | |
| 955 | #[test] |
| 956 | fn test_insert_utf8_cross_unicode_1() { |
| 957 | let doc = LoroDoc::new_auto_commit(); |
| 958 | let text = doc.get_text("text"); |
| 959 | text.insert_utf8(0, "你好").unwrap(); |
| 960 | text.insert_utf8(3, "World").unwrap(); |
| 961 | assert_eq!( |
| 962 | text.get_richtext_value().to_json_value(), |
| 963 | json!([{"insert":"你World好"}]) |
| 964 | ) |
| 965 | } |
| 966 | |
| 967 | #[test] |
| 968 | fn test_insert_utf8_cross_unicode_2() { |
| 969 | let doc = LoroDoc::new_auto_commit(); |
| 970 | let text = doc.get_text("text"); |
| 971 | text.insert_utf8(0, "你好").unwrap(); |
| 972 | text.insert_utf8(6, "World").unwrap(); |
| 973 | assert_eq!( |
| 974 | text.get_richtext_value().to_json_value(), |
| 975 | json!([{"insert":"你好World"}]) |
| 976 | ) |
| 977 | } |
| 978 | |
| 979 | #[test] |
| 980 | fn test_insert_utf8_detached() { |
| 981 | let text = TextHandler::new_detached(); |
| 982 | text.insert_utf8(0, "Hello ").unwrap(); |
| 983 | text.insert_utf8(6, "World").unwrap(); |
| 984 | assert_eq!( |
| 985 | text.get_richtext_value().to_json_value(), |
| 986 | json!([{"insert":"Hello World"}]) |
| 987 | ) |
| 988 | } |
| 989 | |
| 990 | #[test] |
| 991 | #[should_panic] |
| 992 | #[ignore = "fix me later after gbtree support Result for query"] |
| 993 | fn test_insert_utf8_panic_cross_unicode() { |
| 994 | let doc = LoroDoc::new_auto_commit(); |
| 995 | let text = doc.get_text("text"); |
| 996 | text.insert_utf8(0, "你好").unwrap(); |
| 997 | text.insert_utf8(1, "World").unwrap(); |
| 998 | } |
| 999 | |
| 1000 | #[test] |
| 1001 | #[should_panic] |
| 1002 | fn test_insert_utf8_panic_out_bound() { |
| 1003 | let doc = LoroDoc::new_auto_commit(); |
| 1004 | let text = doc.get_text("text"); |
| 1005 | text.insert_utf8(0, "Hello ").unwrap(); |
| 1006 | text.insert_utf8(7, "World").unwrap(); |
| 1007 | } |
| 1008 | |
| 1009 | // println!("{}", text.get_richtext_value().to_json_value().to_string()); |
| 1010 | |
| 1011 | #[test] |
| 1012 | fn test_delete_utf8() { |
| 1013 | let doc = LoroDoc::new_auto_commit(); |
| 1014 | let text = doc.get_text("text"); |
| 1015 | text.insert_utf8(0, "Hello").unwrap(); |
| 1016 | text.delete_utf8(1, 3).unwrap(); |
| 1017 | assert_eq!( |
| 1018 | text.get_richtext_value().to_json_value(), |
| 1019 | json!([{"insert":"Ho"}]) |
| 1020 | ) |
| 1021 | } |
| 1022 | |
| 1023 | #[test] |
| 1024 | fn test_delete_utf8_with_zero_len() { |
| 1025 | let doc = LoroDoc::new_auto_commit(); |
| 1026 | let text = doc.get_text("text"); |
| 1027 | text.insert_utf8(0, "Hello").unwrap(); |
| 1028 | text.delete_utf8(1, 0).unwrap(); |
| 1029 | assert_eq!( |
| 1030 | text.get_richtext_value().to_json_value(), |
| 1031 | json!([{"insert":"Hello"}]) |
| 1032 | ) |
| 1033 | } |
| 1034 | |
| 1035 | #[test] |
| 1036 | fn test_delete_utf8_cross_unicode() { |
| 1037 | let doc = LoroDoc::new_auto_commit(); |
| 1038 | let text = doc.get_text("text"); |
| 1039 | text.insert_utf8(0, "你好").unwrap(); |
| 1040 | text.delete_utf8(0, 3).unwrap(); |
| 1041 | assert_eq!( |
| 1042 | text.get_richtext_value().to_json_value(), |
| 1043 | json!([{"insert":"好"}]) |
| 1044 | ) |
| 1045 | } |
| 1046 | |
| 1047 | #[test] |
| 1048 | fn test_delete_utf8_detached() { |
| 1049 | let text = TextHandler::new_detached(); |
| 1050 | text.insert_utf8(0, "Hello").unwrap(); |
| 1051 | text.delete_utf8(1, 3).unwrap(); |
| 1052 | assert_eq!( |
| 1053 | text.get_richtext_value().to_json_value(), |
| 1054 | json!([{"insert":"Ho"}]) |
| 1055 | ) |
| 1056 | } |
| 1057 | |
| 1058 | // WARNING: |
| 1059 | // Due to the current inability to report an error on |
| 1060 | // get_offset_and_found on BTree, this test won't be ok. |
| 1061 | // #[test] |
| 1062 | // #[should_panic] |
| 1063 | // fn test_delete_utf8_panic_cross_unicode() { |
| 1064 | // let doc = LoroDoc::new_auto_commit(); |
| 1065 | // let text = doc.get_text("text"); |
| 1066 | // text.insert_utf8(0, "你好").unwrap(); |
| 1067 | // text.delete_utf8(0, 2).unwrap(); |
| 1068 | // } |
| 1069 | |
| 1070 | #[test] |
| 1071 | #[should_panic] |
| 1072 | fn test_delete_utf8_panic_out_bound_pos() { |
| 1073 | let doc = LoroDoc::new_auto_commit(); |
| 1074 | let text = doc.get_text("text"); |
| 1075 | text.insert_unicode(0, "Hello").unwrap(); |
| 1076 | text.delete_utf8(10, 1).unwrap(); |
| 1077 | } |
| 1078 | |
| 1079 | #[test] |
| 1080 | #[should_panic] |
| 1081 | fn test_delete_utf8_panic_out_bound_len() { |
| 1082 | let doc = LoroDoc::new_auto_commit(); |
| 1083 | let text = doc.get_text("text"); |
| 1084 | text.insert_unicode(0, "Hello").unwrap(); |
| 1085 | text.delete_utf8(1, 10).unwrap(); |
| 1086 | } |
| 1087 | |
| 1088 | #[test] |
| 1089 | fn test_char_at() { |
| 1090 | let doc = LoroDoc::new_auto_commit(); |
| 1091 | let text = doc.get_text("text"); |
| 1092 | text.insert_unicode(0, "Herld").unwrap(); |
| 1093 | text.insert_unicode(2, "llo Wo").unwrap(); |
| 1094 | assert_eq!(text.char_at(0, PosType::Unicode).unwrap(), 'H'); |
| 1095 | assert_eq!(text.char_at(1, PosType::Unicode).unwrap(), 'e'); |
| 1096 | assert_eq!(text.char_at(2, PosType::Unicode).unwrap(), 'l'); |
| 1097 | assert_eq!(text.char_at(3, PosType::Unicode).unwrap(), 'l'); |
| 1098 | let err = text.char_at(15, PosType::Unicode).unwrap_err(); |
| 1099 | assert!(matches!(err, loro_common::LoroError::OutOfBound { .. })) |
| 1100 | } |
| 1101 | |
| 1102 | #[test] |
| 1103 | fn test_char_at_detached() { |
| 1104 | let text = TextHandler::new_detached(); |
| 1105 | text.insert_unicode(0, "Herld").unwrap(); |
| 1106 | text.insert_unicode(2, "llo Wo").unwrap(); |
| 1107 | assert_eq!(text.char_at(0, PosType::Unicode).unwrap(), 'H'); |
| 1108 | assert_eq!(text.char_at(1, PosType::Unicode).unwrap(), 'e'); |
| 1109 | assert_eq!(text.char_at(2, PosType::Unicode).unwrap(), 'l'); |
| 1110 | assert_eq!(text.char_at(3, PosType::Unicode).unwrap(), 'l'); |
| 1111 | let err = text.char_at(15, PosType::Unicode).unwrap_err(); |
| 1112 | assert!(matches!(err, loro_common::LoroError::OutOfBound { .. })) |
| 1113 | } |
| 1114 | |
| 1115 | #[test] |
| 1116 | fn test_char_at_wchar() { |
| 1117 | let doc = LoroDoc::new_auto_commit(); |
| 1118 | let text = doc.get_text("text"); |
| 1119 | text.insert_unicode(0, "你好").unwrap(); |
| 1120 | text.insert_unicode(1, "世界").unwrap(); |
| 1121 | assert_eq!(text.char_at(0, PosType::Unicode).unwrap(), '你'); |
| 1122 | assert_eq!(text.char_at(1, PosType::Unicode).unwrap(), '世'); |
| 1123 | assert_eq!(text.char_at(2, PosType::Unicode).unwrap(), '界'); |
| 1124 | assert_eq!(text.char_at(3, PosType::Unicode).unwrap(), '好'); |
| 1125 | let err = text.char_at(5, PosType::Unicode).unwrap_err(); |
| 1126 | assert!(matches!(err, loro_common::LoroError::OutOfBound { .. })) |
| 1127 | } |
| 1128 | |
| 1129 | #[test] |
| 1130 | fn test_text_slice() { |
| 1131 | let doc = LoroDoc::new_auto_commit(); |
| 1132 | let text = doc.get_text("text"); |
| 1133 | text.insert_unicode(0, "Hello").unwrap(); |
| 1134 | text.insert_unicode(1, "World").unwrap(); |
| 1135 | assert_eq!(text.slice(0, 4, PosType::Unicode).unwrap(), "HWor"); |
| 1136 | assert_eq!(text.slice(0, 1, PosType::Unicode).unwrap(), "H"); |
| 1137 | } |
| 1138 | |
| 1139 | #[test] |
| 1140 | fn test_text_slice_detached() { |
| 1141 | let text = TextHandler::new_detached(); |
| 1142 | text.insert_unicode(0, "Herld").unwrap(); |
| 1143 | text.insert_unicode(2, "llo Wo").unwrap(); |
| 1144 | assert_eq!(text.slice(0, 4, PosType::Unicode).unwrap(), "Hell"); |
| 1145 | assert_eq!(text.slice(0, 1, PosType::Unicode).unwrap(), "H"); |
| 1146 | } |
| 1147 | |
| 1148 | #[test] |
| 1149 | fn test_text_slice_wchar() { |
| 1150 | let doc = LoroDoc::new_auto_commit(); |
| 1151 | let text = doc.get_text("text"); |
| 1152 | text.insert_unicode(0, "你好").unwrap(); |
| 1153 | text.insert_unicode(1, "世界").unwrap(); |
| 1154 | assert_eq!(text.slice(0, 3, PosType::Unicode).unwrap(), "你世界"); |
| 1155 | } |
| 1156 | |
| 1157 | #[test] |
| 1158 | #[should_panic] |
| 1159 | fn test_text_slice_end_index_less_than_start() { |
| 1160 | let doc = LoroDoc::new_auto_commit(); |
| 1161 | let text = doc.get_text("text"); |
| 1162 | text.insert_unicode(0, "你好").unwrap(); |
| 1163 | text.insert_unicode(1, "世界").unwrap(); |
| 1164 | text.slice(2, 1, PosType::Unicode).unwrap(); |
| 1165 | } |
| 1166 | |
| 1167 | #[test] |
| 1168 | #[should_panic] |
| 1169 | fn test_text_slice_out_of_bound() { |
| 1170 | let doc = LoroDoc::new_auto_commit(); |
| 1171 | let text = doc.get_text("text"); |
| 1172 | text.insert_unicode(0, "你好").unwrap(); |
| 1173 | text.insert_unicode(1, "世界").unwrap(); |
| 1174 | text.slice(1, 10, PosType::Unicode).unwrap(); |
| 1175 | } |
| 1176 | |
| 1177 | #[test] |
| 1178 | fn test_text_splice() { |
| 1179 | let doc = LoroDoc::new_auto_commit(); |
| 1180 | let text = doc.get_text("text"); |
| 1181 | text.insert_unicode(0, "你好").unwrap(); |
| 1182 | assert_eq!(text.splice(1, 1, "世界", PosType::Unicode).unwrap(), "好"); |
| 1183 | assert_eq!(text.to_string(), "你世界"); |
| 1184 | } |
| 1185 | |
| 1186 | #[test] |
| 1187 | fn test_text_iter() { |
| 1188 | let mut str = String::new(); |
| 1189 | let doc = LoroDoc::new_auto_commit(); |
| 1190 | let text = doc.get_text("text"); |
| 1191 | text.insert_unicode(0, "Hello").unwrap(); |
| 1192 | text.insert_unicode(1, "Hello").unwrap(); |
| 1193 | text.iter(|s| { |
| 1194 | str.push_str(s); |
| 1195 | true |
| 1196 | }); |
| 1197 | assert_eq!(str, "HHelloello"); |
| 1198 | str = String::new(); |
| 1199 | let mut i = 0; |
| 1200 | text.iter(|s| { |
| 1201 | if i == 1 { |
| 1202 | return false; |
| 1203 | } |
| 1204 | str.push_str(s); |
| 1205 | i += 1; |
| 1206 | true |
| 1207 | }); |
| 1208 | assert_eq!(str, "H"); |
| 1209 | } |
| 1210 | |
| 1211 | #[test] |
| 1212 | fn test_text_iter_detached() { |
| 1213 | let mut str = String::new(); |
| 1214 | let text = TextHandler::new_detached(); |
| 1215 | text.insert_unicode(0, "Hello").unwrap(); |
| 1216 | text.insert_unicode(1, "Hello").unwrap(); |
| 1217 | text.iter(|s| { |
| 1218 | str.push_str(s); |
| 1219 | true |
| 1220 | }); |
| 1221 | assert_eq!(str, "HHelloello"); |
| 1222 | } |
| 1223 | |
| 1224 | #[test] |
| 1225 | fn test_text_update() { |
| 1226 | let doc = LoroDoc::new_auto_commit(); |
| 1227 | let text = doc.get_text("text"); |
| 1228 | text.insert_unicode(0, "Hello 😊Bro").unwrap(); |
| 1229 | text.update("Hello World Bro😊", Default::default()) |
| 1230 | .unwrap(); |
| 1231 | assert_eq!(text.to_string(), "Hello World Bro😊"); |
| 1232 | } |
| 1233 | |
| 1234 | #[test] |
| 1235 | fn test_map_contains_key() { |
| 1236 | let doc = LoroDoc::new_auto_commit(); |
| 1237 | let map = doc.get_map("m"); |
| 1238 | assert!(!map.contains_key("bro")); |
| 1239 | map.insert("bro", 114514).unwrap(); |
| 1240 | assert!(map.contains_key("bro")); |
| 1241 | map.delete("bro").unwrap(); |
| 1242 | assert!(!map.contains_key("bro")); |
| 1243 | } |
| 1244 | |
| 1245 | #[test] |
| 1246 | fn set_max_peer_id() { |
| 1247 | let doc = LoroDoc::new_auto_commit(); |
| 1248 | assert_eq!( |
| 1249 | doc.set_peer_id(PeerID::MAX), |
| 1250 | Result::Err(LoroError::InvalidPeerID) |
| 1251 | ); |
| 1252 | } |
| 1253 | |
| 1254 | #[test] |
| 1255 | fn import_status() -> LoroResult<()> { |
| 1256 | let doc = LoroDoc::new_auto_commit(); |
| 1257 | doc.set_peer_id(0)?; |
| 1258 | let t = doc.get_text("text"); |
| 1259 | t.insert_unicode(0, "a")?; |
| 1260 | |
| 1261 | let doc2 = LoroDoc::new_auto_commit(); |
| 1262 | doc2.set_peer_id(1)?; |
| 1263 | let t2 = doc2.get_text("text"); |
| 1264 | t2.insert_unicode(0, "b")?; |
| 1265 | doc2.commit_then_renew(); |
| 1266 | let update1 = doc2.export(ExportMode::Snapshot).unwrap(); |
| 1267 | let vv1 = doc2.oplog_vv(); |
| 1268 | t2.insert_unicode(1, "c")?; |
| 1269 | let update2 = doc2.export(ExportMode::updates(&vv1)).unwrap(); |
| 1270 | |
| 1271 | let status1 = doc.import(&update2)?; |
| 1272 | let status2 = doc.import(&update1)?; |
| 1273 | assert_eq!( |
| 1274 | status1, |
| 1275 | ImportStatus { |
| 1276 | success: Default::default(), |
| 1277 | pending: Some(VersionRange::from_map(fx_map!(1=>(1, 2)))) |
| 1278 | } |
| 1279 | ); |
| 1280 | assert_eq!( |
| 1281 | status2, |
| 1282 | ImportStatus { |
| 1283 | success: VersionRange::from_map(fx_map!(1=>(0, 2))), |
| 1284 | pending: None |
| 1285 | } |
| 1286 | ); |
| 1287 | |
| 1288 | Ok(()) |
| 1289 | } |
| 1290 | |
| 1291 | #[test] |
| 1292 | fn test_on_first_commit_from_peer() { |
| 1293 | let doc = LoroDoc::new_auto_commit(); |
| 1294 | doc.set_peer_id(0).unwrap(); |
| 1295 | let p = Arc::new(Mutex::new(vec![])); |
| 1296 | let p2 = Arc::clone(&p); |
| 1297 | let sub = doc.subscribe_first_commit_from_peer(Box::new(move |e| { |
| 1298 | p2.lock().push(e.peer); |
| 1299 | true |
| 1300 | })); |
| 1301 | doc.get_text("text").insert_unicode(0, "a").unwrap(); |
| 1302 | doc.commit_then_renew(); |
| 1303 | doc.get_text("text").insert_unicode(0, "b").unwrap(); |
| 1304 | doc.commit_then_renew(); |
| 1305 | doc.set_peer_id(1).unwrap(); |
| 1306 | doc.get_text("text").insert_unicode(0, "c").unwrap(); |
| 1307 | doc.commit_then_renew(); |
| 1308 | sub.unsubscribe(); |
| 1309 | assert_eq!(p.lock().as_slice(), &[0, 1]); |
| 1310 | } |
| 1311 | |
| 1312 | #[test] |
| 1313 | fn test_on_first_commit_from_peer_when_drop_doc() { |
| 1314 | let doc = LoroDoc::new_auto_commit(); |
| 1315 | doc.set_peer_id(0).unwrap(); |
| 1316 | let p = Arc::new(Mutex::new(vec![])); |
| 1317 | let p2 = Arc::clone(&p); |
| 1318 | let _sub = doc.subscribe_first_commit_from_peer(Box::new(move |e| { |
| 1319 | p2.lock().push(e.peer); |
| 1320 | true |
| 1321 | })); |
| 1322 | doc.get_text("text").insert_unicode(0, "a").unwrap(); |
| 1323 | drop(doc); |
| 1324 | assert_eq!(p.lock().as_slice(), &[0]); |
| 1325 | } |
| 1326 | |
| 1327 | #[test] |
| 1328 | fn test_on_first_commit_from_peer_and_set_peer_id() { |
| 1329 | let doc = LoroDoc::new_auto_commit(); |
| 1330 | doc.set_peer_id(0).unwrap(); |
| 1331 | let f = Arc::new(AtomicBool::new(false)); |
| 1332 | let f2 = Arc::clone(&f); |
| 1333 | let sub = doc.subscribe_first_commit_from_peer(Box::new(move |_e| { |
| 1334 | f2.store(true, std::sync::atomic::Ordering::Relaxed); |
| 1335 | true |
| 1336 | })); |
| 1337 | doc.get_text("text").insert_unicode(0, "a").unwrap(); |
| 1338 | doc.set_peer_id(1).unwrap(); |
| 1339 | sub.unsubscribe(); |
| 1340 | assert!(f.load(std::sync::atomic::Ordering::Relaxed)); |
| 1341 | } |
| 1342 | |
| 1343 | #[test] |
| 1344 | fn test_on_first_commit_from_peer_with_lock() { |
| 1345 | let doc = LoroDoc::new_auto_commit(); |
| 1346 | doc.set_peer_id(0).unwrap(); |
| 1347 | let doc_clone = doc.clone(); |
| 1348 | let sub = doc.subscribe_first_commit_from_peer(Box::new(move |_e| { |
| 1349 | doc_clone.get_text("text").insert_unicode(0, "b").unwrap(); |
| 1350 | true |
| 1351 | })); |
| 1352 | doc.get_text("text").insert_unicode(0, "a").unwrap(); |
| 1353 | doc.commit_then_renew(); |
| 1354 | sub.unsubscribe(); |
| 1355 | assert_eq!(doc.get_text("text").to_string(), "ba"); |
| 1356 | assert_eq!( |
| 1357 | doc.export_json_updates(&Default::default(), &doc.oplog_vv(), false) |
| 1358 | .unwrap() |
| 1359 | .changes |
| 1360 | .len(), |
| 1361 | 1 |
| 1362 | ); |
| 1363 | } |
| 1364 | |
| 1365 | #[test] |
| 1366 | fn test_on_first_peer_commit_attach_user_id() { |
| 1367 | let doc = LoroDoc::new_auto_commit(); |
| 1368 | doc.set_peer_id(0).unwrap(); |
| 1369 | let doc_clone = doc.clone(); |
| 1370 | let sub = doc.subscribe_first_commit_from_peer(Box::new(move |e| { |
| 1371 | doc_clone |
| 1372 | .get_map("::loro::user_id") |
| 1373 | .insert(e.peer.to_string().as_str(), "user_bob") |
| 1374 | .unwrap(); |
| 1375 | true |
| 1376 | })); |
| 1377 | doc.get_text("text").insert_unicode(0, "a").unwrap(); |
| 1378 | doc.commit_then_renew(); |
| 1379 | sub.unsubscribe(); |
| 1380 | assert_eq!( |
| 1381 | doc.get_map("::loro::user_id").get_value(), |
| 1382 | loro_value!({ |
| 1383 | "0": "user_bob" |
| 1384 | }) |
| 1385 | ); |
| 1386 | } |
| 1387 | |
| 1388 | #[test] |
| 1389 | fn test_pre_commit_with_lock() { |
| 1390 | let doc = LoroDoc::new_auto_commit(); |
| 1391 | let doc_clone = doc.clone(); |
| 1392 | let sub = doc.subscribe_pre_commit(Box::new(move |_e| { |
| 1393 | // state lock |
| 1394 | doc_clone.get_deep_value(); |
| 1395 | // oplog lock |
| 1396 | doc_clone.oplog_vv(); |
| 1397 | true |
| 1398 | })); |
| 1399 | doc.get_text("text").insert_unicode(0, "a").unwrap(); |
| 1400 | doc.commit_then_renew(); |
| 1401 | sub.unsubscribe(); |
| 1402 | } |
| 1403 | |
| 1404 | #[test] |
| 1405 | fn test_pre_commit_with_hash() { |
| 1406 | let doc = LoroDoc::new_auto_commit(); |
| 1407 | doc.set_peer_id(0).unwrap(); |
| 1408 | let doc_clone = doc.clone(); |
| 1409 | let sub = doc.subscribe_pre_commit(Box::new(move |e| { |
| 1410 | let change_json = |
| 1411 | doc_clone.export_json_in_id_span(e.change_meta.id.to_span(e.change_meta.len)); |
| 1412 | assert!(change_json.len() == 1); |
| 1413 | let mut deps = vec![]; |
| 1414 | for dep in e.change_meta.deps.iter() { |
| 1415 | let dep_msg = doc_clone |
| 1416 | .oplog() |
| 1417 | .lock() |
| 1418 | .get_change_at(dep) |
| 1419 | .unwrap() |
| 1420 | .message() |
| 1421 | .cloned() |
| 1422 | .unwrap_or(Arc::from("")); |
| 1423 | deps.push(dep_msg); |
| 1424 | } |
| 1425 | e.modifier.set_timestamp(0).set_message(&format!( |
| 1426 | "{:08x}\n{}", |
| 1427 | // just for test example, should use sha256 or blake3 for hash |
| 1428 | xxhash_rust::xxh32::xxh32( |
| 1429 | serde_json::to_string(&(&change_json, &deps)) |
| 1430 | .unwrap() |
| 1431 | .as_bytes(), |
| 1432 | 0 |
| 1433 | ), |
| 1434 | e.change_meta.message() |
| 1435 | )); |
| 1436 | true |
| 1437 | })); |
| 1438 | doc.get_text("text").insert_unicode(0, "a").unwrap(); |
| 1439 | doc.commit_with( |
| 1440 | CommitOptions::default() |
| 1441 | .commit_msg("add a") |
| 1442 | .immediate_renew(true), |
| 1443 | ); |
| 1444 | doc.get_text("text").insert_unicode(0, "b").unwrap(); |
| 1445 | doc.commit_with( |
| 1446 | CommitOptions::default() |
| 1447 | .commit_msg("add b") |
| 1448 | .immediate_renew(true), |
| 1449 | ); |
| 1450 | sub.unsubscribe(); |
| 1451 | let changes = doc |
| 1452 | .export_json_updates(&Default::default(), &doc.oplog_vv(), false) |
| 1453 | .unwrap() |
| 1454 | .changes; |
| 1455 | assert_eq!(changes.len(), 2); |
| 1456 | for c in changes { |
| 1457 | let mut msg = c.msg.as_ref().unwrap().lines(); |
| 1458 | assert_eq!(msg.next().unwrap().len(), 8); |
| 1459 | assert!(msg.next().is_some()); |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | #[test] |
| 1464 | fn test_change_to_json_schema_include_uncommit() { |
| 1465 | let doc = LoroDoc::new_auto_commit(); |
| 1466 | doc.set_peer_id(0).unwrap(); |
| 1467 | doc.get_text("text").insert_unicode(0, "a").unwrap(); |
| 1468 | doc.commit_then_renew(); |
| 1469 | let doc_clone = doc.clone(); |
| 1470 | let _sub = doc.subscribe_pre_commit(Box::new(move |e| { |
| 1471 | let changes = doc_clone.export_json_in_id_span(IdSpan::new( |
| 1472 | 0, |
| 1473 | 0, |
| 1474 | e.change_meta.id.counter + e.change_meta.len as i32, |
| 1475 | )); |
| 1476 | assert_eq!(changes.len(), 2); |
| 1477 | true |
| 1478 | })); |
| 1479 | doc.get_text("text").insert_unicode(0, "b").unwrap(); |
| 1480 | let changes = doc.export_json_in_id_span(IdSpan::new(0, 0, 2)); |
| 1481 | assert_eq!(changes.len(), 1); |
| 1482 | doc.commit_then_renew(); |
| 1483 | // change merged |
| 1484 | assert_eq!(changes.len(), 1); |
| 1485 | } |
| 1486 | |
| 1487 | #[test] |
| 1488 | fn test_ephemeral_store() { |
| 1489 | let store = EphemeralStore::new(1000); |
| 1490 | let store_clone = store.clone(); |
| 1491 | let _sub = store.subscribe(Box::new(move |_| { |
| 1492 | store_clone.get_all_states(); |
| 1493 | true |
| 1494 | })); |
| 1495 | store.set("a", 1); |
| 1496 | store.set("b", 2); |
| 1497 | store.set("c", 3); |
| 1498 | } |
| 1499 | |
| 1500 | #[test] |
| 1501 | fn test_origin() { |
| 1502 | let doc = LoroDoc::new_auto_commit(); |
| 1503 | doc.set_peer_id(0).unwrap(); |
| 1504 | let remote = LoroDoc::new_auto_commit(); |
| 1505 | remote.set_peer_id(1).unwrap(); |
| 1506 | doc.get_map("map").insert("a", 1).unwrap(); |
| 1507 | doc.commit_then_renew(); |
| 1508 | let snapshot = doc.export(ExportMode::Snapshot).unwrap(); |
| 1509 | let expected_origin_string = "expectedOriginString"; |
| 1510 | |
| 1511 | let sub = remote.subscribe_root(Arc::new(move |e| { |
| 1512 | assert_eq!(e.event_meta.origin, expected_origin_string.into()); |
| 1513 | })); |
| 1514 | remote |
| 1515 | .import_with(&snapshot, expected_origin_string.into()) |
| 1516 | .unwrap(); |
| 1517 | sub.unsubscribe(); |
| 1518 | } |
| 1519 | |
| 1520 | #[test] |
| 1521 | fn apply_diff_with_same_peer_id_should_not_hang() { |
| 1522 | let doc1 = LoroDoc::new_auto_commit(); |
| 1523 | doc1.set_peer_id(1).unwrap(); |
| 1524 | |
| 1525 | let doc2 = doc1.fork(); |
| 1526 | doc2.set_peer_id(1).unwrap(); // Same peer ID |
| 1527 | |
| 1528 | // The key issue: setContainer creates a nested container |
| 1529 | let map = doc2.get_map("root"); |
| 1530 | let nested = map |
| 1531 | .insert_container("nested", MapHandler::new_detached()) |
| 1532 | .unwrap(); // This is what triggers the hang |
| 1533 | nested.insert("key", "value").unwrap(); |
| 1534 | doc2.commit_then_renew(); |
| 1535 | |
| 1536 | let frontiers_before = doc1.oplog_frontiers(); |
| 1537 | let frontiers_after = doc2.oplog_frontiers(); |
| 1538 | let diff = doc2.diff(&frontiers_before, &frontiers_after).unwrap(); |
| 1539 | |
| 1540 | // This should NOT hang |
| 1541 | doc1.apply_diff(diff).unwrap(); |
| 1542 | assert_eq!( |
| 1543 | doc1.get_deep_value().to_json_value(), |
| 1544 | doc2.get_deep_value().to_json_value() |
| 1545 | ); |
| 1546 | } |