util.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 collab_integrate::collab_builder::{AppFlowyCollabBuilder, DefaultCollabStorageProvider};
  12. use collab_integrate::RocksCollabDB;
  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, FlowyError> {
  52. Ok(1)
  53. }
  54. fn token(&self) -> Result<Option<String>, FlowyError> {
  55. Ok(None)
  56. }
  57. fn collab_db(&self, _uid: i64) -> Result<std::sync::Weak<RocksCollabDB>, FlowyError> {
  58. Ok(Arc::downgrade(&self.collab_db))
  59. }
  60. }
  61. pub fn db() -> Arc<RocksCollabDB> {
  62. static START: Once = Once::new();
  63. START.call_once(|| {
  64. std::env::set_var("RUST_LOG", "collab_persistence=trace");
  65. let subscriber = Subscriber::builder()
  66. .with_env_filter(EnvFilter::from_default_env())
  67. .with_ansi(true)
  68. .finish();
  69. subscriber.try_init().unwrap();
  70. });
  71. let tempdir = TempDir::new().unwrap();
  72. let path = tempdir.into_path();
  73. Arc::new(RocksCollabDB::open(path).unwrap())
  74. }
  75. pub fn default_collab_builder() -> Arc<AppFlowyCollabBuilder> {
  76. let builder = AppFlowyCollabBuilder::new(DefaultCollabStorageProvider());
  77. builder.set_sync_device(uuid::Uuid::new_v4().to_string());
  78. builder.initialize(uuid::Uuid::new_v4().to_string());
  79. Arc::new(builder)
  80. }
  81. pub async fn create_and_open_empty_document() -> (DocumentTest, Arc<MutexDocument>, String) {
  82. let test = DocumentTest::new();
  83. let doc_id: String = gen_document_id();
  84. let data = default_document_data();
  85. let uid = test.user.user_id().unwrap();
  86. // create a document
  87. _ = test
  88. .create_document(uid, &doc_id, Some(data.clone()))
  89. .await
  90. .unwrap();
  91. let document = test.get_document(&doc_id).await.unwrap();
  92. (test, document, data.page_id)
  93. }
  94. pub fn gen_document_id() -> String {
  95. let uuid = uuid::Uuid::new_v4();
  96. uuid.to_string()
  97. }
  98. pub fn gen_id() -> String {
  99. nanoid!(10)
  100. }
  101. pub struct LocalTestDocumentCloudServiceImpl();
  102. impl DocumentCloudService for LocalTestDocumentCloudServiceImpl {
  103. fn get_document_updates(&self, _document_id: &str) -> FutureResult<Vec<Vec<u8>>, Error> {
  104. FutureResult::new(async move { Ok(vec![]) })
  105. }
  106. fn get_document_snapshots(
  107. &self,
  108. _document_id: &str,
  109. _limit: usize,
  110. ) -> FutureResult<Vec<DocumentSnapshot>, Error> {
  111. FutureResult::new(async move { Ok(vec![]) })
  112. }
  113. fn get_document_data(&self, _document_id: &str) -> FutureResult<Option<DocumentData>, Error> {
  114. FutureResult::new(async move { Ok(None) })
  115. }
  116. }
  117. pub struct DocumentTestFileStorageService;
  118. impl FileStorageService for DocumentTestFileStorageService {
  119. fn create_object(&self, _object: StorageObject) -> FutureResult<String, FlowyError> {
  120. todo!()
  121. }
  122. fn delete_object_by_url(&self, _object_url: String) -> FutureResult<(), FlowyError> {
  123. todo!()
  124. }
  125. fn get_object_by_url(&self, _object_url: String) -> FutureResult<Bytes, FlowyError> {
  126. todo!()
  127. }
  128. }