forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

undo.rs281 lines, 8.5 KiBHistoryRawDownload
1use std::borrow::Cow;
2
3use loro_internal::{
4 cursor::PosType, handler::UpdateOptions, loro::ExportMode, LoroDoc, UndoManager,
5};
6
7#[test]
8fn consecutive_noops_consume_one_item_and_notify_once() {
9 use std::sync::{
10 atomic::{AtomicUsize, Ordering},
11 Arc,
12 };
13
14 let doc = LoroDoc::new_auto_commit();
15 doc.set_peer_id(1).unwrap();
16 let undo = UndoManager::new(&doc);
17 undo.set_merge_interval(0);
18 let pops = Arc::new(AtomicUsize::new(0));
19 let popped = pops.clone();
20 undo.set_on_pop(Some(Box::new(move |_, _, _| {
21 popped.fetch_add(1, Ordering::Relaxed);
22 })));
23 doc.get_map("map").insert("prior", 42).unwrap();
24 doc.commit_then_renew();
25 let text = doc.get_text("text");
26 text.insert(0, "a", PosType::Unicode).unwrap();
27 doc.commit_then_renew();
28 text.insert(1, "b", PosType::Unicode).unwrap();
29 doc.commit_then_renew();
30
31 let other = LoroDoc::from_snapshot(&doc.export(ExportMode::Snapshot).unwrap()).unwrap();
32 other.set_peer_id(2).unwrap();
33 other
34 .get_text("text")
35 .delete(0, 2, PosType::Unicode)
36 .unwrap();
37 doc.import(&other.export(ExportMode::all_updates()).unwrap())
38 .unwrap();
39 let before = doc.get_deep_value();
40 assert_eq!(undo.undo_count(), 3);
41 for remaining in [2, 1] {
42 assert!(!undo.undo().unwrap());
43 assert_eq!(undo.undo_count(), remaining);
44 assert_eq!(undo.redo_count(), 0);
45 assert_eq!(pops.load(Ordering::Relaxed), 3 - remaining);
46 assert_eq!(doc.get_deep_value(), before);
47 }
48 assert!(undo.undo().unwrap());
49 assert_eq!(pops.load(Ordering::Relaxed), 3);
50 assert!(!undo.can_undo());
51 assert!(!undo.undo().unwrap());
52 assert_eq!(pops.load(Ordering::Relaxed), 3);
53 assert!(undo.redo().unwrap());
54 assert_eq!(doc.get_deep_value(), before);
55}
56
57#[test]
58fn test_basic_undo_group_checkpoint() {
59 let doc = LoroDoc::new();
60 let undo_manager = UndoManager::new(&doc);
61 let text = doc.get_text("text");
62
63 text.update("0", UpdateOptions::default()).unwrap();
64 doc.commit_then_renew();
65 undo_manager.group_start().unwrap();
66 text.update("1", UpdateOptions::default()).unwrap();
67 doc.commit_then_renew();
68 text.update("12", UpdateOptions::default()).unwrap();
69 doc.commit_then_renew();
70 undo_manager.group_end();
71 undo_manager.undo().unwrap();
72 assert_eq!(
73 text.to_string(),
74 "0",
75 "undo should undo the grouped updates"
76 );
77 undo_manager.redo().unwrap();
78 assert_eq!(
79 text.to_string(),
80 "12",
81 "redo should redo the grouped updates"
82 );
83}
84
85#[test]
86fn test_invalid_nested_group() {
87 let doc = LoroDoc::new();
88
89 let undo_manager = UndoManager::new(&doc);
90
91 assert!(
92 undo_manager.group_start().is_ok(),
93 "group start should succeed"
94 );
95 assert!(
96 undo_manager.group_start().is_err(),
97 "nested group start should fail"
98 );
99 undo_manager.group_end();
100 assert!(
101 undo_manager.group_start().is_ok(),
102 "nested group end should fail"
103 );
104}
105
106#[test]
107fn test_simulate_intersecting_remote_undo() {
108 let doc = LoroDoc::new();
109 let undo_manager = UndoManager::new(&doc);
110 let text = doc.get_text("text");
111 undo_manager.group_start().unwrap();
112 println!("pushing 1");
113 text.update("1", UpdateOptions::default()).unwrap();
114 doc.commit_then_renew();
115 println!("pushing 2");
116 text.update("12", UpdateOptions::default()).unwrap();
117 doc.commit_then_renew();
118
119 // At this point, the doc has state of "12" in the "text" container
120
121 let snapshot = doc.export(ExportMode::Snapshot).unwrap();
122 let doc2 = LoroDoc::from_snapshot(&snapshot).unwrap();
123 let text2 = doc2.get_text("text");
124 let vv = doc2.state_vv();
125 text2.update("123", UpdateOptions::default()).unwrap();
126 doc2.commit_then_renew();
127
128 let update = doc2
129 .export(ExportMode::Updates {
130 from: Cow::Borrowed(&vv),
131 })
132 .unwrap();
133
134 println!("importing 3");
135 doc.import(&update).unwrap();
136
137 // At this point, the doc has state of "123" in the "text" container
138
139 text.update("1234", UpdateOptions::default()).unwrap();
140 println!("pushing 4");
141 doc.commit_then_renew();
142 println!("ending group");
143 undo_manager.group_end();
144
145 undo_manager.undo().unwrap();
146
147 assert_eq!(text.to_string(), "123");
148}
149
150#[test]
151fn test_simulate_non_intersecting_remote_undo() {
152 let doc = LoroDoc::new();
153 let undo_manager = UndoManager::new(&doc);
154 let text = doc.get_text("text");
155 undo_manager.group_start().unwrap();
156 text.update("1", UpdateOptions::default()).unwrap();
157 doc.commit_then_renew();
158 text.update("12", UpdateOptions::default()).unwrap();
159 doc.commit_then_renew();
160 let snapshot = doc.export(ExportMode::Snapshot).unwrap();
161 // At the point the first doc has state of "12" in the "text" container
162
163 // Doc 2 makes changes to a separate container
164 // The doc 2 has state of "123" in the "text2" container
165 let doc2 = LoroDoc::from_snapshot(&snapshot).unwrap();
166 let text2 = doc2.get_text("text2");
167 let vv = doc2.state_vv();
168 text2.update("123", UpdateOptions::default()).unwrap();
169 doc2.commit_then_renew();
170 let update = doc2
171 .export(ExportMode::Updates {
172 from: Cow::Borrowed(&vv),
173 })
174 .unwrap();
175 doc.import(&update).unwrap();
176
177 text.update("123", UpdateOptions::default()).unwrap();
178 doc.commit_then_renew();
179 undo_manager.group_end();
180
181 undo_manager.undo().unwrap();
182
183 assert_eq!(text.to_string(), "");
184}
185
186#[test]
187fn test_undo_group_start_with_remote_ops() {
188 let doc = LoroDoc::new();
189 let doc2 = LoroDoc::new();
190 let undo_manager = UndoManager::new(&doc);
191 doc.get_text("text")
192 .insert(0, "hi", PosType::Unicode)
193 .unwrap();
194 doc2.import(&doc.export(ExportMode::Snapshot).unwrap())
195 .unwrap();
196 doc2.get_text("text")
197 .insert(0, "test", PosType::Unicode)
198 .unwrap();
199 doc.import(&doc2.export(ExportMode::Snapshot).unwrap())
200 .unwrap();
201 undo_manager.group_start().unwrap();
202 doc.get_text("text")
203 .insert(0, "t", PosType::Unicode)
204 .unwrap();
205 undo_manager.undo().unwrap();
206 assert_eq!(doc.get_text("text").to_string(), "testhi");
207 undo_manager.undo().unwrap();
208 assert_eq!(doc.get_text("text").to_string(), "test");
209 undo_manager.redo().unwrap();
210 assert_eq!(doc.get_text("text").to_string(), "testhi");
211 undo_manager.redo().unwrap();
212 assert_eq!(doc.get_text("text").to_string(), "ttesthi");
213 undo_manager.undo().unwrap();
214 assert_eq!(doc.get_text("text").to_string(), "testhi");
215 undo_manager.undo().unwrap();
216 assert_eq!(doc.get_text("text").to_string(), "test");
217}
218
219#[test]
220fn test_clear_redo() {
221 let doc = LoroDoc::new();
222 let undo_manager = UndoManager::new(&doc);
223 let text = doc.get_text("text");
224
225 // Make some edits
226 text.update("hello", UpdateOptions::default()).unwrap();
227 doc.commit_then_renew();
228 text.update("hello world", UpdateOptions::default())
229 .unwrap();
230 doc.commit_then_renew();
231
232 // Undo to create redo stack
233 undo_manager.undo().unwrap();
234 assert_eq!(text.to_string(), "hello");
235 assert!(undo_manager.can_redo(), "should be able to redo");
236 assert!(undo_manager.can_undo(), "should be able to undo");
237
238 // Clear only redo stack
239 undo_manager.clear_redo();
240 assert!(!undo_manager.can_redo(), "redo stack should be empty");
241 assert!(
242 undo_manager.can_undo(),
243 "undo stack should still have items"
244 );
245
246 // Verify undo still works
247 undo_manager.undo().unwrap();
248 assert_eq!(text.to_string(), "");
249}
250
251#[test]
252fn test_clear_undo() {
253 let doc = LoroDoc::new();
254 let undo_manager = UndoManager::new(&doc);
255 let text = doc.get_text("text");
256
257 // Make some edits
258 text.update("hello", UpdateOptions::default()).unwrap();
259 doc.commit_then_renew();
260 text.update("hello world", UpdateOptions::default())
261 .unwrap();
262 doc.commit_then_renew();
263
264 // Undo to create redo stack
265 undo_manager.undo().unwrap();
266 assert_eq!(text.to_string(), "hello");
267 assert!(undo_manager.can_redo(), "should be able to redo");
268 assert!(undo_manager.can_undo(), "should be able to undo");
269
270 // Clear only undo stack
271 undo_manager.clear_undo();
272 assert!(
273 undo_manager.can_redo(),
274 "redo stack should still have items"
275 );
276 assert!(!undo_manager.can_undo(), "undo stack should be empty");
277
278 // Verify redo still works
279 undo_manager.redo().unwrap();
280 assert_eq!(text.to_string(), "hello world");
281}