util.rs 4.1 KB

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