2023-04-24 06:25:00 +00:00
|
|
|
use std::{collections::HashMap, sync::Arc, vec};
|
|
|
|
|
|
|
|
use collab_document::blocks::{Block, BlockAction, BlockActionPayload, BlockActionType};
|
|
|
|
use serde_json::{json, to_value, Value};
|
|
|
|
|
2023-06-05 01:42:11 +00:00
|
|
|
use flowy_document2::document_block_keys::PARAGRAPH_BLOCK_TYPE;
|
2023-06-03 05:55:43 +00:00
|
|
|
use flowy_document2::document_data::default_document_data;
|
|
|
|
use flowy_document2::manager::DocumentManager;
|
2023-05-15 14:16:05 +00:00
|
|
|
|
2023-06-15 02:37:51 +00:00
|
|
|
use crate::document::util::{default_collab_builder, gen_document_id, gen_id};
|
2023-06-05 01:42:11 +00:00
|
|
|
|
2023-04-24 06:25:00 +00:00
|
|
|
use super::util::FakeUser;
|
|
|
|
|
2023-05-16 06:58:24 +00:00
|
|
|
#[test]
|
|
|
|
fn restore_document() {
|
2023-04-24 06:25:00 +00:00
|
|
|
let user = FakeUser::new();
|
2023-05-15 14:16:05 +00:00
|
|
|
let manager = DocumentManager::new(Arc::new(user), default_collab_builder());
|
2023-04-24 06:25:00 +00:00
|
|
|
|
|
|
|
// create a document
|
2023-06-15 02:37:51 +00:00
|
|
|
let doc_id: String = gen_document_id();
|
2023-06-03 05:55:43 +00:00
|
|
|
let data = default_document_data();
|
2023-04-24 06:25:00 +00:00
|
|
|
let document_a = manager
|
2023-06-17 06:25:30 +00:00
|
|
|
.create_document(&doc_id, Some(data.clone()))
|
2023-04-24 06:25:00 +00:00
|
|
|
.unwrap();
|
|
|
|
let data_a = document_a.lock().get_document().unwrap();
|
2023-06-03 05:55:43 +00:00
|
|
|
assert_eq!(data_a, data);
|
2023-04-24 06:25:00 +00:00
|
|
|
|
|
|
|
// open a document
|
|
|
|
let data_b = manager
|
2023-06-17 06:25:30 +00:00
|
|
|
.get_or_open_document(&doc_id)
|
2023-04-24 06:25:00 +00:00
|
|
|
.unwrap()
|
|
|
|
.lock()
|
|
|
|
.get_document()
|
|
|
|
.unwrap();
|
|
|
|
// close a document
|
2023-06-05 01:42:11 +00:00
|
|
|
_ = manager.close_document(&doc_id);
|
2023-06-03 05:55:43 +00:00
|
|
|
assert_eq!(data_b, data);
|
2023-04-24 06:25:00 +00:00
|
|
|
|
|
|
|
// restore
|
2023-06-17 06:25:30 +00:00
|
|
|
_ = manager.create_document(&doc_id, Some(data.clone()));
|
2023-04-24 06:25:00 +00:00
|
|
|
// open a document
|
|
|
|
let data_b = manager
|
2023-06-17 06:25:30 +00:00
|
|
|
.get_or_open_document(&doc_id)
|
2023-04-24 06:25:00 +00:00
|
|
|
.unwrap()
|
|
|
|
.lock()
|
|
|
|
.get_document()
|
|
|
|
.unwrap();
|
|
|
|
// close a document
|
2023-06-05 01:42:11 +00:00
|
|
|
_ = manager.close_document(&doc_id);
|
2023-04-24 06:25:00 +00:00
|
|
|
|
2023-06-03 05:55:43 +00:00
|
|
|
assert_eq!(data_b, data);
|
2023-04-24 06:25:00 +00:00
|
|
|
}
|
|
|
|
|
2023-05-16 06:58:24 +00:00
|
|
|
#[test]
|
|
|
|
fn document_apply_insert_action() {
|
2023-04-24 06:25:00 +00:00
|
|
|
let user = FakeUser::new();
|
2023-05-15 14:16:05 +00:00
|
|
|
let manager = DocumentManager::new(Arc::new(user), default_collab_builder());
|
2023-04-24 06:25:00 +00:00
|
|
|
|
2023-06-15 02:37:51 +00:00
|
|
|
let doc_id: String = gen_document_id();
|
2023-06-03 05:55:43 +00:00
|
|
|
let data = default_document_data();
|
2023-04-24 06:25:00 +00:00
|
|
|
|
|
|
|
// create a document
|
2023-06-17 06:25:30 +00:00
|
|
|
_ = manager.create_document(&doc_id, Some(data.clone()));
|
2023-04-24 06:25:00 +00:00
|
|
|
|
|
|
|
// open a document
|
2023-06-17 06:25:30 +00:00
|
|
|
let document = manager.get_or_open_document(&doc_id).unwrap();
|
2023-06-03 05:55:43 +00:00
|
|
|
let page_block = document.lock().get_block(&data.page_id).unwrap();
|
2023-04-24 06:25:00 +00:00
|
|
|
|
|
|
|
// insert a text block
|
|
|
|
let text_block = Block {
|
2023-06-15 02:37:51 +00:00
|
|
|
id: gen_id(),
|
2023-06-03 12:43:46 +00:00
|
|
|
ty: PARAGRAPH_BLOCK_TYPE.to_string(),
|
2023-04-28 12:47:40 +00:00
|
|
|
parent: page_block.id,
|
2023-06-15 02:37:51 +00:00
|
|
|
children: gen_id(),
|
2023-04-24 06:25:00 +00:00
|
|
|
external_id: None,
|
|
|
|
external_type: None,
|
|
|
|
data: HashMap::new(),
|
|
|
|
};
|
|
|
|
let insert_text_action = BlockAction {
|
|
|
|
action: BlockActionType::Insert,
|
|
|
|
payload: BlockActionPayload {
|
2023-04-28 12:47:40 +00:00
|
|
|
block: text_block,
|
2023-04-24 06:25:00 +00:00
|
|
|
parent_id: None,
|
|
|
|
prev_id: None,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
document.lock().apply_action(vec![insert_text_action]);
|
|
|
|
let data_a = document.lock().get_document().unwrap();
|
|
|
|
// close the original document
|
2023-06-05 01:42:11 +00:00
|
|
|
_ = manager.close_document(&doc_id);
|
2023-04-24 06:25:00 +00:00
|
|
|
|
|
|
|
// re-open the document
|
|
|
|
let data_b = manager
|
2023-06-17 06:25:30 +00:00
|
|
|
.get_or_open_document(&doc_id)
|
2023-04-24 06:25:00 +00:00
|
|
|
.unwrap()
|
|
|
|
.lock()
|
|
|
|
.get_document()
|
|
|
|
.unwrap();
|
|
|
|
// close a document
|
2023-06-05 01:42:11 +00:00
|
|
|
_ = manager.close_document(&doc_id);
|
2023-04-24 06:25:00 +00:00
|
|
|
|
|
|
|
assert_eq!(data_b, data_a);
|
|
|
|
}
|
|
|
|
|
2023-05-16 06:58:24 +00:00
|
|
|
#[test]
|
|
|
|
fn document_apply_update_page_action() {
|
2023-04-24 06:25:00 +00:00
|
|
|
let user = FakeUser::new();
|
2023-05-15 14:16:05 +00:00
|
|
|
let manager = DocumentManager::new(Arc::new(user), default_collab_builder());
|
2023-04-24 06:25:00 +00:00
|
|
|
|
2023-06-15 02:37:51 +00:00
|
|
|
let doc_id: String = gen_document_id();
|
2023-06-03 05:55:43 +00:00
|
|
|
let data = default_document_data();
|
2023-04-24 06:25:00 +00:00
|
|
|
|
|
|
|
// create a document
|
2023-06-17 06:25:30 +00:00
|
|
|
_ = manager.create_document(&doc_id, Some(data.clone()));
|
2023-04-24 06:25:00 +00:00
|
|
|
|
|
|
|
// open a document
|
2023-06-17 06:25:30 +00:00
|
|
|
let document = manager.get_or_open_document(&doc_id).unwrap();
|
2023-06-03 05:55:43 +00:00
|
|
|
let page_block = document.lock().get_block(&data.page_id).unwrap();
|
2023-04-24 06:25:00 +00:00
|
|
|
|
2023-04-28 12:47:40 +00:00
|
|
|
let mut page_block_clone = page_block;
|
2023-04-24 06:25:00 +00:00
|
|
|
page_block_clone.data = HashMap::new();
|
|
|
|
page_block_clone.data.insert(
|
|
|
|
"delta".to_string(),
|
|
|
|
to_value(json!([{"insert": "Hello World!"}])).unwrap(),
|
|
|
|
);
|
|
|
|
let action = BlockAction {
|
|
|
|
action: BlockActionType::Update,
|
|
|
|
payload: BlockActionPayload {
|
|
|
|
block: page_block_clone,
|
|
|
|
parent_id: None,
|
|
|
|
prev_id: None,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let actions = vec![action];
|
|
|
|
tracing::trace!("{:?}", &actions);
|
|
|
|
document.lock().apply_action(actions);
|
2023-06-03 05:55:43 +00:00
|
|
|
let page_block_old = document.lock().get_block(&data.page_id).unwrap();
|
2023-06-05 01:42:11 +00:00
|
|
|
_ = manager.close_document(&doc_id);
|
2023-04-24 06:25:00 +00:00
|
|
|
|
|
|
|
// re-open the document
|
2023-06-17 06:25:30 +00:00
|
|
|
let document = manager.get_or_open_document(&doc_id).unwrap();
|
2023-06-03 05:55:43 +00:00
|
|
|
let page_block_new = document.lock().get_block(&data.page_id).unwrap();
|
2023-04-24 06:25:00 +00:00
|
|
|
assert_eq!(page_block_old, page_block_new);
|
|
|
|
assert!(page_block_new.data.contains_key("delta"));
|
|
|
|
}
|
|
|
|
|
2023-05-16 06:58:24 +00:00
|
|
|
#[test]
|
|
|
|
fn document_apply_update_action() {
|
2023-04-24 06:25:00 +00:00
|
|
|
let user = FakeUser::new();
|
2023-05-15 14:16:05 +00:00
|
|
|
let manager = DocumentManager::new(Arc::new(user), default_collab_builder());
|
2023-04-24 06:25:00 +00:00
|
|
|
|
2023-06-15 02:37:51 +00:00
|
|
|
let doc_id: String = gen_document_id();
|
2023-06-03 05:55:43 +00:00
|
|
|
let data = default_document_data();
|
2023-04-24 06:25:00 +00:00
|
|
|
|
|
|
|
// create a document
|
2023-06-17 06:25:30 +00:00
|
|
|
_ = manager.create_document(&doc_id, Some(data.clone()));
|
2023-04-24 06:25:00 +00:00
|
|
|
|
|
|
|
// open a document
|
2023-06-17 06:25:30 +00:00
|
|
|
let document = manager.get_or_open_document(&doc_id).unwrap();
|
2023-06-03 05:55:43 +00:00
|
|
|
let page_block = document.lock().get_block(&data.page_id).unwrap();
|
2023-04-24 06:25:00 +00:00
|
|
|
|
|
|
|
// insert a text block
|
2023-06-15 02:37:51 +00:00
|
|
|
let text_block_id = gen_id();
|
2023-04-24 06:25:00 +00:00
|
|
|
let text_block = Block {
|
|
|
|
id: text_block_id.clone(),
|
2023-06-03 12:43:46 +00:00
|
|
|
ty: PARAGRAPH_BLOCK_TYPE.to_string(),
|
2023-04-28 12:47:40 +00:00
|
|
|
parent: page_block.id,
|
2023-06-15 02:37:51 +00:00
|
|
|
children: gen_id(),
|
2023-04-24 06:25:00 +00:00
|
|
|
external_id: None,
|
|
|
|
external_type: None,
|
|
|
|
data: HashMap::new(),
|
|
|
|
};
|
|
|
|
let insert_text_action = BlockAction {
|
|
|
|
action: BlockActionType::Insert,
|
|
|
|
payload: BlockActionPayload {
|
2023-04-28 12:47:40 +00:00
|
|
|
block: text_block,
|
2023-04-24 06:25:00 +00:00
|
|
|
parent_id: None,
|
|
|
|
prev_id: None,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
document.lock().apply_action(vec![insert_text_action]);
|
|
|
|
|
|
|
|
// update the text block
|
|
|
|
let existing_text_block = document.lock().get_block(&text_block_id).unwrap();
|
|
|
|
let mut updated_text_block_data = HashMap::new();
|
|
|
|
updated_text_block_data.insert("delta".to_string(), Value::String("delta".to_string()));
|
|
|
|
let updated_text_block = Block {
|
|
|
|
id: existing_text_block.id,
|
|
|
|
ty: existing_text_block.ty,
|
|
|
|
parent: existing_text_block.parent,
|
|
|
|
children: existing_text_block.children,
|
|
|
|
external_id: None,
|
|
|
|
external_type: None,
|
|
|
|
data: updated_text_block_data.clone(),
|
|
|
|
};
|
|
|
|
let update_text_action = BlockAction {
|
|
|
|
action: BlockActionType::Update,
|
|
|
|
payload: BlockActionPayload {
|
2023-04-28 12:47:40 +00:00
|
|
|
block: updated_text_block,
|
2023-04-24 06:25:00 +00:00
|
|
|
parent_id: None,
|
|
|
|
prev_id: None,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
document.lock().apply_action(vec![update_text_action]);
|
|
|
|
// close the original document
|
2023-06-05 01:42:11 +00:00
|
|
|
_ = manager.close_document(&doc_id);
|
2023-04-24 06:25:00 +00:00
|
|
|
|
|
|
|
// re-open the document
|
2023-06-17 06:25:30 +00:00
|
|
|
let document = manager.get_or_open_document(&doc_id).unwrap();
|
2023-04-24 06:25:00 +00:00
|
|
|
let block = document.lock().get_block(&text_block_id).unwrap();
|
|
|
|
assert_eq!(block.data, updated_text_block_data);
|
|
|
|
// close a document
|
2023-06-05 01:42:11 +00:00
|
|
|
_ = manager.close_document(&doc_id);
|
2023-04-24 06:25:00 +00:00
|
|
|
}
|