@@ -285,7 +285,7 @@ public function test_sync_response_structure() {
285285 $ this ->assertArrayHasKey ( 'updates ' , $ room_data );
286286 $ this ->assertArrayHasKey ( 'end_cursor ' , $ room_data );
287287 $ this ->assertArrayHasKey ( 'total_updates ' , $ room_data );
288- $ this ->assertArrayHasKey ( 'compaction_request ' , $ room_data );
288+ $ this ->assertArrayHasKey ( 'should_compact ' , $ room_data );
289289 }
290290
291291 public function test_sync_response_room_matches_request () {
@@ -536,6 +536,97 @@ public function test_sync_total_updates_increments() {
536536 $ this ->assertSame ( 3 , $ data ['rooms ' ][0 ]['total_updates ' ] );
537537 }
538538
539+ /*
540+ * Compaction tests.
541+ */
542+
543+ public function test_sync_should_compact_is_false_below_threshold () {
544+ wp_set_current_user ( self ::$ editor_id );
545+
546+ $ room = $ this ->get_post_room ();
547+ $ update = array (
548+ 'type ' => 'update ' ,
549+ 'data ' => 'dGVzdA== ' ,
550+ );
551+
552+ // Client 1 sends a single update.
553+ $ response = $ this ->dispatch_sync (
554+ array (
555+ $ this ->build_room ( $ room , 1 , 0 , array ( 'user ' => 'c1 ' ), array ( $ update ) ),
556+ )
557+ );
558+
559+ $ data = $ response ->get_data ();
560+ $ this ->assertFalse ( $ data ['rooms ' ][0 ]['should_compact ' ] );
561+ }
562+
563+ public function test_sync_should_compact_is_true_above_threshold_for_compactor () {
564+ wp_set_current_user ( self ::$ editor_id );
565+
566+ $ room = $ this ->get_post_room ();
567+ $ updates = array ();
568+ for ( $ i = 0 ; $ i < 51 ; $ i ++ ) {
569+ $ updates [] = array (
570+ 'type ' => 'update ' ,
571+ 'data ' => base64_encode ( "update- $ i " ),
572+ );
573+ }
574+
575+ // Client 1 sends enough updates to exceed the compaction threshold.
576+ $ this ->dispatch_sync (
577+ array (
578+ $ this ->build_room ( $ room , 1 , 0 , array ( 'user ' => 'c1 ' ), $ updates ),
579+ )
580+ );
581+
582+ // Client 2 (lowest connected client) should be told to compact.
583+ $ response = $ this ->dispatch_sync (
584+ array (
585+ $ this ->build_room ( $ room , 2 , 0 , array ( 'user ' => 'c2 ' ) ),
586+ )
587+ );
588+
589+ $ data = $ response ->get_data ();
590+ $ this ->assertTrue ( $ data ['rooms ' ][0 ]['should_compact ' ] );
591+ }
592+
593+ public function test_sync_should_compact_is_false_for_non_compactor () {
594+ wp_set_current_user ( self ::$ editor_id );
595+
596+ $ room = $ this ->get_post_room ();
597+ $ updates = array ();
598+ for ( $ i = 0 ; $ i < 51 ; $ i ++ ) {
599+ $ updates [] = array (
600+ 'type ' => 'update ' ,
601+ 'data ' => base64_encode ( "update- $ i " ),
602+ );
603+ }
604+
605+ // Client 1 sends enough updates to exceed the compaction threshold.
606+ $ this ->dispatch_sync (
607+ array (
608+ $ this ->build_room ( $ room , 1 , 0 , array ( 'user ' => 'c1 ' ), $ updates ),
609+ )
610+ );
611+
612+ // Connect client 2 (lower ID) so client 3 is not the compactor.
613+ $ this ->dispatch_sync (
614+ array (
615+ $ this ->build_room ( $ room , 2 , 0 , array ( 'user ' => 'c2 ' ) ),
616+ )
617+ );
618+
619+ // Client 3 (higher ID) should not be told to compact.
620+ $ response = $ this ->dispatch_sync (
621+ array (
622+ $ this ->build_room ( $ room , 3 , 0 , array ( 'user ' => 'c3 ' ) ),
623+ )
624+ );
625+
626+ $ data = $ response ->get_data ();
627+ $ this ->assertFalse ( $ data ['rooms ' ][0 ]['should_compact ' ] );
628+ }
629+
539630 /*
540631 * Awareness tests.
541632 */
0 commit comments