edit.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. use crate::document::helper::{DocScript, DocumentTest};
  2. use flowy_document::services::doc::{Document, FlowyDoc};
  3. #[rustfmt::skip]
  4. // ┌─────────┐ ┌─────────┐
  5. // │ Server │ │ Client │
  6. // └─────────┘ └─────────┘
  7. // ┌────────────────┐ │ │ ┌────────────────┐
  8. // │ops: [] rev: 0 │◀┼───── ws ────┼─┤ops: [] rev: 0 │
  9. // └────────────────┘ │ │ └────────────────┘
  10. // ┌────────────────────┐ │ │ ┌────────────────────┐
  11. // │ops: ["abc"] rev: 1 │◀┼───── ws ────┼─│ops: ["abc"] rev: 1 │
  12. // └────────────────────┘ │ │ └────────────────────┘
  13. // ┌──────────────────────────┐ │ │ ┌──────────────────────┐
  14. // │ops: ["abc", "123"] rev: 2│◀┼───── ws ────┼─│ops: ["123"] rev: 2 │
  15. // └──────────────────────────┘ │ │ └──────────────────────┘
  16. // │ │
  17. #[actix_rt::test]
  18. async fn delta_sync_after_ws_connection() {
  19. let test = DocumentTest::new().await;
  20. test.run_scripts(vec![
  21. DocScript::ConnectWs,
  22. DocScript::OpenDoc,
  23. DocScript::SendText(0, "abc"),
  24. DocScript::SendText(3, "123"),
  25. DocScript::AssertClient(r#"[{"insert":"abc123\n"}]"#),
  26. DocScript::AssertServer(r#"[{"insert":"abc123\n"}]"#),
  27. ])
  28. .await;
  29. }
  30. #[rustfmt::skip]
  31. // ┌─────────┐ ┌─────────┐
  32. // │ Server │ │ Client │
  33. // └─────────┘ └─────────┘
  34. // ┌──────────────────────────┐ │ │
  35. // │ops: ["123", "456"] rev: 2│ │ │
  36. // └──────────────────────────┘ │ │
  37. // │ │
  38. // ◀── http request ─┤ Open doc
  39. // │ │
  40. // │ │ ┌──────────────────────────┐
  41. // ├──http response──┼─▶│ops: ["123", "456"] rev: 2│
  42. // │ │ └──────────────────────────┘
  43. #[actix_rt::test]
  44. async fn delta_sync_with_http_request() {
  45. let test = DocumentTest::new().await;
  46. let mut document = Document::new::<FlowyDoc>();
  47. document.insert(0, "123").unwrap();
  48. document.insert(3, "456").unwrap();
  49. let json = document.to_json();
  50. test.run_scripts(vec![
  51. DocScript::SetServerDocument(json, 3),
  52. DocScript::OpenDoc,
  53. DocScript::AssertClient(r#"[{"insert":"123456\n"}]"#),
  54. DocScript::AssertServer(r#"[{"insert":"123456\n"}]"#),
  55. ])
  56. .await;
  57. }
  58. #[actix_rt::test]
  59. async fn delta_sync_with_server_push_delta() {
  60. let test = DocumentTest::new().await;
  61. let mut document = Document::new::<FlowyDoc>();
  62. document.insert(0, "123").unwrap();
  63. let json = document.to_json();
  64. test.run_scripts(vec![
  65. DocScript::OpenDoc,
  66. DocScript::SetServerDocument(json, 3),
  67. DocScript::ConnectWs,
  68. DocScript::AssertClient(r#"[{"insert":"\n123\n"}]"#),
  69. ])
  70. .await;
  71. }
  72. #[rustfmt::skip]
  73. // ┌─────────┐ ┌─────────┐
  74. // │ Server │ │ Client │
  75. // └─────────┘ └─────────┘
  76. // │ │
  77. // │ │
  78. // ◀── http request ─┤ Open doc
  79. // │ │
  80. // │ │ ┌───────────────┐
  81. // ├──http response──┼─▶│ops: [] rev: 0 │
  82. // ┌───────────────────┐│ │ └───────────────┘
  83. // │ops: ["123"] rev: 3││ │
  84. // └───────────────────┘│ │ ┌────────────────────┐
  85. // │ │ │ops: ["abc"] rev: 1 │
  86. // │ │ └────────────────────┘
  87. // │ │
  88. // ◀─────────────────┤ start ws connection
  89. // │ │
  90. // ◀─────────────────┤ notify with rev: 1
  91. // ┌───────────────────┐ │ │
  92. // │ops: ["123"] rev: 3│ ├────Push Rev─────▶ transform
  93. // └───────────────────┘ │ │ ┌──────────────────────────┐
  94. // │ │ │ops: ["abc", "123"] rev: 4│
  95. // │ │ └──────────────────────────┘
  96. // │ │ ┌────────────────────────────────┐
  97. // compose ◀────Push Rev─────┤ │ops: ["abc", "retain 3"] rev: 4 │
  98. // │ │ └────────────────────────────────┘
  99. // ┌──────────────────────────┐ │
  100. // │ops: ["abc", "123"] rev: 4│ │
  101. // └──────────────────────────┘ │
  102. // │ │
  103. #[actix_rt::test]
  104. async fn delta_sync_while_local_rev_less_than_server_rev() {
  105. let test = DocumentTest::new().await;
  106. let mut document = Document::new::<FlowyDoc>();
  107. document.insert(0, "123").unwrap();
  108. let json = document.to_json();
  109. test.run_scripts(vec![
  110. DocScript::OpenDoc,
  111. DocScript::SetServerDocument(json, 3),
  112. DocScript::SendText(0, "abc"),
  113. DocScript::ConnectWs,
  114. DocScript::AssertClient(r#"[{"insert":"abc\n123\n"}]"#),
  115. DocScript::AssertServer(r#"[{"insert":"abc\n123\n"}]"#),
  116. ])
  117. .await;
  118. }
  119. #[rustfmt::skip]
  120. // ┌─────────┐ ┌─────────┐
  121. // │ Server │ │ Client │
  122. // └─────────┘ └─────────┘
  123. // ┌───────────────────┐ │ │
  124. // │ops: ["123"] rev: 1│ │ │
  125. // └───────────────────┘ │ │
  126. // ◀── http request ─┤ Open doc
  127. // │ │
  128. // │ │ ┌───────────────┐
  129. // ├──http response──┼──▶│ops: [123] rev:│
  130. // │ │ └───────────────┘
  131. // │ │ ┌──────────────────────────────────┐
  132. // │ │ │ops: ["123","abc", "efg"] rev: 3 │
  133. // │ │ └──────────────────────────────────┘
  134. // ◀─────────────────┤ start ws connection
  135. // │ │
  136. // ◀─────────────────┤ notify with rev: 3
  137. // │ │
  138. // ├────Pull Rev─────▶
  139. // │ │ ┌──────────────────────────────────┐
  140. // compose ◀────Push Rev─────┤ │ops: ["retain 3", "abcefg"] rev: 3│
  141. // ┌──────────────────────────────────┐│ │ └──────────────────────────────────┘
  142. // │ops: ["123","abc", "efg"] rev: 3 ││ │
  143. // └──────────────────────────────────┘│ │
  144. #[actix_rt::test]
  145. async fn delta_sync_while_local_rev_greater_than_server_rev() {
  146. let test = DocumentTest::new().await;
  147. let mut document = Document::new::<FlowyDoc>();
  148. document.insert(0, "123").unwrap();
  149. let json = document.to_json();
  150. test.run_scripts(vec![
  151. DocScript::SetServerDocument(json, 1),
  152. DocScript::OpenDoc,
  153. DocScript::AssertClient(r#"[{"insert":"123\n"}]"#),
  154. DocScript::SendText(3, "abc"),
  155. DocScript::SendText(6, "efg"),
  156. DocScript::ConnectWs,
  157. DocScript::AssertClient(r#"[{"insert":"123abcefg\n"}]"#),
  158. DocScript::AssertServer(r#"[{"insert":"123abcefg\n"}]"#),
  159. ])
  160. .await;
  161. }