| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | use std::ops::Deref;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use assert_json_diff::assert_json_eq;
 | 
					
						
							|  |  |  | use collab::core::collab::MutexCollab;
 | 
					
						
							|  |  |  | use collab::core::origin::CollabOrigin;
 | 
					
						
							|  |  |  | use collab::preclude::updates::decoder::Decode;
 | 
					
						
							| 
									
										
										
										
											2024-04-15 14:50:28 +08:00
										 |  |  | use collab::preclude::{Collab, JsonValue, Update};
 | 
					
						
							| 
									
										
										
										
											2023-12-29 13:02:27 +08:00
										 |  |  | use collab_entity::CollabType;
 | 
					
						
							| 
									
										
										
										
											2023-11-01 11:45:35 +08:00
										 |  |  | use collab_folder::FolderData;
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-26 18:00:56 +08:00
										 |  |  | use event_integration_test::event_builder::EventBuilder;
 | 
					
						
							| 
									
										
										
										
											2023-12-31 07:29:40 +08:00
										 |  |  | use flowy_folder::entities::{FolderSnapshotPB, RepeatedFolderSnapshotPB, WorkspaceIdPB};
 | 
					
						
							|  |  |  | use flowy_folder::event_map::FolderEvent::GetFolderSnapshots;
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | use crate::util::FlowySupabaseTest;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | pub struct FlowySupabaseFolderTest {
 | 
					
						
							|  |  |  |   inner: FlowySupabaseTest,
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | impl FlowySupabaseFolderTest {
 | 
					
						
							|  |  |  |   pub async fn new() -> Option<Self> {
 | 
					
						
							| 
									
										
										
										
											2023-10-30 12:35:06 +08:00
										 |  |  |     let inner = FlowySupabaseTest::new().await?;
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |     let uuid = uuid::Uuid::new_v4().to_string();
 | 
					
						
							| 
									
										
										
										
											2023-10-02 17:22:22 +08:00
										 |  |  |     let _ = inner.supabase_sign_up_with_uuid(&uuid, None).await;
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |     Some(Self { inner })
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   pub async fn get_collab_json(&self) -> JsonValue {
 | 
					
						
							|  |  |  |     let folder = self.folder_manager.get_mutex_folder().lock();
 | 
					
						
							|  |  |  |     folder.as_ref().unwrap().to_json_value()
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |   pub async fn get_local_folder_data(&self) -> FolderData {
 | 
					
						
							|  |  |  |     let folder = self.folder_manager.get_mutex_folder().lock();
 | 
					
						
							|  |  |  |     folder.as_ref().unwrap().get_folder_data().unwrap()
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |   pub async fn get_folder_snapshots(&self, workspace_id: &str) -> Vec<FolderSnapshotPB> {
 | 
					
						
							|  |  |  |     EventBuilder::new(self.inner.deref().clone())
 | 
					
						
							|  |  |  |       .event(GetFolderSnapshots)
 | 
					
						
							|  |  |  |       .payload(WorkspaceIdPB {
 | 
					
						
							| 
									
										
										
										
											2023-11-01 11:45:35 +08:00
										 |  |  |         value: workspace_id.to_string(),
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |       })
 | 
					
						
							|  |  |  |       .async_send()
 | 
					
						
							|  |  |  |       .await
 | 
					
						
							|  |  |  |       .parse::<RepeatedFolderSnapshotPB>()
 | 
					
						
							|  |  |  |       .items
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   pub async fn get_collab_update(&self, workspace_id: &str) -> Vec<u8> {
 | 
					
						
							|  |  |  |     let cloud_service = self.folder_manager.get_cloud_service().clone();
 | 
					
						
							| 
									
										
										
										
											2023-12-29 13:02:27 +08:00
										 |  |  |     cloud_service
 | 
					
						
							| 
									
										
										
										
											2024-02-04 05:49:45 +08:00
										 |  |  |       .get_folder_doc_state(
 | 
					
						
							| 
									
										
										
										
											2023-12-29 13:02:27 +08:00
										 |  |  |         workspace_id,
 | 
					
						
							|  |  |  |         self.user_manager.user_id().unwrap(),
 | 
					
						
							|  |  |  |         CollabType::Folder,
 | 
					
						
							|  |  |  |         workspace_id,
 | 
					
						
							|  |  |  |       )
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |       .await
 | 
					
						
							| 
									
										
										
										
											2023-12-29 13:02:27 +08:00
										 |  |  |       .unwrap()
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |   }
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | pub fn assert_folder_collab_content(workspace_id: &str, collab_update: &[u8], expected: JsonValue) {
 | 
					
						
							|  |  |  |   if collab_update.is_empty() {
 | 
					
						
							|  |  |  |     panic!("collab update is empty");
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-15 14:50:28 +08:00
										 |  |  |   let collab = MutexCollab::new(Collab::new_with_origin(
 | 
					
						
							|  |  |  |     CollabOrigin::Server,
 | 
					
						
							|  |  |  |     workspace_id,
 | 
					
						
							|  |  |  |     vec![],
 | 
					
						
							|  |  |  |     false,
 | 
					
						
							|  |  |  |   ));
 | 
					
						
							| 
									
										
										
										
											2023-08-12 17:36:31 +08:00
										 |  |  |   collab.lock().with_origin_transact_mut(|txn| {
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |     let update = Update::decode_v1(collab_update).unwrap();
 | 
					
						
							|  |  |  |     txn.apply_update(update);
 | 
					
						
							|  |  |  |   });
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let json = collab.to_json_value();
 | 
					
						
							|  |  |  |   assert_json_eq!(json["folder"], expected);
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | impl Deref for FlowySupabaseFolderTest {
 | 
					
						
							|  |  |  |   type Target = FlowySupabaseTest;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   fn deref(&self) -> &Self::Target {
 | 
					
						
							|  |  |  |     &self.inner
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | }
 |