util.rs 3.1 KB

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