| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | use std::ops::Deref;
 | 
					
						
							| 
									
										
										
										
											2023-04-28 20:47:40 +08:00
										 |  |  | use std::sync::Arc;
 | 
					
						
							| 
									
										
										
										
											2023-05-10 13:27:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-12 17:36:31 +08:00
										 |  |  | use anyhow::Error;
 | 
					
						
							| 
									
										
										
										
											2023-09-01 22:27:29 +08:00
										 |  |  | use bytes::Bytes;
 | 
					
						
							| 
									
										
										
										
											2023-11-28 15:49:47 -08:00
										 |  |  | use collab::preclude::CollabPlugin;
 | 
					
						
							| 
									
										
										
										
											2023-07-14 13:37:13 +08:00
										 |  |  | use collab_document::blocks::DocumentData;
 | 
					
						
							| 
									
										
										
										
											2023-08-03 09:14:52 +08:00
										 |  |  | use collab_document::document_data::default_document_data;
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | use nanoid::nanoid;
 | 
					
						
							| 
									
										
										
										
											2023-05-10 13:27:50 +08:00
										 |  |  | use parking_lot::Once;
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  | use tempfile::TempDir;
 | 
					
						
							|  |  |  | use tracing_subscriber::{fmt::Subscriber, util::SubscriberInitExt, EnvFilter};
 | 
					
						
							| 
									
										
										
										
											2023-10-23 11:43:31 +08:00
										 |  |  | use uuid::Uuid;
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-28 15:49:47 -08:00
										 |  |  | use collab_integrate::collab_builder::{
 | 
					
						
							|  |  |  |   AppFlowyCollabBuilder, CollabDataSource, CollabStorageProvider, CollabStorageProviderContext,
 | 
					
						
							|  |  |  | };
 | 
					
						
							| 
									
										
										
										
											2023-09-17 17:14:34 +08:00
										 |  |  | use collab_integrate::RocksCollabDB;
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | use flowy_document2::document::MutexDocument;
 | 
					
						
							| 
									
										
										
										
											2023-07-29 09:46:24 +08:00
										 |  |  | use flowy_document2::manager::{DocumentManager, DocumentUser};
 | 
					
						
							|  |  |  | use flowy_document_deps::cloud::*;
 | 
					
						
							| 
									
										
										
										
											2023-09-01 22:27:29 +08:00
										 |  |  | use flowy_error::FlowyError;
 | 
					
						
							|  |  |  | use flowy_storage::{FileStorageService, StorageObject};
 | 
					
						
							| 
									
										
										
										
											2023-11-28 15:49:47 -08:00
										 |  |  | use lib_infra::async_trait::async_trait;
 | 
					
						
							|  |  |  | use lib_infra::future::{to_fut, Fut, FutureResult};
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | pub struct DocumentTest {
 | 
					
						
							|  |  |  |   inner: DocumentManager,
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | impl DocumentTest {
 | 
					
						
							|  |  |  |   pub fn new() -> Self {
 | 
					
						
							|  |  |  |     let user = FakeUser::new();
 | 
					
						
							|  |  |  |     let cloud_service = Arc::new(LocalTestDocumentCloudServiceImpl());
 | 
					
						
							| 
									
										
										
										
											2023-09-01 22:27:29 +08:00
										 |  |  |     let file_storage = Arc::new(DocumentTestFileStorageService) as Arc<dyn FileStorageService>;
 | 
					
						
							|  |  |  |     let manager = DocumentManager::new(
 | 
					
						
							|  |  |  |       Arc::new(user),
 | 
					
						
							|  |  |  |       default_collab_builder(),
 | 
					
						
							|  |  |  |       cloud_service,
 | 
					
						
							|  |  |  |       Arc::downgrade(&file_storage),
 | 
					
						
							|  |  |  |     );
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |     Self { inner: manager }
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | impl Deref for DocumentTest {
 | 
					
						
							|  |  |  |   type Target = DocumentManager;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   fn deref(&self) -> &Self::Target {
 | 
					
						
							|  |  |  |     &self.inner
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2023-05-10 13:27:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  | pub struct FakeUser {
 | 
					
						
							| 
									
										
										
										
											2023-07-29 09:46:24 +08:00
										 |  |  |   collab_db: Arc<RocksCollabDB>,
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | impl FakeUser {
 | 
					
						
							|  |  |  |   pub fn new() -> Self {
 | 
					
						
							| 
									
										
										
										
											2023-07-29 09:46:24 +08:00
										 |  |  |     Self { collab_db: db() }
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |   }
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | impl DocumentUser for FakeUser {
 | 
					
						
							| 
									
										
										
										
											2023-09-17 17:14:34 +08:00
										 |  |  |   fn user_id(&self) -> Result<i64, FlowyError> {
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |     Ok(1)
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-23 11:43:31 +08:00
										 |  |  |   fn workspace_id(&self) -> Result<String, FlowyError> {
 | 
					
						
							|  |  |  |     Ok(Uuid::new_v4().to_string())
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-17 17:14:34 +08:00
										 |  |  |   fn token(&self) -> Result<Option<String>, FlowyError> {
 | 
					
						
							| 
									
										
										
										
											2023-05-21 18:53:59 +08:00
										 |  |  |     Ok(None)
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-17 17:14:34 +08:00
										 |  |  |   fn collab_db(&self, _uid: i64) -> Result<std::sync::Weak<RocksCollabDB>, FlowyError> {
 | 
					
						
							| 
									
										
										
										
											2023-07-29 09:46:24 +08:00
										 |  |  |     Ok(Arc::downgrade(&self.collab_db))
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |   }
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-28 20:47:40 +08:00
										 |  |  | pub fn db() -> Arc<RocksCollabDB> {
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  |   static START: Once = Once::new();
 | 
					
						
							|  |  |  |   START.call_once(|| {
 | 
					
						
							|  |  |  |     std::env::set_var("RUST_LOG", "collab_persistence=trace");
 | 
					
						
							|  |  |  |     let subscriber = Subscriber::builder()
 | 
					
						
							|  |  |  |       .with_env_filter(EnvFilter::from_default_env())
 | 
					
						
							|  |  |  |       .with_ansi(true)
 | 
					
						
							|  |  |  |       .finish();
 | 
					
						
							|  |  |  |     subscriber.try_init().unwrap();
 | 
					
						
							|  |  |  |   });
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let tempdir = TempDir::new().unwrap();
 | 
					
						
							|  |  |  |   let path = tempdir.into_path();
 | 
					
						
							| 
									
										
										
										
											2023-04-28 20:47:40 +08:00
										 |  |  |   Arc::new(RocksCollabDB::open(path).unwrap())
 | 
					
						
							| 
									
										
										
										
											2023-04-24 14:25:00 +08:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | pub fn default_collab_builder() -> Arc<AppFlowyCollabBuilder> {
 | 
					
						
							| 
									
										
										
										
											2023-11-20 20:54:47 +08:00
										 |  |  |   let builder =
 | 
					
						
							|  |  |  |     AppFlowyCollabBuilder::new(DefaultCollabStorageProvider(), "fake_device_id".to_string());
 | 
					
						
							| 
									
										
										
										
											2023-09-17 17:14:34 +08:00
										 |  |  |   builder.initialize(uuid::Uuid::new_v4().to_string());
 | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |   Arc::new(builder)
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2023-06-15 10:37:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-14 13:37:13 +08:00
										 |  |  | pub async fn create_and_open_empty_document() -> (DocumentTest, Arc<MutexDocument>, String) {
 | 
					
						
							| 
									
										
										
										
											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();
 | 
					
						
							|  |  |  |   let data = default_document_data();
 | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |   let uid = test.user.user_id().unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-06-15 10:37:51 +08:00
										 |  |  |   // create a document
 | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |   _ = test
 | 
					
						
							|  |  |  |     .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-06-15 10:37:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-14 13:37:13 +08:00
										 |  |  |   let document = test.get_document(&doc_id).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-06-15 10:37:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |   (test, document, data.page_id)
 | 
					
						
							| 
									
										
										
										
											2023-06-15 10:37:51 +08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | pub fn gen_document_id() -> String {
 | 
					
						
							|  |  |  |   let uuid = uuid::Uuid::new_v4();
 | 
					
						
							|  |  |  |   uuid.to_string()
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | pub fn gen_id() -> String {
 | 
					
						
							|  |  |  |   nanoid!(10)
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | pub struct LocalTestDocumentCloudServiceImpl();
 | 
					
						
							|  |  |  | impl DocumentCloudService for LocalTestDocumentCloudServiceImpl {
 | 
					
						
							| 
									
										
										
										
											2023-10-23 11:43:31 +08:00
										 |  |  |   fn get_document_updates(
 | 
					
						
							|  |  |  |     &self,
 | 
					
						
							|  |  |  |     _document_id: &str,
 | 
					
						
							|  |  |  |     _workspace_id: &str,
 | 
					
						
							| 
									
										
										
										
											2023-11-05 14:00:24 +08:00
										 |  |  |   ) -> FutureResult<Vec<Vec<u8>>, FlowyError> {
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |     FutureResult::new(async move { Ok(vec![]) })
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |   fn get_document_snapshots(
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |     &self,
 | 
					
						
							|  |  |  |     _document_id: &str,
 | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |     _limit: usize,
 | 
					
						
							| 
									
										
										
										
											2023-10-23 11:43:31 +08:00
										 |  |  |     _workspace_id: &str,
 | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |   ) -> FutureResult<Vec<DocumentSnapshot>, Error> {
 | 
					
						
							|  |  |  |     FutureResult::new(async move { Ok(vec![]) })
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2023-07-14 13:37:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-23 11:43:31 +08:00
										 |  |  |   fn get_document_data(
 | 
					
						
							|  |  |  |     &self,
 | 
					
						
							|  |  |  |     _document_id: &str,
 | 
					
						
							|  |  |  |     _workspace_id: &str,
 | 
					
						
							|  |  |  |   ) -> FutureResult<Option<DocumentData>, Error> {
 | 
					
						
							| 
									
										
										
										
											2023-07-14 13:37:13 +08:00
										 |  |  |     FutureResult::new(async move { Ok(None) })
 | 
					
						
							|  |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2023-09-01 22:27:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | pub struct DocumentTestFileStorageService;
 | 
					
						
							|  |  |  | impl FileStorageService for DocumentTestFileStorageService {
 | 
					
						
							|  |  |  |   fn create_object(&self, _object: StorageObject) -> FutureResult<String, FlowyError> {
 | 
					
						
							|  |  |  |     todo!()
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   fn delete_object_by_url(&self, _object_url: String) -> FutureResult<(), FlowyError> {
 | 
					
						
							|  |  |  |     todo!()
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   fn get_object_by_url(&self, _object_url: String) -> FutureResult<Bytes, FlowyError> {
 | 
					
						
							|  |  |  |     todo!()
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2023-11-28 15:49:47 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | struct DefaultCollabStorageProvider();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #[async_trait]
 | 
					
						
							|  |  |  | impl CollabStorageProvider for DefaultCollabStorageProvider {
 | 
					
						
							|  |  |  |   fn storage_source(&self) -> CollabDataSource {
 | 
					
						
							|  |  |  |     CollabDataSource::Local
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   fn get_plugins(&self, _context: CollabStorageProviderContext) -> Fut<Vec<Arc<dyn CollabPlugin>>> {
 | 
					
						
							|  |  |  |     to_fut(async move { vec![] })
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   fn is_sync_enabled(&self) -> bool {
 | 
					
						
							|  |  |  |     false
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | }
 |