#!/bin/bash ################################################################################ # test-integration.sh # Integration tests for the complete ROM organization workflow ################################################################################ # Get the directory of the current script SCRIPT_DIR="$(dirname "$(realpath "$0")")" # Determine the project root directory PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" # Source the test framework source "${SCRIPT_DIR}/test-framework.sh" # Source the script utilities we need for testing source "${PROJECT_ROOT}/core/config.sh" source "${PROJECT_ROOT}/core/logger.sh" source "${PROJECT_ROOT}/core/utils.sh" # Override setup for our tests setup_test() { # Create mock directory structure TEST_EMU_DIR="$TEST_TEMP_DIR/mock_emu" TEST_SOURCE_DIR="$TEST_EMU_DIR/source/myrient.erista.me/files/No-Intro/Nintendo - Nintendo Entertainment System (Headered)" TEST_FILTER_DIR="$TEST_EMU_DIR/filter" TEST_ORG_DIR="$TEST_EMU_DIR/org" TEST_STAGE_DIR="$TEST_EMU_DIR/stage" TEST_SHARE_DIR="$TEST_EMU_DIR/share" TEST_LISTS_DIR="$TEST_EMU_DIR/lists" TEST_EXTRA_DIR="$TEST_EMU_DIR/source/extra/nes" TEST_ROMORG_DIR="$TEST_EMU_DIR/rom-organizer" TEST_SYSTEMS_DIR="$TEST_ROMORG_DIR/systems" # Create directory structure create_test_dir "$TEST_SOURCE_DIR" create_test_dir "$TEST_FILTER_DIR" create_test_dir "$TEST_ORG_DIR" create_test_dir "$TEST_STAGE_DIR" create_test_dir "$TEST_SHARE_DIR" create_test_dir "$TEST_LISTS_DIR" create_test_dir "$TEST_EXTRA_DIR" create_test_dir "$TEST_ROMORG_DIR" create_test_dir "$TEST_SYSTEMS_DIR" # Create sample ROMs in source directory create_test_file "$TEST_SOURCE_DIR/Super Mario Bros (USA).nes" "fake rom content 1" create_test_file "$TEST_SOURCE_DIR/Super Mario Bros (Europe).nes" "fake rom content 2" create_test_file "$TEST_SOURCE_DIR/Legend of Zelda (USA).nes" "fake rom content 3" create_test_file "$TEST_SOURCE_DIR/Problematic Game (USA).nes" "fake problem rom" create_test_file "$TEST_SOURCE_DIR/Good Game (USA).nes" "fake good rom" # Create sample ROMs in extra directory create_test_file "$TEST_EXTRA_DIR/Homebrew Game.nes" "fake homebrew content" create_test_file "$TEST_EXTRA_DIR/Translation Patch.nes" "fake translation content" # Create test list files cat > "$TEST_LISTS_DIR/nes-problematic-games" << EOF Problematic Game (USA).nes EOF cat > "$TEST_LISTS_DIR/nes+best-games" << EOF Super Mario Bros (USA).nes Legend of Zelda (USA).nes EOF cat > "$TEST_LISTS_DIR/nes.homebrew-games" << EOF Homebrew Game.nes Translation Patch.nes EOF # Create mock system-specific script cat > "$TEST_SYSTEMS_DIR/custom-nes.sh" << 'EOF' #!/bin/bash # Mock NES system-specific processing source "${ROMGROOMER_SCRIPT_DIR}/logger.sh" log "INFO" "Mock NES processing: checking ROM headers" log "INFO" "Mock NES processing: validating mappers" # Simulate successful processing exit 0 EOF chmod +x "$TEST_SYSTEMS_DIR/custom-nes.sh" # Copy required scripts to test rom-organizer directory cp "${PROJECT_ROOT}/config.sh" "$TEST_ROMORG_DIR/" cp "${PROJECT_ROOT}/logger.sh" "$TEST_ROMORG_DIR/" cp "${PROJECT_ROOT}/utils.sh" "$TEST_ROMORG_DIR/" cp "${PROJECT_ROOT}/config-arrays.sh" "$TEST_ROMORG_DIR/" cp "${PROJECT_ROOT${PROJECT_ROOT}/tools/preflight-checks.sh" "$TEST_ROMORG_DIR/" cp "${PROJECT_ROOT}/nointro-processor.sh" "$TEST_ROMORG_DIR/" cp "${PROJECT_ROOT}/organize-roms.sh" "$TEST_ROMORG_DIR/" cp "${PROJECT_ROOT}/delete-listfiles.sh" "$TEST_ROMORG_DIR/" cp "${PROJECT_ROOT}/link-listfiles.sh" "$TEST_ROMORG_DIR/" # Override configuration for test environment export EMUBASEDIR="$TEST_EMU_DIR" export SOURCEBASEDIR="$TEST_EMU_DIR/source" export FILTERBASEDIR="$TEST_EMU_DIR/filter" export ORGBASEDIR="$TEST_EMU_DIR/org" export STAGEBASEDIR="$TEST_EMU_DIR/stage" export SHAREBASEDIR="$TEST_EMU_DIR/share" export EMULISTSDIR="$TEST_EMU_DIR/lists" export EXTRABASEDIR="$TEST_EMU_DIR/source/extra" export ROMGROOMERDIR="$TEST_ROMORG_DIR" export EMUROMSDIR="$TEST_EMU_DIR/roms" export LOG_LEVEL="ERROR" # Reduce noise in tests } # Test Step 1: Copy Source to Filter Directory test_step1_copy_source_to_filter() { local filter_dest="$TEST_FILTER_DIR/Nintendo - Nintendo Entertainment System (Headered).usa.1g1r" # Manually call copy_files to test this step copy_files "$TEST_SOURCE_DIR" "$filter_dest" # Verify files were copied assert_file_exists "$filter_dest/Super Mario Bros (USA).nes" "Source files should be copied to filter" assert_file_exists "$filter_dest/Legend of Zelda (USA).nes" "Source files should be copied to filter" # Verify originals still exist assert_file_exists "$TEST_SOURCE_DIR/Super Mario Bros (USA).nes" "Original source files should remain" } # Test Step 3: Delete Problematic ROMs test_step3_delete_problematic_roms() { local filter_dir="$TEST_FILTER_DIR/Nintendo - Nintendo Entertainment System (Headered).usa.1g1r" # First copy files to filter directory copy_files "$TEST_SOURCE_DIR" "$filter_dir" # Verify problematic file exists before deletion assert_file_exists "$filter_dir/Problematic Game (USA).nes" "Problematic file should exist before deletion" # Run delete script "${PROJECT_ROOT}/delete-listfiles.sh" "$filter_dir" "$TEST_LISTS_DIR/nes-problematic-games" # Verify problematic file was deleted assert_file_not_exists "$filter_dir/Problematic Game (USA).nes" "Problematic file should be deleted" # Verify good files remain assert_file_exists "$filter_dir/Good Game (USA).nes" "Good files should remain" } # Test Step 6: Organize ROMs into subdirectories test_step6_organize_roms() { local org_dir="$TEST_ORG_DIR/nes.usa.1g1r" # Create organization directory with test files create_test_dir "$org_dir" create_test_file "$org_dir/Super Mario Bros (USA).nes" "fake rom content" create_test_file "$org_dir/Legend of Zelda (Europe).nes" "fake rom content" # Run organization with region flag "${PROJECT_ROOT}/organize-roms.sh" "$org_dir" -r # Verify organization occurred assert_file_exists "$org_dir/By Region/USA/Super Mario Bros (USA).nes" "USA ROM should be organized" assert_file_exists "$org_dir/By Region/Europe/Legend of Zelda (Europe).nes" "Europe ROM should be organized" } # Test Step 7: Add Extra Games with myrient source (+) test_step7_add_extra_games_myrient() { local org_dir="$TEST_ORG_DIR/nes.usa.1g1r" local best_games_dir="$org_dir/Best Games" create_test_dir "$org_dir" # Run link script for myrient source (+) "${PROJECT_ROOT}/link-listfiles.sh" "$TEST_LISTS_DIR/nes+best-games" "$TEST_SOURCE_DIR" "$best_games_dir" # Verify games were linked assert_file_exists "$best_games_dir/Super Mario Bros (USA).nes" "Best game should be linked from myrient" assert_file_exists "$best_games_dir/Legend of Zelda (USA).nes" "Best game should be linked from myrient" } # Test Step 7: Add Extra Games with extra directory (.) test_step7_add_extra_games_extra() { local org_dir="$TEST_ORG_DIR/nes.usa.1g1r" local homebrew_dir="$org_dir/Homebrew Games" create_test_dir "$org_dir" # Run link script for extra directory (.) "${PROJECT_ROOT}/link-listfiles.sh" "$TEST_LISTS_DIR/nes.homebrew-games" "$TEST_EXTRA_DIR" "$homebrew_dir" # Verify homebrew games were linked assert_file_exists "$homebrew_dir/Homebrew Game.nes" "Homebrew game should be linked from extra" assert_file_exists "$homebrew_dir/Translation Patch.nes" "Translation should be linked from extra" } # Test Step 8: Copy Organized Games to Stage Directory test_step8_copy_to_stage() { local org_dir="$TEST_ORG_DIR/nes.usa.1g1r" local stage_dir="$TEST_STAGE_DIR/usa.1g1r/nes" # Create organized directory with test files create_test_dir "$org_dir" create_test_file "$org_dir/Game1.nes" "fake content 1" create_test_file "$org_dir/Game2.nes" "fake content 2" # Copy to stage copy_files "$org_dir" "$stage_dir" # Verify files were copied to stage assert_file_exists "$stage_dir/Game1.nes" "Files should be copied to stage" assert_file_exists "$stage_dir/Game2.nes" "Files should be copied to stage" } # Test Step 11: Copy Staged Games to Share Directory test_step11_copy_to_share() { local stage_dir="$TEST_STAGE_DIR/usa.1g1r/nes" local share_dir="$TEST_SHARE_DIR/usa.1g1r/nes" # Create staged directory with test files create_test_dir "$stage_dir" create_test_file "$stage_dir/Game1.nes" "fake content 1" create_test_file "$stage_dir/Game2.nes" "fake content 2" # Copy to share copy_files "$stage_dir" "$share_dir" # Verify files were copied to share assert_file_exists "$share_dir/Game1.nes" "Files should be copied to share" assert_file_exists "$share_dir/Game2.nes" "Files should be copied to share" } # Test the pre-flight checks in the integration context test_integration_preflight_checks() { # Change to test rom-organizer directory cd "$TEST_ROMORG_DIR" # Override LOG_LEVEL to reduce noise local original_log_level="$LOG_LEVEL" LOG_LEVEL="ERROR" # Source and run pre-flight checks source "${PROJECT_ROOT}/tools/preflight-checks.sh" # Should pass with our mock directory structure assert_exit_code "0" "run_preflight_checks" # Restore log level LOG_LEVEL="$original_log_level" # Return to original directory cd "$SCRIPT_DIR" } # Test system-specific script execution (Step 5) test_integration_system_specific_script() { # Change to test rom-organizer directory cd "$TEST_ROMORG_DIR" # Set up environment variables export SYSTEM="nes" export FILTER="usa.1g1r" export ORGSYSTEMDIR="$TEST_ORG_DIR/nes.usa.1g1r" export ROMGROOMER_SCRIPT_DIR="$TEST_ROMORG_DIR" # Execute the system-specific script assert_exit_code "0" "./systems/custom-nes.sh" # Return to original directory cd "$SCRIPT_DIR" } # Test comprehensive workflow simulation (non-interactive) test_integration_workflow_simulation() { # This test simulates a workflow without user interaction # Step 1-2: Set up source and filter (simulate manual ROM Manager filtering) local filter_dir="$TEST_FILTER_DIR/Nintendo - Nintendo Entertainment System (Headered).usa.1g1r" copy_files "$TEST_SOURCE_DIR" "$filter_dir" # Remove problematic game to simulate ROM Manager filtering rm -f "$filter_dir/Problematic Game (USA).nes" # Step 3: Delete any remaining problematic ROMs if [ -f "$TEST_LISTS_DIR/nes-problematic-games" ]; then "${PROJECT_ROOT}/delete-listfiles.sh" "$filter_dir" "$TEST_LISTS_DIR/nes-problematic-games" 2>/dev/null || true fi # Step 4: Copy to organization directory local org_dir="$TEST_ORG_DIR/nes.usa.1g1r" copy_files "$filter_dir" "$org_dir" # Step 5: System-specific processing (simulate) export SYSTEM="nes" export ROMGROOMER_SCRIPT_DIR="$TEST_ROMORG_DIR" cd "$TEST_ROMORG_DIR" "./systems/custom-nes.sh" 2>/dev/null || true cd "$SCRIPT_DIR" # Step 6: Organize ROMs "${PROJECT_ROOT}/organize-roms.sh" "$org_dir" -r -u # Step 7: Add extra games "${PROJECT_ROOT}/link-listfiles.sh" "$TEST_LISTS_DIR/nes+best-games" "$TEST_SOURCE_DIR" "$org_dir/Best Games" "${PROJECT_ROOT}/link-listfiles.sh" "$TEST_LISTS_DIR/nes.homebrew-games" "$TEST_EXTRA_DIR" "$org_dir/Homebrew Games" # Step 8: Copy to stage local stage_dir="$TEST_STAGE_DIR/usa.1g1r/nes" copy_files "$org_dir" "$stage_dir" # Step 11: Copy to share local share_dir="$TEST_SHARE_DIR/usa.1g1r/nes" copy_files "$stage_dir" "$share_dir" # Verify final result assert_file_exists "$share_dir/By Region/USA/Super Mario Bros (USA).nes" "Final organized ROM should exist in share" assert_file_exists "$share_dir/Best Games/Legend of Zelda (USA).nes" "Extra game should exist in share" assert_file_exists "$share_dir/Homebrew Games/Homebrew Game.nes" "Homebrew game should exist in share" # Verify problematic game was removed assert_file_not_exists "$share_dir/Problematic Game (USA).nes" "Problematic game should not exist in final result" } # Main test runner main() { echo "Running integration tests..." run_tests } # Run tests if this script is executed directly if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main "$@" fi