//! A local insert into a parent that has since been deleted (oficina/docs //! #145, finding 9; found by gc_undo_kept_fuzz.rs at seed 42 and by the engine //! branch's convergence fuzz at seeds 76 and 113). //! //! Yjs's `Item.integrate` deletes an item whose parent is deleted, for every //! insert, local or remote; so does yrs's remote path (`Update::integrate`). //! yrs 0.23.5's three local paths dropped `integrate`'s "delete me" answer: //! `ItemPtr::redo`, `TransactionMut::create_item` (text, element and attribute //! inserts) and `BlockIter::insert_contents` (array inserts). The inserting //! replica alone kept the item live: invisible under its deleted parent, but //! with a delete set no one else had. The editor reaches it through undo: //! undoing a deletion re-creates the item in its parent, following a deleted //! parent's `redone` chain to its restored copy, which a peer may since have //! deleted. Upstream's `integrate_item` (yrs 0.27) deletes for every caller. use yrs::types::xml::XmlTextPrelim; use yrs::updates::decoder::Decode; use yrs::{Array, ArrayPrelim, ArrayRef, Doc, GetString, Map, MapPrelim, Options, ReadTxn, StateVector, Text, Transact, UndoManager, Update, Xml, XmlElementPrelim, XmlElementRef, XmlFragment, XmlOut, XmlTextRef}; fn doc(client: u64) -> Doc { doc_gc(client, false) } fn doc_gc(client: u64, skip_gc: bool) -> Doc { let mut options = Options::with_guid_and_client_id("insert-into-deleted-parent".into(), client); options.offset_kind = yrs::OffsetKind::Utf16; options.skip_gc = skip_gc; Doc::with_options(options) } fn sync(from: &Doc, to: &Doc) { let update = from.transact().encode_state_as_update_v1(&to.transact().state_vector()); to.transact_mut_with("remote").apply_update(Update::decode_v1(&update).unwrap()).unwrap(); } fn state(doc: &Doc) -> Update { Update::decode_v1(&doc.transact().encode_state_as_update_v1(&StateVector::default())).unwrap() } fn deletes(doc: &Doc) -> String { format!("{:?}", state(doc).delete_set().iter().map(|(c, r)| (*c, format!("{r:?}"))).collect::>()) } #[test] fn an_item_undone_into_a_deleted_restored_parent_is_deleted_as_everywhere_else() { let editor = doc(1); let peer = doc(2); let root = editor.get_or_insert_xml_fragment("prosemirror"); root.insert(&mut editor.transact_mut_with("remote"), 0, XmlElementPrelim::new("paragraph", [yrs::types::xml::XmlIn::Text(XmlTextPrelim::new("ab cd").into())])); sync(&editor, &peer); let mut undo = UndoManager::with_scope_and_options(&editor, &root, yrs::undo::Options { capture_timeout_millis: 0, tracked_origins: ["local".into()].into(), capture_transaction: None, timestamp: std::sync::Arc::new(|| 1), }); let text = || match root.get(&editor.transact(), 0).unwrap() { XmlOut::Element(paragraph) => match paragraph.get(&editor.transact(), 0).unwrap() { XmlOut::Text(text) => text, _ => unreachable!() }, _ => unreachable!(), }; text().remove_range(&mut editor.transact_mut_with("local"), 2, 1); // delete " " root.remove_range(&mut editor.transact_mut_with("local"), 0, 1); // delete the paragraph assert!(undo.undo_blocking()); // the paragraph comes back as a new one, "abcd" sync(&editor, &peer); // A peer deletes the restored paragraph. let peer_root = peer.get_or_insert_xml_fragment("prosemirror"); peer_root.remove_range(&mut peer.transact_mut(), 0, 1); sync(&peer, &editor); // Undo the " ": its parent's restored copy is deleted, so it is too. assert!(undo.undo_blocking()); sync(&editor, &peer); assert_eq!(state(&editor).state_vector(), state(&peer).state_vector()); assert_eq!(deletes(&editor), deletes(&peer), "the undoing editor disagrees about what is deleted"); let fresh = doc(3); fresh.transact_mut().apply_update(state(&editor)).unwrap(); assert_eq!(deletes(&fresh), deletes(&editor)); assert_eq!(root.get_string(&editor.transact()), ""); assert_eq!(peer_root.get_string(&peer.transact()), ""); } /// Every local insert path into a type a peer has deleted. With gc off, so the /// local references stay valid after the deletion (the editor reaches deleted /// types only through undo, above). #[test] fn local_inserts_into_a_type_a_peer_deleted_are_deleted_as_everywhere_else() { let editor = doc_gc(1, true); let peer = doc_gc(2, true); let root = editor.get_or_insert_xml_fragment("prosemirror"); let map = editor.get_or_insert_map("map"); { let mut txn = editor.transact_mut(); root.insert(&mut txn, 0, XmlElementPrelim::new("paragraph", [yrs::types::xml::XmlIn::Text(XmlTextPrelim::new("ab").into())])); map.insert(&mut txn, "list", ArrayPrelim::from([1, 2])); map.insert(&mut txn, "nested", MapPrelim::from([("k", "v")])); } sync(&editor, &peer); let (paragraph, text): (XmlElementRef, XmlTextRef) = { let txn = editor.transact(); let XmlOut::Element(paragraph) = root.get(&txn, 0).unwrap() else { unreachable!() }; let XmlOut::Text(text) = paragraph.get(&txn, 0).unwrap() else { unreachable!() }; (paragraph, text) }; let list: ArrayRef = map.get(&editor.transact(), "list").unwrap().cast().unwrap(); let nested: yrs::MapRef = map.get(&editor.transact(), "nested").unwrap().cast().unwrap(); let (peer_root, peer_map) = (peer.get_or_insert_xml_fragment("prosemirror"), peer.get_or_insert_map("map")); { let mut txn = peer.transact_mut(); peer_root.remove_range(&mut txn, 0, 1); peer_map.remove(&mut txn, "list"); peer_map.remove(&mut txn, "nested"); } sync(&peer, &editor); { let mut txn = editor.transact_mut(); text.insert(&mut txn, 1, "X"); // create_item paragraph.insert(&mut txn, 0, XmlTextPrelim::new("Y")); // create_item via insert_at paragraph.insert_attribute(&mut txn, "align", "center"); // create_item with a key nested.insert(&mut txn, "k2", "v2"); // create_item with a key list.insert_range(&mut txn, 0, [3, 4]); // BlockIter::insert_contents } sync(&editor, &peer); assert_eq!(state(&editor).state_vector(), state(&peer).state_vector()); assert_eq!(deletes(&editor), deletes(&peer), "the inserting replica disagrees about what is deleted"); let fresh = doc_gc(3, true); fresh.transact_mut().apply_update(state(&editor)).unwrap(); assert_eq!(deletes(&fresh), deletes(&editor)); } /// Redo origins (found by gc_undo_kept_fuzz.rs at seed 98, reduced). "xyz" /// is deleted and undone: it comes back as one item x'y'z', whose `redone` the /// original carries. "y'" is deleted, splitting it; then the paragraph. Undoing /// that restores A'', x'' and z''. For z'' the left neighbour traces y' (not /// restored), then x' to x''; the right neighbour traces the original "xyz", /// whose restored copy starts at x', to x'' as well. yrs and Yjs both put z'' /// right after x'' locally, but the origins they wrote (after the end of the /// merged A''x'' block, before x'' inside it) put it before x'' on every /// receiver. Unpatched, this replica shows "Axz" and every peer "Azx". #[test] fn an_undo_whose_neighbours_trace_to_one_item_places_it_as_every_peer_does() { let editor = doc(1); let peer = doc(2); let root = editor.get_or_insert_xml_fragment("prosemirror"); root.insert(&mut editor.transact_mut_with("remote"), 0, XmlElementPrelim::new("paragraph", [yrs::types::xml::XmlIn::Text(XmlTextPrelim::new("Axyz").into())])); let mut undo = UndoManager::with_scope_and_options(&editor, &root, yrs::undo::Options { capture_timeout_millis: 0, tracked_origins: ["local".into()].into(), capture_transaction: None, timestamp: std::sync::Arc::new(|| 1), }); let text = || match root.get(&editor.transact(), 0).unwrap() { XmlOut::Element(paragraph) => match paragraph.get(&editor.transact(), 0).unwrap() { XmlOut::Text(text) => text, _ => unreachable!() }, _ => unreachable!(), }; text().remove_range(&mut editor.transact_mut_with("local"), 1, 3); // delete "xyz" assert!(undo.undo_blocking()); // back, as one item x'y'z' text().remove_range(&mut editor.transact_mut_with("local"), 2, 1); // delete "y'" root.remove_range(&mut editor.transact_mut_with("local"), 0, 1); // delete the paragraph assert!(undo.undo_blocking()); // the paragraph back: A'', x'', z'' assert_eq!(root.get_string(&editor.transact()), "Axz"); sync(&editor, &peer); assert_eq!(peer.get_or_insert_xml_fragment("prosemirror").get_string(&peer.transact()), "Axz", "a peer puts the restored z elsewhere"); let fresh = doc(3); fresh.transact_mut().apply_update(state(&editor)).unwrap(); assert_eq!(fresh.get_or_insert_xml_fragment("prosemirror").get_string(&fresh.transact()), "Axz"); }