From a1b3b61c439a8566f2335f7a308c55744ad2f3f1 Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Sat, 5 Apr 2025 14:46:34 +0100 Subject: [PATCH] Move & add tests --- .../reconcile/src/operation_transformation.rs | 246 ------------------ backend/reconcile/tests/examples/1.yml | 6 - backend/reconcile/tests/examples/10.yml | 4 - backend/reconcile/tests/examples/11.yml | 4 - backend/reconcile/tests/examples/12.yml | 4 - backend/reconcile/tests/examples/13.yml | 4 - backend/reconcile/tests/examples/2.yml | 4 - backend/reconcile/tests/examples/3.yml | 4 - backend/reconcile/tests/examples/4.yml | 4 - backend/reconcile/tests/examples/5.yml | 4 - backend/reconcile/tests/examples/6.yml | 4 - backend/reconcile/tests/examples/7.yml | 4 - backend/reconcile/tests/examples/8.yml | 4 - backend/reconcile/tests/examples/9.yml | 4 - backend/reconcile/tests/examples/deletes.yml | 28 ++ .../tests/examples/deletes_and_inserts.yml | 13 + .../tests/examples/idempotent_inserts.yml | 27 ++ .../reconcile/tests/examples/multiline.yml | 6 +- .../reconcile/tests/examples/replacing.yml | 21 ++ backend/reconcile/tests/examples/utf-8.yml | 11 + backend/reconcile/tests/examples/various.yml | 130 +++++++++ 21 files changed, 233 insertions(+), 303 deletions(-) delete mode 100644 backend/reconcile/tests/examples/1.yml delete mode 100644 backend/reconcile/tests/examples/10.yml delete mode 100644 backend/reconcile/tests/examples/11.yml delete mode 100644 backend/reconcile/tests/examples/12.yml delete mode 100644 backend/reconcile/tests/examples/13.yml delete mode 100644 backend/reconcile/tests/examples/2.yml delete mode 100644 backend/reconcile/tests/examples/3.yml delete mode 100644 backend/reconcile/tests/examples/4.yml delete mode 100644 backend/reconcile/tests/examples/5.yml delete mode 100644 backend/reconcile/tests/examples/6.yml delete mode 100644 backend/reconcile/tests/examples/7.yml delete mode 100644 backend/reconcile/tests/examples/8.yml delete mode 100644 backend/reconcile/tests/examples/9.yml create mode 100644 backend/reconcile/tests/examples/deletes.yml create mode 100644 backend/reconcile/tests/examples/deletes_and_inserts.yml create mode 100644 backend/reconcile/tests/examples/idempotent_inserts.yml create mode 100644 backend/reconcile/tests/examples/replacing.yml create mode 100644 backend/reconcile/tests/examples/utf-8.yml create mode 100644 backend/reconcile/tests/examples/various.yml diff --git a/backend/reconcile/src/operation_transformation.rs b/backend/reconcile/src/operation_transformation.rs index 6a2ee8ee..36239dfa 100644 --- a/backend/reconcile/src/operation_transformation.rs +++ b/backend/reconcile/src/operation_transformation.rs @@ -59,247 +59,6 @@ mod test { use super::*; use crate::CursorPosition; - #[test] - fn test_merges() { - // Both replaced one token but different - test_merge_both_ways( - "original_1 original_2 original_3", - "original_1 edit_1 original_3", - "original_1 original_2 edit_2", - "original_1 edit_1 edit_2", - ); - - // Both replaced the same one token - test_merge_both_ways( - "original_1 original_2 original_3", - "original_1 edit_1 original_3", - "original_1 edit_1 original_3", - "original_1 edit_1 original_3", - ); - - // One deleted a large range, the other deleted subranges and inserted as - // well - test_merge_both_ways( - "original_1 original_2 original_3 original_4 original_5", - "original_1 original_5", - "original_1 edit_1 original_3 edit_2 original_5", - "original_1 edit_1 edit_2 original_5", - ); - - // One deleted a large range, the other inserted and deleted a partially - // overlapping range - test_merge_both_ways( - "original_1 original_2 original_3 original_4 original_5", - "original_1 original_5", - "original_1 edit_1 original_3 edit_2", - "original_1 edit_1 edit_2", - ); - - // Merge a replace and an append - test_merge_both_ways("a b ", "c d ", "a b c d ", "c d c d "); - - test_merge_both_ways("a b c d e", "a e", "a c e", "a e"); - - test_merge_both_ways("a 0 1 2 b", "a b", "a E 1 F b", "a E F b"); - - test_merge_both_ways( - "a this one delete b", - "a b", - "a my one change b", - "a my change b", - ); - - test_merge_both_ways( - "this stays, this is one big delete, don't touch this", - "this stays, don't touch this", - "this stays, my one change, don't touch this", - "this stays, my change, don't touch this", - ); - - test_merge_both_ways("1 2 3 4 5 6", "1 6", "1 2 4 ", "1 "); - - test_merge_both_ways( - "hello world", - "hi, world", - "hello my friend!", - "hi, my friend!", - ); - - test_merge_both_ways( - "both delete the same word", - "both the same word", - "both the same word", - "both the same word", - ); - - test_merge_both_ways(" ", "it’s utf-8!", " ", "it’s utf-8!"); - - test_merge_both_ways( - "both delete the same word but one a bit more", - "both the same word", - "both same word", - "both same word", - ); - - test_merge_both_ways( - "long text with one big delete and many small", - "long small", - "long with big and small", - "long small", - ); - } - - #[test] - fn test_reconcile_idempotent_inserts() { - // Both inserted the same prefix; this should get deduped - test_merge_both_ways( - "hi ", - "hi there ", - "hi there my friend ", - "hi there my friend ", - ); - - // The prefix of the 2nd appears on the 1st so it shouldn't get duplicated - test_merge_both_ways( - "hi ", - "hi there you ", - "hi there my friend ", - "hi there my friend you ", - ); - - test_merge_both_ways("a", "a b c", "a b c d", "a b c d"); - - test_merge_both_ways( - " |7ca2b36d-6ee7-49eb-8eb1-d77e4cc1a001| ", - " |7ca2b36d-6ee7-49eb-8eb1-d77e4cc1a001| |cd9195cc-103a-4f13-90c8-4fba0ba421ee| |d39156cc-cfd6-42a8-b70a-75020896069d| |fbad794c-9c47-41f2-a343-490284ecb5a0| |dup| ", - " |7ca2b36d-6ee7-49eb-8eb1-d77e4cc1a001| |cd9195cc-103a-4f13-90c8-4fba0ba421ee| |dup| ", - " |7ca2b36d-6ee7-49eb-8eb1-d77e4cc1a001| |cd9195cc-103a-4f13-90c8-4fba0ba421ee| |d39156cc-cfd6-42a8-b70a-75020896069d| |fbad794c-9c47-41f2-a343-490284ecb5a0| |dup| |dup| "); - } - - #[test] - fn test_cursor_position_no_updates() { - let original = "hello world"; - let left = TextWithCursors::new( - "hello world", - vec![CursorPosition { - id: 0, - char_index: 0, - }], - ); - let right = TextWithCursors::new( - "hello world", - vec![CursorPosition { - id: 1, - char_index: 5, - }], - ); - - let merged = reconcile_with_cursors(original, left, right); - - assert_eq!( - merged, - TextWithCursors::new( - "hello world", - vec![ - CursorPosition { - id: 0, - char_index: 0 - }, - CursorPosition { - id: 1, - char_index: 5 - } - ] - ) - ); - } - - #[test] - fn test_cursor_position_updates_with_inserts() { - let original = "hi"; - let left = TextWithCursors::new( - "hi there", - vec![CursorPosition { - id: 0, - char_index: 7, - }], - ); - let right = TextWithCursors::new( - "hi world!", - vec![ - CursorPosition { - id: 1, - char_index: 9, - }, - CursorPosition { - id: 2, - char_index: 1, - }, - ], - ); - - let merged = reconcile_with_cursors(original, left, right); - - assert_eq!( - merged, - TextWithCursors::new( - "hi there world!", - vec![ - CursorPosition { - id: 2, - char_index: 1, - }, - CursorPosition { - id: 0, - char_index: 7 - }, - CursorPosition { - id: 1, - char_index: 15 - }, - ] - ) - ); - } - - #[test] - fn test_cursor_position_updates_with_deleted() { - let original = "a b c d"; - let left = TextWithCursors::new( - "a b d", - vec![CursorPosition { - id: 0, - char_index: 1, // after a - }], - ); - let right = TextWithCursors::new( - "c d", - vec![CursorPosition { - id: 1, - char_index: 1, // after c - }], - ); - - let merged = reconcile_with_cursors(original, left, right); - - assert_eq!( - merged, - TextWithCursors::new( - " d", - vec![ - CursorPosition { - id: 0, - char_index: 0 - }, - CursorPosition { - id: 1, - char_index: 1 - } - ] - ) - ); - } - #[test] fn test_cursor_complex() { let original = "this is some complex text to test cursor positions"; @@ -407,9 +166,4 @@ mod test { let _ = reconcile(&contents[0], &contents[1], &contents[2]); } - - fn test_merge_both_ways(original: &str, edit_1: &str, edit_2: &str, expected: &str) { - assert_eq!(reconcile(original, edit_1, edit_2), expected); - assert_eq!(reconcile(original, edit_2, edit_1), expected); - } } diff --git a/backend/reconcile/tests/examples/1.yml b/backend/reconcile/tests/examples/1.yml deleted file mode 100644 index ce90f51f..00000000 --- a/backend/reconcile/tests/examples/1.yml +++ /dev/null @@ -1,6 +0,0 @@ -# The `|` characters denote cursor positions which are stripped before the actual reconcile logic is run ---- -parent: You're Annual Savings Statement is available in our online portal -left: Your| annual record is available in our online portal| -right: You're Annual Savings information| is available online -expected: Your| annual record information| is available online| diff --git a/backend/reconcile/tests/examples/10.yml b/backend/reconcile/tests/examples/10.yml deleted file mode 100644 index 0ee73838..00000000 --- a/backend/reconcile/tests/examples/10.yml +++ /dev/null @@ -1,4 +0,0 @@ -parent: marketplace -left: market| place -right: market|space -expected: market| placemarket|space diff --git a/backend/reconcile/tests/examples/11.yml b/backend/reconcile/tests/examples/11.yml deleted file mode 100644 index d576c04d..00000000 --- a/backend/reconcile/tests/examples/11.yml +++ /dev/null @@ -1,4 +0,0 @@ -parent: Please remember to bring your laptop and charger -left: Please remember to bring your laptop| -right: Please remember to bring your |new |laptop and charger -expected: Please remember to bring your |new |laptop| diff --git a/backend/reconcile/tests/examples/12.yml b/backend/reconcile/tests/examples/12.yml deleted file mode 100644 index 879b398f..00000000 --- a/backend/reconcile/tests/examples/12.yml +++ /dev/null @@ -1,4 +0,0 @@ -parent: Party A shall pay Party B -left: Party C shall pay Party B -right: Party A shall receive from Party B -expected: Party C shall receive from Party B diff --git a/backend/reconcile/tests/examples/13.yml b/backend/reconcile/tests/examples/13.yml deleted file mode 100644 index e12c635b..00000000 --- a/backend/reconcile/tests/examples/13.yml +++ /dev/null @@ -1,4 +0,0 @@ -parent: Please submit your assignment by Friday -left: Please submit your |completed |assignment by Friday -right: Please submit your assignment |online |by Friday -expected: Please submit your |completed |assignment |online |by Friday diff --git a/backend/reconcile/tests/examples/2.yml b/backend/reconcile/tests/examples/2.yml deleted file mode 100644 index 77a03755..00000000 --- a/backend/reconcile/tests/examples/2.yml +++ /dev/null @@ -1,4 +0,0 @@ -parent: -left: hi my friend| -right: hi there| -expected: hi my friend| there| diff --git a/backend/reconcile/tests/examples/3.yml b/backend/reconcile/tests/examples/3.yml deleted file mode 100644 index 8e2dd222..00000000 --- a/backend/reconcile/tests/examples/3.yml +++ /dev/null @@ -1,4 +0,0 @@ -parent: Buy milk and eggs -left: Buy organic milk| and eggs| -right: Buy milk and eggs| and bread -expected: Buy organic milk| and eggs|| and bread diff --git a/backend/reconcile/tests/examples/4.yml b/backend/reconcile/tests/examples/4.yml deleted file mode 100644 index f06d3287..00000000 --- a/backend/reconcile/tests/examples/4.yml +++ /dev/null @@ -1,4 +0,0 @@ -parent: Meeting at 2pm in 会议室 -left: Meeting at |3pm in the 会议室 -right: Team meeting at 2pm in conference room| -expected: Team meeting at |3pm in conference room| the diff --git a/backend/reconcile/tests/examples/5.yml b/backend/reconcile/tests/examples/5.yml deleted file mode 100644 index aac8a98c..00000000 --- a/backend/reconcile/tests/examples/5.yml +++ /dev/null @@ -1,4 +0,0 @@ -parent: Send the report to the team -left: Send the |detailed |report to the |entire |team -right: Send the |quarterly |detailed |report to the team -expected: Send the |detailed |quarterly |detailed ||report to the |entire |team diff --git a/backend/reconcile/tests/examples/6.yml b/backend/reconcile/tests/examples/6.yml deleted file mode 100644 index 16d25fb2..00000000 --- a/backend/reconcile/tests/examples/6.yml +++ /dev/null @@ -1,4 +0,0 @@ -parent: Ready, Set go -left: Ready! Set go| -right: Ready, Set, go!| -expected: Ready! Set, go!|| diff --git a/backend/reconcile/tests/examples/7.yml b/backend/reconcile/tests/examples/7.yml deleted file mode 100644 index 579e9271..00000000 --- a/backend/reconcile/tests/examples/7.yml +++ /dev/null @@ -1,4 +0,0 @@ -parent: "Total: $100" -left: "Total: |$150" -right: "Total: |€100" -expected: "Total: |$150 |€100" diff --git a/backend/reconcile/tests/examples/8.yml b/backend/reconcile/tests/examples/8.yml deleted file mode 100644 index 6c316ef6..00000000 --- a/backend/reconcile/tests/examples/8.yml +++ /dev/null @@ -1,4 +0,0 @@ -parent: Start middle end -left: Start [important] middle end| -right: Start middle [critical] end| -expected: Start [important] middle [critical] end|| diff --git a/backend/reconcile/tests/examples/9.yml b/backend/reconcile/tests/examples/9.yml deleted file mode 100644 index 6f534b76..00000000 --- a/backend/reconcile/tests/examples/9.yml +++ /dev/null @@ -1,4 +0,0 @@ -parent: A B C D -left: A X B D| -right: A B Y| -expected: A X B Y|| diff --git a/backend/reconcile/tests/examples/deletes.yml b/backend/reconcile/tests/examples/deletes.yml new file mode 100644 index 00000000..a4fb2e4d --- /dev/null +++ b/backend/reconcile/tests/examples/deletes.yml @@ -0,0 +1,28 @@ +# Both delete the same range +parent: original_1 original_2 original_3 original_4 original_5 +left: original_1 original_5| +right: "|original_1 original_5" +expected: "|original_1 original_5|" + +--- + +# Both delete a range and one range contains the other +parent: original_1 original_2 original_3 original_4 original_5 +left: original_1 original_5 +right: original_1 original_4 original_5 +expected: original_1 original_5 + +--- + +# Deleting overlapping ranges +parent: original_1 original_2 original_3 original_4 original_5 +left: original_1 original_4| original_5 +right: original_1 original_2| original_5 +expected: original_1| original_5| + +--- + +parent: long text with one big delete and many small +left: long small +right: long with big and small +expected: long small diff --git a/backend/reconcile/tests/examples/deletes_and_inserts.yml b/backend/reconcile/tests/examples/deletes_and_inserts.yml new file mode 100644 index 00000000..ed83604e --- /dev/null +++ b/backend/reconcile/tests/examples/deletes_and_inserts.yml @@ -0,0 +1,13 @@ +# One deleted a large range, the other deleted subranges and inserted as well +parent: original_1 original_2 original_3 original_4 original_5 +left: original_1 original_5 +right: original_1 edit_1 original_3 edit_2 original_5 +expected: original_1 edit_1 edit_2 original_5 + +--- + +# One deleted a large range, the other inserted and deleted a partially overlapping range +parent: original_1 original_2 original_3 original_4 original_5 +left: original_1 original_5 +right: original_1 edit_1 original_3 edit_2 +expected: original_1 edit_1 edit_2 diff --git a/backend/reconcile/tests/examples/idempotent_inserts.yml b/backend/reconcile/tests/examples/idempotent_inserts.yml new file mode 100644 index 00000000..f45792ed --- /dev/null +++ b/backend/reconcile/tests/examples/idempotent_inserts.yml @@ -0,0 +1,27 @@ +# Both inserted the same prefix; this should get deduplicateed +parent: "hi " +left: "hi there " +right: "hi there my friend " +expected: "hi there my friend " + +--- + +# The prefix of the 2nd appears on the 1st so it shouldn't get duplicatelicated +parent: "hi " +left: "hi there you " +right: "hi there my friend " +expected: "hi there my friend you " + +--- + +parent: a +left: a b c +right: a b c d +expected: a b c d + +--- + +parent: a +left: abc +right: abcd +expected: abcabcd diff --git a/backend/reconcile/tests/examples/multiline.yml b/backend/reconcile/tests/examples/multiline.yml index 00de7cd9..b2460c09 100644 --- a/backend/reconcile/tests/examples/multiline.yml +++ b/backend/reconcile/tests/examples/multiline.yml @@ -55,11 +55,11 @@ parent: | a a left: | - a + a| a right: | - a + a| a expected: | - a + a|| a diff --git a/backend/reconcile/tests/examples/replacing.yml b/backend/reconcile/tests/examples/replacing.yml new file mode 100644 index 00000000..97d7c897 --- /dev/null +++ b/backend/reconcile/tests/examples/replacing.yml @@ -0,0 +1,21 @@ +# Both replaced one token but the tokens are different +parent: original_1 original_2 original_3 +left: original_1 edit_1| original_3 +right: original_1 original_2| edit_2 +expected: original_1 edit_1|| edit_2 + +--- + +# Both replace the same token with the same value +parent: original_1 original_2 original_3 +left: original_1 edit_1| original_3 +right: original_1 edit_1 original_3| +expected: original_1 edit_1| original_3| + +--- + +# Both replace the same token with different value +parent: original_1 original_2 original_3 +left: original_1 edit_1| original_3 +right: original_1 conflicting_edit_1| original_3 +expected: original_1 conflicting_edit_1| edit_1| original_3 diff --git a/backend/reconcile/tests/examples/utf-8.yml b/backend/reconcile/tests/examples/utf-8.yml new file mode 100644 index 00000000..662d7a73 --- /dev/null +++ b/backend/reconcile/tests/examples/utf-8.yml @@ -0,0 +1,11 @@ +parent: Meeting at 2pm in 会议室 +left: Meeting at |3pm in 会议室 +right: Team meeting at 2pm in conference room| +expected: Team meeting at |3pm in conference room| + +--- + +parent: " " +left: "it’|s utf-8!" +right: " " +expected: "it’|s utf-8!" diff --git a/backend/reconcile/tests/examples/various.yml b/backend/reconcile/tests/examples/various.yml new file mode 100644 index 00000000..91c12024 --- /dev/null +++ b/backend/reconcile/tests/examples/various.yml @@ -0,0 +1,130 @@ +parent: You're Annual Savings Statement is available in our online portal +left: Your| annual record is available in our online portal| +right: You're Annual Savings information| is available online +expected: Your| annual record information| is available online| + +--- + +parent: Party A shall pay Party B +left: Party C shall pay Party B +right: Party A shall receive from Party B +expected: Party C shall receive from Party B + +--- + +parent: +left: hi my friend| +right: hi there| +expected: hi my friend| there| + +--- + +parent: Buy milk and eggs +left: Buy organic milk| and eggs| +right: Buy milk and eggs| and bread +expected: Buy organic milk| and eggs|| and bread + +--- + +parent: Send the report to the team +left: Send the |detailed report to the |entire |team +right: Send the |quarterly |detailed report to the team +expected: Send the |detailed |quarterly |detailed report to the |entire |team + +--- + +parent: Ready, Set go +left: Ready! Set go| +right: Ready, Set, go!| +expected: Ready! Set, go!|| + +--- + +parent: "Total: $100" +left: "Total: |$150" +right: "Total: |€100" +expected: "Total: |$150 |€100" + +--- + +parent: Start middle end +left: Start [important] middle end| +right: Start middle [critical] end| +expected: Start [important] middle [critical] end|| + +--- + +parent: marketplace +left: market| place +right: market|space +expected: market| placemarket|space + +--- + +parent: A B C D +left: A X B D| +right: A B Y| +expected: A X B Y|| + +--- + +parent: Please submit your assignment by Friday +left: Please submit your |completed |assignment by Friday +right: Please submit your assignment |online |by Friday +expected: Please submit your |completed |assignment |online |by Friday + +--- + +parent: "a b " +left: "c d " +right: "a b c d " +expected: "c d c d " + +--- + +parent: a b c d e +left: a e| +right: a c e| +expected: a e|| + +--- + +parent: a 0 1 2 b +left: a 0 1| 2 b +right: a b| +expected: a| b| + +--- + +parent: a 0 1 2 b +left: "|a b" +right: "|a E 1 F b" +expected: "||a E F b" + +--- + +parent: a this one delete b +left: a b| +right: a my one change b| +expected: a my change b|| + +--- + +parent: this stays, this is one big delete, don't touch this +left: this stays, don't touch this| +right: this stays, my one change, don't touch this| +expected: this stays, my change, don't touch this|| + +--- + +parent: 1 2 3 4 5 6 +left: 1| 6 +right: 1 2 4| +expected: 1|| + +--- + +parent: hello world +left: hi, world +right: hello my friend! +expected: hi, my friend!