2022-10-22 13:57:44 +00:00
|
|
|
use flowy_document::editor::{AppFlowyDocumentEditor, Document, DocumentTransaction};
|
2022-10-20 03:35:11 +00:00
|
|
|
|
2022-10-22 13:57:44 +00:00
|
|
|
use flowy_document::entities::DocumentVersionPB;
|
2022-10-20 03:35:11 +00:00
|
|
|
use flowy_test::helper::ViewTest;
|
|
|
|
use flowy_test::FlowySDKTest;
|
2022-10-29 12:54:11 +00:00
|
|
|
use lib_ot::core::{Changeset, NodeDataBuilder, NodeOperation, Path, Transaction};
|
2022-10-22 13:57:44 +00:00
|
|
|
use lib_ot::text_delta::DeltaTextOperations;
|
2022-10-20 03:35:11 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
pub enum EditScript {
|
2022-10-22 13:57:44 +00:00
|
|
|
InsertText {
|
|
|
|
path: Path,
|
|
|
|
delta: DeltaTextOperations,
|
|
|
|
},
|
|
|
|
UpdateText {
|
|
|
|
path: Path,
|
|
|
|
delta: DeltaTextOperations,
|
|
|
|
},
|
|
|
|
#[allow(dead_code)]
|
|
|
|
ComposeTransaction {
|
|
|
|
transaction: Transaction,
|
|
|
|
},
|
|
|
|
ComposeTransactionStr {
|
|
|
|
transaction: &'static str,
|
|
|
|
},
|
|
|
|
Delete {
|
|
|
|
path: Path,
|
|
|
|
},
|
|
|
|
AssertContent {
|
|
|
|
expected: &'static str,
|
|
|
|
},
|
|
|
|
AssertPrettyContent {
|
|
|
|
expected: &'static str,
|
|
|
|
},
|
2022-10-20 03:35:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct DocumentEditorTest {
|
|
|
|
pub sdk: FlowySDKTest,
|
|
|
|
pub editor: Arc<AppFlowyDocumentEditor>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DocumentEditorTest {
|
|
|
|
pub async fn new() -> Self {
|
2022-10-22 13:57:44 +00:00
|
|
|
let version = DocumentVersionPB::V1;
|
|
|
|
let sdk = FlowySDKTest::new(version.clone());
|
2022-10-20 03:35:11 +00:00
|
|
|
let _ = sdk.init_user().await;
|
|
|
|
|
|
|
|
let test = ViewTest::new_document_view(&sdk).await;
|
|
|
|
let document_editor = sdk.document_manager.open_document_editor(&test.view.id).await.unwrap();
|
|
|
|
let editor = match document_editor.as_any().downcast_ref::<Arc<AppFlowyDocumentEditor>>() {
|
|
|
|
None => panic!(),
|
|
|
|
Some(editor) => editor.clone(),
|
|
|
|
};
|
|
|
|
|
|
|
|
Self { sdk, editor }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run_scripts(&self, scripts: Vec<EditScript>) {
|
|
|
|
for script in scripts {
|
|
|
|
self.run_script(script).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn run_script(&self, script: EditScript) {
|
|
|
|
match script {
|
|
|
|
EditScript::InsertText { path, delta } => {
|
2022-10-29 12:54:11 +00:00
|
|
|
let node_data = NodeDataBuilder::new("text").insert_delta(delta).build();
|
2022-10-20 03:35:11 +00:00
|
|
|
let operation = NodeOperation::Insert {
|
|
|
|
path,
|
|
|
|
nodes: vec![node_data],
|
|
|
|
};
|
|
|
|
self.editor
|
|
|
|
.apply_transaction(Transaction::from_operations(vec![operation]))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
EditScript::UpdateText { path, delta } => {
|
|
|
|
let inverted = delta.invert_str("");
|
|
|
|
let changeset = Changeset::Delta { delta, inverted };
|
|
|
|
let operation = NodeOperation::Update { path, changeset };
|
|
|
|
self.editor
|
|
|
|
.apply_transaction(Transaction::from_operations(vec![operation]))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
}
|
2022-10-22 13:57:44 +00:00
|
|
|
EditScript::ComposeTransaction { transaction } => {
|
|
|
|
self.editor.apply_transaction(transaction).await.unwrap();
|
|
|
|
}
|
|
|
|
EditScript::ComposeTransactionStr { transaction } => {
|
|
|
|
let document_transaction = serde_json::from_str::<DocumentTransaction>(transaction).unwrap();
|
|
|
|
let transaction: Transaction = document_transaction.into();
|
|
|
|
self.editor.apply_transaction(transaction).await.unwrap();
|
|
|
|
}
|
2022-10-20 03:35:11 +00:00
|
|
|
EditScript::Delete { path } => {
|
|
|
|
let operation = NodeOperation::Delete { path, nodes: vec![] };
|
|
|
|
self.editor
|
|
|
|
.apply_transaction(Transaction::from_operations(vec![operation]))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
EditScript::AssertContent { expected } => {
|
|
|
|
//
|
|
|
|
let content = self.editor.get_content(false).await.unwrap();
|
2022-10-22 13:57:44 +00:00
|
|
|
let expected_document: Document = serde_json::from_str(expected).unwrap();
|
|
|
|
let expected = serde_json::to_string(&expected_document).unwrap();
|
|
|
|
|
2022-10-20 03:35:11 +00:00
|
|
|
assert_eq!(content, expected);
|
|
|
|
}
|
|
|
|
EditScript::AssertPrettyContent { expected } => {
|
|
|
|
//
|
|
|
|
let content = self.editor.get_content(true).await.unwrap();
|
|
|
|
assert_eq!(content, expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|