script.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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::{Body, 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.document_manager.open_document_editor(&test.view.id).await.unwrap();
  45. let editor = match document_editor.as_any().downcast_ref::<Arc<AppFlowyDocumentEditor>>() {
  46. None => panic!(),
  47. Some(editor) => editor.clone(),
  48. };
  49. Self { sdk, editor }
  50. }
  51. pub async fn run_scripts(&self, scripts: Vec<EditScript>) {
  52. for script in scripts {
  53. self.run_script(script).await;
  54. }
  55. }
  56. async fn run_script(&self, script: EditScript) {
  57. match script {
  58. EditScript::InsertText { path, delta } => {
  59. let node_data = NodeDataBuilder::new("text").insert_body(Body::Delta(delta)).build();
  60. let operation = NodeOperation::Insert {
  61. path,
  62. nodes: vec![node_data],
  63. };
  64. self.editor
  65. .apply_transaction(Transaction::from_operations(vec![operation]))
  66. .await
  67. .unwrap();
  68. }
  69. EditScript::UpdateText { path, delta } => {
  70. let inverted = delta.invert_str("");
  71. let changeset = Changeset::Delta { delta, inverted };
  72. let operation = NodeOperation::Update { path, changeset };
  73. self.editor
  74. .apply_transaction(Transaction::from_operations(vec![operation]))
  75. .await
  76. .unwrap();
  77. }
  78. EditScript::ComposeTransaction { transaction } => {
  79. self.editor.apply_transaction(transaction).await.unwrap();
  80. }
  81. EditScript::ComposeTransactionStr { transaction } => {
  82. let document_transaction = serde_json::from_str::<DocumentTransaction>(transaction).unwrap();
  83. let transaction: Transaction = document_transaction.into();
  84. self.editor.apply_transaction(transaction).await.unwrap();
  85. }
  86. EditScript::Delete { path } => {
  87. let operation = NodeOperation::Delete { path, nodes: vec![] };
  88. self.editor
  89. .apply_transaction(Transaction::from_operations(vec![operation]))
  90. .await
  91. .unwrap();
  92. }
  93. EditScript::AssertContent { expected } => {
  94. //
  95. let content = self.editor.get_content(false).await.unwrap();
  96. let expected_document: Document = serde_json::from_str(expected).unwrap();
  97. let expected = serde_json::to_string(&expected_document).unwrap();
  98. assert_eq!(content, expected);
  99. }
  100. EditScript::AssertPrettyContent { expected } => {
  101. //
  102. let content = self.editor.get_content(true).await.unwrap();
  103. assert_eq!(content, expected);
  104. }
  105. }
  106. }
  107. }