script.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. use flowy_document::editor::{AppFlowyDocumentEditor, Document, DocumentTransaction};
  2. use flowy_document::entities::DocumentVersionPB;
  3. use flowy_test::helper::ViewTest;
  4. use flowy_test::FlowySDKTest;
  5. use lib_ot::core::{Changeset, NodeDataBuilder, NodeOperation, Path, Transaction};
  6. use lib_ot::text_delta::DeltaTextOperations;
  7. use std::sync::Arc;
  8. pub enum EditScript {
  9. InsertText {
  10. path: Path,
  11. delta: DeltaTextOperations,
  12. },
  13. UpdateText {
  14. path: Path,
  15. delta: DeltaTextOperations,
  16. },
  17. #[allow(dead_code)]
  18. ComposeTransaction {
  19. transaction: Transaction,
  20. },
  21. ComposeTransactionStr {
  22. transaction: &'static str,
  23. },
  24. Delete {
  25. path: Path,
  26. },
  27. AssertContent {
  28. expected: &'static str,
  29. },
  30. AssertPrettyContent {
  31. expected: &'static str,
  32. },
  33. }
  34. pub struct DocumentEditorTest {
  35. pub sdk: FlowySDKTest,
  36. pub editor: Arc<AppFlowyDocumentEditor>,
  37. }
  38. impl DocumentEditorTest {
  39. pub async fn new() -> Self {
  40. let version = DocumentVersionPB::V1;
  41. let sdk = FlowySDKTest::new(version.clone());
  42. let _ = sdk.init_user().await;
  43. let test = ViewTest::new_document_view(&sdk).await;
  44. let document_editor = sdk
  45. .document_manager
  46. .open_document_editor(&test.view.id)
  47. .await
  48. .unwrap();
  49. let editor = match document_editor
  50. .as_any()
  51. .downcast_ref::<Arc<AppFlowyDocumentEditor>>()
  52. {
  53. None => panic!(),
  54. Some(editor) => editor.clone(),
  55. };
  56. Self { sdk, editor }
  57. }
  58. pub async fn run_scripts(&self, scripts: Vec<EditScript>) {
  59. for script in scripts {
  60. self.run_script(script).await;
  61. }
  62. }
  63. async fn run_script(&self, script: EditScript) {
  64. match script {
  65. EditScript::InsertText { path, delta } => {
  66. let node_data = NodeDataBuilder::new("text").insert_delta(delta).build();
  67. let operation = NodeOperation::Insert {
  68. path,
  69. nodes: vec![node_data],
  70. };
  71. self
  72. .editor
  73. .apply_transaction(Transaction::from_operations(vec![operation]))
  74. .await
  75. .unwrap();
  76. },
  77. EditScript::UpdateText { path, delta } => {
  78. let inverted = delta.invert_str("");
  79. let changeset = Changeset::Delta { delta, inverted };
  80. let operation = NodeOperation::Update { path, changeset };
  81. self
  82. .editor
  83. .apply_transaction(Transaction::from_operations(vec![operation]))
  84. .await
  85. .unwrap();
  86. },
  87. EditScript::ComposeTransaction { transaction } => {
  88. self.editor.apply_transaction(transaction).await.unwrap();
  89. },
  90. EditScript::ComposeTransactionStr { transaction } => {
  91. let document_transaction =
  92. serde_json::from_str::<DocumentTransaction>(transaction).unwrap();
  93. let transaction: Transaction = document_transaction.into();
  94. self.editor.apply_transaction(transaction).await.unwrap();
  95. },
  96. EditScript::Delete { path } => {
  97. let operation = NodeOperation::Delete {
  98. path,
  99. nodes: vec![],
  100. };
  101. self
  102. .editor
  103. .apply_transaction(Transaction::from_operations(vec![operation]))
  104. .await
  105. .unwrap();
  106. },
  107. EditScript::AssertContent { expected } => {
  108. //
  109. let content = self.editor.get_content(false).await.unwrap();
  110. let expected_document: Document = serde_json::from_str(expected).unwrap();
  111. let expected = serde_json::to_string(&expected_document).unwrap();
  112. assert_eq!(content, expected);
  113. },
  114. EditScript::AssertPrettyContent { expected } => {
  115. //
  116. let content = self.editor.get_content(true).await.unwrap();
  117. assert_eq!(content, expected);
  118. },
  119. }
  120. }
  121. }