2021-10-01 11:39:08 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2021-09-29 09:40:34 +00:00
|
|
|
use actix_web::web::Data;
|
2021-10-01 11:39:08 +00:00
|
|
|
use futures_util::{stream, stream::StreamExt};
|
|
|
|
use sqlx::PgPool;
|
|
|
|
use tokio::time::{sleep, Duration};
|
|
|
|
|
2021-09-30 09:24:02 +00:00
|
|
|
use backend::service::doc::doc::DocManager;
|
2021-10-02 09:19:54 +00:00
|
|
|
use flowy_document::{entities::doc::QueryDocParams, services::doc::edit::ClientEditDoc as ClientEditDocContext};
|
2021-09-28 07:29:29 +00:00
|
|
|
use flowy_net::config::ServerConfig;
|
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-10-01 11:39:08 +00:00
|
|
|
|
|
|
|
// use crate::helper::*;
|
|
|
|
use crate::helper::{spawn_server, TestServer};
|
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-09-30 09:24:02 +00:00
|
|
|
ConnectWs,
|
2021-09-29 09:40:34 +00:00
|
|
|
SendText(usize, &'static str),
|
|
|
|
AssertClient(&'static str),
|
|
|
|
AssertServer(&'static str),
|
2021-09-27 15:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DocumentTest {
|
|
|
|
pub async fn new() -> Self {
|
|
|
|
let server = spawn_server().await;
|
2021-09-28 07:29:29 +00:00
|
|
|
let server_config = ServerConfig::new(&server.host, "http", "ws");
|
|
|
|
let flowy_test = FlowyTest::setup_with(server_config);
|
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-09-30 09:24:02 +00:00
|
|
|
let script_context = ScriptContext {
|
|
|
|
client_edit_context: create_doc(&flowy_test).await,
|
|
|
|
user_session: flowy_test.sdk.user_session.clone(),
|
|
|
|
doc_manager: server.app_ctx.doc_biz.manager.clone(),
|
|
|
|
pool: Data::new(server.pg_pool.clone()),
|
|
|
|
};
|
|
|
|
|
|
|
|
run_scripts(script_context, scripts).await;
|
|
|
|
std::mem::forget(flowy_test);
|
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 {
|
|
|
|
client_edit_context: Arc<ClientEditDocContext>,
|
|
|
|
user_session: Arc<UserSession>,
|
|
|
|
doc_manager: Arc<DocManager>,
|
|
|
|
pool: Data<PgPool>,
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn run_scripts(context: ScriptContext, scripts: Vec<DocScript>) {
|
|
|
|
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 {
|
|
|
|
match script {
|
|
|
|
DocScript::ConnectWs => {
|
|
|
|
let token = context.user_session.token().unwrap();
|
|
|
|
let _ = context.user_session.start_ws_connection(&token).await.unwrap();
|
|
|
|
},
|
|
|
|
DocScript::SendText(index, s) => {
|
2021-10-01 11:39:08 +00:00
|
|
|
context.client_edit_context.insert(index, s).await.unwrap();
|
2021-09-30 09:24:02 +00:00
|
|
|
},
|
|
|
|
DocScript::AssertClient(s) => {
|
2021-10-01 11:39:08 +00:00
|
|
|
let json = context.client_edit_context.doc_json().await.unwrap();
|
2021-09-30 09:24:02 +00:00
|
|
|
assert_eq(s, &json);
|
|
|
|
},
|
|
|
|
DocScript::AssertServer(s) => {
|
|
|
|
let edit_doc = context
|
|
|
|
.doc_manager
|
|
|
|
.get(&context.client_edit_context.doc_id, context.pool)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
let json = edit_doc.document_json().await.unwrap();
|
|
|
|
assert_eq(s, &json);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
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-09-28 07:29:29 +00:00
|
|
|
|
2021-09-29 09:40:34 +00:00
|
|
|
fn assert_eq(expect: &str, receive: &str) {
|
|
|
|
if expect != receive {
|
|
|
|
log::error!("expect: {}", expect);
|
|
|
|
log::error!("but receive: {}", receive);
|
2021-09-27 15:23:23 +00:00
|
|
|
}
|
2021-09-29 09:40:34 +00:00
|
|
|
assert_eq!(expect, receive);
|
2021-09-27 15:23:23 +00:00
|
|
|
}
|
2021-09-28 07:29:29 +00:00
|
|
|
|
2021-09-29 09:40:34 +00:00
|
|
|
async fn create_doc(flowy_test: &FlowyTest) -> Arc<ClientEditDocContext> {
|
2021-09-28 07:29:29 +00:00
|
|
|
let view_test = ViewTest::new(flowy_test).await;
|
|
|
|
let doc_id = view_test.view.id.clone();
|
|
|
|
let user_session = flowy_test.sdk.user_session.clone();
|
|
|
|
let flowy_document = flowy_test.sdk.flowy_document.clone();
|
|
|
|
|
|
|
|
let edit_context = flowy_document
|
|
|
|
.open(QueryDocParams { doc_id }, user_session.db_pool().unwrap())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
edit_context
|
|
|
|
}
|