AppFlowy/backend/tests/document/helper.rs

120 lines
3.8 KiB
Rust
Raw Normal View History

use std::sync::Arc;
use actix_web::web::Data;
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;
use flowy_document::{entities::doc::QueryDocParams, services::doc::edit::ClientEditDoc as ClientEditDocContext};
use flowy_net::config::ServerConfig;
use flowy_test::{workspace::ViewTest, FlowyTest};
2021-09-30 09:24:02 +00:00
use flowy_user::services::user::UserSession;
// use crate::helper::*;
use crate::helper::{spawn_server, TestServer};
2021-09-27 15:23:23 +00:00
pub struct DocumentTest {
server: TestServer,
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,
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;
let server_config = ServerConfig::new(&server.host, "http", "ws");
let flowy_test = FlowyTest::setup_with(server_config);
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;
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);
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![];
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) => {
context.client_edit_context.insert(index, s).await.unwrap();
2021-09-30 09:24:02 +00:00
},
DocScript::AssertClient(s) => {
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;
}
}
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
}
assert_eq!(expect, receive);
2021-09-27 15:23:23 +00:00
}
async fn create_doc(flowy_test: &FlowyTest) -> Arc<ClientEditDocContext> {
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
}