oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1//! A local insert into a parent that has since been deleted (oficina/docs
2//! #145, finding 9; found by gc_undo_kept_fuzz.rs at seed 42 and by the engine
3//! branch's convergence fuzz at seeds 76 and 113).
4//!
5//! Yjs's `Item.integrate` deletes an item whose parent is deleted, for every
6//! insert, local or remote; so does yrs's remote path (`Update::integrate`).
7//! yrs 0.23.5's three local paths dropped `integrate`'s "delete me" answer:
8//! `ItemPtr::redo`, `TransactionMut::create_item` (text, element and attribute
9//! inserts) and `BlockIter::insert_contents` (array inserts). The inserting
10//! replica alone kept the item live: invisible under its deleted parent, but
11//! with a delete set no one else had. The editor reaches it through undo:
12//! undoing a deletion re-creates the item in its parent, following a deleted
13//! parent's `redone` chain to its restored copy, which a peer may since have
14//! deleted. Upstream's `integrate_item` (yrs 0.27) deletes for every caller.
15use yrs::types::xml::XmlTextPrelim;
16use yrs::updates::decoder::Decode;
17use yrs::{Array, ArrayPrelim, ArrayRef, Doc, GetString, Map, MapPrelim, Options, ReadTxn, StateVector, Text, Transact, UndoManager, Update, Xml, XmlElementPrelim, XmlElementRef, XmlFragment, XmlOut, XmlTextRef};
18
19fn doc(client: u64) -> Doc { doc_gc(client, false) }
20fn doc_gc(client: u64, skip_gc: bool) -> Doc {
21 let mut options = Options::with_guid_and_client_id("insert-into-deleted-parent".into(), client);
22 options.offset_kind = yrs::OffsetKind::Utf16;
23 options.skip_gc = skip_gc;
24 Doc::with_options(options)
25}
26fn sync(from: &Doc, to: &Doc) {
27 let update = from.transact().encode_state_as_update_v1(&to.transact().state_vector());
28 to.transact_mut_with("remote").apply_update(Update::decode_v1(&update).unwrap()).unwrap();
29}
30fn state(doc: &Doc) -> Update { Update::decode_v1(&doc.transact().encode_state_as_update_v1(&StateVector::default())).unwrap() }
31fn deletes(doc: &Doc) -> String { format!("{:?}", state(doc).delete_set().iter().map(|(c, r)| (*c, format!("{r:?}"))).collect::<std::collections::BTreeMap<_, _>>()) }
32
33#[test]
34fn an_item_undone_into_a_deleted_restored_parent_is_deleted_as_everywhere_else() {
35 let editor = doc(1);
36 let peer = doc(2);
37 let root = editor.get_or_insert_xml_fragment("prosemirror");
38 root.insert(&mut editor.transact_mut_with("remote"), 0, XmlElementPrelim::new("paragraph", [yrs::types::xml::XmlIn::Text(XmlTextPrelim::new("ab cd").into())]));
39 sync(&editor, &peer);
40 let mut undo = UndoManager::with_scope_and_options(&editor, &root, yrs::undo::Options {
41 capture_timeout_millis: 0,
42 tracked_origins: ["local".into()].into(),
43 capture_transaction: None,
44 timestamp: std::sync::Arc::new(|| 1),
45 });
46 let text = || match root.get(&editor.transact(), 0).unwrap() {
47 XmlOut::Element(paragraph) => match paragraph.get(&editor.transact(), 0).unwrap() { XmlOut::Text(text) => text, _ => unreachable!() },
48 _ => unreachable!(),
49 };
50 text().remove_range(&mut editor.transact_mut_with("local"), 2, 1); // delete " "
51 root.remove_range(&mut editor.transact_mut_with("local"), 0, 1); // delete the paragraph
52 assert!(undo.undo_blocking()); // the paragraph comes back as a new one, "abcd"
53 sync(&editor, &peer);
54 // A peer deletes the restored paragraph.
55 let peer_root = peer.get_or_insert_xml_fragment("prosemirror");
56 peer_root.remove_range(&mut peer.transact_mut(), 0, 1);
57 sync(&peer, &editor);
58 // Undo the " ": its parent's restored copy is deleted, so it is too.
59 assert!(undo.undo_blocking());
60 sync(&editor, &peer);
61 assert_eq!(state(&editor).state_vector(), state(&peer).state_vector());
62 assert_eq!(deletes(&editor), deletes(&peer), "the undoing editor disagrees about what is deleted");
63 let fresh = doc(3);
64 fresh.transact_mut().apply_update(state(&editor)).unwrap();
65 assert_eq!(deletes(&fresh), deletes(&editor));
66 assert_eq!(root.get_string(&editor.transact()), "");
67 assert_eq!(peer_root.get_string(&peer.transact()), "");
68}
69
70/// Every local insert path into a type a peer has deleted. With gc off, so the
71/// local references stay valid after the deletion (the editor reaches deleted
72/// types only through undo, above).
73#[test]
74fn local_inserts_into_a_type_a_peer_deleted_are_deleted_as_everywhere_else() {
75 let editor = doc_gc(1, true);
76 let peer = doc_gc(2, true);
77 let root = editor.get_or_insert_xml_fragment("prosemirror");
78 let map = editor.get_or_insert_map("map");
79 {
80 let mut txn = editor.transact_mut();
81 root.insert(&mut txn, 0, XmlElementPrelim::new("paragraph", [yrs::types::xml::XmlIn::Text(XmlTextPrelim::new("ab").into())]));
82 map.insert(&mut txn, "list", ArrayPrelim::from([1, 2]));
83 map.insert(&mut txn, "nested", MapPrelim::from([("k", "v")]));
84 }
85 sync(&editor, &peer);
86 let (paragraph, text): (XmlElementRef, XmlTextRef) = {
87 let txn = editor.transact();
88 let XmlOut::Element(paragraph) = root.get(&txn, 0).unwrap() else { unreachable!() };
89 let XmlOut::Text(text) = paragraph.get(&txn, 0).unwrap() else { unreachable!() };
90 (paragraph, text)
91 };
92 let list: ArrayRef = map.get(&editor.transact(), "list").unwrap().cast().unwrap();
93 let nested: yrs::MapRef = map.get(&editor.transact(), "nested").unwrap().cast().unwrap();
94 let (peer_root, peer_map) = (peer.get_or_insert_xml_fragment("prosemirror"), peer.get_or_insert_map("map"));
95 {
96 let mut txn = peer.transact_mut();
97 peer_root.remove_range(&mut txn, 0, 1);
98 peer_map.remove(&mut txn, "list");
99 peer_map.remove(&mut txn, "nested");
100 }
101 sync(&peer, &editor);
102 {
103 let mut txn = editor.transact_mut();
104 text.insert(&mut txn, 1, "X"); // create_item
105 paragraph.insert(&mut txn, 0, XmlTextPrelim::new("Y")); // create_item via insert_at
106 paragraph.insert_attribute(&mut txn, "align", "center"); // create_item with a key
107 nested.insert(&mut txn, "k2", "v2"); // create_item with a key
108 list.insert_range(&mut txn, 0, [3, 4]); // BlockIter::insert_contents
109 }
110 sync(&editor, &peer);
111 assert_eq!(state(&editor).state_vector(), state(&peer).state_vector());
112 assert_eq!(deletes(&editor), deletes(&peer), "the inserting replica disagrees about what is deleted");
113 let fresh = doc_gc(3, true);
114 fresh.transact_mut().apply_update(state(&editor)).unwrap();
115 assert_eq!(deletes(&fresh), deletes(&editor));
116}
117
118/// Redo origins (found by gc_undo_kept_fuzz.rs at seed 98, reduced). "xyz"
119/// is deleted and undone: it comes back as one item x'y'z', whose `redone` the
120/// original carries. "y'" is deleted, splitting it; then the paragraph. Undoing
121/// that restores A'', x'' and z''. For z'' the left neighbour traces y' (not
122/// restored), then x' to x''; the right neighbour traces the original "xyz",
123/// whose restored copy starts at x', to x'' as well. yrs and Yjs both put z''
124/// right after x'' locally, but the origins they wrote (after the end of the
125/// merged A''x'' block, before x'' inside it) put it before x'' on every
126/// receiver. Unpatched, this replica shows "Axz" and every peer "Azx".
127#[test]
128fn an_undo_whose_neighbours_trace_to_one_item_places_it_as_every_peer_does() {
129 let editor = doc(1);
130 let peer = doc(2);
131 let root = editor.get_or_insert_xml_fragment("prosemirror");
132 root.insert(&mut editor.transact_mut_with("remote"), 0, XmlElementPrelim::new("paragraph", [yrs::types::xml::XmlIn::Text(XmlTextPrelim::new("Axyz").into())]));
133 let mut undo = UndoManager::with_scope_and_options(&editor, &root, yrs::undo::Options {
134 capture_timeout_millis: 0,
135 tracked_origins: ["local".into()].into(),
136 capture_transaction: None,
137 timestamp: std::sync::Arc::new(|| 1),
138 });
139 let text = || match root.get(&editor.transact(), 0).unwrap() {
140 XmlOut::Element(paragraph) => match paragraph.get(&editor.transact(), 0).unwrap() { XmlOut::Text(text) => text, _ => unreachable!() },
141 _ => unreachable!(),
142 };
143 text().remove_range(&mut editor.transact_mut_with("local"), 1, 3); // delete "xyz"
144 assert!(undo.undo_blocking()); // back, as one item x'y'z'
145 text().remove_range(&mut editor.transact_mut_with("local"), 2, 1); // delete "y'"
146 root.remove_range(&mut editor.transact_mut_with("local"), 0, 1); // delete the paragraph
147 assert!(undo.undo_blocking()); // the paragraph back: A'', x'', z''
148 assert_eq!(root.get_string(&editor.transact()), "<paragraph>Axz</paragraph>");
149 sync(&editor, &peer);
150 assert_eq!(peer.get_or_insert_xml_fragment("prosemirror").get_string(&peer.transact()), "<paragraph>Axz</paragraph>", "a peer puts the restored z elsewhere");
151 let fresh = doc(3);
152 fresh.transact_mut().apply_update(state(&editor)).unwrap();
153 assert_eq!(fresh.get_or_insert_xml_fragment("prosemirror").get_string(&fresh.transact()), "<paragraph>Axz</paragraph>");
154}
155