util.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 collab_document::blocks::DocumentData;
  7. use collab_document::document_data::default_document_data;
  8. use nanoid::nanoid;
  9. use parking_lot::Once;
  10. use tempfile::TempDir;
  11. use tracing_subscriber::{fmt::Subscriber, util::SubscriberInitExt, EnvFilter};
  12. use flowy_document2::document::MutexDocument;
  13. use flowy_document2::manager::{DocumentManager, DocumentUser};
  14. use flowy_document_deps::cloud::*;
  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. collab_db: Arc<RocksCollabDB>,
  35. }
  36. impl FakeUser {
  37. pub fn new() -> Self {
  38. Self { collab_db: 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(
  49. &self,
  50. _uid: i64,
  51. ) -> Result<std::sync::Weak<RocksCollabDB>, flowy_error::FlowyError> {
  52. Ok(Arc::downgrade(&self.collab_db))
  53. }
  54. }
  55. pub fn db() -> Arc<RocksCollabDB> {
  56. static START: Once = Once::new();
  57. START.call_once(|| {
  58. std::env::set_var("RUST_LOG", "collab_persistence=trace");
  59. let subscriber = Subscriber::builder()
  60. .with_env_filter(EnvFilter::from_default_env())
  61. .with_ansi(true)
  62. .finish();
  63. subscriber.try_init().unwrap();
  64. });
  65. let tempdir = TempDir::new().unwrap();
  66. let path = tempdir.into_path();
  67. Arc::new(RocksCollabDB::open(path).unwrap())
  68. }
  69. pub fn default_collab_builder() -> Arc<AppFlowyCollabBuilder> {
  70. let builder = AppFlowyCollabBuilder::new(DefaultCollabStorageProvider(), None);
  71. builder.set_sync_device(uuid::Uuid::new_v4().to_string());
  72. Arc::new(builder)
  73. }
  74. pub async fn create_and_open_empty_document() -> (DocumentTest, Arc<MutexDocument>, String) {
  75. let test = DocumentTest::new();
  76. let doc_id: String = gen_document_id();
  77. let data = default_document_data();
  78. // create a document
  79. _ = test.create_document(&doc_id, Some(data.clone())).unwrap();
  80. let document = test.get_document(&doc_id).await.unwrap();
  81. (test, document, data.page_id)
  82. }
  83. pub fn gen_document_id() -> String {
  84. let uuid = uuid::Uuid::new_v4();
  85. uuid.to_string()
  86. }
  87. pub fn gen_id() -> String {
  88. nanoid!(10)
  89. }
  90. pub struct LocalTestDocumentCloudServiceImpl();
  91. impl DocumentCloudService for LocalTestDocumentCloudServiceImpl {
  92. fn get_document_updates(&self, _document_id: &str) -> FutureResult<Vec<Vec<u8>>, Error> {
  93. FutureResult::new(async move { Ok(vec![]) })
  94. }
  95. fn get_document_latest_snapshot(
  96. &self,
  97. _document_id: &str,
  98. ) -> FutureResult<Option<DocumentSnapshot>, Error> {
  99. FutureResult::new(async move { Ok(None) })
  100. }
  101. fn get_document_data(&self, _document_id: &str) -> FutureResult<Option<DocumentData>, Error> {
  102. FutureResult::new(async move { Ok(None) })
  103. }
  104. }