edit_test.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. use crate::document_test::edit_script::{DocScript, DocumentTest};
  2. use flowy_collaboration::document::{Document, NewlineDoc};
  3. use lib_ot::{core::Interval, rich_text::RichTextAttribute};
  4. #[rustfmt::skip]
  5. // ┌─────────┐ ┌─────────┐
  6. // │ Server │ │ Client │
  7. // └─────────┘ └─────────┘
  8. // ┌────────────────┐ │ │ ┌────────────────┐
  9. // │ops: [] rev: 0 │◀┼──── Ping ─────┼─┤ops: [] rev: 0 │
  10. // └────────────────┘ │ │ └────────────────┘
  11. // ┌────────────────────┐ │ │ ┌────────────────────┐
  12. // │ops: ["abc"] rev: 1 │◀┼───ClientPush ───┼─│ops: ["abc"] rev: 1 │
  13. // └────────────────────┘ │ │ └────────────────────┘
  14. // ┌──────────────────────────┐ │ │ ┌──────────────────────┐
  15. // │ops: ["abc", "123"] rev: 2│◀┼── ClientPush ───┼─│ops: ["123"] rev: 2 │
  16. // └──────────────────────────┘ │ │ └──────────────────────┘
  17. // │ │
  18. #[actix_rt::test]
  19. async fn delta_sync_while_editing() {
  20. let test = DocumentTest::new().await;
  21. test.run_scripts(vec![
  22. DocScript::ClientOpenDoc,
  23. DocScript::ClientInsertText(0, "abc"),
  24. DocScript::ClientInsertText(3, "123"),
  25. DocScript::AssertClient(r#"[{"insert":"abc123\n"}]"#),
  26. DocScript::AssertServer(r#"[{"insert":"abc123\n"}]"#, 2),
  27. ])
  28. .await;
  29. }
  30. #[actix_rt::test]
  31. async fn delta_sync_multi_revs() {
  32. let test = DocumentTest::new().await;
  33. test.run_scripts(vec![
  34. DocScript::ClientOpenDoc,
  35. DocScript::ClientInsertText(0, "abc"),
  36. DocScript::ClientInsertText(3, "123"),
  37. DocScript::ClientInsertText(6, "efg"),
  38. DocScript::ClientInsertText(9, "456"),
  39. ])
  40. .await;
  41. }
  42. #[actix_rt::test]
  43. async fn delta_sync_while_editing_with_attribute() {
  44. let test = DocumentTest::new().await;
  45. test.run_scripts(vec![
  46. DocScript::ClientOpenDoc,
  47. DocScript::ClientInsertText(0, "abc"),
  48. DocScript::ClientFormatText(Interval::new(0, 3), RichTextAttribute::Bold(true)),
  49. DocScript::AssertClient(r#"[{"insert":"abc","attributes":{"bold":true}},{"insert":"\n"}]"#),
  50. DocScript::AssertServer(r#"[{"insert":"abc","attributes":{"bold":true}},{"insert":"\n"}]"#, 2),
  51. DocScript::ClientInsertText(3, "efg"),
  52. DocScript::ClientFormatText(Interval::new(3, 5), RichTextAttribute::Italic(true)),
  53. DocScript::AssertClient(r#"[{"insert":"abc","attributes":{"bold":true}},{"insert":"ef","attributes":{"bold":true,"italic":true}},{"insert":"g","attributes":{"bold":true}},{"insert":"\n"}]"#),
  54. DocScript::AssertServer(r#"[{"insert":"abc","attributes":{"bold":true}},{"insert":"ef","attributes":{"bold":true,"italic":true}},{"insert":"g","attributes":{"bold":true}},{"insert":"\n"}]"#, 4),
  55. ])
  56. .await;
  57. }
  58. #[rustfmt::skip]
  59. // ┌─────────┐ ┌─────────┐
  60. // │ Server │ │ Client │
  61. // └─────────┘ └─────────┘
  62. // ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │
  63. // ops: ["123", "456"] rev: 3│ │ │
  64. // └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │
  65. // │ │
  66. // ◀───── Ping ───┤ Open doc
  67. // │ │
  68. // │ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
  69. // ├───ServerPush────┼─▶ ops: ["123", "456"] rev: 3│
  70. // │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
  71. #[actix_rt::test]
  72. async fn delta_sync_with_server_push() {
  73. let test = DocumentTest::new().await;
  74. let mut document = Document::new::<NewlineDoc>();
  75. document.insert(0, "123").unwrap();
  76. document.insert(3, "456").unwrap();
  77. let json = document.to_json();
  78. test.run_scripts(vec![
  79. DocScript::ServerResetDocument(json, 3),
  80. DocScript::ClientOpenDoc,
  81. DocScript::AssertClient(r#"[{"insert":"123456\n"}]"#),
  82. DocScript::AssertServer(r#"[{"insert":"123456\n"}]"#, 3),
  83. ])
  84. .await;
  85. }
  86. #[rustfmt::skip]
  87. // ┌─────────┐ ┌─────────┐
  88. // │ Server │ │ Client │
  89. // └─────────┘ └─────────┘
  90. // ┌ ─ ─ ─ ─ ┐ │ │
  91. // ops: [] │ │
  92. // └ ─ ─ ─ ─ ┘ │ │
  93. // │ │
  94. // ◀───── Ping ───┤ Open doc
  95. // ◀───── Ping ───┤
  96. // ◀───── Ping ───┤
  97. // ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │
  98. // ops: ["123"], rev: 3 │ │
  99. // └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐
  100. // ├────ServerPush───▶ ops: ["123"] rev: 3
  101. // │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘
  102. // │ │
  103. #[actix_rt::test]
  104. async fn delta_sync_with_server_push_after_reset_document() {
  105. let test = DocumentTest::new().await;
  106. let mut document = Document::new::<NewlineDoc>();
  107. document.insert(0, "123").unwrap();
  108. let json = document.to_json();
  109. test.run_scripts(vec![
  110. DocScript::ClientOpenDoc,
  111. DocScript::ServerResetDocument(json, 3),
  112. DocScript::AssertClient(r#"[{"insert":"123\n"}]"#),
  113. DocScript::AssertServer(r#"[{"insert":"123\n"}]"#, 3),
  114. ])
  115. .await;
  116. }
  117. #[rustfmt::skip]
  118. // ┌─────────┐ ┌─────────┐
  119. // │ Server │ │ Client │
  120. // └─────────┘ └─────────┘
  121. // │ │
  122. // │ │
  123. // ◀────── Ping ─────┤ Open doc
  124. // ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │
  125. // ops: ["123"] rev: 3 │ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
  126. // └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ │ ops: ["abc"] rev: 1 │
  127. // │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
  128. // │ │ ┌────────────────────┐
  129. // ◀───ClientPush ───┤ │ops: ["abc"] rev: 1 │
  130. // ┌───────────────────┐ │ │ └────────────────────┘
  131. // │ops: ["123"] rev: 3│ ├────ServerPush───▶ transform
  132. // └───────────────────┘ │ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
  133. // │ │ ops: ["abc", "123"] rev: 4│
  134. // │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
  135. // │ │ ┌────────────────────────────────┐
  136. // ◀────ClientPush───┤ │ops: ["retain 3","abc"] rev: 4 │
  137. // ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ └────────────────────────────────┘
  138. // ops: ["abc", "123"] rev: 4│ ├────ServerAck────▶
  139. // └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │
  140. #[actix_rt::test]
  141. async fn delta_sync_while_local_rev_less_than_server_rev() {
  142. let test = DocumentTest::new().await;
  143. let mut document = Document::new::<NewlineDoc>();
  144. document.insert(0, "123").unwrap();
  145. let json = document.to_json();
  146. test.run_scripts(vec![
  147. DocScript::ClientOpenDoc,
  148. DocScript::ServerResetDocument(json, 3),
  149. DocScript::ClientInsertText(0, "abc"),
  150. DocScript::AssertClient(r#"[{"insert":"abc123\n"}]"#),
  151. DocScript::AssertServer(r#"[{"insert":"abc123\n"}]"#, 4),
  152. ])
  153. .await;
  154. }
  155. #[rustfmt::skip]
  156. // ┌─────────┐ ┌─────────┐
  157. // │ Server │ │ Client │
  158. // └─────────┘ └─────────┘
  159. // ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │
  160. // ops: ["123"] rev: 1 │ │
  161. // └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ │
  162. // ◀──── Ping ────┤ Open doc
  163. // │ │
  164. // │ │ ┌──────────────────┐
  165. // ├───ServerPush────▶ │ops: [123] rev: 1 │
  166. // │ │ └──────────────────┘
  167. // │ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
  168. // │ │ ops: ["123","abc", "efg"] rev: 3 │
  169. // │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
  170. // │ │ ┌──────────────────────────────┐
  171. // ◀────ClientPush───┤ │ops: [retain 3, "abc"] rev: 2 │
  172. // ┌──────────────────────────┐ │ │ └──────────────────────────────┘
  173. // │ops: ["123","abc"] rev: 2 │ ├────ServerAck────▶
  174. // └──────────────────────────┘ │ │
  175. // │ │ ┌──────────────────────────────┐
  176. // ◀────ClientPush───┤ │ops: [retain 6, "efg"] rev: 3 │
  177. // ┌──────────────────────────────────┐ │ │ └──────────────────────────────┘
  178. // │ops: ["123","abc", "efg"] rev: 3 │ ├────ServerAck────▶
  179. // └──────────────────────────────────┘ │ │
  180. #[actix_rt::test]
  181. async fn delta_sync_while_local_rev_greater_than_server_rev() {
  182. let test = DocumentTest::new().await;
  183. let mut document = Document::new::<NewlineDoc>();
  184. document.insert(0, "123").unwrap();
  185. let json = document.to_json();
  186. test.run_scripts(vec![
  187. DocScript::ServerResetDocument(json, 1),
  188. DocScript::ClientOpenDoc,
  189. DocScript::AssertClient(r#"[{"insert":"123\n"}]"#),
  190. DocScript::ClientInsertText(3, "abc"),
  191. DocScript::ClientInsertText(6, "efg"),
  192. DocScript::AssertClient(r#"[{"insert":"123abcefg\n"}]"#),
  193. DocScript::AssertServer(r#"[{"insert":"123abcefg\n"}]"#, 3),
  194. ])
  195. .await;
  196. }