2023-06-20 23:48:34 +08:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2023-06-15 10:37:51 +08:00
|
|
|
use collab_document::blocks::{Block, BlockAction, BlockActionPayload, BlockActionType};
|
2023-08-03 09:14:52 +08:00
|
|
|
use collab_document::document_data::{default_document_data, PARAGRAPH_BLOCK_TYPE};
|
2023-06-20 23:48:34 +08:00
|
|
|
|
2023-07-05 20:57:09 +08:00
|
|
|
use crate::document::util::{gen_document_id, gen_id, DocumentTest};
|
2023-06-15 10:37:51 +08:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn undo_redo_test() {
|
2023-07-05 20:57:09 +08:00
|
|
|
let test = DocumentTest::new();
|
2023-06-15 10:37:51 +08:00
|
|
|
|
|
|
|
let doc_id: String = gen_document_id();
|
|
|
|
let data = default_document_data();
|
|
|
|
|
|
|
|
// create a document
|
2023-08-28 13:28:24 +08:00
|
|
|
_ = test
|
2024-01-07 11:12:05 +08:00
|
|
|
.create_document(
|
|
|
|
test.user_service.user_id().unwrap(),
|
|
|
|
&doc_id,
|
|
|
|
Some(data.clone()),
|
|
|
|
)
|
2023-08-28 13:28:24 +08:00
|
|
|
.await;
|
2023-06-15 10:37:51 +08:00
|
|
|
|
|
|
|
// open a document
|
2023-07-14 13:37:13 +08:00
|
|
|
let document = test.get_document(&doc_id).await.unwrap();
|
2023-06-15 10:37:51 +08:00
|
|
|
let document = document.lock();
|
|
|
|
let page_block = document.get_block(&data.page_id).unwrap();
|
|
|
|
let page_id = page_block.id;
|
|
|
|
let text_block_id = gen_id();
|
|
|
|
|
|
|
|
// insert a text block
|
|
|
|
let text_block = Block {
|
|
|
|
id: text_block_id.clone(),
|
|
|
|
ty: PARAGRAPH_BLOCK_TYPE.to_string(),
|
|
|
|
parent: page_id.clone(),
|
|
|
|
children: gen_id(),
|
|
|
|
external_id: None,
|
|
|
|
external_type: None,
|
|
|
|
data: HashMap::new(),
|
|
|
|
};
|
|
|
|
let insert_text_action = BlockAction {
|
|
|
|
action: BlockActionType::Insert,
|
|
|
|
payload: BlockActionPayload {
|
2023-09-12 20:49:03 +08:00
|
|
|
block: Some(text_block),
|
2023-06-20 23:48:34 +08:00
|
|
|
parent_id: Some(page_id),
|
2023-06-15 10:37:51 +08:00
|
|
|
prev_id: None,
|
2023-09-12 20:49:03 +08:00
|
|
|
delta: None,
|
|
|
|
text_id: None,
|
2023-06-15 10:37:51 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
document.apply_action(vec![insert_text_action]);
|
|
|
|
|
|
|
|
let can_undo = document.can_undo();
|
2023-06-20 23:48:34 +08:00
|
|
|
assert!(can_undo);
|
2023-06-15 10:37:51 +08:00
|
|
|
// undo the insert
|
|
|
|
let undo = document.undo();
|
2023-06-20 23:48:34 +08:00
|
|
|
assert!(undo);
|
2023-06-15 10:37:51 +08:00
|
|
|
assert_eq!(document.get_block(&text_block_id), None);
|
|
|
|
|
|
|
|
let can_redo = document.can_redo();
|
|
|
|
assert!(can_redo);
|
|
|
|
// redo the insert
|
|
|
|
let redo = document.redo();
|
2023-06-20 23:48:34 +08:00
|
|
|
assert!(redo);
|
2023-06-15 10:37:51 +08:00
|
|
|
}
|