2021-12-08 09:33:22 +00:00
|
|
|
use crate::{helper::ViewTest, FlowySDKTest};
|
2021-12-11 05:47:16 +00:00
|
|
|
use flowy_collaboration::entities::{
|
2021-12-09 13:39:53 +00:00
|
|
|
doc::DocIdentifier,
|
|
|
|
ws::{WsDocumentData, WsDocumentDataBuilder},
|
|
|
|
};
|
2021-12-15 08:28:18 +00:00
|
|
|
use flowy_document::services::doc::{edit::ClientDocEditor, revision::RevisionIterator, SYNC_INTERVAL_IN_MILLIS};
|
|
|
|
|
2021-12-09 11:01:58 +00:00
|
|
|
use lib_ot::{
|
|
|
|
core::Interval,
|
2021-12-09 13:39:53 +00:00
|
|
|
revision::{RevState, RevType, Revision, RevisionRange},
|
2021-12-09 11:01:58 +00:00
|
|
|
rich_text::RichTextDelta,
|
|
|
|
};
|
2021-12-09 14:28:11 +00:00
|
|
|
use std::sync::Arc;
|
2021-12-09 03:00:05 +00:00
|
|
|
use tokio::time::{sleep, Duration};
|
2021-12-08 09:33:22 +00:00
|
|
|
|
2021-12-09 13:39:53 +00:00
|
|
|
pub enum EditorScript {
|
2021-12-15 08:28:18 +00:00
|
|
|
StartWs,
|
|
|
|
StopWs,
|
2021-12-09 13:39:53 +00:00
|
|
|
InsertText(&'static str, usize),
|
|
|
|
Delete(Interval),
|
|
|
|
Replace(Interval, &'static str),
|
|
|
|
Undo(),
|
|
|
|
Redo(),
|
2021-12-15 08:28:18 +00:00
|
|
|
WaitSyncFinished,
|
2021-12-09 13:39:53 +00:00
|
|
|
SimulatePushRevisionMessageWithDelta(RichTextDelta),
|
|
|
|
SimulatePullRevisionMessage(RevisionRange),
|
|
|
|
SimulateAckedMessage(i64),
|
|
|
|
AssertRevisionState(i64, RevState),
|
2021-12-15 08:28:18 +00:00
|
|
|
AssertNextRevId(Option<i64>),
|
2021-12-09 13:39:53 +00:00
|
|
|
AssertCurrentRevId(i64),
|
|
|
|
AssertJson(&'static str),
|
|
|
|
}
|
|
|
|
|
2021-12-08 09:33:22 +00:00
|
|
|
pub struct EditorTest {
|
|
|
|
pub sdk: FlowySDKTest,
|
2021-12-09 03:00:05 +00:00
|
|
|
pub editor: Arc<ClientDocEditor>,
|
2021-12-08 09:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EditorTest {
|
|
|
|
pub async fn new() -> Self {
|
|
|
|
let sdk = FlowySDKTest::setup();
|
|
|
|
let _ = sdk.init_user().await;
|
2021-12-09 03:00:05 +00:00
|
|
|
let test = ViewTest::new(&sdk).await;
|
|
|
|
let doc_identifier: DocIdentifier = test.view.id.clone().into();
|
|
|
|
let editor = sdk.flowy_document.open(doc_identifier).await.unwrap();
|
|
|
|
Self { sdk, editor }
|
2021-12-08 09:33:22 +00:00
|
|
|
}
|
|
|
|
|
2021-12-09 03:00:05 +00:00
|
|
|
pub async fn run_scripts(mut self, scripts: Vec<EditorScript>) {
|
|
|
|
for script in scripts {
|
|
|
|
self.run_script(script).await;
|
|
|
|
}
|
|
|
|
|
2021-12-15 08:28:18 +00:00
|
|
|
sleep(Duration::from_secs(5)).await;
|
2021-12-09 03:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn run_script(&mut self, script: EditorScript) {
|
|
|
|
let rev_manager = self.editor.rev_manager();
|
|
|
|
let cache = rev_manager.revision_cache();
|
2021-12-09 11:01:58 +00:00
|
|
|
let _memory_cache = cache.memory_cache();
|
|
|
|
let _disk_cache = cache.dish_cache();
|
|
|
|
let doc_id = self.editor.doc_id.clone();
|
2021-12-09 14:28:11 +00:00
|
|
|
let user_id = self.sdk.user_session.user_id().unwrap();
|
2021-12-15 08:28:18 +00:00
|
|
|
let ws_manager = self.sdk.ws_manager.clone();
|
|
|
|
let token = self.sdk.user_session.token().unwrap();
|
2021-12-09 03:00:05 +00:00
|
|
|
|
|
|
|
match script {
|
2021-12-15 08:28:18 +00:00
|
|
|
EditorScript::StartWs => {
|
|
|
|
ws_manager.start(token.clone()).await.unwrap();
|
|
|
|
},
|
|
|
|
EditorScript::StopWs => {
|
|
|
|
sleep(Duration::from_millis(SYNC_INTERVAL_IN_MILLIS)).await;
|
|
|
|
ws_manager.stop().await;
|
|
|
|
},
|
2021-12-09 03:00:05 +00:00
|
|
|
EditorScript::InsertText(s, offset) => {
|
|
|
|
self.editor.insert(offset, s).await.unwrap();
|
2021-12-15 08:28:18 +00:00
|
|
|
sleep(Duration::from_millis(SYNC_INTERVAL_IN_MILLIS)).await;
|
2021-12-09 03:00:05 +00:00
|
|
|
},
|
|
|
|
EditorScript::Delete(interval) => {
|
|
|
|
self.editor.delete(interval).await.unwrap();
|
|
|
|
},
|
|
|
|
EditorScript::Replace(interval, s) => {
|
|
|
|
self.editor.replace(interval, s).await.unwrap();
|
|
|
|
},
|
|
|
|
EditorScript::Undo() => {
|
|
|
|
self.editor.undo().await.unwrap();
|
|
|
|
},
|
|
|
|
EditorScript::Redo() => {
|
|
|
|
self.editor.redo().await.unwrap();
|
|
|
|
},
|
2021-12-15 08:28:18 +00:00
|
|
|
EditorScript::WaitSyncFinished => {
|
|
|
|
sleep(Duration::from_millis(1000)).await;
|
|
|
|
},
|
2021-12-09 11:01:58 +00:00
|
|
|
EditorScript::AssertRevisionState(rev_id, state) => {
|
2021-12-13 14:46:35 +00:00
|
|
|
let record = cache.query_revision(&doc_id, rev_id).await.unwrap();
|
2021-12-09 11:01:58 +00:00
|
|
|
assert_eq!(record.state, state);
|
|
|
|
},
|
|
|
|
EditorScript::AssertCurrentRevId(rev_id) => {
|
2021-12-09 03:00:05 +00:00
|
|
|
assert_eq!(self.editor.rev_manager().rev_id(), rev_id);
|
|
|
|
},
|
2021-12-15 08:28:18 +00:00
|
|
|
EditorScript::AssertNextRevId(rev_id) => {
|
2021-12-09 11:01:58 +00:00
|
|
|
let next_revision = cache.next().await.unwrap();
|
|
|
|
if rev_id.is_none() {
|
|
|
|
assert_eq!(next_revision.is_none(), true);
|
2021-12-15 08:28:18 +00:00
|
|
|
return;
|
2021-12-09 11:01:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let next_revision = next_revision.unwrap();
|
|
|
|
assert_eq!(next_revision.revision.rev_id, rev_id.unwrap());
|
|
|
|
},
|
2021-12-09 13:39:53 +00:00
|
|
|
EditorScript::SimulatePushRevisionMessageWithDelta(delta) => {
|
|
|
|
let local_base_rev_id = rev_manager.rev_id();
|
|
|
|
let local_rev_id = local_base_rev_id + 1;
|
|
|
|
let revision = Revision::new(
|
|
|
|
local_base_rev_id,
|
|
|
|
local_rev_id,
|
|
|
|
delta.to_bytes().to_vec(),
|
|
|
|
&doc_id,
|
|
|
|
RevType::Remote,
|
2021-12-09 14:28:11 +00:00
|
|
|
user_id,
|
2021-12-09 13:39:53 +00:00
|
|
|
);
|
|
|
|
let data = WsDocumentDataBuilder::build_push_rev_message(&doc_id, revision);
|
|
|
|
self.send_ws_message(data).await;
|
|
|
|
},
|
2021-12-09 11:01:58 +00:00
|
|
|
EditorScript::SimulatePullRevisionMessage(_range) => {},
|
|
|
|
EditorScript::SimulateAckedMessage(i64) => {
|
|
|
|
let data = WsDocumentDataBuilder::build_acked_message(&doc_id, i64);
|
2021-12-09 13:39:53 +00:00
|
|
|
self.send_ws_message(data).await;
|
2021-12-09 11:01:58 +00:00
|
|
|
},
|
2021-12-09 03:00:05 +00:00
|
|
|
EditorScript::AssertJson(expected) => {
|
|
|
|
let expected_delta: RichTextDelta = serde_json::from_str(expected).unwrap();
|
|
|
|
let delta = self.editor.doc_delta().await.unwrap();
|
|
|
|
|
|
|
|
if expected_delta != delta {
|
|
|
|
eprintln!("✅ expect: {}", expected,);
|
|
|
|
eprintln!("❌ receive: {}", delta.to_json());
|
|
|
|
}
|
|
|
|
assert_eq!(expected_delta, delta);
|
|
|
|
},
|
|
|
|
}
|
2021-12-08 09:33:22 +00:00
|
|
|
}
|
2021-12-08 13:51:06 +00:00
|
|
|
|
2021-12-09 13:39:53 +00:00
|
|
|
async fn send_ws_message(&self, data: WsDocumentData) {
|
|
|
|
self.editor.handle_ws_message(data).await.unwrap();
|
|
|
|
sleep(Duration::from_millis(200)).await;
|
|
|
|
}
|
2021-12-08 13:51:06 +00:00
|
|
|
}
|