2024-04-15 14:50:28 +08:00
|
|
|
use collab::core::collab::DataSource;
|
2023-12-29 13:02:27 +08:00
|
|
|
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;
|
2024-08-18 05:16:42 +02:00
|
|
|
use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};
|
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;
|
2024-05-22 14:24:11 +08:00
|
|
|
use semver::Version;
|
2023-12-27 11:42:39 +08:00
|
|
|
use tokio::select;
|
2024-08-20 14:16:24 +08:00
|
|
|
use tokio::task::LocalSet;
|
2023-12-27 11:42:39 +08:00
|
|
|
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;
|
2024-02-04 05:50:23 +08:00
|
|
|
use lib_dispatch::runtime::AFPluginRuntime;
|
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
|
|
|
|
2024-06-03 14:27:28 +08:00
|
|
|
mod chat_event;
|
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 {
|
2024-08-18 05:16:42 +02:00
|
|
|
pub authenticator: Arc<AtomicU8>,
|
2023-12-27 11:42:39 +08:00
|
|
|
pub appflowy_core: AppFlowyCore,
|
2023-08-06 11:51:03 +08:00
|
|
|
#[allow(dead_code)]
|
2024-08-18 05:16:42 +02:00
|
|
|
cleaner: Arc<Cleaner>,
|
2023-07-05 20:57:09 +08:00
|
|
|
pub notification_sender: TestNotificationSender,
|
2024-08-20 14:16:24 +08:00
|
|
|
local_set: Arc<LocalSet>,
|
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
|
|
|
|
2024-06-20 07:44:57 +08:00
|
|
|
pub async fn new_with_config(config: AppFlowyCoreConfig) -> Self {
|
|
|
|
let clean_path = config.storage_path.clone();
|
|
|
|
let inner = init_core(config).await;
|
|
|
|
let notification_sender = TestNotificationSender::new();
|
2024-08-18 05:16:42 +02:00
|
|
|
let authenticator = Arc::new(AtomicU8::new(AuthenticatorPB::Local as u8));
|
2024-06-20 07:44:57 +08:00
|
|
|
register_notification_sender(notification_sender.clone());
|
|
|
|
|
|
|
|
// In case of dropping the runtime that runs the core, we need to forget the dispatcher
|
|
|
|
std::mem::forget(inner.dispatcher());
|
|
|
|
Self {
|
|
|
|
appflowy_core: inner,
|
|
|
|
authenticator,
|
|
|
|
notification_sender,
|
2024-08-18 05:16:42 +02:00
|
|
|
cleaner: Arc::new(Cleaner::new(PathBuf::from(clean_path))),
|
2024-08-20 17:07:54 +08:00
|
|
|
#[allow(clippy::arc_with_non_send_sync)]
|
2024-08-20 14:16:24 +08:00
|
|
|
local_set: Arc::new(Default::default()),
|
2024-06-20 07:44:57 +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();
|
2024-04-07 21:36:55 +08:00
|
|
|
let config = AppFlowyCoreConfig::new(
|
2024-05-22 14:24:11 +08:00
|
|
|
Version::new(0, 5, 8),
|
2024-04-07 21:36:55 +08:00
|
|
|
path.clone(),
|
|
|
|
path,
|
|
|
|
device_id,
|
|
|
|
"test".to_string(),
|
|
|
|
name,
|
|
|
|
)
|
|
|
|
.log_filter(
|
|
|
|
"trace",
|
|
|
|
vec![
|
|
|
|
"flowy_test".to_string(),
|
|
|
|
"tokio".to_string(),
|
|
|
|
// "lib_dispatch".to_string(),
|
|
|
|
],
|
|
|
|
);
|
2024-06-20 07:44:57 +08:00
|
|
|
Self::new_with_config(config).await
|
|
|
|
}
|
2023-07-05 20:57:09 +08:00
|
|
|
|
2024-08-11 20:39:25 +08:00
|
|
|
pub fn skip_clean(&mut self) {
|
2024-08-18 05:16:42 +02:00
|
|
|
self.cleaner.should_clean.store(false, Ordering::Release);
|
2023-02-13 09:29:49 +08:00
|
|
|
}
|
2023-12-27 11:42:39 +08:00
|
|
|
|
2024-04-12 10:21:41 +02:00
|
|
|
pub fn instance_name(&self) -> String {
|
|
|
|
self.appflowy_core.config.name.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn user_data_path(&self) -> String {
|
|
|
|
self.appflowy_core.config.application_path.clone()
|
|
|
|
}
|
|
|
|
|
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,
|
2024-03-23 09:18:47 +08:00
|
|
|
collab_type: CollabType,
|
|
|
|
) -> Result<Vec<u8>, 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()
|
2024-03-23 09:18:47 +08:00
|
|
|
.get_folder_doc_state(&workspace_id, uid, collab_type, oid)
|
2023-12-29 13:02:27 +08:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(doc_state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-23 09:18:47 +08:00
|
|
|
pub fn document_data_from_document_doc_state(doc_id: &str, doc_state: Vec<u8>) -> 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-03-23 09:18:47 +08:00
|
|
|
pub fn document_from_document_doc_state(doc_id: &str, doc_state: Vec<u8>) -> Document {
|
2024-08-18 05:16:42 +02:00
|
|
|
Document::open_with_options(
|
2024-03-23 09:18:47 +08:00
|
|
|
CollabOrigin::Empty,
|
2024-04-15 14:50:28 +08:00
|
|
|
DataSource::DocStateV1(doc_state),
|
2024-03-23 09:18:47 +08:00
|
|
|
doc_id,
|
|
|
|
vec![],
|
|
|
|
)
|
|
|
|
.unwrap()
|
2024-01-07 11:12:05 +08:00
|
|
|
}
|
|
|
|
|
2023-10-30 12:35:06 +08:00
|
|
|
async fn init_core(config: AppFlowyCoreConfig) -> AppFlowyCore {
|
2024-08-19 22:08:10 +08:00
|
|
|
let runtime = Arc::new(AFPluginRuntime::new().unwrap());
|
2024-08-13 23:36:44 +08:00
|
|
|
let cloned_runtime = runtime.clone();
|
|
|
|
AppFlowyCore::new(config, cloned_runtime, None).await
|
2023-10-30 12:35:06 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2024-06-20 07:44:57 +08:00
|
|
|
pub struct Cleaner {
|
|
|
|
dir: PathBuf,
|
2024-08-18 05:16:42 +02:00
|
|
|
should_clean: AtomicBool,
|
2024-06-20 07:44:57 +08:00
|
|
|
}
|
2023-07-05 20:57:09 +08:00
|
|
|
|
|
|
|
impl Cleaner {
|
2023-08-03 10:33:25 +08:00
|
|
|
pub fn new(dir: PathBuf) -> Self {
|
2024-06-20 07:44:57 +08:00
|
|
|
Self {
|
|
|
|
dir,
|
2024-08-18 05:16:42 +02:00
|
|
|
should_clean: AtomicBool::new(true),
|
2024-06-20 07:44:57 +08:00
|
|
|
}
|
2023-07-05 20:57:09 +08:00
|
|
|
}
|
|
|
|
|
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) {
|
2024-08-18 05:16:42 +02:00
|
|
|
if self.should_clean.load(Ordering::Acquire) {
|
2024-06-20 07:44:57 +08:00
|
|
|
Self::cleanup(&self.dir)
|
|
|
|
}
|
2023-07-05 20:57:09 +08:00
|
|
|
}
|
|
|
|
}
|