forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

richtext.rs426 lines, 12.9 KiBHistoryRawDownload
1//! All the tests in this file are based on [richtext.md]
2
3use std::ops::Range;
4
5use loro_common::LoroValue;
6use loro_internal::{cursor::PosType, loro::ExportMode, LoroDoc, ToJson};
7use serde_json::json;
8
9fn init(s: &str) -> LoroDoc {
10 let doc = LoroDoc::new_auto_commit();
11 doc.set_peer_id(1).unwrap();
12 let richtext = doc.get_text("r");
13 richtext.insert(0, s, PosType::Unicode).unwrap();
14 doc
15}
16
17fn clone(doc: &LoroDoc, peer_id: u64) -> LoroDoc {
18 let doc2 = LoroDoc::new_auto_commit();
19 doc2.set_peer_id(peer_id).unwrap();
20 doc2.import(&doc.export(ExportMode::all_updates()).unwrap())
21 .unwrap();
22 doc2
23}
24
25#[derive(Debug)]
26enum Kind {
27 Bold,
28 Italic,
29 Link,
30}
31
32impl Kind {
33 fn key(&self) -> &str {
34 match self {
35 Kind::Bold => "bold",
36 Kind::Link => "link",
37 Kind::Italic => "italic",
38 }
39 }
40}
41
42fn insert(doc: &LoroDoc, pos: usize, s: &str) {
43 let richtext = doc.get_text("r");
44 richtext.insert(pos, s, PosType::Unicode).unwrap();
45}
46
47fn delete(doc: &LoroDoc, pos: usize, len: usize) {
48 let richtext = doc.get_text("r");
49 richtext.delete(pos, len, PosType::Unicode).unwrap();
50}
51
52fn mark(doc: &LoroDoc, range: Range<usize>, kind: Kind) {
53 let richtext = doc.get_text("r");
54 richtext
55 .mark(
56 range.start,
57 range.end,
58 kind.key(),
59 true.into(),
60 PosType::Event,
61 )
62 .unwrap();
63}
64
65fn unmark(doc: &LoroDoc, range: Range<usize>, kind: Kind) {
66 let richtext = doc.get_text("r");
67 richtext
68 .mark(
69 range.start,
70 range.end,
71 kind.key(),
72 false.into(),
73 PosType::Event,
74 )
75 .unwrap();
76}
77
78fn mark_kv(doc: &LoroDoc, range: Range<usize>, key: &str, value: impl Into<LoroValue>) {
79 let richtext = doc.get_text("r");
80 richtext
81 .mark(range.start, range.end, key, value.into(), PosType::Event)
82 .unwrap();
83}
84
85fn merge(a: &LoroDoc, b: &LoroDoc) {
86 a.import(&b.export(ExportMode::updates(&a.oplog_vv())).unwrap())
87 .unwrap();
88 b.import(&a.export(ExportMode::updates(&b.oplog_vv())).unwrap())
89 .unwrap();
90}
91
92fn expect_result(doc: &LoroDoc, json: serde_json::Value) {
93 let richtext = doc.get_text("r");
94 let s = richtext.get_richtext_value().to_json_value();
95 assert_eq!(
96 &s,
97 &json,
98 "expect: {}, got: {}",
99 serde_json::to_string_pretty(&json).unwrap(),
100 serde_json::to_string_pretty(&s).unwrap()
101 );
102}
103
104#[test]
105fn case0() {
106 let doc_a = init("Hello World");
107 let doc_b = clone(&doc_a, 2);
108 mark(&doc_a, 0..11, Kind::Bold);
109 insert(&doc_b, 6, "New ");
110 merge(&doc_a, &doc_b);
111 expect_result(
112 &doc_a,
113 json!([{"insert":"Hello New World","attributes":{"bold":true}}]),
114 );
115 doc_a.check_state_diff_calc_consistency_slow();
116 doc_b.check_state_diff_calc_consistency_slow();
117}
118
119#[test]
120fn case1() {
121 let doc_a = init("Hello World");
122 let doc_b = clone(&doc_a, 2);
123 mark(&doc_a, 0..5, Kind::Bold);
124 mark(&doc_b, 3..11, Kind::Bold);
125 merge(&doc_a, &doc_b);
126 expect_result(
127 &doc_a,
128 json!([{"insert":"Hello World","attributes":{"bold":true}}]),
129 );
130 doc_a.check_state_diff_calc_consistency_slow();
131 doc_b.check_state_diff_calc_consistency_slow();
132}
133
134#[test]
135fn case2() {
136 let doc_a = init("Hello World");
137 mark(&doc_a, 0..11, Kind::Bold);
138 let doc_b = clone(&doc_a, 2);
139 unmark(&doc_a, 0..6, Kind::Bold);
140 insert(&doc_b, 5, " a");
141 merge(&doc_a, &doc_b);
142 expect_result(
143 &doc_a,
144 json!([{"insert":"Hello a ","attributes":{"bold":false}},{"insert":"World","attributes":{"bold":true}}]),
145 );
146 doc_a.check_state_diff_calc_consistency_slow();
147 doc_b.check_state_diff_calc_consistency_slow();
148}
149
150/// | Name | Text |
151/// |:----------------|:-----------------------|
152/// | Origin | `Hello World` |
153/// | Concurrent A | `Hello <b>World</b>` |
154/// | Concurrent B | `Hello a World` |
155/// | Expected Result | `Hello a <b>World</b>` |
156///
157#[test]
158fn case3() {
159 let doc_a = init("Hello World");
160 let doc_b = clone(&doc_a, 2);
161 mark(&doc_a, 6..11, Kind::Bold);
162 insert(&doc_b, 5, " a");
163 merge(&doc_a, &doc_b);
164 expect_result(
165 &doc_a,
166 json!([{"insert":"Hello a "},{"insert":"World","attributes":{"bold":true}}]),
167 );
168 doc_a.check_state_diff_calc_consistency_slow();
169 doc_b.check_state_diff_calc_consistency_slow();
170}
171
172/// | Name | Text |
173/// |:----------------|:---------------------------|
174/// | Origin | `Hello World` |
175/// | Concurrent A | `<link>Hello</link> World` |
176/// | Concurrent B | `Hey World` |
177/// | Expected Result | `<link>Hey</link> World` |
178#[test]
179fn case4() {
180 let doc_a = init("Hello World");
181 let doc_b = clone(&doc_a, 2);
182 mark(&doc_a, 0..5, Kind::Link);
183 delete(&doc_b, 2, 3);
184 expect_result(&doc_b, json!([{"insert":"He World"}]));
185 insert(&doc_b, 2, "y");
186 expect_result(&doc_b, json!([{"insert":"Hey World"}]));
187 merge(&doc_a, &doc_b);
188 expect_result(
189 &doc_b,
190 json!([{"insert":"Hey","attributes":{"link":true}},{"insert":" World"}]),
191 );
192 expect_result(
193 &doc_a,
194 json!([{"insert":"Hey","attributes":{"link":true}},{"insert":" World"}]),
195 );
196 doc_a.check_state_diff_calc_consistency_slow();
197 doc_b.check_state_diff_calc_consistency_slow();
198}
199
200/// When insert a new character after "Hello", the new char should be bold but not link
201///
202/// | Name | Text |
203/// |:----------------|:----------------------------------|
204/// | Origin | `<b><link>Hello</link><b> World` |
205/// | Expected Result | `<b><link>Hello</link>t<b> World` |
206#[test]
207fn case5() {
208 let doc = init("Hello World");
209 mark(&doc, 0..5, Kind::Bold);
210 expect_result(
211 &doc,
212 serde_json::json!([
213 {"insert": "Hello", "attributes": {"bold": true}},
214 {"insert": " World"}
215 ]),
216 );
217 mark(&doc, 0..5, Kind::Link);
218 expect_result(
219 &doc,
220 serde_json::json!([
221 {"insert": "Hello", "attributes": {"bold": true, "link": true}},
222 {"insert": " World"}
223 ]),
224 );
225 insert(&doc, 5, "t");
226 expect_result(
227 &doc,
228 serde_json::json!([
229 {"insert": "Hello", "attributes": {"bold": true, "link": true}},
230 {"insert": "t", "attributes": {"bold": true}},
231 {"insert": " World"}
232 ]),
233 );
234 doc.check_state_diff_calc_consistency_slow();
235}
236
237///
238/// | Name | Text |
239/// |:----------------|:---------------------------------------------|
240/// | Origin | `<b>The fox jumped</b> over the dog.` |
241/// | Concurrent A | `The fox jumped over the dog.` |
242/// | Concurrent B | `<b>The </b>fox<b> jumped</b> over the dog.` |
243/// | Expected Result | `The fox jumped over the dog.` |
244#[test]
245fn case6() {
246 let doc_a = init("The fox jumped over the dog.");
247 mark(&doc_a, 0..3, Kind::Bold);
248 let doc_b = clone(&doc_a, 2);
249 unmark(&doc_a, 0..3, Kind::Bold);
250 unmark(&doc_b, 4..7, Kind::Bold);
251 merge(&doc_a, &doc_b);
252 expect_result(
253 &doc_a,
254 json!([
255 {"insert":"The", "attributes": {"bold": false}},
256 {"insert":" ",},
257 {"insert":"fox", "attributes": {"bold": false}},
258 {"insert":" jumped over the dog."}
259 ]),
260 );
261 doc_a.check_state_diff_calc_consistency_slow();
262 doc_b.check_state_diff_calc_consistency_slow();
263}
264
265/// | Name | Text |
266/// |:----------------|:---------------------------------------------|
267/// | Origin | `<b>The fox jumped</b> over the dog.` |
268/// | Concurrent A | `<b>The fox</b> jumped over the dog.` |
269/// | Concurrent B | `<b>The</b> fox jumped over the <b>dog</b>.` |
270/// | Expected Result | `<b>The</b> fox jumped over the <b>dog</b>.` |
271#[test]
272fn case7() {
273 let doc_a = init("The fox jumped over the dog.");
274 mark(&doc_a, 0..14, Kind::Bold);
275 let doc_b = clone(&doc_a, 2);
276 unmark(&doc_a, 7..14, Kind::Bold);
277 unmark(&doc_b, 3..14, Kind::Bold);
278 mark(&doc_b, 24..27, Kind::Bold);
279 merge(&doc_a, &doc_b);
280 expect_result(
281 &doc_a,
282 serde_json::json!([
283 {"insert": "The", "attributes": {"bold": true}},
284 {"insert": " fox jumped", "attributes": {"bold": false}},
285 {"insert": " over the "},
286 {"insert": "dog", "attributes": {"bold": true}},
287 {"insert": "."}
288 ]),
289 );
290 doc_a.check_state_diff_calc_consistency_slow();
291 doc_b.check_state_diff_calc_consistency_slow();
292}
293
294/// | Name | Text |
295/// |:----------------|:-----------------------------|
296/// | Origin | The fox jumped. |
297/// | Concurrent A | **The fox** jumped. |
298/// | Concurrent B | The *fox jumped*. |
299/// | Expected Result | **The _fox_**<i> jumped</i>. |
300#[test]
301fn case8() {
302 let doc_a = init("The fox jumped.");
303 let doc_b = clone(&doc_a, 2);
304 mark(&doc_a, 0..7, Kind::Bold);
305 mark(&doc_a, 4..14, Kind::Italic);
306 merge(&doc_a, &doc_b);
307 expect_result(
308 &doc_a,
309 serde_json::json!([
310 {"insert": "The ", "attributes": {"bold": true}},
311 {"insert": "fox", "attributes": {"bold": true, "italic": true}},
312 {"insert": " jumped", "attributes": {"italic": true}},
313 {"insert": "."}
314 ]),
315 );
316 doc_a.check_state_diff_calc_consistency_slow();
317 doc_b.check_state_diff_calc_consistency_slow();
318}
319
320/// ![](https://i.postimg.cc/MTNGq8cH/Clean-Shot-2023-10-09-at-12-16-29-2x.png)
321#[test]
322fn case9() {
323 let doc_a = init("The fox jumped.");
324 let doc_b = clone(&doc_a, 2);
325 mark_kv(&doc_a, 0..7, "comment:alice", "alice comment");
326 mark_kv(&doc_a, 4..14, "comment:bob", "bob comment");
327 merge(&doc_a, &doc_b);
328 expect_result(
329 &doc_a,
330 serde_json::json!([
331 {"insert": "The ", "attributes": {"comment:alice": "alice comment"}},
332 {"insert": "fox", "attributes": {"comment:alice": "alice comment", "comment:bob": "bob comment"}},
333 {"insert": " jumped", "attributes": {"comment:bob": "bob comment"}},
334 {"insert": "."}
335 ]),
336 );
337}
338
339#[test]
340fn insert_after_link() {
341 let doc_a = init("The fox jumped.");
342 let doc_b = clone(&doc_a, 2);
343 mark(&doc_a, 0..3, Kind::Link);
344 merge(&doc_a, &doc_b);
345 insert(&doc_a, 3, "a");
346 merge(&doc_a, &doc_b);
347 expect_result(
348 &doc_a,
349 serde_json::json!([
350 {"insert": "The", "attributes": {"link": true}},
351 {"insert": "a fox jumped."},
352 ]),
353 );
354 expect_result(
355 &doc_b,
356 serde_json::json!([
357 {"insert": "The", "attributes": {"link": true}},
358 {"insert": "a fox jumped."},
359 ]),
360 );
361 doc_a.check_state_diff_calc_consistency_slow();
362 doc_b.check_state_diff_calc_consistency_slow();
363}
364
365#[test]
366fn redundant_mark_spanning_style_boundaries_creates_no_ops() {
367 let doc = init("Hello World");
368 mark(&doc, 0..5, Kind::Bold);
369 // This range spans a style boundary, so it genuinely changes 5..11.
370 mark(&doc, 0..11, Kind::Bold);
371 doc.commit_then_renew();
372 let vv = doc.oplog_vv();
373
374 // The whole text is already bold; re-asserting the mark must not record
375 // new ops, even though the range spans multiple style ranges. Every
376 // redundant mark op leaves a pair of style anchors in the container state
377 // forever, permanently slowing down styled reads.
378 mark(&doc, 0..11, Kind::Bold);
379 doc.commit_then_renew();
380 assert_eq!(doc.oplog_vv(), vv);
381 expect_result(
382 &doc,
383 serde_json::json!([
384 {"insert": "Hello World", "attributes": {"bold": true}},
385 ]),
386 );
387}
388
389#[test]
390fn redundant_unmark_spanning_style_boundaries_creates_no_ops() {
391 let doc = init("Hello World");
392 mark(&doc, 0..5, Kind::Bold);
393 mark(&doc, 6..11, Kind::Bold);
394 unmark(&doc, 0..11, Kind::Bold);
395 doc.commit_then_renew();
396 let vv = doc.oplog_vv();
397
398 unmark(&doc, 0..11, Kind::Bold);
399 doc.commit_then_renew();
400 assert_eq!(doc.oplog_vv(), vv);
401 expect_result(
402 &doc,
403 serde_json::json!([
404 {"insert": "Hello World", "attributes": {"bold": false}},
405 ]),
406 );
407}
408
409#[test]
410fn mark_that_changes_part_of_the_range_still_applies() {
411 let doc = init("Hello World");
412 mark(&doc, 0..5, Kind::Bold);
413 doc.commit_then_renew();
414 let vv = doc.oplog_vv();
415
416 // 0..5 is already bold but 5..11 is not, so this must not be skipped.
417 mark(&doc, 0..11, Kind::Bold);
418 doc.commit_then_renew();
419 assert_ne!(doc.oplog_vv(), vv);
420 expect_result(
421 &doc,
422 serde_json::json!([
423 {"insert": "Hello World", "attributes": {"bold": true}},
424 ]),
425 );
426}