2023-12-29 13:02:27 +08:00
|
|
|
use collab::core::collab::CollabDocState;
|
|
|
|
use collab::core::origin::CollabOrigin;
|
|
|
|
use collab_document::blocks::DocumentData;
|
|
|
|
use collab_document::document::Document;
|
|
|
|
use collab_entity::CollabType;
|
2023-05-21 11:13:22 +08:00
|
|
|
use std::env::temp_dir;
|
2023-07-05 20:57:09 +08:00
|
|
|
use std::path::PathBuf;
|
2023-05-23 23:55:21 +08:00
|
|
|
use std::sync::Arc;
|
2023-12-27 11:42:39 +08:00
|
|
|
use std::time::Duration;
|
2022-10-20 11:35:11 +08:00
|
|
|
|
2023-06-07 00:05:27 +08:00
|
|
|
use nanoid::nanoid;
|
|
|
|
use parking_lot::RwLock;
|
2023-12-27 11:42:39 +08:00
|
|
|
use tokio::select;
|
|
|
|
use tokio::time::sleep;
|
2023-06-07 00:05:27 +08:00
|
|
|
|
2023-11-17 15:38:56 +08:00
|
|
|
use flowy_core::config::AppFlowyCoreConfig;
|
|
|
|
use flowy_core::AppFlowyCore;
|
2023-10-24 20:11:06 +08:00
|
|
|
use flowy_notification::register_notification_sender;
|
2023-12-27 11:42:39 +08:00
|
|
|
use flowy_server::AppFlowyServer;
|
2023-12-26 02:03:42 +08:00
|
|
|
use flowy_user::entities::AuthenticatorPB;
|
2023-12-29 13:02:27 +08:00
|
|
|
use flowy_user::errors::FlowyError;
|
2023-05-17 09:49:39 +08:00
|
|
|
|
2023-10-25 15:25:31 +08:00
|
|
|
use crate::user_event::TestNotificationSender;
|
2023-06-07 00:05:27 +08:00
|
|
|
|
2023-10-25 15:25:31 +08:00
|
|
|
pub mod database_event;
|
2023-07-04 17:17:25 +08:00
|
|
|
pub mod document;
|
2023-10-25 15:25:31 +08:00
|
|
|
pub mod document_event;
|
2023-05-17 09:49:39 +08:00
|
|
|
pub mod event_builder;
|
2023-10-25 15:25:31 +08:00
|
|
|
pub mod folder_event;
|
|
|
|
pub mod user_event;
|
2021-07-06 14:14:47 +08:00
|
|
|
|
2021-09-04 15:12:53 +08:00
|
|
|
#[derive(Clone)]
|
2023-10-24 20:11:06 +08:00
|
|
|
pub struct EventIntegrationTest {
|
2023-12-27 11:42:39 +08:00
|
|
|
pub authenticator: Arc<RwLock<AuthenticatorPB>>,
|
|
|
|
pub appflowy_core: AppFlowyCore,
|
2023-08-06 11:51:03 +08:00
|
|
|
#[allow(dead_code)]
|
|
|
|
cleaner: Arc<Cleaner>,
|
2023-07-05 20:57:09 +08:00
|
|
|
pub notification_sender: TestNotificationSender,
|
2022-01-07 17:37:11 +08:00
|
|
|
}
|
2021-12-08 17:33:22 +08:00
|
|
|
|
2023-10-30 12:35:06 +08:00
|
|
|
impl EventIntegrationTest {
|
|
|
|
pub async fn new() -> Self {
|
2023-12-29 13:02:27 +08:00
|
|
|
Self::new_with_name(nanoid!(6)).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn new_with_name<T: ToString>(name: T) -> Self {
|
2023-08-09 12:43:03 +08:00
|
|
|
let temp_dir = temp_dir().join(nanoid!(6));
|
2023-08-06 11:51:03 +08:00
|
|
|
std::fs::create_dir_all(&temp_dir).unwrap();
|
2023-12-29 13:02:27 +08:00
|
|
|
Self::new_with_user_data_path(temp_dir, name.to_string()).await
|
2023-08-03 09:14:52 +08:00
|
|
|
}
|
2023-12-27 11:42:39 +08:00
|
|
|
|
2023-11-20 20:54:47 +08:00
|
|
|
pub async fn new_with_user_data_path(path_buf: PathBuf, name: String) -> Self {
|
|
|
|
let path = path_buf.to_str().unwrap().to_string();
|
|
|
|
let device_id = uuid::Uuid::new_v4().to_string();
|
2023-12-29 13:02:27 +08:00
|
|
|
|
2023-11-20 20:54:47 +08:00
|
|
|
let config = AppFlowyCoreConfig::new(path.clone(), path, device_id, name).log_filter(
|
2024-01-04 08:02:12 +08:00
|
|
|
"trace",
|
2023-10-02 17:22:22 +08:00
|
|
|
vec![
|
|
|
|
"flowy_test".to_string(),
|
2023-10-30 12:35:06 +08:00
|
|
|
"tokio".to_string(),
|
2023-12-27 11:42:39 +08:00
|
|
|
// "lib_dispatch".to_string(),
|
2023-10-02 17:22:22 +08:00
|
|
|
],
|
2023-07-29 09:46:24 +08:00
|
|
|
);
|
2023-07-05 20:57:09 +08:00
|
|
|
|
2023-10-30 12:35:06 +08:00
|
|
|
let inner = init_core(config).await;
|
2023-07-05 20:57:09 +08:00
|
|
|
let notification_sender = TestNotificationSender::new();
|
2023-12-27 11:42:39 +08:00
|
|
|
let authenticator = Arc::new(RwLock::new(AuthenticatorPB::Local));
|
2023-07-05 20:57:09 +08:00
|
|
|
register_notification_sender(notification_sender.clone());
|
2023-12-27 11:42:39 +08:00
|
|
|
|
|
|
|
// In case of dropping the runtime that runs the core, we need to forget the dispatcher
|
2023-05-23 23:55:21 +08:00
|
|
|
std::mem::forget(inner.dispatcher());
|
2023-07-05 20:57:09 +08:00
|
|
|
Self {
|
2023-12-27 11:42:39 +08:00
|
|
|
appflowy_core: inner,
|
|
|
|
authenticator,
|
2023-07-05 20:57:09 +08:00
|
|
|
notification_sender,
|
2023-11-20 20:54:47 +08:00
|
|
|
cleaner: Arc::new(Cleaner(path_buf)),
|
2023-07-05 20:57:09 +08:00
|
|
|
}
|
2023-02-13 09:29:49 +08:00
|
|
|
}
|
2023-12-27 11:42:39 +08:00
|
|
|
|
2024-01-04 08:02:12 +08:00
|
|
|
pub fn get_server(&self) -> Arc<dyn AppFlowyServer> {
|
|
|
|
self.appflowy_core.server_provider.get_server().unwrap()
|
2023-12-27 11:42:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn wait_ws_connected(&self) {
|
2024-01-04 08:02:12 +08:00
|
|
|
if self.get_server().get_ws_state().is_connected() {
|
2023-12-27 11:42:39 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-04 08:02:12 +08:00
|
|
|
let mut ws_state = self.get_server().subscribe_ws_state().unwrap();
|
2023-12-27 11:42:39 +08:00
|
|
|
loop {
|
|
|
|
select! {
|
|
|
|
_ = sleep(Duration::from_secs(20)) => {
|
|
|
|
panic!("wait_ws_connected timeout");
|
|
|
|
}
|
|
|
|
state = ws_state.recv() => {
|
|
|
|
if let Ok(state) = &state {
|
|
|
|
if state.is_connected() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-29 13:02:27 +08:00
|
|
|
|
|
|
|
pub async fn get_collab_doc_state(
|
|
|
|
&self,
|
|
|
|
oid: &str,
|
|
|
|
collay_type: CollabType,
|
|
|
|
) -> Result<CollabDocState, FlowyError> {
|
2024-01-04 08:02:12 +08:00
|
|
|
let server = self.server_provider.get_server().unwrap();
|
2023-12-29 13:02:27 +08:00
|
|
|
let workspace_id = self.get_current_workspace().await.id;
|
|
|
|
let uid = self.get_user_profile().await?.id;
|
|
|
|
let doc_state = server
|
|
|
|
.folder_service()
|
|
|
|
.get_collab_doc_state_f(&workspace_id, uid, collay_type, oid)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(doc_state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn document_data_from_document_doc_state(
|
|
|
|
doc_id: &str,
|
|
|
|
doc_state: CollabDocState,
|
|
|
|
) -> DocumentData {
|
2024-01-07 11:12:05 +08:00
|
|
|
document_from_document_doc_state(doc_id, doc_state)
|
2023-12-29 13:02:27 +08:00
|
|
|
.get_document_data()
|
|
|
|
.unwrap()
|
2021-07-06 14:14:47 +08:00
|
|
|
}
|
2023-05-23 23:55:21 +08:00
|
|
|
|
2024-01-07 11:12:05 +08:00
|
|
|
pub fn document_from_document_doc_state(doc_id: &str, doc_state: CollabDocState) -> Document {
|
|
|
|
Document::from_doc_state(CollabOrigin::Empty, doc_state, doc_id, vec![]).unwrap()
|
|
|
|
}
|
|
|
|
|
2023-10-30 12:35:06 +08:00
|
|
|
#[cfg(feature = "single_thread")]
|
|
|
|
async fn init_core(config: AppFlowyCoreConfig) -> AppFlowyCore {
|
|
|
|
// let runtime = tokio::runtime::Runtime::new().unwrap();
|
|
|
|
// let local_set = tokio::task::LocalSet::new();
|
|
|
|
// runtime.block_on(AppFlowyCore::new(config))
|
|
|
|
AppFlowyCore::new(config).await
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "single_thread"))]
|
|
|
|
async fn init_core(config: AppFlowyCoreConfig) -> AppFlowyCore {
|
|
|
|
std::thread::spawn(|| AppFlowyCore::new(config))
|
|
|
|
.join()
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
2023-10-24 20:11:06 +08:00
|
|
|
impl std::ops::Deref for EventIntegrationTest {
|
2023-05-23 23:55:21 +08:00
|
|
|
type Target = AppFlowyCore;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2023-12-27 11:42:39 +08:00
|
|
|
&self.appflowy_core
|
2023-05-23 23:55:21 +08:00
|
|
|
}
|
|
|
|
}
|
2023-06-05 16:09:18 +08:00
|
|
|
|
2023-08-03 10:33:25 +08:00
|
|
|
pub struct Cleaner(PathBuf);
|
2023-07-05 20:57:09 +08:00
|
|
|
|
|
|
|
impl Cleaner {
|
2023-08-03 10:33:25 +08:00
|
|
|
pub fn new(dir: PathBuf) -> Self {
|
2023-07-05 20:57:09 +08:00
|
|
|
Cleaner(dir)
|
|
|
|
}
|
|
|
|
|
2023-08-06 11:51:03 +08:00
|
|
|
fn cleanup(dir: &PathBuf) {
|
|
|
|
let _ = std::fs::remove_dir_all(dir);
|
2023-07-05 20:57:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Cleaner {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
Self::cleanup(&self.0)
|
|
|
|
}
|
|
|
|
}
|