2021-12-04 15:54:14 +00:00
|
|
|
#![allow(clippy::all)]
|
|
|
|
#![cfg_attr(rustfmt, rustfmt::skip)]
|
2021-09-29 09:40:34 +00:00
|
|
|
use actix_web::web::Data;
|
2021-12-07 02:39:01 +00:00
|
|
|
use backend::services::doc::{crud::update_doc, manager::DocManager};
|
2021-12-03 14:40:56 +00:00
|
|
|
use flowy_document::services::doc::ClientDocEditor as ClientEditDocContext;
|
2021-09-29 09:40:34 +00:00
|
|
|
use flowy_test::{workspace::ViewTest, FlowyTest};
|
2021-09-30 09:24:02 +00:00
|
|
|
use flowy_user::services::user::UserSession;
|
2021-11-13 03:11:24 +00:00
|
|
|
use futures_util::{stream, stream::StreamExt};
|
|
|
|
use sqlx::PgPool;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use tokio::time::{sleep, Duration};
|
2021-10-01 11:39:08 +00:00
|
|
|
// use crate::helper::*;
|
2021-11-19 05:42:53 +00:00
|
|
|
use crate::util::helper::{spawn_server, TestServer};
|
2021-11-13 03:11:24 +00:00
|
|
|
use flowy_document_infra::{entities::doc::DocIdentifier, protobuf::UpdateDocParams};
|
2021-12-07 11:59:08 +00:00
|
|
|
use lib_ot::rich_text::{RichTextAttribute, RichTextDelta};
|
2021-10-04 06:24:35 +00:00
|
|
|
use parking_lot::RwLock;
|
2021-12-07 11:59:08 +00:00
|
|
|
use lib_ot::core::Interval;
|
2021-09-27 15:23:23 +00:00
|
|
|
|
|
|
|
pub struct DocumentTest {
|
|
|
|
server: TestServer,
|
2021-09-28 07:29:29 +00:00
|
|
|
flowy_test: FlowyTest,
|
2021-09-27 15:23:23 +00:00
|
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum DocScript {
|
2021-11-19 05:42:53 +00:00
|
|
|
ClientConnectWs,
|
|
|
|
ClientInsertText(usize, &'static str),
|
2021-12-07 02:39:01 +00:00
|
|
|
ClientFormatText(Interval, RichTextAttribute),
|
2021-11-19 05:42:53 +00:00
|
|
|
ClientOpenDoc,
|
2021-09-29 09:40:34 +00:00
|
|
|
AssertClient(&'static str),
|
2021-10-05 03:46:56 +00:00
|
|
|
AssertServer(&'static str, i64),
|
2021-11-19 05:42:53 +00:00
|
|
|
ServerSaveDocument(String, i64), // delta_json, rev_id
|
2021-10-04 06:24:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 15:23:23 +00:00
|
|
|
impl DocumentTest {
|
|
|
|
pub async fn new() -> Self {
|
|
|
|
let server = spawn_server().await;
|
2021-12-05 08:39:41 +00:00
|
|
|
let flowy_test = FlowyTest::setup_with(server.client_server_config.clone());
|
2021-09-29 09:40:34 +00:00
|
|
|
Self { server, flowy_test }
|
2021-09-27 15:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run_scripts(self, scripts: Vec<DocScript>) {
|
2021-09-30 09:24:02 +00:00
|
|
|
let _ = self.flowy_test.sign_up().await;
|
2021-09-29 09:40:34 +00:00
|
|
|
let DocumentTest { server, flowy_test } = self;
|
2021-10-04 06:24:35 +00:00
|
|
|
let script_context = Arc::new(RwLock::new(ScriptContext::new(flowy_test, server).await));
|
2021-09-30 09:24:02 +00:00
|
|
|
run_scripts(script_context, scripts).await;
|
2021-09-29 09:40:34 +00:00
|
|
|
sleep(Duration::from_secs(5)).await;
|
|
|
|
}
|
|
|
|
}
|
2021-09-27 15:23:23 +00:00
|
|
|
|
2021-09-30 09:24:02 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
struct ScriptContext {
|
2021-10-04 06:24:35 +00:00
|
|
|
client_edit_context: Option<Arc<ClientEditDocContext>>,
|
|
|
|
flowy_test: FlowyTest,
|
2021-10-05 06:37:45 +00:00
|
|
|
client_user_session: Arc<UserSession>,
|
|
|
|
server_doc_manager: Arc<DocManager>,
|
|
|
|
server_pg_pool: Data<PgPool>,
|
2021-10-04 06:24:35 +00:00
|
|
|
doc_id: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ScriptContext {
|
|
|
|
async fn new(flowy_test: FlowyTest, server: TestServer) -> Self {
|
|
|
|
let user_session = flowy_test.sdk.user_session.clone();
|
|
|
|
let doc_id = create_doc(&flowy_test).await;
|
|
|
|
|
|
|
|
Self {
|
|
|
|
client_edit_context: None,
|
|
|
|
flowy_test,
|
2021-10-05 06:37:45 +00:00
|
|
|
client_user_session: user_session,
|
|
|
|
server_doc_manager: server.app_ctx.doc_biz.manager.clone(),
|
|
|
|
server_pg_pool: Data::new(server.pg_pool.clone()),
|
2021-10-04 06:24:35 +00:00
|
|
|
doc_id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn open_doc(&mut self) {
|
|
|
|
let flowy_document = self.flowy_test.sdk.flowy_document.clone();
|
|
|
|
let doc_id = self.doc_id.clone();
|
|
|
|
|
2021-11-13 03:53:50 +00:00
|
|
|
let edit_context = flowy_document.open(DocIdentifier { doc_id }).await.unwrap();
|
2021-10-04 06:24:35 +00:00
|
|
|
self.client_edit_context = Some(edit_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn client_edit_context(&self) -> Arc<ClientEditDocContext> { self.client_edit_context.as_ref().unwrap().clone() }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for ScriptContext {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
// std::mem::forget(self.flowy_test);
|
|
|
|
}
|
2021-09-30 09:24:02 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 06:24:35 +00:00
|
|
|
async fn run_scripts(context: Arc<RwLock<ScriptContext>>, scripts: Vec<DocScript>) {
|
2021-09-30 09:24:02 +00:00
|
|
|
let mut fut_scripts = vec![];
|
2021-09-29 09:40:34 +00:00
|
|
|
for script in scripts {
|
2021-09-30 09:24:02 +00:00
|
|
|
let context = context.clone();
|
|
|
|
let fut = async move {
|
2021-10-04 06:24:35 +00:00
|
|
|
let doc_id = context.read().doc_id.clone();
|
2021-09-30 09:24:02 +00:00
|
|
|
match script {
|
2021-11-19 05:42:53 +00:00
|
|
|
DocScript::ClientConnectWs => {
|
2021-10-05 02:19:43 +00:00
|
|
|
// sleep(Duration::from_millis(300)).await;
|
2021-10-05 06:37:45 +00:00
|
|
|
let user_session = context.read().client_user_session.clone();
|
2021-10-04 06:24:35 +00:00
|
|
|
let token = user_session.token().unwrap();
|
|
|
|
let _ = user_session.start_ws_connection(&token).await.unwrap();
|
|
|
|
},
|
2021-11-19 05:42:53 +00:00
|
|
|
DocScript::ClientOpenDoc => {
|
2021-10-04 06:24:35 +00:00
|
|
|
context.write().open_doc().await;
|
2021-09-30 09:24:02 +00:00
|
|
|
},
|
2021-11-19 05:42:53 +00:00
|
|
|
DocScript::ClientInsertText(index, s) => {
|
2021-10-04 06:24:35 +00:00
|
|
|
context.read().client_edit_context().insert(index, s).await.unwrap();
|
2021-09-30 09:24:02 +00:00
|
|
|
},
|
2021-11-19 05:42:53 +00:00
|
|
|
DocScript::ClientFormatText(interval, attribute) => {
|
2021-10-05 03:46:56 +00:00
|
|
|
context
|
|
|
|
.read()
|
|
|
|
.client_edit_context()
|
|
|
|
.format(interval, attribute)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
},
|
2021-09-30 09:24:02 +00:00
|
|
|
DocScript::AssertClient(s) => {
|
2021-10-05 02:19:43 +00:00
|
|
|
sleep(Duration::from_millis(100)).await;
|
2021-10-04 06:24:35 +00:00
|
|
|
let json = context.read().client_edit_context().doc_json().await.unwrap();
|
2021-09-30 09:24:02 +00:00
|
|
|
assert_eq(s, &json);
|
|
|
|
},
|
2021-10-05 03:46:56 +00:00
|
|
|
DocScript::AssertServer(s, rev_id) => {
|
2021-10-05 02:19:43 +00:00
|
|
|
sleep(Duration::from_millis(100)).await;
|
2021-10-05 06:37:45 +00:00
|
|
|
let pg_pool = context.read().server_pg_pool.clone();
|
|
|
|
let doc_manager = context.read().server_doc_manager.clone();
|
2021-10-04 06:24:35 +00:00
|
|
|
let edit_doc = doc_manager.get(&doc_id, pg_pool).await.unwrap().unwrap();
|
2021-09-30 09:24:02 +00:00
|
|
|
let json = edit_doc.document_json().await.unwrap();
|
|
|
|
assert_eq(s, &json);
|
2021-12-03 10:13:13 +00:00
|
|
|
assert_eq!(edit_doc.rev_id().await.unwrap(), rev_id);
|
2021-09-30 09:24:02 +00:00
|
|
|
},
|
2021-11-19 05:42:53 +00:00
|
|
|
DocScript::ServerSaveDocument(json, rev_id) => {
|
2021-10-05 06:37:45 +00:00
|
|
|
let pg_pool = context.read().server_pg_pool.clone();
|
2021-10-04 06:24:35 +00:00
|
|
|
save_doc(&doc_id, json, rev_id, pg_pool).await;
|
|
|
|
},
|
2021-12-05 08:39:41 +00:00
|
|
|
// DocScript::Sleep(sec) => {
|
|
|
|
// sleep(Duration::from_secs(sec)).await;
|
|
|
|
// },
|
2021-09-30 09:24:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
fut_scripts.push(fut);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut stream = stream::iter(fut_scripts);
|
|
|
|
while let Some(script) = stream.next().await {
|
|
|
|
let _ = script.await;
|
2021-09-29 09:40:34 +00:00
|
|
|
}
|
2021-10-04 06:24:35 +00:00
|
|
|
|
|
|
|
std::mem::forget(context);
|
2021-09-29 09:40:34 +00:00
|
|
|
}
|
2021-09-28 07:29:29 +00:00
|
|
|
|
2021-09-29 09:40:34 +00:00
|
|
|
fn assert_eq(expect: &str, receive: &str) {
|
2021-12-07 02:39:01 +00:00
|
|
|
let expected_delta: RichTextDelta = serde_json::from_str(expect).unwrap();
|
|
|
|
let target_delta: RichTextDelta = serde_json::from_str(receive).unwrap();
|
2021-10-06 07:23:38 +00:00
|
|
|
|
|
|
|
if expected_delta != target_delta {
|
|
|
|
log::error!("✅ expect: {}", expect,);
|
|
|
|
log::error!("❌ receive: {}", receive);
|
2021-09-27 15:23:23 +00:00
|
|
|
}
|
2021-10-06 07:23:38 +00:00
|
|
|
assert_eq!(target_delta, expected_delta);
|
2021-09-27 15:23:23 +00:00
|
|
|
}
|
2021-09-28 07:29:29 +00:00
|
|
|
|
2021-10-04 06:24:35 +00:00
|
|
|
async fn create_doc(flowy_test: &FlowyTest) -> String {
|
2021-09-28 07:29:29 +00:00
|
|
|
let view_test = ViewTest::new(flowy_test).await;
|
2021-12-02 13:54:21 +00:00
|
|
|
view_test.view.id
|
2021-10-04 06:24:35 +00:00
|
|
|
}
|
2021-09-28 07:29:29 +00:00
|
|
|
|
2021-10-04 06:24:35 +00:00
|
|
|
async fn save_doc(doc_id: &str, json: String, rev_id: i64, pool: Data<PgPool>) {
|
|
|
|
let mut params = UpdateDocParams::new();
|
|
|
|
params.set_doc_id(doc_id.to_owned());
|
|
|
|
params.set_data(json);
|
|
|
|
params.set_rev_id(rev_id);
|
|
|
|
let _ = update_doc(pool.get_ref(), params).await.unwrap();
|
2021-09-28 07:29:29 +00:00
|
|
|
}
|