| 1 | //! An undo-kept item under a garbage-collected parent (oficina/docs #145, |
| 2 | //! finding 1; upstream y-crdt #667 and #676, fixed by 0e5df00 in yrs 0.28.1). |
| 3 | //! |
| 4 | //! Clearing the redo stack un-keeps an item and, with it, every parent up the |
| 5 | //! chain (`Item::keep(false)`), though a sibling the undo stack still holds |
| 6 | //! stays kept. When a peer then deletes that parent, unpatched yrs collected |
| 7 | //! the parent but skipped the kept child, freeing the branch the child still |
| 8 | //! pointed into: the next snapshot panicked ("Could not get parent branch info |
| 9 | //! for item"), or, reading freed memory, wrote a struct a fresh document |
| 10 | //! cannot integrate. Yjs collects every child of a collected type whatever its |
| 11 | //! keep flag (`ContentType.gc`), and so does the patched yrs. |
| 12 | use docs_native_crdt::Editor; |
| 13 | use serde_json::{json, Value}; |
| 14 | use yrs::types::ToJson; |
| 15 | use yrs::updates::decoder::Decode; |
| 16 | use yrs::{Doc, Map, MapPrelim, MapRef, Options, ReadTxn, StateVector, Transact, UndoManager, Update}; |
| 17 | |
| 18 | fn reread(snapshot: &[u8]) -> Editor { |
| 19 | let fresh = Editor::new("keep", 99); |
| 20 | fresh.apply(snapshot).unwrap(); |
| 21 | fresh |
| 22 | } |
| 23 | |
| 24 | /// Fable's repro: delete a word, type in the same paragraph, undo, type |
| 25 | /// anything (clearing the redo stack), then a peer deletes that paragraph. |
| 26 | fn repro() -> (Editor, Editor, Vec<Vec<u8>>) { |
| 27 | let mut a = Editor::new("keep", 11); |
| 28 | a.command(&json!({"op":"insert_element","at":0,"type":"paragraph"})).unwrap(); |
| 29 | a.command(&json!({"op":"display_edit","at":0,"length":0,"text":"aaa\nhello world\nzzz"})).unwrap(); |
| 30 | let base = a.snapshot(); |
| 31 | let mut editor = Editor::new("keep", 12); |
| 32 | editor.apply(&base).unwrap(); |
| 33 | let mut peer = Editor::new("keep", 13); |
| 34 | peer.apply(&base).unwrap(); |
| 35 | // display: "aaa\nhello world\nzzz\n": hello at 4..9 |
| 36 | let mut sent = vec![base]; |
| 37 | for command in [ |
| 38 | json!({"op":"display_edit","at":4,"length":5,"text":""}), // delete "hello" (undo item 1) |
| 39 | json!({"op":"display_edit","at":6,"length":0,"text":"Q"}), // type in the same paragraph (undo item 2) |
| 40 | json!({"op":"undo"}), // item 2 moves to the redo stack |
| 41 | json!({"op":"display_edit","at":0,"length":0,"text":"x"}), // clears the redo stack: un-keeps the paragraph |
| 42 | ] { |
| 43 | let update = editor.command(&command).unwrap(); |
| 44 | peer.apply(&update).unwrap(); |
| 45 | sent.push(update); |
| 46 | } |
| 47 | // The peer deletes from inside the first paragraph to inside the third: |
| 48 | // the middle one, holding the still-kept "hello", goes. |
| 49 | let remote = peer.command(&json!({"op":"display_edit","at":2,"length":12,"text":""})).unwrap(); |
| 50 | editor.apply(&remote).unwrap(); |
| 51 | sent.push(remote); |
| 52 | (editor, peer, sent) |
| 53 | } |
| 54 | |
| 55 | fn children(editor: &Editor) -> Value { editor.projection()["children"].clone() } |
| 56 | |
| 57 | #[test] |
| 58 | fn a_snapshot_after_a_peer_deletes_a_kept_items_paragraph_holds_what_the_editor_shows() { |
| 59 | let (editor, peer, _) = repro(); |
| 60 | assert!(editor.is_whole()); |
| 61 | let snapshot = editor.snapshot(); |
| 62 | let fresh = reread(&snapshot); |
| 63 | assert!(fresh.is_whole(), "a fresh document parks part of the snapshot"); |
| 64 | assert_eq!(children(&fresh), children(&editor), "the snapshot does not hold what the editor shows"); |
| 65 | assert_eq!(children(&editor), children(&peer)); |
| 66 | assert_eq!(fresh.snapshot(), reread(&fresh.snapshot()).snapshot()); |
| 67 | } |
| 68 | |
| 69 | #[test] |
| 70 | fn undoing_past_the_collected_paragraph_converges_with_the_peer() { |
| 71 | let (mut editor, peer, _) = repro(); |
| 72 | // The collected "hello" cannot come back (Yjs drops it the same way); the |
| 73 | // rest of the history undoes, and every step's snapshot stays whole. |
| 74 | for _ in 0..8 { |
| 75 | let update = editor.command(&json!({"op":"undo"})).unwrap(); |
| 76 | peer.apply(&update).unwrap(); |
| 77 | let fresh = reread(&editor.snapshot()); |
| 78 | assert!(fresh.is_whole()); |
| 79 | assert_eq!(children(&fresh), children(&editor)); |
| 80 | assert_eq!(children(&editor), children(&peer)); |
| 81 | } |
| 82 | for _ in 0..8 { |
| 83 | let update = editor.command(&json!({"op":"redo"})).unwrap(); |
| 84 | peer.apply(&update).unwrap(); |
| 85 | assert_eq!(children(&reread(&editor.snapshot())), children(&editor)); |
| 86 | assert_eq!(children(&editor), children(&peer)); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | #[test] |
| 91 | fn every_update_and_snapshot_replays_into_plain_yrs_with_gc_on_and_off() { |
| 92 | let (editor, _, sent) = repro(); |
| 93 | for skip_gc in [false, true] { |
| 94 | let mut options = Options::with_guid_and_client_id("keep".into(), 50); |
| 95 | options.skip_gc = skip_gc; |
| 96 | let replayed = Doc::with_options(options.clone()); |
| 97 | for update in &sent { replayed.transact_mut().apply_update(Update::decode_v1(update).unwrap()).unwrap(); } |
| 98 | let from_snapshot = Doc::with_options({ let mut o = options.clone(); o.client_id = 51; o }); |
| 99 | from_snapshot.transact_mut().apply_update(Update::decode_v1(&editor.snapshot()).unwrap()).unwrap(); |
| 100 | for doc in [&replayed, &from_snapshot] { |
| 101 | let state = doc.transact().encode_state_as_update_v1(&StateVector::default()); |
| 102 | assert_eq!(children(&reread(&state)), children(&editor), "skip_gc={skip_gc}"); |
| 103 | assert!(doc.transact().store().pending_update().is_none() && doc.transact().store().pending_ds().is_none()); |
| 104 | } |
| 105 | assert_eq!(replayed.transact().state_vector(), editor_state_vector(&editor)); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | fn editor_state_vector(editor: &Editor) -> StateVector { |
| 110 | Update::decode_v1(&editor.snapshot()).unwrap().state_vector() |
| 111 | } |
| 112 | |
| 113 | /// Upstream's own regression (y-crdt 0e5df00, `gc_collects_kept_children_of_a_collected_type`), |
| 114 | /// on yrs 0.23.5's API: a map instead of the editor's XML. |
| 115 | #[test] |
| 116 | fn yrs_collects_kept_children_of_a_collected_type() { |
| 117 | let d1 = Doc::with_client_id(1); |
| 118 | let d2 = Doc::with_client_id(2); |
| 119 | let styles1 = d1.get_or_insert_map("styles"); |
| 120 | let styles2 = d2.get_or_insert_map("styles"); |
| 121 | styles1.insert(&mut d1.transact_mut(), "p", MapPrelim::from([("kind", "number")])); |
| 122 | exchange(&d1, &d2); |
| 123 | |
| 124 | let mut mgr = UndoManager::new(&d2, &styles2); |
| 125 | { |
| 126 | let mut txn = d2.transact_mut(); |
| 127 | let p = styles2.get(&txn, "p").unwrap().cast::<MapRef>().unwrap(); |
| 128 | p.insert(&mut txn, "kind", "bullet"); |
| 129 | } |
| 130 | mgr.undo_blocking(); |
| 131 | mgr.reset(); |
| 132 | styles2.insert(&mut d2.transact_mut(), "q", MapPrelim::default()); // clears the redo stack |
| 133 | |
| 134 | styles1.remove(&mut d1.transact_mut(), "p"); |
| 135 | exchange(&d1, &d2); |
| 136 | |
| 137 | let state = d2.transact().encode_state_as_update_v1(&StateVector::default()); |
| 138 | let fresh = Doc::with_client_id(3); |
| 139 | fresh.transact_mut().apply_update(Update::decode_v1(&state).unwrap()).unwrap(); |
| 140 | assert!(fresh.transact().store().pending_update().is_none()); |
| 141 | while mgr.undo_blocking() {} |
| 142 | exchange(&d1, &d2); |
| 143 | assert_eq!(styles1.to_json(&d1.transact()), styles2.to_json(&d2.transact())); |
| 144 | } |
| 145 | |
| 146 | fn exchange(a: &Doc, b: &Doc) { |
| 147 | for (from, to) in [(a, b), (b, a)] { |
| 148 | let sv = to.transact().state_vector(); |
| 149 | let update = from.transact().encode_state_as_update_v1(&sv); |
| 150 | to.transact_mut().apply_update(Update::decode_v1(&update).unwrap()).unwrap(); |
| 151 | } |
| 152 | } |
| 153 | |