@@ -13,13 +13,12 @@ fn test_rga_basic_insert() {
1313 rga. insert ( 1 , 'i' ) . unwrap ( ) ;
1414 assert_eq ! ( rga. get_text( ) . unwrap( ) , "Hi" ) ;
1515
16- // Insert at position 0 - both 'H' and '!' have left=root
17- // RGA orders by HLC timestamp: 'H' (earlier) comes before '!' (later)
16+ // Insert at position 0 (before everything)
17+ // Both 'H' and '!' have left=root, but '!' has later timestamp
18+ // With REVERSED sort (latest first), '!' comes BEFORE 'H' - correct for sequential edits!
1819 rga. insert ( 0 , '!' ) . unwrap ( ) ;
19- // '!' has left=root, later timestamp than 'H', so comes after 'H'
2020 let text = rga. get_text ( ) . unwrap ( ) ;
21- assert ! ( text. starts_with( 'H' ) ) ; // 'H' was first, has earliest timestamp
22- assert_eq ! ( text. len( ) , 3 ) ;
21+ assert_eq ! ( text, "!Hi" , "Insert at position 0 should prepend" ) ;
2322}
2423
2524#[ test]
@@ -71,6 +70,32 @@ fn test_rga_insert_str_middle() {
7170 assert_eq ! ( text. len( ) , 21 ) ; // "Hello" + " Beautiful" + " World"
7271}
7372
73+ #[ test]
74+ fn test_rga_insert_str_position_bug ( ) {
75+ env:: reset_for_testing ( ) ;
76+
77+ let mut rga = ReplicatedGrowableArray :: new ( ) ;
78+
79+ // Insert "Hello World" as a single operation
80+ rga. insert_str ( 0 , "Hello World" ) . unwrap ( ) ;
81+ assert_eq ! ( rga. get_text( ) . unwrap( ) , "Hello World" ) ;
82+
83+ // Insert "Beautiful " at position 6 (after "Hello ", before "World")
84+ // Position 6 should be right before 'W'
85+ rga. insert_str ( 6 , "Beautiful " ) . unwrap ( ) ;
86+ let result = rga. get_text ( ) . unwrap ( ) ;
87+
88+ eprintln ! ( "Result: '{}'" , result) ;
89+ eprintln ! ( "Expected: 'Hello Beautiful World'" ) ;
90+
91+ // Expected: "Hello Beautiful World"
92+ assert_eq ! (
93+ result, "Hello Beautiful World" ,
94+ "insert_str at position 6 should insert before 'World', got: '{}'" ,
95+ result
96+ ) ;
97+ }
98+
7499#[ test]
75100fn test_rga_delete_range ( ) {
76101 env:: reset_for_testing ( ) ;
0 commit comments