script.rs 3.5 KB

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