AppFlowy/backend/tests/document_test/edit_script.rs

181 lines
6.8 KiB
Rust
Raw Normal View History

2021-12-04 15:54:14 +00:00
#![allow(clippy::all)]
#![cfg_attr(rustfmt, rustfmt::skip)]
2021-12-27 03:15:15 +00:00
use std::convert::TryInto;
use actix_web::web::Data;
2022-01-04 07:05:52 +00:00
use flowy_document::core::edit::ClientDocumentEditor;
2021-12-08 09:33:22 +00:00
use flowy_test::{helper::ViewTest, FlowySDKTest};
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 std::sync::Arc;
2021-12-28 16:34:00 +00:00
use bytes::Bytes;
2021-11-13 03:11:24 +00:00
use tokio::time::{sleep, Duration};
2021-11-19 05:42:53 +00:00
use crate::util::helper::{spawn_server, TestServer};
2022-01-04 02:47:32 +00:00
use flowy_collaboration::{entities::doc::DocumentId, protobuf::ResetDocumentParams as ResetDocumentParamsPB};
2021-12-07 11:59:08 +00:00
use lib_ot::rich_text::{RichTextAttribute, RichTextDelta};
use parking_lot::RwLock;
2022-01-02 14:23:33 +00:00
use backend::services::document::persistence::{read_document, reset_document};
2022-01-01 15:09:13 +00:00
use flowy_collaboration::entities::revision::{RepeatedRevision, Revision};
2022-01-04 02:47:32 +00:00
use flowy_collaboration::protobuf::{RepeatedRevision as RepeatedRevisionPB, DocumentId as DocumentIdPB};
2022-01-02 14:23:33 +00:00
use flowy_collaboration::sync::ServerDocumentManager;
2021-12-07 11:59:08 +00:00
use lib_ot::core::Interval;
2021-12-27 03:15:15 +00:00
use flowy_net::services::ws::FlowyWSConnect;
2021-12-28 16:34:00 +00:00
2021-09-27 15:23:23 +00:00
pub struct DocumentTest {
server: TestServer,
2021-12-08 09:33:22 +00:00
flowy_test: FlowySDKTest,
2021-09-27 15:23:23 +00:00
}
#[derive(Clone)]
pub enum DocScript {
2021-11-19 05:42:53 +00:00
ClientInsertText(usize, &'static str),
2021-12-07 02:39:01 +00:00
ClientFormatText(Interval, RichTextAttribute),
2021-11-19 05:42:53 +00:00
ClientOpenDoc,
AssertClient(&'static str),
2021-10-05 03:46:56 +00:00
AssertServer(&'static str, i64),
2022-01-03 04:20:06 +00:00
ServerResetDocument(String, i64), // delta_json, rev_id
}
2021-09-27 15:23:23 +00:00
impl DocumentTest {
pub async fn new() -> Self {
let server = spawn_server().await;
2021-12-08 09:33:22 +00:00
let flowy_test = FlowySDKTest::setup_with(server.client_server_config.clone());
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;
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;
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 {
2022-01-02 02:34:42 +00:00
client_editor: Option<Arc<ClientDocumentEditor>>,
2021-12-08 09:33:22 +00:00
client_sdk: FlowySDKTest,
2021-10-05 06:37:45 +00:00
client_user_session: Arc<UserSession>,
2021-12-27 03:15:15 +00:00
ws_conn: Arc<FlowyWSConnect>,
server: TestServer,
doc_id: String,
}
impl ScriptContext {
2021-12-08 09:33:22 +00:00
async fn new(client_sdk: FlowySDKTest, server: TestServer) -> Self {
let user_session = client_sdk.user_session.clone();
2021-12-14 07:31:44 +00:00
let ws_manager = client_sdk.ws_manager.clone();
2021-12-08 09:33:22 +00:00
let doc_id = create_doc(&client_sdk).await;
Self {
2022-01-02 02:34:42 +00:00
client_editor: None,
2021-12-08 09:33:22 +00:00
client_sdk,
2021-10-05 06:37:45 +00:00
client_user_session: user_session,
2021-12-27 03:15:15 +00:00
ws_conn: ws_manager,
server,
doc_id,
}
}
async fn open_doc(&mut self) {
let doc_id = self.doc_id.clone();
2022-01-01 15:09:13 +00:00
let edit_context = self.client_sdk.document_ctx.controller.open(doc_id).await.unwrap();
2022-01-02 02:34:42 +00:00
self.client_editor = Some(edit_context);
}
2022-01-02 02:34:42 +00:00
fn client_editor(&self) -> Arc<ClientDocumentEditor> { self.client_editor.as_ref().unwrap().clone() }
}
async fn run_scripts(context: Arc<RwLock<ScriptContext>>, scripts: Vec<DocScript>) {
2021-09-30 09:24:02 +00:00
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 {
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::ClientOpenDoc => {
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) => {
2022-01-02 02:34:42 +00:00
context.read().client_editor().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()
2022-01-02 02:34:42 +00:00
.client_editor()
2021-10-05 03:46:56 +00:00
.format(interval, attribute)
.await
.unwrap();
},
2021-09-30 09:24:02 +00:00
DocScript::AssertClient(s) => {
2021-12-15 08:28:18 +00:00
sleep(Duration::from_millis(2000)).await;
2022-01-02 02:34:42 +00:00
let json = context.read().client_editor().doc_json().await.unwrap();
2021-09-30 09:24:02 +00:00
assert_eq(s, &json);
},
2021-12-20 12:59:33 +00:00
DocScript::AssertServer(s, rev_id) => {
2022-01-02 02:34:42 +00:00
sleep(Duration::from_millis(2000)).await;
2021-12-28 16:34:00 +00:00
let persistence = Data::new(context.read().server.app_ctx.persistence.kv_store());
2022-01-04 02:47:32 +00:00
let doc_identifier: DocumentIdPB = DocumentId {
2021-12-28 16:34:00 +00:00
doc_id
}.try_into().unwrap();
2021-12-27 03:15:15 +00:00
2021-12-28 16:34:00 +00:00
let document_info = read_document(persistence.get_ref(), doc_identifier).await.unwrap();
assert_eq(s, &document_info.text);
assert_eq!(document_info.rev_id, rev_id);
2021-09-30 09:24:02 +00:00
},
2022-01-03 04:20:06 +00:00
DocScript::ServerResetDocument(document_json, rev_id) => {
2021-12-28 16:34:00 +00:00
let delta_data = Bytes::from(document_json);
let user_id = context.read().client_user_session.user_id().unwrap();
let md5 = format!("{:x}", md5::compute(&delta_data));
let base_rev_id = if rev_id == 0 { rev_id } else { rev_id - 1 };
let revision = Revision::new(
&doc_id,
base_rev_id,
rev_id,
delta_data,
&user_id,
md5,
);
2022-01-02 14:23:33 +00:00
let document_manager = context.read().server.app_ctx.document_manager.clone();
reset_doc(&doc_id, RepeatedRevision::new(vec![revision]), document_manager.get_ref()).await;
2022-01-02 02:34:42 +00:00
sleep(Duration::from_millis(2000)).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;
}
std::mem::forget(context);
}
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-12-08 09:33:22 +00:00
async fn create_doc(flowy_test: &FlowySDKTest) -> String {
let view_test = ViewTest::new(flowy_test).await;
2021-12-02 13:54:21 +00:00
view_test.view.id
}
2022-01-02 14:23:33 +00:00
async fn reset_doc(doc_id: &str, repeated_revision: RepeatedRevision, document_manager: &Arc<ServerDocumentManager>) {
2022-01-04 02:47:32 +00:00
let pb: RepeatedRevisionPB = repeated_revision.try_into().unwrap();
let mut params = ResetDocumentParamsPB::new();
params.set_doc_id(doc_id.to_owned());
2021-12-27 03:15:15 +00:00
params.set_revisions(pb);
2022-01-02 14:23:33 +00:00
let _ = reset_document(document_manager, params).await.unwrap();
}