AppFlowy/backend/tests/document_test/edit_test.rs

208 lines
13 KiB
Rust
Raw Normal View History

2021-12-27 03:15:15 +00:00
use crate::document_test::edit_script::{DocScript, DocumentTest};
2022-01-02 02:34:42 +00:00
use flowy_collaboration::document::{Document, NewlineDoc};
2021-12-07 11:59:08 +00:00
use lib_ot::{core::Interval, rich_text::RichTextAttribute};
2021-09-27 15:23:23 +00:00
#[rustfmt::skip]
// ┌─────────┐ ┌─────────┐
// │ Server │ │ Client │
// └─────────┘ └─────────┘
// ┌────────────────┐ │ │ ┌────────────────┐
2022-01-03 04:20:06 +00:00
// │ops: [] rev: 0 │◀┼──── Ping ─────┼─┤ops: [] rev: 0 │
// └────────────────┘ │ │ └────────────────┘
// ┌────────────────────┐ │ │ ┌────────────────────┐
2022-01-03 04:20:06 +00:00
// │ops: ["abc"] rev: 1 │◀┼───ClientPush ───┼─│ops: ["abc"] rev: 1 │
// └────────────────────┘ │ │ └────────────────────┘
// ┌──────────────────────────┐ │ │ ┌──────────────────────┐
2022-01-03 04:20:06 +00:00
// │ops: ["abc", "123"] rev: 2│◀┼── ClientPush ───┼─│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;
test.run_scripts(vec![
2021-11-19 05:42:53 +00:00
DocScript::ClientOpenDoc,
DocScript::ClientInsertText(0, "abc"),
DocScript::ClientInsertText(3, "123"),
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::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::ClientOpenDoc,
DocScript::ClientInsertText(0, "abc"),
2021-12-07 02:39:01 +00:00
DocScript::ClientFormatText(Interval::new(0, 3), RichTextAttribute::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"),
2021-12-07 02:39:01 +00:00
DocScript::ClientFormatText(Interval::new(3, 5), RichTextAttribute::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),
])
.await;
2021-09-27 15:23:23 +00:00
}
2021-09-30 09:24:02 +00:00
#[rustfmt::skip]
// ┌─────────┐ ┌─────────┐
// │ Server │ │ Client │
// └─────────┘ └─────────┘
2022-01-03 04:20:06 +00:00
// ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │
// ops: ["123", "456"] rev: 3│ │ │
// └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │
// │ │
2022-01-03 04:20:06 +00:00
// ◀───── Ping ───┤ Open doc
// │ │
2022-01-03 04:20:06 +00:00
// │ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
// ├───ServerPush────┼─▶ ops: ["123", "456"] rev: 3│
// │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
2021-09-30 09:24:02 +00:00
#[actix_rt::test]
2022-01-03 04:20:06 +00:00
async fn delta_sync_with_server_push() {
2021-09-30 09:24:02 +00:00
let test = DocumentTest::new().await;
2022-01-02 02:34:42 +00:00
let mut document = Document::new::<NewlineDoc>();
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![
2022-01-03 04:20:06 +00:00
DocScript::ServerResetDocument(json, 3),
2021-11-19 05:42:53 +00:00
DocScript::ClientOpenDoc,
DocScript::AssertClient(r#"[{"insert":"123456\n"}]"#),
2021-10-05 03:46:56 +00:00
DocScript::AssertServer(r#"[{"insert":"123456\n"}]"#, 3),
])
.await;
}
2022-01-03 04:20:06 +00:00
#[rustfmt::skip]
// ┌─────────┐ ┌─────────┐
// │ Server │ │ Client │
// └─────────┘ └─────────┘
// ┌ ─ ─ ─ ─ ┐ │ │
// ops: [] │ │
// └ ─ ─ ─ ─ ┘ │ │
// │ │
// ◀───── Ping ───┤ Open doc
// ◀───── Ping ───┤
// ◀───── Ping ───┤
// ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │
// ops: ["123"], rev: 3 │ │
// └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐
// ├────ServerPush───▶ ops: ["123"] rev: 3
// │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘
// │ │
#[actix_rt::test]
2022-01-03 04:20:06 +00:00
async fn delta_sync_with_server_push_after_reset_document() {
let test = DocumentTest::new().await;
2022-01-02 02:34:42 +00:00
let mut document = Document::new::<NewlineDoc>();
document.insert(0, "123").unwrap();
let json = document.to_json();
test.run_scripts(vec![
2021-11-19 05:42:53 +00:00
DocScript::ClientOpenDoc,
2022-01-03 04:20:06 +00:00
DocScript::ServerResetDocument(json, 3),
DocScript::AssertClient(r#"[{"insert":"123\n"}]"#),
DocScript::AssertServer(r#"[{"insert":"123\n"}]"#, 3),
])
.await;
}
#[rustfmt::skip]
// ┌─────────┐ ┌─────────┐
// │ Server │ │ Client │
// └─────────┘ └─────────┘
// │ │
// │ │
2022-01-03 04:20:06 +00:00
// ◀────── Ping ─────┤ Open doc
// ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │
// ops: ["123"] rev: 3 │ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
// └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ │ ops: ["abc"] rev: 1 │
// │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
// │ │ ┌────────────────────┐
// ◀───ClientPush ───┤ │ops: ["abc"] rev: 1 │
// ┌───────────────────┐ │ │ └────────────────────┘
// │ops: ["123"] rev: 3│ ├────ServerPush───▶ transform
// └───────────────────┘ │ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
// │ │ ops: ["abc", "123"] rev: 4│
// │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
// │ │ ┌────────────────────────────────┐
2022-01-03 04:20:06 +00:00
// ◀────ClientPush───┤ │ops: ["retain 3","abc"] rev: 4 │
// ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ └────────────────────────────────┘
// ops: ["abc", "123"] rev: 4│ ├────ServerAck────▶
// └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │
#[actix_rt::test]
async fn delta_sync_while_local_rev_less_than_server_rev() {
let test = DocumentTest::new().await;
2022-01-02 02:34:42 +00:00
let mut document = Document::new::<NewlineDoc>();
document.insert(0, "123").unwrap();
let json = document.to_json();
test.run_scripts(vec![
2021-11-19 05:42:53 +00:00
DocScript::ClientOpenDoc,
2022-01-03 04:20:06 +00:00
DocScript::ServerResetDocument(json, 3),
2021-11-19 05:42:53 +00:00
DocScript::ClientInsertText(0, "abc"),
2022-01-03 04:20:06 +00:00
DocScript::AssertClient(r#"[{"insert":"abc123\n"}]"#),
DocScript::AssertServer(r#"[{"insert":"abc123\n"}]"#, 4),
2021-09-30 09:24:02 +00:00
])
.await;
}
#[rustfmt::skip]
2022-01-03 04:20:06 +00:00
// ┌─────────┐ ┌─────────┐
// │ Server │ │ Client │
// └─────────┘ └─────────┘
// ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │
// ops: ["123"] rev: 1 │ │
// └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ │
// ◀──── Ping ────┤ Open doc
// │ │
// │ │ ┌──────────────────┐
// ├───ServerPush────▶ │ops: [123] rev: 1 │
// │ │ └──────────────────┘
// │ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
// │ │ ops: ["123","abc", "efg"] rev: 3 │
// │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
// │ │ ┌──────────────────────────────┐
// ◀────ClientPush───┤ │ops: [retain 3, "abc"] rev: 2 │
// ┌──────────────────────────┐ │ │ └──────────────────────────────┘
// │ops: ["123","abc"] rev: 2 │ ├────ServerAck────▶
// └──────────────────────────┘ │ │
// │ │ ┌──────────────────────────────┐
// ◀────ClientPush───┤ │ops: [retain 6, "efg"] rev: 3 │
// ┌──────────────────────────────────┐ │ │ └──────────────────────────────┘
// │ops: ["123","abc", "efg"] rev: 3 │ ├────ServerAck────▶
// └──────────────────────────────────┘ │ │
#[actix_rt::test]
async fn delta_sync_while_local_rev_greater_than_server_rev() {
let test = DocumentTest::new().await;
2022-01-02 02:34:42 +00:00
let mut document = Document::new::<NewlineDoc>();
document.insert(0, "123").unwrap();
let json = document.to_json();
test.run_scripts(vec![
2022-01-03 04:20:06 +00:00
DocScript::ServerResetDocument(json, 1),
2021-11-19 05:42:53 +00:00
DocScript::ClientOpenDoc,
DocScript::AssertClient(r#"[{"insert":"123\n"}]"#),
2021-11-19 05:42:53 +00:00
DocScript::ClientInsertText(3, "abc"),
DocScript::ClientInsertText(6, "efg"),
DocScript::AssertClient(r#"[{"insert":"123abcefg\n"}]"#),
2021-12-15 08:28:18 +00:00
DocScript::AssertServer(r#"[{"insert":"123abcefg\n"}]"#, 3),
])
.await;
}