edit_script.rs 3.3 KB

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