edit_script.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. use flowy_collaboration::entities::revision::RevisionState;
  2. use flowy_document::core::{edit::ClientDocumentEditor, SYNC_INTERVAL_IN_MILLIS};
  3. use flowy_test::{helper::ViewTest, FlowySDKTest};
  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. InsertText(&'static str, usize),
  9. Delete(Interval),
  10. Replace(Interval, &'static str),
  11. AssertRevisionState(i64, RevisionState),
  12. AssertNextRevId(Option<i64>),
  13. AssertCurrentRevId(i64),
  14. AssertJson(&'static str),
  15. }
  16. pub struct EditorTest {
  17. pub sdk: FlowySDKTest,
  18. pub editor: Arc<ClientDocumentEditor>,
  19. }
  20. impl EditorTest {
  21. pub async fn new() -> Self {
  22. let sdk = FlowySDKTest::default();
  23. let _ = sdk.init_user().await;
  24. let test = ViewTest::new(&sdk).await;
  25. let editor = sdk.document_ctx.controller.open_document(&test.view.id).await.unwrap();
  26. Self { sdk, editor }
  27. }
  28. pub async fn run_scripts(mut self, scripts: Vec<EditorScript>) {
  29. for script in scripts {
  30. self.run_script(script).await;
  31. }
  32. }
  33. async fn run_script(&mut self, script: EditorScript) {
  34. let rev_manager = self.editor.rev_manager();
  35. let cache = rev_manager.revision_cache();
  36. let _user_id = self.sdk.user_session.user_id().unwrap();
  37. // let ws_manager = self.sdk.ws_conn.clone();
  38. // let token = self.sdk.user_session.token().unwrap();
  39. match script {
  40. EditorScript::InsertText(s, offset) => {
  41. self.editor.insert(offset, s).await.unwrap();
  42. },
  43. EditorScript::Delete(interval) => {
  44. self.editor.delete(interval).await.unwrap();
  45. },
  46. EditorScript::Replace(interval, s) => {
  47. self.editor.replace(interval, s).await.unwrap();
  48. },
  49. EditorScript::AssertRevisionState(rev_id, state) => {
  50. let record = cache.get(rev_id).await.unwrap();
  51. assert_eq!(record.state, state);
  52. },
  53. EditorScript::AssertCurrentRevId(rev_id) => {
  54. assert_eq!(self.editor.rev_manager().rev_id(), rev_id);
  55. },
  56. EditorScript::AssertNextRevId(rev_id) => {
  57. let next_revision = rev_manager.next_sync_revision().await.unwrap();
  58. if rev_id.is_none() {
  59. assert_eq!(next_revision.is_none(), true, "Next revision should be None");
  60. return;
  61. }
  62. let next_revision = next_revision.unwrap();
  63. assert_eq!(next_revision.rev_id, rev_id.unwrap());
  64. },
  65. EditorScript::AssertJson(expected) => {
  66. let expected_delta: RichTextDelta = serde_json::from_str(expected).unwrap();
  67. let delta = self.editor.doc_delta().await.unwrap();
  68. if expected_delta != delta {
  69. eprintln!("✅ expect: {}", expected,);
  70. eprintln!("❌ receive: {}", delta.to_json());
  71. }
  72. assert_eq!(expected_delta, delta);
  73. },
  74. }
  75. sleep(Duration::from_millis(SYNC_INTERVAL_IN_MILLIS)).await;
  76. }
  77. }