util.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. use std::ops::Deref;
  2. use std::sync::Arc;
  3. use anyhow::Error;
  4. use bytes::Bytes;
  5. use collab_document::blocks::DocumentData;
  6. use collab_document::document_data::default_document_data;
  7. use nanoid::nanoid;
  8. use parking_lot::Once;
  9. use tempfile::TempDir;
  10. use tracing_subscriber::{fmt::Subscriber, util::SubscriberInitExt, EnvFilter};
  11. use uuid::Uuid;
  12. use collab_integrate::collab_builder::{AppFlowyCollabBuilder, DefaultCollabStorageProvider};
  13. use collab_integrate::RocksCollabDB;
  14. use flowy_document2::document::MutexDocument;
  15. use flowy_document2::manager::{DocumentManager, DocumentUser};
  16. use flowy_document_deps::cloud::*;
  17. use flowy_error::FlowyError;
  18. use flowy_storage::{FileStorageService, StorageObject};
  19. use lib_infra::future::FutureResult;
  20. pub struct DocumentTest {
  21. inner: DocumentManager,
  22. }
  23. impl DocumentTest {
  24. pub fn new() -> Self {
  25. let user = FakeUser::new();
  26. let cloud_service = Arc::new(LocalTestDocumentCloudServiceImpl());
  27. let file_storage = Arc::new(DocumentTestFileStorageService) as Arc<dyn FileStorageService>;
  28. let manager = DocumentManager::new(
  29. Arc::new(user),
  30. default_collab_builder(),
  31. cloud_service,
  32. Arc::downgrade(&file_storage),
  33. );
  34. Self { inner: manager }
  35. }
  36. }
  37. impl Deref for DocumentTest {
  38. type Target = DocumentManager;
  39. fn deref(&self) -> &Self::Target {
  40. &self.inner
  41. }
  42. }
  43. pub struct FakeUser {
  44. collab_db: Arc<RocksCollabDB>,
  45. }
  46. impl FakeUser {
  47. pub fn new() -> Self {
  48. Self { collab_db: db() }
  49. }
  50. }
  51. impl DocumentUser for FakeUser {
  52. fn user_id(&self) -> Result<i64, FlowyError> {
  53. Ok(1)
  54. }
  55. fn workspace_id(&self) -> Result<String, FlowyError> {
  56. Ok(Uuid::new_v4().to_string())
  57. }
  58. fn token(&self) -> Result<Option<String>, FlowyError> {
  59. Ok(None)
  60. }
  61. fn collab_db(&self, _uid: i64) -> Result<std::sync::Weak<RocksCollabDB>, FlowyError> {
  62. Ok(Arc::downgrade(&self.collab_db))
  63. }
  64. }
  65. pub fn db() -> Arc<RocksCollabDB> {
  66. static START: Once = Once::new();
  67. START.call_once(|| {
  68. std::env::set_var("RUST_LOG", "collab_persistence=trace");
  69. let subscriber = Subscriber::builder()
  70. .with_env_filter(EnvFilter::from_default_env())
  71. .with_ansi(true)
  72. .finish();
  73. subscriber.try_init().unwrap();
  74. });
  75. let tempdir = TempDir::new().unwrap();
  76. let path = tempdir.into_path();
  77. Arc::new(RocksCollabDB::open(path).unwrap())
  78. }
  79. pub fn default_collab_builder() -> Arc<AppFlowyCollabBuilder> {
  80. let builder = AppFlowyCollabBuilder::new(DefaultCollabStorageProvider());
  81. builder.set_sync_device(uuid::Uuid::new_v4().to_string());
  82. builder.initialize(uuid::Uuid::new_v4().to_string());
  83. Arc::new(builder)
  84. }
  85. pub async fn create_and_open_empty_document() -> (DocumentTest, Arc<MutexDocument>, String) {
  86. let test = DocumentTest::new();
  87. let doc_id: String = gen_document_id();
  88. let data = default_document_data();
  89. let uid = test.user.user_id().unwrap();
  90. // create a document
  91. _ = test
  92. .create_document(uid, &doc_id, Some(data.clone()))
  93. .await
  94. .unwrap();
  95. let document = test.get_document(&doc_id).await.unwrap();
  96. (test, document, data.page_id)
  97. }
  98. pub fn gen_document_id() -> String {
  99. let uuid = uuid::Uuid::new_v4();
  100. uuid.to_string()
  101. }
  102. pub fn gen_id() -> String {
  103. nanoid!(10)
  104. }
  105. pub struct LocalTestDocumentCloudServiceImpl();
  106. impl DocumentCloudService for LocalTestDocumentCloudServiceImpl {
  107. fn get_document_updates(
  108. &self,
  109. _document_id: &str,
  110. _workspace_id: &str,
  111. ) -> FutureResult<Vec<Vec<u8>>, Error> {
  112. FutureResult::new(async move { Ok(vec![]) })
  113. }
  114. fn get_document_snapshots(
  115. &self,
  116. _document_id: &str,
  117. _limit: usize,
  118. _workspace_id: &str,
  119. ) -> FutureResult<Vec<DocumentSnapshot>, Error> {
  120. FutureResult::new(async move { Ok(vec![]) })
  121. }
  122. fn get_document_data(
  123. &self,
  124. _document_id: &str,
  125. _workspace_id: &str,
  126. ) -> FutureResult<Option<DocumentData>, Error> {
  127. FutureResult::new(async move { Ok(None) })
  128. }
  129. }
  130. pub struct DocumentTestFileStorageService;
  131. impl FileStorageService for DocumentTestFileStorageService {
  132. fn create_object(&self, _object: StorageObject) -> FutureResult<String, FlowyError> {
  133. todo!()
  134. }
  135. fn delete_object_by_url(&self, _object_url: String) -> FutureResult<(), FlowyError> {
  136. todo!()
  137. }
  138. fn get_object_by_url(&self, _object_url: String) -> FutureResult<Bytes, FlowyError> {
  139. todo!()
  140. }
  141. }