doc_script.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. use crate::{helper::ViewTest, FlowySDKTest};
  2. use flowy_collaboration::entities::revision::RevisionState;
  3. use flowy_document::core::{edit::ClientDocumentEditor, SYNC_INTERVAL_IN_MILLIS};
  4. use lib_ot::{core::Interval, rich_text::RichTextDelta};
  5. use std::sync::Arc;
  6. use tokio::time::{sleep, Duration};
  7. pub enum EditorScript {
  8. StartWs,
  9. StopWs,
  10. InsertText(&'static str, usize),
  11. Delete(Interval),
  12. Replace(Interval, &'static str),
  13. AssertRevisionState(i64, RevisionState),
  14. AssertNextRevId(Option<i64>),
  15. AssertCurrentRevId(i64),
  16. AssertJson(&'static str),
  17. WaitSyncFinished,
  18. }
  19. pub struct EditorTest {
  20. pub sdk: FlowySDKTest,
  21. pub editor: Arc<ClientDocumentEditor>,
  22. }
  23. impl EditorTest {
  24. pub async fn new() -> Self {
  25. let sdk = FlowySDKTest::setup();
  26. let _ = sdk.init_user().await;
  27. let test = ViewTest::new(&sdk).await;
  28. let editor = sdk.document_ctx.controller.open(&test.view.id).await.unwrap();
  29. Self { sdk, editor }
  30. }
  31. pub async fn run_scripts(mut self, scripts: Vec<EditorScript>) {
  32. for script in scripts {
  33. self.run_script(script).await;
  34. }
  35. sleep(Duration::from_secs(3)).await;
  36. }
  37. async fn run_script(&mut self, script: EditorScript) {
  38. let rev_manager = self.editor.rev_manager();
  39. let cache = rev_manager.revision_cache();
  40. let _user_id = self.sdk.user_session.user_id().unwrap();
  41. let ws_manager = self.sdk.ws_manager.clone();
  42. let token = self.sdk.user_session.token().unwrap();
  43. let wait_millis = 2 * SYNC_INTERVAL_IN_MILLIS;
  44. match script {
  45. EditorScript::StartWs => {
  46. ws_manager.start(token.clone()).await.unwrap();
  47. },
  48. EditorScript::StopWs => {
  49. ws_manager.stop().await;
  50. },
  51. EditorScript::InsertText(s, offset) => {
  52. self.editor.insert(offset, s).await.unwrap();
  53. },
  54. EditorScript::Delete(interval) => {
  55. self.editor.delete(interval).await.unwrap();
  56. },
  57. EditorScript::Replace(interval, s) => {
  58. self.editor.replace(interval, s).await.unwrap();
  59. },
  60. EditorScript::AssertRevisionState(rev_id, state) => {
  61. let record = cache.get(rev_id).await.unwrap();
  62. assert_eq!(record.state, state);
  63. },
  64. EditorScript::AssertCurrentRevId(rev_id) => {
  65. assert_eq!(self.editor.rev_manager().rev_id(), rev_id);
  66. },
  67. EditorScript::AssertNextRevId(rev_id) => {
  68. let next_revision = rev_manager.next_sync_revision().await.unwrap();
  69. if rev_id.is_none() {
  70. assert_eq!(next_revision.is_none(), true, "Next revision should be None");
  71. return;
  72. }
  73. let next_revision = next_revision.unwrap();
  74. assert_eq!(next_revision.rev_id, rev_id.unwrap());
  75. },
  76. EditorScript::AssertJson(expected) => {
  77. let expected_delta: RichTextDelta = serde_json::from_str(expected).unwrap();
  78. let delta = self.editor.doc_delta().await.unwrap();
  79. if expected_delta != delta {
  80. eprintln!("✅ expect: {}", expected,);
  81. eprintln!("❌ receive: {}", delta.to_json());
  82. }
  83. assert_eq!(expected_delta, delta);
  84. },
  85. EditorScript::WaitSyncFinished => {
  86. // Workaround: just wait two seconds
  87. sleep(Duration::from_millis(2000)).await;
  88. },
  89. }
  90. sleep(Duration::from_millis(wait_millis)).await;
  91. }
  92. }