script.rs 3.3 KB

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