edit.rs 13 KB

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