script.rs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. use flowy_revision::disk::RevisionState;
  2. use flowy_test::{helper::ViewTest, FlowySDKTest};
  3. use flowy_text_block::editor::TextBlockEditor;
  4. use flowy_text_block::TEXT_BLOCK_SYNC_INTERVAL_IN_MILLIS;
  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 TextBlockEditorTest {
  18. pub sdk: FlowySDKTest,
  19. pub editor: Arc<TextBlockEditor>,
  20. }
  21. impl TextBlockEditorTest {
  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.text_block_manager.open_text_editor(&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. match script {
  39. EditorScript::InsertText(s, offset) => {
  40. self.editor.insert(offset, s).await.unwrap();
  41. }
  42. EditorScript::Delete(interval) => {
  43. self.editor.delete(interval).await.unwrap();
  44. }
  45. EditorScript::Replace(interval, s) => {
  46. self.editor.replace(interval, s).await.unwrap();
  47. }
  48. EditorScript::AssertRevisionState(rev_id, state) => {
  49. let record = cache.get(rev_id).await.unwrap();
  50. assert_eq!(record.state, state);
  51. }
  52. EditorScript::AssertCurrentRevId(rev_id) => {
  53. assert_eq!(self.editor.rev_manager().rev_id(), rev_id);
  54. }
  55. EditorScript::AssertNextSyncRevId(rev_id) => {
  56. let next_revision = rev_manager.next_sync_revision().await.unwrap();
  57. if rev_id.is_none() {
  58. assert!(next_revision.is_none(), "Next revision should be None");
  59. return;
  60. }
  61. let next_revision = next_revision.unwrap();
  62. let mut notify = rev_manager.ack_notify();
  63. let _ = notify.recv().await;
  64. assert_eq!(next_revision.rev_id, rev_id.unwrap());
  65. }
  66. EditorScript::AssertJson(expected) => {
  67. let expected_delta: TextOperations = serde_json::from_str(expected).unwrap();
  68. let delta = self.editor.text_block_delta().await.unwrap();
  69. if expected_delta != delta {
  70. eprintln!("✅ expect: {}", expected,);
  71. eprintln!("❌ receive: {}", delta.json_str());
  72. }
  73. assert_eq!(expected_delta, delta);
  74. }
  75. }
  76. sleep(Duration::from_millis(TEXT_BLOCK_SYNC_INTERVAL_IN_MILLIS)).await;
  77. }
  78. }