2022-03-02 07:15:46 +00:00
|
|
|
use flowy_block::block_editor::ClientBlockEditor;
|
|
|
|
use flowy_block::DOCUMENT_SYNC_INTERVAL_IN_MILLIS;
|
2022-01-01 06:23:58 +00:00
|
|
|
use flowy_collaboration::entities::revision::RevisionState;
|
2022-01-08 10:21:13 +00:00
|
|
|
use flowy_test::{helper::ViewTest, FlowySDKTest};
|
2021-12-22 13:13:52 +00:00
|
|
|
use lib_ot::{core::Interval, 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 {
|
|
|
|
InsertText(&'static str, usize),
|
|
|
|
Delete(Interval),
|
|
|
|
Replace(Interval, &'static str),
|
2021-12-15 15:01:50 +00:00
|
|
|
|
2022-01-01 06:23:58 +00:00
|
|
|
AssertRevisionState(i64, RevisionState),
|
2022-01-26 15:29:18 +00:00
|
|
|
AssertNextSyncRevId(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,
|
2022-02-25 14:27:44 +00:00
|
|
|
pub editor: Arc<ClientBlockEditor>,
|
2021-12-08 09:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EditorTest {
|
|
|
|
pub async fn new() -> Self {
|
2022-01-08 10:21:13 +00:00
|
|
|
let sdk = FlowySDKTest::default();
|
2021-12-08 09:33:22 +00:00
|
|
|
let _ = sdk.init_user().await;
|
2021-12-09 03:00:05 +00:00
|
|
|
let test = ViewTest::new(&sdk).await;
|
2022-02-25 14:27:44 +00:00
|
|
|
let editor = sdk.document_manager.open_block(&test.view.id).await.unwrap();
|
2021-12-09 03:00:05 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn run_script(&mut self, script: EditorScript) {
|
|
|
|
let rev_manager = self.editor.rev_manager();
|
2022-01-25 12:37:48 +00:00
|
|
|
let cache = rev_manager.revision_cache().await;
|
2021-12-15 15:01:50 +00:00
|
|
|
let _user_id = self.sdk.user_session.user_id().unwrap();
|
2022-01-07 09:37:11 +00:00
|
|
|
// let ws_manager = self.sdk.ws_conn.clone();
|
|
|
|
// let token = self.sdk.user_session.token().unwrap();
|
2021-12-09 03:00:05 +00:00
|
|
|
|
|
|
|
match script {
|
|
|
|
EditorScript::InsertText(s, offset) => {
|
|
|
|
self.editor.insert(offset, s).await.unwrap();
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
2021-12-09 03:00:05 +00:00
|
|
|
EditorScript::Delete(interval) => {
|
|
|
|
self.editor.delete(interval).await.unwrap();
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
2021-12-09 03:00:05 +00:00
|
|
|
EditorScript::Replace(interval, s) => {
|
|
|
|
self.editor.replace(interval, s).await.unwrap();
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
2021-12-09 11:01:58 +00:00
|
|
|
EditorScript::AssertRevisionState(rev_id, state) => {
|
2022-01-01 08:16:06 +00:00
|
|
|
let record = cache.get(rev_id).await.unwrap();
|
2021-12-09 11:01:58 +00:00
|
|
|
assert_eq!(record.state, state);
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
2021-12-09 11:01:58 +00:00
|
|
|
EditorScript::AssertCurrentRevId(rev_id) => {
|
2021-12-09 03:00:05 +00:00
|
|
|
assert_eq!(self.editor.rev_manager().rev_id(), rev_id);
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
2022-01-26 15:29:18 +00:00
|
|
|
EditorScript::AssertNextSyncRevId(rev_id) => {
|
2021-12-16 13:31:36 +00:00
|
|
|
let next_revision = rev_manager.next_sync_revision().await.unwrap();
|
2021-12-09 11:01:58 +00:00
|
|
|
if rev_id.is_none() {
|
2022-01-24 09:35:58 +00:00
|
|
|
assert!(next_revision.is_none(), "Next revision should be None");
|
2021-12-15 08:28:18 +00:00
|
|
|
return;
|
2021-12-09 11:01:58 +00:00
|
|
|
}
|
|
|
|
let next_revision = next_revision.unwrap();
|
2022-02-18 15:04:55 +00:00
|
|
|
let mut notify = rev_manager.ack_notify();
|
|
|
|
let _ = notify.recv().await;
|
2021-12-16 13:31:36 +00:00
|
|
|
assert_eq!(next_revision.rev_id, rev_id.unwrap());
|
2022-01-23 04:14:00 +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,);
|
2022-03-01 15:38:26 +00:00
|
|
|
eprintln!("❌ receive: {}", delta.to_delta_json());
|
2021-12-09 03:00:05 +00:00
|
|
|
}
|
|
|
|
assert_eq!(expected_delta, delta);
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
2021-12-09 03:00:05 +00:00
|
|
|
}
|
2022-01-22 10:48:43 +00:00
|
|
|
sleep(Duration::from_millis(DOCUMENT_SYNC_INTERVAL_IN_MILLIS)).await;
|
2021-12-09 13:39:53 +00:00
|
|
|
}
|
2021-12-08 13:51:06 +00:00
|
|
|
}
|