2023-09-12 20:49:03 +08:00
|
|
|
use collab_document::blocks::json_str_to_hashmap;
|
2023-10-11 20:02:09 +08:00
|
|
|
use event_integration::document::document_event::DocumentEventTest;
|
|
|
|
use event_integration::document::utils::*;
|
2023-06-15 10:37:51 +08:00
|
|
|
use flowy_document2::entities::*;
|
2023-11-02 22:13:29 +08:00
|
|
|
use flowy_document2::parser::parser_entities::{
|
|
|
|
ConvertDataToJsonPayloadPB, ConvertDocumentPayloadPB, InputType, NestedBlock, ParseTypePB,
|
|
|
|
};
|
2023-09-12 20:49:03 +08:00
|
|
|
use serde_json::{json, Value};
|
|
|
|
use std::collections::HashMap;
|
2023-06-15 10:37:51 +08:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn get_document_event_test() {
|
|
|
|
let test = DocumentEventTest::new().await;
|
|
|
|
let view = test.create_document().await;
|
|
|
|
let document = test.open_document(view.id).await;
|
|
|
|
let document_data = document.data;
|
|
|
|
assert!(!document_data.page_id.is_empty());
|
|
|
|
assert!(document_data.blocks.len() > 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn apply_document_event_test() {
|
|
|
|
let test = DocumentEventTest::new().await;
|
|
|
|
let view = test.create_document().await;
|
|
|
|
let doc_id = view.id.clone();
|
|
|
|
let document = test.open_document(doc_id.clone()).await;
|
|
|
|
let block_count = document.data.blocks.len();
|
|
|
|
let insert_action = gen_insert_block_action(document);
|
|
|
|
let payload = ApplyActionPayloadPB {
|
|
|
|
document_id: doc_id.clone(),
|
|
|
|
actions: vec![insert_action],
|
|
|
|
};
|
|
|
|
test.apply_actions(payload).await;
|
|
|
|
let document = test.open_document(doc_id).await;
|
|
|
|
let document_data = document.data;
|
|
|
|
let block_count_after = document_data.blocks.len();
|
|
|
|
assert_eq!(block_count_after, block_count + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn undo_redo_event_test() {
|
|
|
|
let test = DocumentEventTest::new().await;
|
|
|
|
let view = test.create_document().await;
|
|
|
|
let doc_id = view.id.clone();
|
|
|
|
|
|
|
|
let document = test.open_document(doc_id.clone()).await;
|
|
|
|
let insert_action = gen_insert_block_action(document);
|
|
|
|
let payload = ApplyActionPayloadPB {
|
|
|
|
document_id: doc_id.clone(),
|
|
|
|
actions: vec![insert_action],
|
|
|
|
};
|
|
|
|
test.apply_actions(payload).await;
|
|
|
|
let block_count_after_insert = test.open_document(doc_id.clone()).await.data.blocks.len();
|
|
|
|
|
|
|
|
// undo insert action
|
|
|
|
let can_undo = test.can_undo_redo(doc_id.clone()).await.can_undo;
|
|
|
|
assert!(can_undo);
|
|
|
|
test.undo(doc_id.clone()).await;
|
|
|
|
let block_count_after_undo = test.open_document(doc_id.clone()).await.data.blocks.len();
|
|
|
|
assert_eq!(block_count_after_undo, block_count_after_insert - 1);
|
|
|
|
|
|
|
|
// redo insert action
|
|
|
|
let can_redo = test.can_undo_redo(doc_id.clone()).await.can_redo;
|
|
|
|
assert!(can_redo);
|
|
|
|
test.redo(doc_id.clone()).await;
|
|
|
|
let block_count_after_redo = test.open_document(doc_id.clone()).await.data.blocks.len();
|
|
|
|
assert_eq!(block_count_after_redo, block_count_after_insert);
|
|
|
|
}
|
2023-07-05 20:57:09 +08:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn insert_text_block_test() {
|
|
|
|
let test = DocumentEventTest::new().await;
|
|
|
|
let view = test.create_document().await;
|
|
|
|
let text = "Hello World";
|
|
|
|
let block_id = test.insert_index(&view.id, text, 1, None).await;
|
|
|
|
let block = test.get_block(&view.id, &block_id).await;
|
|
|
|
assert!(block.is_some());
|
|
|
|
let block = block.unwrap();
|
2023-09-12 20:49:03 +08:00
|
|
|
assert!(block.external_id.is_some());
|
|
|
|
let external_id = block.external_id.unwrap();
|
|
|
|
let delta = test.get_block_text_delta(&view.id, &external_id).await;
|
|
|
|
assert_eq!(delta.unwrap(), json!([{ "insert": text }]).to_string());
|
2023-07-05 20:57:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2023-09-12 20:49:03 +08:00
|
|
|
async fn update_block_test() {
|
2023-07-05 20:57:09 +08:00
|
|
|
let test = DocumentEventTest::new().await;
|
|
|
|
let view = test.create_document().await;
|
|
|
|
let block_id = test.insert_index(&view.id, "Hello World", 1, None).await;
|
2023-09-12 20:49:03 +08:00
|
|
|
let data: HashMap<String, Value> = HashMap::from([
|
|
|
|
(
|
|
|
|
"bg_color".to_string(),
|
|
|
|
serde_json::to_value("#000000").unwrap(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"text_color".to_string(),
|
|
|
|
serde_json::to_value("#ffffff").unwrap(),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
test.update_data(&view.id, &block_id, data.clone()).await;
|
2023-07-05 20:57:09 +08:00
|
|
|
let block = test.get_block(&view.id, &block_id).await;
|
|
|
|
assert!(block.is_some());
|
|
|
|
let block = block.unwrap();
|
2023-09-12 20:49:03 +08:00
|
|
|
let block_data = json_str_to_hashmap(&block.data).ok().unwrap();
|
|
|
|
assert_eq!(block_data, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn apply_text_delta_test() {
|
|
|
|
let test = DocumentEventTest::new().await;
|
|
|
|
let view = test.create_document().await;
|
|
|
|
let text = "Hello World";
|
|
|
|
let block_id = test.insert_index(&view.id, text, 1, None).await;
|
|
|
|
let update_delta = json!([{ "retain": 5 }, { "insert": "!" }]).to_string();
|
|
|
|
test
|
|
|
|
.apply_delta_for_block(&view.id, &block_id, update_delta)
|
|
|
|
.await;
|
|
|
|
let block = test.get_block(&view.id, &block_id).await;
|
|
|
|
let text_id = block.unwrap().external_id.unwrap();
|
|
|
|
let block_delta = test.get_block_text_delta(&view.id, &text_id).await;
|
|
|
|
assert_eq!(
|
|
|
|
block_delta.unwrap(),
|
|
|
|
json!([{ "insert": "Hello! World" }]).to_string()
|
|
|
|
);
|
2023-07-05 20:57:09 +08:00
|
|
|
}
|
2023-10-30 12:50:31 +08:00
|
|
|
|
|
|
|
macro_rules! generate_convert_document_test_cases {
|
|
|
|
($($json:ident, $text:ident, $html:ident),*) => {
|
|
|
|
[
|
2023-11-02 22:13:29 +08:00
|
|
|
$((ParseTypePB { json: $json, text: $text, html: $html }, ($json, $text, $html))),*
|
2023-10-30 12:50:31 +08:00
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn convert_document_test() {
|
|
|
|
let test = DocumentEventTest::new().await;
|
|
|
|
let view = test.create_document().await;
|
|
|
|
|
|
|
|
let test_cases = generate_convert_document_test_cases! {
|
|
|
|
true, true, true,
|
|
|
|
false, true, true,
|
|
|
|
false, false, false
|
|
|
|
};
|
|
|
|
|
|
|
|
for (export_types, (json_assert, text_assert, html_assert)) in test_cases.iter() {
|
|
|
|
let copy_payload = ConvertDocumentPayloadPB {
|
|
|
|
document_id: view.id.to_string(),
|
|
|
|
range: None,
|
2023-11-02 22:13:29 +08:00
|
|
|
parse_types: export_types.clone(),
|
2023-10-30 12:50:31 +08:00
|
|
|
};
|
|
|
|
let result = test.convert_document(copy_payload).await;
|
|
|
|
assert_eq!(result.json.is_some(), *json_assert);
|
|
|
|
assert_eq!(result.text.is_some(), *text_assert);
|
|
|
|
assert_eq!(result.html.is_some(), *html_assert);
|
|
|
|
}
|
|
|
|
}
|
2023-11-02 22:13:29 +08:00
|
|
|
|
|
|
|
/// test convert data to json
|
|
|
|
/// - input html: <p>Hello</p><p> World!</p>
|
|
|
|
/// - input plain text: Hello World!
|
|
|
|
/// - output json: { "type": "page", "data": {}, "children": [{ "type": "paragraph", "children": [], "data": { "delta": [{ "insert": "Hello" }] } }, { "type": "paragraph", "children": [], "data": { "delta": [{ "insert": " World!" }] } }] }
|
|
|
|
#[tokio::test]
|
|
|
|
async fn convert_data_to_json_test() {
|
|
|
|
let test = DocumentEventTest::new().await;
|
|
|
|
let _ = test.create_document().await;
|
|
|
|
|
|
|
|
let html = r#"<p>Hello</p><p>World!</p>"#;
|
|
|
|
let payload = ConvertDataToJsonPayloadPB {
|
|
|
|
data: html.to_string(),
|
|
|
|
input_type: InputType::Html,
|
|
|
|
};
|
|
|
|
let result = test.convert_data_to_json(payload).await;
|
|
|
|
let expect_json = json!({
|
|
|
|
"type": "page",
|
|
|
|
"data": {},
|
|
|
|
"children": [{
|
|
|
|
"type": "paragraph",
|
|
|
|
"children": [],
|
|
|
|
"data": {
|
|
|
|
"delta": [{ "insert": "Hello" }]
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
"type": "paragraph",
|
|
|
|
"children": [],
|
|
|
|
"data": {
|
|
|
|
"delta": [{ "insert": "World!" }]
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
|
|
|
|
let expect_json = serde_json::from_value::<NestedBlock>(expect_json).unwrap();
|
|
|
|
assert!(serde_json::from_str::<NestedBlock>(&result.json)
|
|
|
|
.unwrap()
|
|
|
|
.eq(&expect_json));
|
|
|
|
|
|
|
|
let plain_text = "Hello\nWorld!";
|
|
|
|
let payload = ConvertDataToJsonPayloadPB {
|
|
|
|
data: plain_text.to_string(),
|
|
|
|
input_type: InputType::PlainText,
|
|
|
|
};
|
|
|
|
let result = test.convert_data_to_json(payload).await;
|
|
|
|
|
|
|
|
assert!(serde_json::from_str::<NestedBlock>(&result.json)
|
|
|
|
.unwrap()
|
|
|
|
.eq(&expect_json));
|
|
|
|
}
|