document_test.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. use crate::document::edit_script::{EditorScript::*, *};
  2. use flowy_collaboration::entities::revision::RevisionState;
  3. use lib_ot::core::{count_utf16_code_units, Interval};
  4. #[tokio::test]
  5. async fn document_sync_current_rev_id_check() {
  6. let scripts = vec![
  7. InsertText("1", 0),
  8. AssertCurrentRevId(1),
  9. InsertText("2", 1),
  10. AssertCurrentRevId(2),
  11. InsertText("3", 2),
  12. AssertCurrentRevId(3),
  13. AssertNextSyncRevId(None),
  14. AssertJson(r#"[{"insert":"123\n"}]"#),
  15. ];
  16. EditorTest::new().await.run_scripts(scripts).await;
  17. }
  18. #[tokio::test]
  19. async fn document_sync_state_check() {
  20. let scripts = vec![
  21. InsertText("1", 0),
  22. InsertText("2", 1),
  23. InsertText("3", 2),
  24. AssertRevisionState(1, RevisionState::Ack),
  25. AssertRevisionState(2, RevisionState::Ack),
  26. AssertRevisionState(3, RevisionState::Ack),
  27. AssertJson(r#"[{"insert":"123\n"}]"#),
  28. ];
  29. EditorTest::new().await.run_scripts(scripts).await;
  30. }
  31. #[tokio::test]
  32. async fn document_sync_insert_test() {
  33. let scripts = vec![
  34. InsertText("1", 0),
  35. InsertText("2", 1),
  36. InsertText("3", 2),
  37. AssertJson(r#"[{"insert":"123\n"}]"#),
  38. AssertNextSyncRevId(None),
  39. ];
  40. EditorTest::new().await.run_scripts(scripts).await;
  41. }
  42. #[tokio::test]
  43. async fn document_sync_insert_in_chinese() {
  44. let s = "好".to_owned();
  45. let offset = count_utf16_code_units(&s);
  46. let scripts = vec![
  47. InsertText("你", 0),
  48. InsertText("好", offset),
  49. AssertJson(r#"[{"insert":"你好\n"}]"#),
  50. ];
  51. EditorTest::new().await.run_scripts(scripts).await;
  52. }
  53. #[tokio::test]
  54. async fn document_sync_insert_with_emoji() {
  55. let s = "😁".to_owned();
  56. let offset = count_utf16_code_units(&s);
  57. let scripts = vec![
  58. InsertText("😁", 0),
  59. InsertText("☺️", offset),
  60. AssertJson(r#"[{"insert":"😁☺️\n"}]"#),
  61. ];
  62. EditorTest::new().await.run_scripts(scripts).await;
  63. }
  64. #[tokio::test]
  65. async fn document_sync_delete_in_english() {
  66. let scripts = vec![
  67. InsertText("1", 0),
  68. InsertText("2", 1),
  69. InsertText("3", 2),
  70. Delete(Interval::new(0, 2)),
  71. AssertJson(r#"[{"insert":"3\n"}]"#),
  72. ];
  73. EditorTest::new().await.run_scripts(scripts).await;
  74. }
  75. #[tokio::test]
  76. async fn document_sync_delete_in_chinese() {
  77. let s = "好".to_owned();
  78. let offset = count_utf16_code_units(&s);
  79. let scripts = vec![
  80. InsertText("你", 0),
  81. InsertText("好", offset),
  82. Delete(Interval::new(0, offset)),
  83. AssertJson(r#"[{"insert":"好\n"}]"#),
  84. ];
  85. EditorTest::new().await.run_scripts(scripts).await;
  86. }
  87. #[tokio::test]
  88. async fn document_sync_replace_test() {
  89. let scripts = vec![
  90. InsertText("1", 0),
  91. InsertText("2", 1),
  92. InsertText("3", 2),
  93. Replace(Interval::new(0, 3), "abc"),
  94. AssertJson(r#"[{"insert":"abc\n"}]"#),
  95. ];
  96. EditorTest::new().await.run_scripts(scripts).await;
  97. }