//! An undo-kept item under a garbage-collected parent (oficina/docs #145, //! finding 1; upstream y-crdt #667 and #676, fixed by 0e5df00 in yrs 0.28.1). //! //! Clearing the redo stack un-keeps an item and, with it, every parent up the //! chain (`Item::keep(false)`), though a sibling the undo stack still holds //! stays kept. When a peer then deletes that parent, unpatched yrs collected //! the parent but skipped the kept child, freeing the branch the child still //! pointed into: the next snapshot panicked ("Could not get parent branch info //! for item"), or, reading freed memory, wrote a struct a fresh document //! cannot integrate. Yjs collects every child of a collected type whatever its //! keep flag (`ContentType.gc`), and so does the patched yrs. use docs_native_crdt::Editor; use serde_json::{json, Value}; use yrs::types::ToJson; use yrs::updates::decoder::Decode; use yrs::{Doc, Map, MapPrelim, MapRef, Options, ReadTxn, StateVector, Transact, UndoManager, Update}; fn reread(snapshot: &[u8]) -> Editor { let fresh = Editor::new("keep", 99); fresh.apply(snapshot).unwrap(); fresh } /// Fable's repro: delete a word, type in the same paragraph, undo, type /// anything (clearing the redo stack), then a peer deletes that paragraph. fn repro() -> (Editor, Editor, Vec>) { let mut a = Editor::new("keep", 11); a.command(&json!({"op":"insert_element","at":0,"type":"paragraph"})).unwrap(); a.command(&json!({"op":"display_edit","at":0,"length":0,"text":"aaa\nhello world\nzzz"})).unwrap(); let base = a.snapshot(); let mut editor = Editor::new("keep", 12); editor.apply(&base).unwrap(); let mut peer = Editor::new("keep", 13); peer.apply(&base).unwrap(); // display: "aaa\nhello world\nzzz\n": hello at 4..9 let mut sent = vec![base]; for command in [ json!({"op":"display_edit","at":4,"length":5,"text":""}), // delete "hello" (undo item 1) json!({"op":"display_edit","at":6,"length":0,"text":"Q"}), // type in the same paragraph (undo item 2) json!({"op":"undo"}), // item 2 moves to the redo stack json!({"op":"display_edit","at":0,"length":0,"text":"x"}), // clears the redo stack: un-keeps the paragraph ] { let update = editor.command(&command).unwrap(); peer.apply(&update).unwrap(); sent.push(update); } // The peer deletes from inside the first paragraph to inside the third: // the middle one, holding the still-kept "hello", goes. let remote = peer.command(&json!({"op":"display_edit","at":2,"length":12,"text":""})).unwrap(); editor.apply(&remote).unwrap(); sent.push(remote); (editor, peer, sent) } fn children(editor: &Editor) -> Value { editor.projection()["children"].clone() } #[test] fn a_snapshot_after_a_peer_deletes_a_kept_items_paragraph_holds_what_the_editor_shows() { let (editor, peer, _) = repro(); assert!(editor.is_whole()); let snapshot = editor.snapshot(); let fresh = reread(&snapshot); assert!(fresh.is_whole(), "a fresh document parks part of the snapshot"); assert_eq!(children(&fresh), children(&editor), "the snapshot does not hold what the editor shows"); assert_eq!(children(&editor), children(&peer)); assert_eq!(fresh.snapshot(), reread(&fresh.snapshot()).snapshot()); } #[test] fn undoing_past_the_collected_paragraph_converges_with_the_peer() { let (mut editor, peer, _) = repro(); // The collected "hello" cannot come back (Yjs drops it the same way); the // rest of the history undoes, and every step's snapshot stays whole. for _ in 0..8 { let update = editor.command(&json!({"op":"undo"})).unwrap(); peer.apply(&update).unwrap(); let fresh = reread(&editor.snapshot()); assert!(fresh.is_whole()); assert_eq!(children(&fresh), children(&editor)); assert_eq!(children(&editor), children(&peer)); } for _ in 0..8 { let update = editor.command(&json!({"op":"redo"})).unwrap(); peer.apply(&update).unwrap(); assert_eq!(children(&reread(&editor.snapshot())), children(&editor)); assert_eq!(children(&editor), children(&peer)); } } #[test] fn every_update_and_snapshot_replays_into_plain_yrs_with_gc_on_and_off() { let (editor, _, sent) = repro(); for skip_gc in [false, true] { let mut options = Options::with_guid_and_client_id("keep".into(), 50); options.skip_gc = skip_gc; let replayed = Doc::with_options(options.clone()); for update in &sent { replayed.transact_mut().apply_update(Update::decode_v1(update).unwrap()).unwrap(); } let from_snapshot = Doc::with_options({ let mut o = options.clone(); o.client_id = 51; o }); from_snapshot.transact_mut().apply_update(Update::decode_v1(&editor.snapshot()).unwrap()).unwrap(); for doc in [&replayed, &from_snapshot] { let state = doc.transact().encode_state_as_update_v1(&StateVector::default()); assert_eq!(children(&reread(&state)), children(&editor), "skip_gc={skip_gc}"); assert!(doc.transact().store().pending_update().is_none() && doc.transact().store().pending_ds().is_none()); } assert_eq!(replayed.transact().state_vector(), editor_state_vector(&editor)); } } fn editor_state_vector(editor: &Editor) -> StateVector { Update::decode_v1(&editor.snapshot()).unwrap().state_vector() } /// Upstream's own regression (y-crdt 0e5df00, `gc_collects_kept_children_of_a_collected_type`), /// on yrs 0.23.5's API: a map instead of the editor's XML. #[test] fn yrs_collects_kept_children_of_a_collected_type() { let d1 = Doc::with_client_id(1); let d2 = Doc::with_client_id(2); let styles1 = d1.get_or_insert_map("styles"); let styles2 = d2.get_or_insert_map("styles"); styles1.insert(&mut d1.transact_mut(), "p", MapPrelim::from([("kind", "number")])); exchange(&d1, &d2); let mut mgr = UndoManager::new(&d2, &styles2); { let mut txn = d2.transact_mut(); let p = styles2.get(&txn, "p").unwrap().cast::().unwrap(); p.insert(&mut txn, "kind", "bullet"); } mgr.undo_blocking(); mgr.reset(); styles2.insert(&mut d2.transact_mut(), "q", MapPrelim::default()); // clears the redo stack styles1.remove(&mut d1.transact_mut(), "p"); exchange(&d1, &d2); let state = d2.transact().encode_state_as_update_v1(&StateVector::default()); let fresh = Doc::with_client_id(3); fresh.transact_mut().apply_update(Update::decode_v1(&state).unwrap()).unwrap(); assert!(fresh.transact().store().pending_update().is_none()); while mgr.undo_blocking() {} exchange(&d1, &d2); assert_eq!(styles1.to_json(&d1.transact()), styles2.to_json(&d2.transact())); } fn exchange(a: &Doc, b: &Doc) { for (from, to) in [(a, b), (b, a)] { let sv = to.transact().state_vector(); let update = from.transact().encode_state_as_update_v1(&sv); to.transact_mut().apply_update(Update::decode_v1(&update).unwrap()).unwrap(); } }