| 
									
										
										
										
											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();
 | 
					
						
							| 
									
										
										
										
											2024-06-09 13:42:09 +08:00
										 |  |  |   let data = default_document_data(&doc_id);
 | 
					
						
							| 
									
										
										
										
											2023-06-15 10:37:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // 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
 | 
					
						
							| 
									
										
										
										
											2024-06-05 12:40:31 +08:00
										 |  |  |   test.open_document(&doc_id).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2024-08-18 05:16:42 +02:00
										 |  |  |   let document = test.editable_document(&doc_id).await.unwrap();
 | 
					
						
							|  |  |  |   let mut document = document.write().await;
 | 
					
						
							| 
									
										
										
										
											2023-06-15 10:37:51 +08:00
										 |  |  |   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
										 |  |  |     },
 | 
					
						
							|  |  |  |   };
 | 
					
						
							| 
									
										
										
										
											2024-08-18 05:16:42 +02:00
										 |  |  |   document.apply_action(vec![insert_text_action]).unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-06-15 10:37:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   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
										 |  |  | }
 |