script.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. use flowy_document::old_editor::editor::DeltaDocumentEditor;
  2. use flowy_document::TEXT_BLOCK_SYNC_INTERVAL_IN_MILLIS;
  3. use flowy_revision_persistence::RevisionState;
  4. use flowy_test::{helper::ViewTest, FlowySDKTest};
  5. use lib_ot::{core::Interval, text_delta::DeltaTextOperations};
  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 DeltaDocumentEditorTest {
  18. pub sdk: FlowySDKTest,
  19. pub editor: Arc<DeltaDocumentEditor>,
  20. }
  21. impl DeltaDocumentEditorTest {
  22. pub async fn new() -> Self {
  23. let sdk = FlowySDKTest::default();
  24. let _ = sdk.init_user().await;
  25. let test = ViewTest::new_document_view(&sdk).await;
  26. let document_editor = sdk
  27. .document_manager
  28. .open_document_editor(&test.view.id)
  29. .await
  30. .unwrap();
  31. let editor = match document_editor
  32. .as_any()
  33. .downcast_ref::<Arc<DeltaDocumentEditor>>()
  34. {
  35. None => panic!(),
  36. Some(editor) => editor.clone(),
  37. };
  38. Self { sdk, editor }
  39. }
  40. pub async fn run_scripts(mut self, scripts: Vec<EditorScript>) {
  41. for script in scripts {
  42. self.run_script(script).await;
  43. }
  44. }
  45. async fn run_script(&mut self, script: EditorScript) {
  46. let rev_manager = self.editor.rev_manager();
  47. let cache = rev_manager.revision_cache().await;
  48. let _user_id = self.sdk.user_session.user_id().unwrap();
  49. match script {
  50. EditorScript::InsertText(s, offset) => {
  51. self.editor.insert(offset, s).await.unwrap();
  52. },
  53. EditorScript::Delete(interval) => {
  54. self.editor.delete(interval).await.unwrap();
  55. },
  56. EditorScript::Replace(interval, s) => {
  57. self.editor.replace(interval, s).await.unwrap();
  58. },
  59. EditorScript::AssertRevisionState(rev_id, state) => {
  60. let record = cache.get(rev_id).await.unwrap();
  61. assert_eq!(record.state, state);
  62. },
  63. EditorScript::AssertCurrentRevId(rev_id) => {
  64. assert_eq!(self.editor.rev_manager().rev_id(), rev_id);
  65. },
  66. EditorScript::AssertNextSyncRevId(rev_id) => {
  67. let next_revision = rev_manager.next_sync_revision().await.unwrap();
  68. if rev_id.is_none() {
  69. assert!(next_revision.is_none(), "Next revision should be None");
  70. return;
  71. }
  72. let next_revision = next_revision.unwrap();
  73. let mut notify = rev_manager.ack_notify();
  74. let _ = notify.recv().await;
  75. assert_eq!(next_revision.rev_id, rev_id.unwrap());
  76. },
  77. EditorScript::AssertJson(expected) => {
  78. let expected_delta: DeltaTextOperations = serde_json::from_str(expected).unwrap();
  79. let delta = self.editor.document_operations().await.unwrap();
  80. if expected_delta != delta {
  81. eprintln!("✅ expect: {}", expected,);
  82. eprintln!("❌ receive: {}", delta.json_str());
  83. }
  84. assert_eq!(expected_delta, delta);
  85. },
  86. }
  87. sleep(Duration::from_millis(TEXT_BLOCK_SYNC_INTERVAL_IN_MILLIS)).await;
  88. }
  89. }