script.rs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. match script {
  49. EditorScript::InsertText(s, offset) => {
  50. self.editor.insert(offset, s).await.unwrap();
  51. },
  52. EditorScript::Delete(interval) => {
  53. self.editor.delete(interval).await.unwrap();
  54. },
  55. EditorScript::Replace(interval, s) => {
  56. self.editor.replace(interval, s).await.unwrap();
  57. },
  58. EditorScript::AssertRevisionState(rev_id, state) => {
  59. let record = cache.get(rev_id).await.unwrap();
  60. assert_eq!(record.state, state);
  61. },
  62. EditorScript::AssertCurrentRevId(rev_id) => {
  63. assert_eq!(self.editor.rev_manager().rev_id(), rev_id);
  64. },
  65. EditorScript::AssertNextSyncRevId(rev_id) => {
  66. let next_revision = rev_manager.next_sync_revision().await.unwrap();
  67. if rev_id.is_none() {
  68. assert!(next_revision.is_none(), "Next revision should be None");
  69. return;
  70. }
  71. let next_revision = next_revision.unwrap();
  72. let mut notify = rev_manager.ack_notify();
  73. let _ = notify.recv().await;
  74. assert_eq!(next_revision.rev_id, rev_id.unwrap());
  75. },
  76. EditorScript::AssertJson(expected) => {
  77. let expected_delta: DeltaTextOperations = serde_json::from_str(expected).unwrap();
  78. let delta = self.editor.document_operations().await.unwrap();
  79. if expected_delta != delta {
  80. eprintln!("✅ expect: {}", expected,);
  81. eprintln!("❌ receive: {}", delta.json_str());
  82. }
  83. assert_eq!(expected_delta, delta);
  84. },
  85. }
  86. sleep(Duration::from_millis(TEXT_BLOCK_SYNC_INTERVAL_IN_MILLIS)).await;
  87. }
  88. }