| 
									
										
										
										
											2023-06-15 10:37:51 +08:00
										 |  |  | use std::{collections::HashMap, vec};
 | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | use collab_document::blocks::{Block, BlockAction, BlockActionPayload, BlockActionType};
 | 
					
						
							| 
									
										
										
										
											2023-08-03 09:14:52 +08:00
										 |  |  | use collab_document::document_data::PARAGRAPH_BLOCK_TYPE;
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-15 10:37:51 +08:00
										 |  |  | use crate::document::util;
 | 
					
						
							|  |  |  | use crate::document::util::gen_id;
 | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | #[tokio::test]
 | 
					
						
							|  |  |  | async fn document_apply_insert_block_with_empty_parent_id() {
 | 
					
						
							| 
									
										
										
										
											2023-07-14 13:37:13 +08:00
										 |  |  |   let (_, document, page_id) = util::create_and_open_empty_document().await;
 | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // create a text block with no parent
 | 
					
						
							| 
									
										
										
										
											2023-06-15 10:37:51 +08:00
										 |  |  |   let text_block_id = gen_id();
 | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +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-05-16 14:58:24 +08:00
										 |  |  |     parent: "".to_string(),
 | 
					
						
							| 
									
										
										
										
											2023-06-15 10:37:51 +08:00
										 |  |  |     children: gen_id(),
 | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +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-05-16 14:58:24 +08:00
										 |  |  |       parent_id: Some(page_id.clone()),
 | 
					
						
							|  |  |  |       prev_id: None,
 | 
					
						
							| 
									
										
										
										
											2023-09-12 20:49:03 +08:00
										 |  |  |       delta: None,
 | 
					
						
							|  |  |  |       text_id: None,
 | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |     },
 | 
					
						
							|  |  |  |   };
 | 
					
						
							| 
									
										
										
										
											2024-08-18 05:16:42 +02:00
										 |  |  |   document
 | 
					
						
							|  |  |  |     .write()
 | 
					
						
							|  |  |  |     .await
 | 
					
						
							|  |  |  |     .apply_action(vec![insert_text_action])
 | 
					
						
							|  |  |  |     .unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // read the text block and it's parent id should be the page id
 | 
					
						
							| 
									
										
										
										
											2024-08-18 05:16:42 +02:00
										 |  |  |   let block = document.read().await.get_block(&text_block_id).unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |   assert_eq!(block.parent, page_id);
 | 
					
						
							|  |  |  | }
 |