| 
									
										
										
										
											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-10-10 19:05:55 +08:00
										 |  |  | use collab_entity::CollabType;
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-26 18:00:56 +08:00
										 |  |  | use event_integration_test::event_builder::EventBuilder;
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | use flowy_database2::entities::{DatabasePB, DatabaseViewIdPB, RepeatedDatabaseSnapshotPB};
 | 
					
						
							|  |  |  | use flowy_database2::event_map::DatabaseEvent::*;
 | 
					
						
							| 
									
										
										
										
											2023-12-31 07:29:40 +08:00
										 |  |  | use flowy_folder::entities::ViewPB;
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | use crate::util::FlowySupabaseTest;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | pub struct FlowySupabaseDatabaseTest {
 | 
					
						
							|  |  |  |   pub uuid: String,
 | 
					
						
							|  |  |  |   inner: FlowySupabaseTest,
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | impl FlowySupabaseDatabaseTest {
 | 
					
						
							| 
									
										
										
										
											2023-07-14 13:37:13 +08:00
										 |  |  |   #[allow(dead_code)]
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |   pub async fn new_with_user(uuid: String) -> Option<Self> {
 | 
					
						
							| 
									
										
										
										
											2023-10-30 12:35:06 +08:00
										 |  |  |     let inner = FlowySupabaseTest::new().await?;
 | 
					
						
							| 
									
										
										
										
											2023-10-02 17:22:22 +08:00
										 |  |  |     inner.supabase_sign_up_with_uuid(&uuid, None).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |     Some(Self { uuid, inner })
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   pub async fn new_with_new_user() -> 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.unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |     Some(Self { uuid, inner })
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   pub async fn create_database(&self) -> (ViewPB, DatabasePB) {
 | 
					
						
							|  |  |  |     let current_workspace = self.inner.get_current_workspace().await;
 | 
					
						
							|  |  |  |     let view = self
 | 
					
						
							|  |  |  |       .inner
 | 
					
						
							| 
									
										
										
										
											2023-11-01 11:45:35 +08:00
										 |  |  |       .create_grid(¤t_workspace.id, "my database".to_string(), vec![])
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |       .await;
 | 
					
						
							|  |  |  |     let database = self.inner.get_database(&view.id).await;
 | 
					
						
							|  |  |  |     (view, database)
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   pub async fn get_collab_json(&self, database_id: &str) -> JsonValue {
 | 
					
						
							|  |  |  |     let database_editor = self
 | 
					
						
							|  |  |  |       .database_manager
 | 
					
						
							|  |  |  |       .get_database(database_id)
 | 
					
						
							|  |  |  |       .await
 | 
					
						
							|  |  |  |       .unwrap();
 | 
					
						
							|  |  |  |     // let address = Arc::into_raw(database_editor.clone());
 | 
					
						
							|  |  |  |     let database = database_editor.get_mutex_database().lock();
 | 
					
						
							|  |  |  |     database.get_mutex_collab().to_json_value()
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   pub async fn get_database_snapshots(&self, view_id: &str) -> RepeatedDatabaseSnapshotPB {
 | 
					
						
							|  |  |  |     EventBuilder::new(self.inner.deref().clone())
 | 
					
						
							|  |  |  |       .event(GetDatabaseSnapshots)
 | 
					
						
							|  |  |  |       .payload(DatabaseViewIdPB {
 | 
					
						
							|  |  |  |         value: view_id.to_string(),
 | 
					
						
							|  |  |  |       })
 | 
					
						
							|  |  |  |       .async_send()
 | 
					
						
							|  |  |  |       .await
 | 
					
						
							|  |  |  |       .parse::<RepeatedDatabaseSnapshotPB>()
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-29 09:46:24 +08:00
										 |  |  |   pub async fn get_database_collab_update(&self, database_id: &str) -> Vec<u8> {
 | 
					
						
							| 
									
										
										
										
											2023-10-23 11:43:31 +08:00
										 |  |  |     let workspace_id = self.user_manager.workspace_id().unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |     let cloud_service = self.database_manager.get_cloud_service().clone();
 | 
					
						
							| 
									
										
										
										
											2023-12-29 13:02:27 +08:00
										 |  |  |     cloud_service
 | 
					
						
							| 
									
										
										
										
											2024-02-04 05:49:45 +08:00
										 |  |  |       .get_database_object_doc_state(database_id, CollabType::Database, &workspace_id)
 | 
					
						
							| 
									
										
										
										
											2023-07-29 09:46:24 +08:00
										 |  |  |       .await
 | 
					
						
							| 
									
										
										
										
											2023-12-29 13:02:27 +08:00
										 |  |  |       .unwrap()
 | 
					
						
							| 
									
										
										
										
											2024-03-30 16:28:24 +08:00
										 |  |  |       .unwrap()
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |   }
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | pub fn assert_database_collab_content(
 | 
					
						
							|  |  |  |   database_id: &str,
 | 
					
						
							|  |  |  |   collab_update: &[u8],
 | 
					
						
							|  |  |  |   expected: JsonValue,
 | 
					
						
							|  |  |  | ) {
 | 
					
						
							| 
									
										
										
										
											2024-04-15 14:50:28 +08:00
										 |  |  |   let collab = MutexCollab::new(Collab::new_with_origin(
 | 
					
						
							|  |  |  |     CollabOrigin::Server,
 | 
					
						
							|  |  |  |     database_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();
 | 
					
						
							| 
									
										
										
										
											2024-08-27 14:53:00 +02:00
										 |  |  |     txn.apply_update(update).unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |   });
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let json = collab.to_json_value();
 | 
					
						
							|  |  |  |   assert_json_eq!(json, expected);
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | impl Deref for FlowySupabaseDatabaseTest {
 | 
					
						
							|  |  |  |   type Target = FlowySupabaseTest;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   fn deref(&self) -> &Self::Target {
 | 
					
						
							|  |  |  |     &self.inner
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | }
 |