2021-09-27 15:23:23 +00:00
|
|
|
use crate::document::helper::{DocScript, DocumentTest};
|
2021-11-19 05:16:14 +00:00
|
|
|
use flowy_document_infra::core::{Document, FlowyDoc};
|
2021-10-05 03:46:56 +00:00
|
|
|
use flowy_ot::core::{Attribute, Interval};
|
2021-09-27 15:23:23 +00:00
|
|
|
|
2021-10-04 13:53:06 +00:00
|
|
|
#[rustfmt::skip]
|
|
|
|
// ┌─────────┐ ┌─────────┐
|
|
|
|
// │ Server │ │ Client │
|
|
|
|
// └─────────┘ └─────────┘
|
|
|
|
// ┌────────────────┐ │ │ ┌────────────────┐
|
|
|
|
// │ops: [] rev: 0 │◀┼───── ws ────┼─┤ops: [] rev: 0 │
|
|
|
|
// └────────────────┘ │ │ └────────────────┘
|
|
|
|
// ┌────────────────────┐ │ │ ┌────────────────────┐
|
|
|
|
// │ops: ["abc"] rev: 1 │◀┼───── ws ────┼─│ops: ["abc"] rev: 1 │
|
|
|
|
// └────────────────────┘ │ │ └────────────────────┘
|
|
|
|
// ┌──────────────────────────┐ │ │ ┌──────────────────────┐
|
|
|
|
// │ops: ["abc", "123"] rev: 2│◀┼───── ws ────┼─│ops: ["123"] rev: 2 │
|
|
|
|
// └──────────────────────────┘ │ │ └──────────────────────┘
|
|
|
|
// │ │
|
2021-09-27 15:23:23 +00:00
|
|
|
#[actix_rt::test]
|
2021-10-05 03:46:56 +00:00
|
|
|
async fn delta_sync_while_editing() {
|
2021-09-27 15:23:23 +00:00
|
|
|
let test = DocumentTest::new().await;
|
2021-09-29 09:40:34 +00:00
|
|
|
test.run_scripts(vec![
|
2021-11-19 05:42:53 +00:00
|
|
|
DocScript::ClientConnectWs,
|
|
|
|
DocScript::ClientOpenDoc,
|
|
|
|
DocScript::ClientInsertText(0, "abc"),
|
|
|
|
DocScript::ClientInsertText(3, "123"),
|
2021-10-04 13:53:06 +00:00
|
|
|
DocScript::AssertClient(r#"[{"insert":"abc123\n"}]"#),
|
2021-10-05 03:46:56 +00:00
|
|
|
DocScript::AssertServer(r#"[{"insert":"abc123\n"}]"#, 2),
|
|
|
|
])
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
|
2021-10-07 12:46:29 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn delta_sync_multi_revs() {
|
|
|
|
let test = DocumentTest::new().await;
|
|
|
|
test.run_scripts(vec![
|
2021-11-19 05:42:53 +00:00
|
|
|
DocScript::ClientConnectWs,
|
|
|
|
DocScript::ClientOpenDoc,
|
|
|
|
DocScript::ClientInsertText(0, "abc"),
|
|
|
|
DocScript::ClientInsertText(3, "123"),
|
|
|
|
DocScript::ClientInsertText(6, "efg"),
|
|
|
|
DocScript::ClientInsertText(9, "456"),
|
2021-10-07 12:46:29 +00:00
|
|
|
])
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
|
2021-10-05 03:46:56 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn delta_sync_while_editing_with_attribute() {
|
|
|
|
let test = DocumentTest::new().await;
|
|
|
|
test.run_scripts(vec![
|
2021-11-19 05:42:53 +00:00
|
|
|
DocScript::ClientConnectWs,
|
|
|
|
DocScript::ClientOpenDoc,
|
|
|
|
DocScript::ClientInsertText(0, "abc"),
|
|
|
|
DocScript::ClientFormatText(Interval::new(0, 3), Attribute::Bold(true)),
|
2021-10-05 03:46:56 +00:00
|
|
|
DocScript::AssertClient(r#"[{"insert":"abc","attributes":{"bold":true}},{"insert":"\n"}]"#),
|
|
|
|
DocScript::AssertServer(r#"[{"insert":"abc","attributes":{"bold":true}},{"insert":"\n"}]"#, 2),
|
2021-11-19 05:42:53 +00:00
|
|
|
DocScript::ClientInsertText(3, "efg"),
|
|
|
|
DocScript::ClientFormatText(Interval::new(3, 5), Attribute::Italic(true)),
|
2021-10-05 03:46:56 +00:00
|
|
|
DocScript::AssertClient(r#"[{"insert":"abc","attributes":{"bold":true}},{"insert":"ef","attributes":{"bold":true,"italic":true}},{"insert":"g","attributes":{"bold":true}},{"insert":"\n"}]"#),
|
|
|
|
DocScript::AssertServer(r#"[{"insert":"abc","attributes":{"bold":true}},{"insert":"ef","attributes":{"bold":true,"italic":true}},{"insert":"g","attributes":{"bold":true}},{"insert":"\n"}]"#, 4),
|
2021-09-29 09:40:34 +00:00
|
|
|
])
|
|
|
|
.await;
|
2021-09-27 15:23:23 +00:00
|
|
|
}
|
2021-09-30 09:24:02 +00:00
|
|
|
|
2021-10-04 13:53:06 +00:00
|
|
|
#[rustfmt::skip]
|
|
|
|
// ┌─────────┐ ┌─────────┐
|
|
|
|
// │ Server │ │ Client │
|
|
|
|
// └─────────┘ └─────────┘
|
|
|
|
// ┌──────────────────────────┐ │ │
|
|
|
|
// │ops: ["123", "456"] rev: 2│ │ │
|
|
|
|
// └──────────────────────────┘ │ │
|
|
|
|
// │ │
|
|
|
|
// ◀── http request ─┤ Open doc
|
|
|
|
// │ │
|
|
|
|
// │ │ ┌──────────────────────────┐
|
|
|
|
// ├──http response──┼─▶│ops: ["123", "456"] rev: 2│
|
|
|
|
// │ │ └──────────────────────────┘
|
2021-09-30 09:24:02 +00:00
|
|
|
#[actix_rt::test]
|
2021-10-04 13:53:06 +00:00
|
|
|
async fn delta_sync_with_http_request() {
|
2021-09-30 09:24:02 +00:00
|
|
|
let test = DocumentTest::new().await;
|
2021-10-04 06:24:35 +00:00
|
|
|
let mut document = Document::new::<FlowyDoc>();
|
|
|
|
document.insert(0, "123").unwrap();
|
|
|
|
document.insert(3, "456").unwrap();
|
|
|
|
let json = document.to_json();
|
2021-09-30 09:24:02 +00:00
|
|
|
|
|
|
|
test.run_scripts(vec![
|
2021-11-19 05:42:53 +00:00
|
|
|
DocScript::ServerSaveDocument(json, 3),
|
|
|
|
DocScript::ClientOpenDoc,
|
2021-10-04 06:24:35 +00:00
|
|
|
DocScript::AssertClient(r#"[{"insert":"123456\n"}]"#),
|
2021-10-05 03:46:56 +00:00
|
|
|
DocScript::AssertServer(r#"[{"insert":"123456\n"}]"#, 3),
|
2021-10-04 06:24:35 +00:00
|
|
|
])
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
2021-10-04 13:53:06 +00:00
|
|
|
async fn delta_sync_with_server_push_delta() {
|
2021-10-04 06:24:35 +00:00
|
|
|
let test = DocumentTest::new().await;
|
|
|
|
let mut document = Document::new::<FlowyDoc>();
|
|
|
|
document.insert(0, "123").unwrap();
|
|
|
|
let json = document.to_json();
|
|
|
|
|
|
|
|
test.run_scripts(vec![
|
2021-11-19 05:42:53 +00:00
|
|
|
DocScript::ClientOpenDoc,
|
|
|
|
DocScript::ServerSaveDocument(json, 3),
|
|
|
|
DocScript::ClientConnectWs,
|
2021-10-04 09:38:56 +00:00
|
|
|
DocScript::AssertClient(r#"[{"insert":"\n123\n"}]"#),
|
2021-10-04 06:24:35 +00:00
|
|
|
])
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
|
2021-10-04 13:53:06 +00:00
|
|
|
#[rustfmt::skip]
|
|
|
|
// ┌─────────┐ ┌─────────┐
|
|
|
|
// │ Server │ │ Client │
|
|
|
|
// └─────────┘ └─────────┘
|
|
|
|
// │ │
|
|
|
|
// │ │
|
|
|
|
// ◀── http request ─┤ Open doc
|
|
|
|
// │ │
|
|
|
|
// │ │ ┌───────────────┐
|
|
|
|
// ├──http response──┼─▶│ops: [] rev: 0 │
|
|
|
|
// ┌───────────────────┐│ │ └───────────────┘
|
|
|
|
// │ops: ["123"] rev: 3││ │
|
|
|
|
// └───────────────────┘│ │ ┌────────────────────┐
|
|
|
|
// │ │ │ops: ["abc"] rev: 1 │
|
|
|
|
// │ │ └────────────────────┘
|
|
|
|
// │ │
|
|
|
|
// ◀─────────────────┤ start ws connection
|
|
|
|
// │ │
|
|
|
|
// ◀─────────────────┤ notify with rev: 1
|
2021-10-05 02:19:43 +00:00
|
|
|
// ┌───────────────────┐ │ │
|
|
|
|
// │ops: ["123"] rev: 3│ ├────Push Rev─────▶ transform
|
|
|
|
// └───────────────────┘ │ │ ┌──────────────────────────┐
|
|
|
|
// │ │ │ops: ["abc", "123"] rev: 4│
|
|
|
|
// │ │ └──────────────────────────┘
|
|
|
|
// │ │ ┌────────────────────────────────┐
|
|
|
|
// compose ◀────Push Rev─────┤ │ops: ["abc", "retain 3"] rev: 4 │
|
|
|
|
// │ │ └────────────────────────────────┘
|
|
|
|
// ┌──────────────────────────┐ │
|
|
|
|
// │ops: ["abc", "123"] rev: 4│ │
|
|
|
|
// └──────────────────────────┘ │
|
|
|
|
// │ │
|
2021-10-04 06:24:35 +00:00
|
|
|
#[actix_rt::test]
|
2021-10-04 13:53:06 +00:00
|
|
|
async fn delta_sync_while_local_rev_less_than_server_rev() {
|
2021-10-04 06:24:35 +00:00
|
|
|
let test = DocumentTest::new().await;
|
|
|
|
let mut document = Document::new::<FlowyDoc>();
|
|
|
|
document.insert(0, "123").unwrap();
|
|
|
|
let json = document.to_json();
|
|
|
|
|
|
|
|
test.run_scripts(vec![
|
2021-11-19 05:42:53 +00:00
|
|
|
DocScript::ClientOpenDoc,
|
|
|
|
DocScript::ServerSaveDocument(json, 3),
|
|
|
|
DocScript::ClientInsertText(0, "abc"),
|
|
|
|
DocScript::ClientConnectWs,
|
2021-10-04 09:38:56 +00:00
|
|
|
DocScript::AssertClient(r#"[{"insert":"abc\n123\n"}]"#),
|
2021-10-05 03:46:56 +00:00
|
|
|
DocScript::AssertServer(r#"[{"insert":"abc\n123\n"}]"#, 4),
|
2021-09-30 09:24:02 +00:00
|
|
|
])
|
|
|
|
.await;
|
|
|
|
}
|
2021-10-04 13:53:06 +00:00
|
|
|
|
2021-10-05 02:19:43 +00:00
|
|
|
#[rustfmt::skip]
|
|
|
|
// ┌─────────┐ ┌─────────┐
|
|
|
|
// │ Server │ │ Client │
|
|
|
|
// └─────────┘ └─────────┘
|
|
|
|
// ┌───────────────────┐ │ │
|
|
|
|
// │ops: ["123"] rev: 1│ │ │
|
|
|
|
// └───────────────────┘ │ │
|
|
|
|
// ◀── http request ─┤ Open doc
|
|
|
|
// │ │
|
|
|
|
// │ │ ┌───────────────┐
|
|
|
|
// ├──http response──┼──▶│ops: [123] rev:│
|
|
|
|
// │ │ └───────────────┘
|
|
|
|
// │ │ ┌──────────────────────────────────┐
|
|
|
|
// │ │ │ops: ["123","abc", "efg"] rev: 3 │
|
|
|
|
// │ │ └──────────────────────────────────┘
|
|
|
|
// ◀─────────────────┤ start ws connection
|
|
|
|
// │ │
|
|
|
|
// ◀─────────────────┤ notify with rev: 3
|
|
|
|
// │ │
|
|
|
|
// ├────Pull Rev─────▶
|
|
|
|
// │ │ ┌──────────────────────────────────┐
|
|
|
|
// compose ◀────Push Rev─────┤ │ops: ["retain 3", "abcefg"] rev: 3│
|
|
|
|
// ┌──────────────────────────────────┐│ │ └──────────────────────────────────┘
|
|
|
|
// │ops: ["123","abc", "efg"] rev: 3 ││ │
|
|
|
|
// └──────────────────────────────────┘│ │
|
2021-10-04 13:53:06 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn delta_sync_while_local_rev_greater_than_server_rev() {
|
|
|
|
let test = DocumentTest::new().await;
|
|
|
|
let mut document = Document::new::<FlowyDoc>();
|
|
|
|
document.insert(0, "123").unwrap();
|
|
|
|
let json = document.to_json();
|
|
|
|
|
|
|
|
test.run_scripts(vec![
|
2021-11-19 05:42:53 +00:00
|
|
|
DocScript::ServerSaveDocument(json, 1),
|
|
|
|
DocScript::ClientOpenDoc,
|
2021-10-04 13:53:06 +00:00
|
|
|
DocScript::AssertClient(r#"[{"insert":"123\n"}]"#),
|
2021-11-19 05:42:53 +00:00
|
|
|
DocScript::ClientInsertText(3, "abc"),
|
|
|
|
DocScript::ClientInsertText(6, "efg"),
|
|
|
|
DocScript::ClientConnectWs,
|
2021-10-04 13:53:06 +00:00
|
|
|
DocScript::AssertClient(r#"[{"insert":"123abcefg\n"}]"#),
|
2021-11-19 05:42:53 +00:00
|
|
|
// DocScript::AssertServer(r#"[{"insert":"123abcefg\n"}]"#, 3),
|
2021-10-04 13:53:06 +00:00
|
|
|
])
|
|
|
|
.await;
|
|
|
|
}
|