edit_test.rs 13 KB

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