edit.rs 12 KB

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