util.rs 3.3 KB

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