2023-07-05 12:57:09 +00:00
|
|
|
use std::ops::Deref;
|
2023-04-28 12:47:40 +00:00
|
|
|
use std::sync::Arc;
|
2023-05-10 05:27:50 +00:00
|
|
|
|
2023-07-05 12:57:09 +00:00
|
|
|
use appflowy_integrate::collab_builder::{AppFlowyCollabBuilder, DefaultCollabStorageProvider};
|
2023-05-15 14:16:05 +00:00
|
|
|
use appflowy_integrate::RocksCollabDB;
|
2023-07-05 12:57:09 +00:00
|
|
|
use nanoid::nanoid;
|
2023-05-10 05:27:50 +00:00
|
|
|
use parking_lot::Once;
|
2023-04-24 06:25:00 +00:00
|
|
|
use tempfile::TempDir;
|
|
|
|
use tracing_subscriber::{fmt::Subscriber, util::SubscriberInitExt, EnvFilter};
|
|
|
|
|
2023-07-05 12:57:09 +00:00
|
|
|
use flowy_document2::deps::{DocumentCloudService, DocumentSnapshot, DocumentUser};
|
|
|
|
use flowy_document2::document::MutexDocument;
|
2023-06-15 02:37:51 +00:00
|
|
|
use flowy_document2::document_data::default_document_data;
|
2023-07-05 12:57:09 +00:00
|
|
|
use flowy_document2::manager::DocumentManager;
|
|
|
|
use flowy_error::FlowyError;
|
|
|
|
use lib_infra::future::FutureResult;
|
|
|
|
|
|
|
|
pub struct DocumentTest {
|
|
|
|
inner: DocumentManager,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DocumentTest {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
let user = FakeUser::new();
|
|
|
|
let cloud_service = Arc::new(LocalTestDocumentCloudServiceImpl());
|
|
|
|
let manager = DocumentManager::new(Arc::new(user), default_collab_builder(), cloud_service);
|
|
|
|
Self { inner: manager }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for DocumentTest {
|
|
|
|
type Target = DocumentManager;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
}
|
2023-05-10 05:27:50 +00:00
|
|
|
|
2023-04-24 06:25:00 +00:00
|
|
|
pub struct FakeUser {
|
2023-04-28 12:47:40 +00:00
|
|
|
kv: Arc<RocksCollabDB>,
|
2023-04-24 06:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FakeUser {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self { kv: db() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DocumentUser for FakeUser {
|
|
|
|
fn user_id(&self) -> Result<i64, flowy_error::FlowyError> {
|
|
|
|
Ok(1)
|
|
|
|
}
|
|
|
|
|
2023-05-21 10:53:59 +00:00
|
|
|
fn token(&self) -> Result<Option<String>, flowy_error::FlowyError> {
|
|
|
|
Ok(None)
|
2023-04-24 06:25:00 +00:00
|
|
|
}
|
|
|
|
|
2023-05-15 14:16:05 +00:00
|
|
|
fn collab_db(&self) -> Result<std::sync::Arc<RocksCollabDB>, flowy_error::FlowyError> {
|
2023-04-24 06:25:00 +00:00
|
|
|
Ok(self.kv.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-28 12:47:40 +00:00
|
|
|
pub fn db() -> Arc<RocksCollabDB> {
|
2023-04-24 06:25:00 +00:00
|
|
|
static START: Once = Once::new();
|
|
|
|
START.call_once(|| {
|
|
|
|
std::env::set_var("RUST_LOG", "collab_persistence=trace");
|
|
|
|
let subscriber = Subscriber::builder()
|
|
|
|
.with_env_filter(EnvFilter::from_default_env())
|
|
|
|
.with_ansi(true)
|
|
|
|
.finish();
|
|
|
|
subscriber.try_init().unwrap();
|
|
|
|
});
|
|
|
|
|
|
|
|
let tempdir = TempDir::new().unwrap();
|
|
|
|
let path = tempdir.into_path();
|
2023-04-28 12:47:40 +00:00
|
|
|
Arc::new(RocksCollabDB::open(path).unwrap())
|
2023-04-24 06:25:00 +00:00
|
|
|
}
|
2023-05-16 06:58:24 +00:00
|
|
|
|
|
|
|
pub fn default_collab_builder() -> Arc<AppFlowyCollabBuilder> {
|
2023-07-05 12:57:09 +00:00
|
|
|
let builder = AppFlowyCollabBuilder::new(DefaultCollabStorageProvider(), None);
|
2023-05-16 06:58:24 +00:00
|
|
|
Arc::new(builder)
|
|
|
|
}
|
2023-06-15 02:37:51 +00:00
|
|
|
|
2023-07-05 12:57:09 +00:00
|
|
|
pub fn create_and_open_empty_document() -> (DocumentTest, Arc<MutexDocument>, String) {
|
|
|
|
let test = DocumentTest::new();
|
2023-06-15 02:37:51 +00:00
|
|
|
let doc_id: String = gen_document_id();
|
|
|
|
let data = default_document_data();
|
|
|
|
|
|
|
|
// create a document
|
2023-07-05 12:57:09 +00:00
|
|
|
_ = test.create_document(&doc_id, Some(data.clone())).unwrap();
|
2023-06-15 02:37:51 +00:00
|
|
|
|
2023-07-05 12:57:09 +00:00
|
|
|
let document = test.get_document(&doc_id).unwrap();
|
2023-06-15 02:37:51 +00:00
|
|
|
|
2023-07-05 12:57:09 +00:00
|
|
|
(test, document, data.page_id)
|
2023-06-15 02:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn gen_document_id() -> String {
|
|
|
|
let uuid = uuid::Uuid::new_v4();
|
|
|
|
uuid.to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn gen_id() -> String {
|
|
|
|
nanoid!(10)
|
|
|
|
}
|
2023-07-05 12:57:09 +00:00
|
|
|
|
|
|
|
pub struct LocalTestDocumentCloudServiceImpl();
|
|
|
|
impl DocumentCloudService for LocalTestDocumentCloudServiceImpl {
|
|
|
|
fn get_document_updates(&self, _document_id: &str) -> FutureResult<Vec<Vec<u8>>, FlowyError> {
|
|
|
|
FutureResult::new(async move { Ok(vec![]) })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_document_latest_snapshot(
|
|
|
|
&self,
|
|
|
|
_document_id: &str,
|
|
|
|
) -> FutureResult<Option<DocumentSnapshot>, FlowyError> {
|
|
|
|
FutureResult::new(async move { Ok(None) })
|
|
|
|
}
|
|
|
|
}
|