@@ -399,8 +399,9 @@ mod tests {
399399 #[ serial]
400400 async fn test_replicate_group ( ) -> Result < ( ) > {
401401 // Initialize the app
402- let path = TmpDir :: new ( "test_api_repo_file_operations" ) . await ?;
403-
402+ let path = TmpDir :: new ( "test_replicate_group" ) . await ?;
403+
404+ // Create second backend (creator) first
404405 let store2 = iroh_blobs:: store:: fs:: Store :: load ( path. to_path_buf ( ) . join ( "iroh2" ) ) . await ?;
405406 let ( veilid_api2, update_rx2) = save_dweb_backend:: common:: init_veilid (
406407 path. to_path_buf ( ) . join ( "test2" ) . as_path ( ) ,
@@ -415,61 +416,60 @@ mod tests {
415416 )
416417 . await
417418 . unwrap ( ) ;
418-
419+
420+ // Initialize main backend (joiner)
419421 BACKEND . get_or_init ( || init_backend ( path. to_path_buf ( ) . as_path ( ) ) ) ;
420422 {
421423 let backend = get_backend ( ) . await ?;
422424 backend. start ( ) . await . expect ( "Backend failed to start" ) ;
423425 }
424-
426+
427+ // Create group and repo in backend2 (creator)
425428 let mut group = backend2. create_group ( ) . await ?;
426-
427429 let join_url = group. get_url ( ) ;
428-
429430 group. set_name ( TEST_GROUP_NAME ) . await ?;
430-
431431 let repo = group. create_repo ( ) . await ?;
432432 repo. set_name ( TEST_GROUP_NAME ) . await ?;
433-
434- // Step 3: Upload a file to the repository
433+
434+ // Upload a file to the repository
435435 let file_name = "example.txt" ;
436436 let file_content = b"Test content for file upload" ;
437-
438437 repo. upload ( file_name, file_content. to_vec ( ) ) . await ?;
439-
438+
440439 tokio:: time:: sleep ( Duration :: from_secs ( 2 ) ) . await ;
441-
440+
442441 let app = test:: init_service (
443442 App :: new ( )
444443 . service ( status)
445444 . service ( web:: scope ( "/api" ) . service ( groups:: scope ( ) ) ) ,
446445 )
447446 . await ;
448-
447+
448+ // Join the group using the main backend
449449 {
450450 let backend = get_backend ( ) . await ?;
451451 backend. join_from_url ( join_url. as_str ( ) ) . await ?;
452452 }
453-
453+
454454 // Wait for replication to complete
455455 tokio:: time:: sleep ( Duration :: from_secs ( 2 ) ) . await ;
456-
456+
457457 // Test HTTP endpoints after replication
458458 // 1. Verify group exists and has correct name
459459 let groups_req = test:: TestRequest :: get ( ) . uri ( "/api/groups" ) . to_request ( ) ;
460460 let groups_resp: GroupsResponse = test:: call_and_read_body_json ( & app, groups_req) . await ;
461461 assert_eq ! ( groups_resp. groups. len( ) , 1 , "Should have one group after joining" ) ;
462462 assert_eq ! ( groups_resp. groups[ 0 ] . name, Some ( TEST_GROUP_NAME . to_string( ) ) ,
463463 "Group should have correct name" ) ;
464-
464+
465465 // 2. Verify repo exists and has correct name
466466 let repos_req = test:: TestRequest :: get ( )
467467 . uri ( & format ! ( "/api/groups/{}/repos" , group. id( ) ) )
468468 . to_request ( ) ;
469469 let repos_resp: ReposResponse = test:: call_and_read_body_json ( & app, repos_req) . await ;
470470 assert_eq ! ( repos_resp. repos. len( ) , 1 , "Should have one repo after joining" ) ;
471471 assert_eq ! ( repos_resp. repos[ 0 ] . name, TEST_GROUP_NAME , "Repo should have correct name" ) ;
472-
472+
473473 // 3. Verify file exists and has correct content
474474 let file_req = test:: TestRequest :: get ( )
475475 . uri ( & format ! (
@@ -482,16 +482,16 @@ mod tests {
482482 let got_content = test:: read_body ( file_resp) . await ;
483483 assert_eq ! ( got_content. to_vec( ) , file_content. to_vec( ) ,
484484 "File content should match after replication" ) ;
485-
485+
486486 // Clean up both backends using the helper function
487487 cleanup_test_resources ( & backend2) . await ?;
488488 {
489489 let backend = get_backend ( ) . await ?;
490490 cleanup_test_resources ( & backend) . await ?;
491491 }
492-
492+
493493 Ok ( ( ) )
494- }
494+ }
495495
496496 #[ actix_web:: test]
497497 #[ serial]
@@ -769,14 +769,14 @@ mod tests {
769769 #[ actix_web:: test]
770770 #[ serial]
771771 async fn test_refresh_joined_group ( ) -> Result < ( ) > {
772- // Initialize logging
772+ // Initialize logging
773773 let _ = env_logger:: try_init ( ) ;
774774 log:: info!( "Testing refresh of joined group" ) ;
775775
776- // Initialize the app with basic setup
777- let path = TmpDir :: new ( "test_refresh_joined " ) . await ?;
776+ // Initialize the app
777+ let path = TmpDir :: new ( "test_refresh_joined_group " ) . await ?;
778778
779- // Initialize backend2 (creator) first
779+ // Create second backend (creator) first
780780 let store2 = iroh_blobs:: store:: fs:: Store :: load ( path. to_path_buf ( ) . join ( "iroh2" ) ) . await ?;
781781 let ( veilid_api2, update_rx2) = save_dweb_backend:: common:: init_veilid (
782782 path. to_path_buf ( ) . join ( "test2" ) . as_path ( ) ,
@@ -792,121 +792,110 @@ mod tests {
792792 . await
793793 . unwrap ( ) ;
794794
795- // Create group and repo in backend2
796- let mut group = backend2. create_group ( ) . await ?;
797- group. set_name ( TEST_GROUP_NAME ) . await ?;
798- let repo = group. create_repo ( ) . await ?;
799- repo. set_name ( "Test Repo" ) . await ?;
800-
801- // Upload a file (using backend2) to ensure repo has a collection/hash
802- let file_name = "test.txt" ;
803- let file_content = b"Test content for joined group" ;
804- repo. upload ( file_name, file_content. to_vec ( ) ) . await ?;
805- log:: info!( "Uploaded test file to creator's repo" ) ;
806-
807- // Wait for DHT propagation
808- tokio:: time:: sleep ( Duration :: from_secs ( 2 ) ) . await ;
809-
810- // Initialize and start the global BACKEND (joiner)
795+ // Initialize main backend (joiner)
811796 BACKEND . get_or_init ( || init_backend ( path. to_path_buf ( ) . as_path ( ) ) ) ;
812797 {
813798 let backend = get_backend ( ) . await ?;
814799 backend. start ( ) . await . expect ( "Backend failed to start" ) ;
815- log:: info!( "Waiting for public internet readiness for global BACKEND..." ) ;
816- wait_for_public_internet_ready ( & backend) . await ?;
817- log:: info!( "Public internet is ready for global BACKEND" ) ;
818800 }
819801
820- // Join the group (using the global BACKEND)
821- {
822- let backend = get_backend ( ) . await ?;
823- backend. join_from_url ( group. get_url ( ) . as_str ( ) ) . await ?;
824- log:: info!( "Successfully joined group" ) ;
825- }
802+ // Create group and repo in backend2 (creator)
803+ let mut group = backend2. create_group ( ) . await ?;
804+ let join_url = group. get_url ( ) ;
805+ group. set_name ( TEST_GROUP_NAME ) . await ?;
806+ let repo = group. create_repo ( ) . await ?;
807+ repo. set_name ( TEST_GROUP_NAME ) . await ?;
808+
809+ // Upload a file to the repository
810+ let file_name = "example.txt" ;
811+ let file_content = b"Test content for file upload" ;
812+ repo. upload ( file_name, file_content. to_vec ( ) ) . await ?;
826813
827- // Wait for replication
828814 tokio:: time:: sleep ( Duration :: from_secs ( 2 ) ) . await ;
829815
830- // Initialize app for API testing
831816 let app = test:: init_service (
832817 App :: new ( )
833818 . service ( status)
834- . service ( health)
835819 . service ( web:: scope ( "/api" ) . service ( groups:: scope ( ) ) ) ,
836820 )
837821 . await ;
838822
839- // Test refresh endpoint
840- log:: info!( "Testing refresh endpoint for joined group" ) ;
823+ // Join the group using the main backend
824+ {
825+ let backend = get_backend ( ) . await ?;
826+ backend. join_from_url ( join_url. as_str ( ) ) . await ?;
827+ }
828+
829+ // Wait for replication to complete
830+ tokio:: time:: sleep ( Duration :: from_secs ( 2 ) ) . await ;
831+
832+ // Test first refresh - should fetch files from network
841833 let refresh_req = test:: TestRequest :: post ( )
842834 . uri ( & format ! ( "/api/groups/{}/refresh" , group. id( ) ) )
843835 . to_request ( ) ;
844836 let refresh_resp = test:: call_service ( & app, refresh_req) . await ;
837+ assert ! ( refresh_resp. status( ) . is_success( ) , "First refresh should succeed" ) ;
845838
846- // Verify response status and content
847- assert ! ( refresh_resp. status( ) . is_success( ) , "Refresh should succeed" ) ;
848839 let refresh_data: serde_json:: Value = test:: read_body_json ( refresh_resp) . await ;
849- log :: info! ( "Refresh response: {:?}" , refresh_data ) ;
850- assert_eq ! ( refresh_data [ "status" ] , "success" , "Response should indicate success" ) ;
840+ assert_eq ! ( refresh_data [ "status" ] , "success" , "First refresh status should be success" ) ;
841+
851842 let repos = refresh_data[ "repos" ] . as_array ( ) . expect ( "repos should be an array" ) ;
852- assert_eq ! ( repos. len( ) , 1 , "Should have one repo" ) ;
843+ assert_eq ! ( repos. len( ) , 1 , "Should have one repo after joining" ) ;
844+
853845 let repo_data = & repos[ 0 ] ;
854- assert ! ( repo_data[ "repo_hash" ] . is_string( ) , "repo should have a hash" ) ;
855- assert_eq ! ( repo_data[ "name" ] , "Test Repo" , "repo name should match" ) ;
846+ assert_eq ! ( repo_data[ "name" ] , TEST_GROUP_NAME , "Repo should have correct name" ) ;
856847
857- // Verify files from the FIRST refresh
858- let refreshed_files_first = repo_data[ "refreshed_files" ] . as_array ( )
859- . expect ( "refreshed_files should be an array for first refresh" ) ;
860- assert_eq ! ( refreshed_files_first. len( ) , 1 , "One file should be refreshed on initial sync" ) ;
861- assert_eq ! ( refreshed_files_first[ 0 ] . as_str( ) . unwrap( ) , file_name, "The correct file should be in refreshed_files on initial sync" ) ;
848+ // First refresh should have refreshed files
849+ let refreshed_files = repo_data[ "refreshed_files" ] . as_array ( )
850+ . expect ( "refreshed_files should be an array" ) ;
851+ assert_eq ! ( refreshed_files. len( ) , 1 , "Should have refreshed 1 file on first refresh" ) ;
852+ assert_eq ! ( refreshed_files[ 0 ] . as_str( ) . unwrap( ) , file_name,
853+ "Should have refreshed the correct file" ) ;
862854
863- let all_files_first = repo_data[ "all_files" ] . as_array ( ) . expect ( "all_files should be an array for first refresh" ) ;
864- assert_eq ! ( all_files_first. len( ) , 1 , "Should have one file in all_files on first refresh" ) ;
865- assert_eq ! ( all_files_first[ 0 ] . as_str( ) . unwrap( ) , file_name, "all_files should contain the uploaded file on first refresh" ) ;
855+ let all_files = repo_data[ "all_files" ] . as_array ( ) . expect ( "all_files should be an array" ) ;
856+ assert_eq ! ( all_files. len( ) , 1 , "Should have one file in all_files" ) ;
857+ assert_eq ! ( all_files[ 0 ] . as_str( ) . unwrap( ) , file_name,
858+ "all_files should contain the uploaded file" ) ;
866859
867- // Verify file is accessible ( after first refresh)
868- let get_file_req_first = test:: TestRequest :: get ( )
860+ // Verify file is accessible after refresh
861+ let get_file_req = test:: TestRequest :: get ( )
869862 . uri ( & format ! (
870863 "/api/groups/{}/repos/{}/media/{}" ,
871864 group. id( ) , repo. id( ) , file_name
872865 ) )
873866 . to_request ( ) ;
874- let get_file_resp_first = test:: call_service ( & app, get_file_req_first ) . await ;
875- assert ! ( get_file_resp_first . status( ) . is_success( ) , "File should be accessible after first refresh" ) ;
876- let got_content_first = test:: read_body ( get_file_resp_first ) . await ;
877- assert_eq ! ( got_content_first . to_vec( ) , file_content. to_vec( ) , "File content should match after first refresh" ) ;
878-
879- // ---- SECOND REFRESH ----
880- log :: info! ( "Testing second refresh endpoint for joined group ( should be no-op)" ) ;
881- let refresh_req_second = test:: TestRequest :: post ( )
867+ let get_file_resp = test:: call_service ( & app, get_file_req ) . await ;
868+ assert ! ( get_file_resp . status( ) . is_success( ) , "File should be accessible after refresh" ) ;
869+ let got_content = test:: read_body ( get_file_resp ) . await ;
870+ assert_eq ! ( got_content . to_vec( ) , file_content. to_vec( ) ,
871+ "File content should match after refresh" ) ;
872+
873+ // Test second refresh - should be no-op since all files are present
874+ let refresh_req2 = test:: TestRequest :: post ( )
882875 . uri ( & format ! ( "/api/groups/{}/refresh" , group. id( ) ) )
883876 . to_request ( ) ;
884- let refresh_resp_second = test:: call_service ( & app, refresh_req_second) . await ;
885- assert ! ( refresh_resp_second. status( ) . is_success( ) , "Second refresh should succeed" ) ;
886- let refresh_data_second: serde_json:: Value = test:: read_body_json ( refresh_resp_second) . await ;
887- assert_eq ! ( refresh_data_second[ "status" ] , "success" , "Second refresh response should indicate success" ) ;
888- let repos_second = refresh_data_second[ "repos" ] . as_array ( ) . expect ( "repos should be an array for second refresh" ) ;
889- assert_eq ! ( repos_second. len( ) , 1 , "Should have one repo in second refresh" ) ;
890- let repo_data_second = & repos_second[ 0 ] ;
891-
892- let refreshed_files_second = repo_data_second[ "refreshed_files" ] . as_array ( )
893- . expect ( "refreshed_files should be an array for second refresh" ) ;
894- assert ! ( refreshed_files_second. is_empty( ) , "No files should be refreshed on second sync as all are present" ) ;
895-
896- let all_files_second = repo_data_second[ "all_files" ] . as_array ( ) . expect ( "all_files should be an array for second refresh" ) ;
897- assert_eq ! ( all_files_second. len( ) , 1 , "Should still have one file in all_files on second refresh" ) ;
898- assert_eq ! ( all_files_second[ 0 ] . as_str( ) . unwrap( ) , file_name, "all_files should still contain the uploaded file on second refresh" ) ;
899-
900- // Clean up (stop backend2, stop global BACKEND)
901- log:: info!( "Cleaning up test resources..." ) ;
877+ let refresh_resp2 = test:: call_service ( & app, refresh_req2) . await ;
878+ assert ! ( refresh_resp2. status( ) . is_success( ) , "Second refresh should succeed" ) ;
879+
880+ let refresh_data2: serde_json:: Value = test:: read_body_json ( refresh_resp2) . await ;
881+ assert_eq ! ( refresh_data2[ "status" ] , "success" , "Second refresh status should be success" ) ;
882+
883+ let repos2 = refresh_data2[ "repos" ] . as_array ( ) . expect ( "repos should be an array" ) ;
884+ assert_eq ! ( repos2. len( ) , 1 , "Should still have one repo" ) ;
885+
886+ let repo_data2 = & repos2[ 0 ] ;
887+ let refreshed_files2 = repo_data2[ "refreshed_files" ] . as_array ( )
888+ . expect ( "refreshed_files should be an array" ) ;
889+ assert ! ( refreshed_files2. is_empty( ) ,
890+ "No files should be refreshed on second call since all are present" ) ;
891+
892+ // Clean up both backends using the helper function
902893 cleanup_test_resources ( & backend2) . await ?;
903894 {
904895 let backend = get_backend ( ) . await ?;
905- backend . stop ( ) . await . expect ( "Backend failed to stop" ) ;
896+ cleanup_test_resources ( & backend ) . await ? ;
906897 }
907898
908- tokio:: time:: sleep ( Duration :: from_secs ( 1 ) ) . await ;
909-
910899 Ok ( ( ) )
911900 }
912901 #[ actix_web:: test]
0 commit comments