edit.rs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. // ┌───────────────────┐ │ │ ┌──────────────────────────┐
  93. // │ops: ["123"] rev: 3│ ├────Push Rev─────▶ │ops: ["abc", "123"] rev: 4│
  94. // └───────────────────┘ │ │ └──────────────────────────┘
  95. // ┌──────────────────────────┐ │ │ ┌────────────────────┐
  96. // │ops: ["abc", "123"] rev: 4│ ◀────Push Rev─────┤ │ops: ["abc"] rev: 4 │
  97. // └──────────────────────────┘ │ │ └────────────────────┘
  98. // │ │
  99. #[actix_rt::test]
  100. async fn delta_sync_while_local_rev_less_than_server_rev() {
  101. let test = DocumentTest::new().await;
  102. let mut document = Document::new::<FlowyDoc>();
  103. document.insert(0, "123").unwrap();
  104. let json = document.to_json();
  105. test.run_scripts(vec![
  106. DocScript::OpenDoc,
  107. DocScript::SetServerDocument(json, 3),
  108. DocScript::SendText(0, "abc"),
  109. DocScript::ConnectWs,
  110. DocScript::AssertClient(r#"[{"insert":"abc\n123\n"}]"#),
  111. DocScript::AssertServer(r#"[{"insert":"abc\n123\n"}]"#),
  112. ])
  113. .await;
  114. }
  115. #[actix_rt::test]
  116. async fn delta_sync_while_local_rev_greater_than_server_rev() {
  117. let test = DocumentTest::new().await;
  118. let mut document = Document::new::<FlowyDoc>();
  119. document.insert(0, "123").unwrap();
  120. let json = document.to_json();
  121. test.run_scripts(vec![
  122. DocScript::SetServerDocument(json, 1),
  123. DocScript::OpenDoc,
  124. DocScript::AssertClient(r#"[{"insert":"123\n"}]"#),
  125. DocScript::SendText(3, "abc"),
  126. DocScript::SendText(6, "efg"),
  127. DocScript::ConnectWs,
  128. DocScript::AssertClient(r#"[{"insert":"123abcefg\n"}]"#),
  129. DocScript::AssertServer(r#"[{"insert":"123abcefg\n"}]"#),
  130. ])
  131. .await;
  132. }