| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | use std::{collections::HashMap, vec};
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +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-04-24 14:25:00 +08:00
										 |  |  | use serde_json::{json, to_value, Value};
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | use crate::document::util::{gen_document_id, gen_id, DocumentTest};
 | 
					
						
							| 
									
										
										
										
											2023-06-05 09:42:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | #[tokio::test]
 | 
					
						
							|  |  |  | async fn restore_document() {
 | 
					
						
							|  |  |  |   let test = DocumentTest::new();
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // create a document
 | 
					
						
							| 
									
										
										
										
											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);
 | 
					
						
							| 
									
										
										
										
											2024-01-07 11:12:05 +08:00
										 |  |  |   let uid = test.user_service.user_id().unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-12-27 11:42:39 +08:00
										 |  |  |   test
 | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |     .create_document(uid, &doc_id, Some(data.clone()))
 | 
					
						
							| 
									
										
										
										
											2023-08-28 13:28:24 +08:00
										 |  |  |     .await
 | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |     .unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-12-27 11:42:39 +08:00
										 |  |  |   let data_a = test.get_document_data(&doc_id).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-06-03 13:55:43 +08:00
										 |  |  |   assert_eq!(data_a, data);
 | 
					
						
							| 
									
										
										
										
											2024-06-05 12:40:31 +08:00
										 |  |  |   test.open_document(&doc_id).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |   let data_b = test
 | 
					
						
							|  |  |  |     .get_document(&doc_id)
 | 
					
						
							| 
									
										
										
										
											2023-07-14 13:37:13 +08:00
										 |  |  |     .await
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |     .unwrap()
 | 
					
						
							|  |  |  |     .lock()
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |     .get_document_data()
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |     .unwrap();
 | 
					
						
							|  |  |  |   // close a document
 | 
					
						
							| 
									
										
										
										
											2023-11-20 20:54:47 +08:00
										 |  |  |   _ = test.close_document(&doc_id).await;
 | 
					
						
							| 
									
										
										
										
											2023-06-03 13:55:43 +08:00
										 |  |  |   assert_eq!(data_b, data);
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // restore
 | 
					
						
							| 
									
										
										
										
											2023-08-28 13:28:24 +08:00
										 |  |  |   _ = test.create_document(uid, &doc_id, Some(data.clone())).await;
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |   // open a document
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |   let data_b = test
 | 
					
						
							|  |  |  |     .get_document(&doc_id)
 | 
					
						
							| 
									
										
										
										
											2023-07-14 13:37:13 +08:00
										 |  |  |     .await
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |     .unwrap()
 | 
					
						
							|  |  |  |     .lock()
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |     .get_document_data()
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |     .unwrap();
 | 
					
						
							|  |  |  |   // close a document
 | 
					
						
							| 
									
										
										
										
											2023-11-20 20:54:47 +08:00
										 |  |  |   _ = test.close_document(&doc_id).await;
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-03 13:55:43 +08:00
										 |  |  |   assert_eq!(data_b, data);
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | #[tokio::test]
 | 
					
						
							|  |  |  | async fn document_apply_insert_action() {
 | 
					
						
							|  |  |  |   let test = DocumentTest::new();
 | 
					
						
							| 
									
										
										
										
											2024-01-07 11:12:05 +08:00
										 |  |  |   let uid = test.user_service.user_id().unwrap();
 | 
					
						
							| 
									
										
										
										
											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-04-24 14:25:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // create a document
 | 
					
						
							| 
									
										
										
										
											2023-08-28 13:28:24 +08:00
										 |  |  |   _ = test.create_document(uid, &doc_id, Some(data.clone())).await;
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // open a document
 | 
					
						
							| 
									
										
										
										
											2024-06-05 12:40:31 +08:00
										 |  |  |   test.open_document(&doc_id).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-07-14 13:37:13 +08:00
										 |  |  |   let document = test.get_document(&doc_id).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-06-03 13:55:43 +08:00
										 |  |  |   let page_block = document.lock().get_block(&data.page_id).unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // insert a text block
 | 
					
						
							|  |  |  |   let text_block = Block {
 | 
					
						
							| 
									
										
										
										
											2023-06-15 10:37:51 +08:00
										 |  |  |     id: gen_id(),
 | 
					
						
							| 
									
										
										
										
											2023-06-03 20:43:46 +08:00
										 |  |  |     ty: PARAGRAPH_BLOCK_TYPE.to_string(),
 | 
					
						
							| 
									
										
										
										
											2023-04-28 20:47:40 +08:00
										 |  |  |     parent: page_block.id,
 | 
					
						
							| 
									
										
										
										
											2023-06-15 10:37:51 +08:00
										 |  |  |     children: gen_id(),
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |     external_id: None,
 | 
					
						
							|  |  |  |     external_type: None,
 | 
					
						
							|  |  |  |     data: HashMap::new(),
 | 
					
						
							|  |  |  |   };
 | 
					
						
							|  |  |  |   let insert_text_action = BlockAction {
 | 
					
						
							|  |  |  |     action: BlockActionType::Insert,
 | 
					
						
							|  |  |  |     payload: BlockActionPayload {
 | 
					
						
							|  |  |  |       parent_id: None,
 | 
					
						
							|  |  |  |       prev_id: None,
 | 
					
						
							| 
									
										
										
										
											2023-09-12 20:49:03 +08:00
										 |  |  |       block: Some(text_block),
 | 
					
						
							|  |  |  |       delta: None,
 | 
					
						
							|  |  |  |       text_id: None,
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |     },
 | 
					
						
							|  |  |  |   };
 | 
					
						
							|  |  |  |   document.lock().apply_action(vec![insert_text_action]);
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |   let data_a = document.lock().get_document_data().unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |   // close the original document
 | 
					
						
							| 
									
										
										
										
											2023-11-20 20:54:47 +08:00
										 |  |  |   _ = test.close_document(&doc_id).await;
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // re-open the document
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |   let data_b = test
 | 
					
						
							|  |  |  |     .get_document(&doc_id)
 | 
					
						
							| 
									
										
										
										
											2023-07-14 13:37:13 +08:00
										 |  |  |     .await
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |     .unwrap()
 | 
					
						
							|  |  |  |     .lock()
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |     .get_document_data()
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |     .unwrap();
 | 
					
						
							|  |  |  |   // close a document
 | 
					
						
							| 
									
										
										
										
											2023-11-20 20:54:47 +08:00
										 |  |  |   _ = test.close_document(&doc_id).await;
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   assert_eq!(data_b, data_a);
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | #[tokio::test]
 | 
					
						
							|  |  |  | async fn document_apply_update_page_action() {
 | 
					
						
							|  |  |  |   let test = DocumentTest::new();
 | 
					
						
							| 
									
										
										
										
											2023-06-15 10:37:51 +08:00
										 |  |  |   let doc_id: String = gen_document_id();
 | 
					
						
							| 
									
										
										
										
											2024-01-07 11:12:05 +08:00
										 |  |  |   let uid = test.user_service.user_id().unwrap();
 | 
					
						
							| 
									
										
										
										
											2024-06-09 13:42:09 +08:00
										 |  |  |   let data = default_document_data(&doc_id);
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // create a document
 | 
					
						
							| 
									
										
										
										
											2023-08-28 13:28:24 +08:00
										 |  |  |   _ = test.create_document(uid, &doc_id, Some(data.clone())).await;
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // open a document
 | 
					
						
							| 
									
										
										
										
											2024-06-05 12:40:31 +08:00
										 |  |  |   test.open_document(&doc_id).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-07-14 13:37:13 +08:00
										 |  |  |   let document = test.get_document(&doc_id).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-06-03 13:55:43 +08:00
										 |  |  |   let page_block = document.lock().get_block(&data.page_id).unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-28 20:47:40 +08:00
										 |  |  |   let mut page_block_clone = page_block;
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08: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 {
 | 
					
						
							|  |  |  |       parent_id: None,
 | 
					
						
							|  |  |  |       prev_id: None,
 | 
					
						
							| 
									
										
										
										
											2023-09-12 20:49:03 +08:00
										 |  |  |       block: Some(page_block_clone),
 | 
					
						
							|  |  |  |       delta: None,
 | 
					
						
							|  |  |  |       text_id: None,
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |     },
 | 
					
						
							|  |  |  |   };
 | 
					
						
							|  |  |  |   let actions = vec![action];
 | 
					
						
							|  |  |  |   tracing::trace!("{:?}", &actions);
 | 
					
						
							|  |  |  |   document.lock().apply_action(actions);
 | 
					
						
							| 
									
										
										
										
											2023-06-03 13:55:43 +08:00
										 |  |  |   let page_block_old = document.lock().get_block(&data.page_id).unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-11-20 20:54:47 +08:00
										 |  |  |   _ = test.close_document(&doc_id).await;
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // re-open the document
 | 
					
						
							| 
									
										
										
										
											2023-07-14 13:37:13 +08:00
										 |  |  |   let document = test.get_document(&doc_id).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-06-03 13:55:43 +08:00
										 |  |  |   let page_block_new = document.lock().get_block(&data.page_id).unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |   assert_eq!(page_block_old, page_block_new);
 | 
					
						
							|  |  |  |   assert!(page_block_new.data.contains_key("delta"));
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | #[tokio::test]
 | 
					
						
							|  |  |  | async fn document_apply_update_action() {
 | 
					
						
							|  |  |  |   let test = DocumentTest::new();
 | 
					
						
							| 
									
										
										
										
											2024-01-07 11:12:05 +08:00
										 |  |  |   let uid = test.user_service.user_id().unwrap();
 | 
					
						
							| 
									
										
										
										
											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-04-24 14:25:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // create a document
 | 
					
						
							| 
									
										
										
										
											2023-08-28 13:28:24 +08:00
										 |  |  |   _ = test.create_document(uid, &doc_id, Some(data.clone())).await;
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // open a document
 | 
					
						
							| 
									
										
										
										
											2024-06-05 12:40:31 +08:00
										 |  |  |   test.open_document(&doc_id).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-07-14 13:37:13 +08:00
										 |  |  |   let document = test.get_document(&doc_id).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-06-03 13:55:43 +08:00
										 |  |  |   let page_block = document.lock().get_block(&data.page_id).unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // insert a text block
 | 
					
						
							| 
									
										
										
										
											2023-06-15 10:37:51 +08:00
										 |  |  |   let text_block_id = gen_id();
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |   let text_block = Block {
 | 
					
						
							|  |  |  |     id: text_block_id.clone(),
 | 
					
						
							| 
									
										
										
										
											2023-06-03 20:43:46 +08:00
										 |  |  |     ty: PARAGRAPH_BLOCK_TYPE.to_string(),
 | 
					
						
							| 
									
										
										
										
											2023-04-28 20:47:40 +08:00
										 |  |  |     parent: page_block.id,
 | 
					
						
							| 
									
										
										
										
											2023-06-15 10:37:51 +08:00
										 |  |  |     children: gen_id(),
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |     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-04-24 14:25:00 +08:00
										 |  |  |       parent_id: None,
 | 
					
						
							|  |  |  |       prev_id: None,
 | 
					
						
							| 
									
										
										
										
											2023-09-12 20:49:03 +08:00
										 |  |  |       delta: None,
 | 
					
						
							|  |  |  |       text_id: None,
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |     },
 | 
					
						
							|  |  |  |   };
 | 
					
						
							|  |  |  |   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-09-12 20:49:03 +08:00
										 |  |  |       block: Some(updated_text_block),
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |       parent_id: None,
 | 
					
						
							|  |  |  |       prev_id: None,
 | 
					
						
							| 
									
										
										
										
											2023-09-12 20:49:03 +08:00
										 |  |  |       delta: None,
 | 
					
						
							|  |  |  |       text_id: None,
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |     },
 | 
					
						
							|  |  |  |   };
 | 
					
						
							|  |  |  |   document.lock().apply_action(vec![update_text_action]);
 | 
					
						
							|  |  |  |   // close the original document
 | 
					
						
							| 
									
										
										
										
											2023-11-20 20:54:47 +08:00
										 |  |  |   _ = test.close_document(&doc_id).await;
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // re-open the document
 | 
					
						
							| 
									
										
										
										
											2023-07-14 13:37:13 +08:00
										 |  |  |   let document = test.get_document(&doc_id).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |   let block = document.lock().get_block(&text_block_id).unwrap();
 | 
					
						
							|  |  |  |   assert_eq!(block.data, updated_text_block_data);
 | 
					
						
							|  |  |  |   // close a document
 | 
					
						
							| 
									
										
										
										
											2023-11-20 20:54:47 +08:00
										 |  |  |   _ = test.close_document(&doc_id).await;
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  | }
 |