#!/bin/bash ################################################################################ # test-organize-roms.sh # Unit tests for organize-roms.sh ROM organization functions ################################################################################ # 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" source "${PROJECT_ROOT}/core/config-arrays.sh" # Override setup for our tests setup_test() { # Create test ROM directory with sample ROMs TEST_ROM_DIR="$TEST_TEMP_DIR/test_roms" create_test_dir "$TEST_ROM_DIR" # Create sample ROM files with various naming patterns create_test_file "$TEST_ROM_DIR/Super Mario Bros (USA).nes" "fake rom content" create_test_file "$TEST_ROM_DIR/Super Mario Bros (Europe).nes" "fake rom content" create_test_file "$TEST_ROM_DIR/Super Mario Bros (Japan).nes" "fake rom content" create_test_file "$TEST_ROM_DIR/Super Mario Bros (En,Fr,De).nes" "fake rom content" create_test_file "$TEST_ROM_DIR/Super Mario Bros (Prototype).nes" "fake rom content" create_test_file "$TEST_ROM_DIR/Super Mario Bros (Aftermarket - Unl).nes" "fake rom content" create_test_file "$TEST_ROM_DIR/Super Mario Bros (Pirate).nes" "fake rom content" create_test_file "$TEST_ROM_DIR/Legend of Zelda (USA).nes" "fake rom content" create_test_file "$TEST_ROM_DIR/Final Fantasy (Japan, En).nes" "fake rom content" } # Test organize_language function test_organize_language() { # Source the organize script to get access to its functions source "${PROJECT_ROOT}/organizers/organize-roms.sh" # Test organizing by English language organize_language "$TEST_ROM_DIR" "En" # Check that language directory was created assert_file_exists "$TEST_ROM_DIR/By Language/En" "English language directory should be created" # Check that English ROMs were moved assert_file_exists "$TEST_ROM_DIR/By Language/En/Super Mario Bros (En,Fr,De).nes" "Multi-language ROM should be in En directory" assert_file_exists "$TEST_ROM_DIR/By Language/En/Final Fantasy (Japan, En).nes" "Japanese ROM with En should be in En directory" # Check that non-English ROMs remain in root assert_file_exists "$TEST_ROM_DIR/Super Mario Bros (USA).nes" "USA ROM should remain in root" assert_file_exists "$TEST_ROM_DIR/Super Mario Bros (Japan).nes" "Japan ROM should remain in root" } # Test organize_region function test_organize_region() { source "${PROJECT_ROOT}/organizers/organize-roms.sh" # Test organizing by USA region organize_region "$TEST_ROM_DIR" "USA" # Check that region directory was created assert_file_exists "$TEST_ROM_DIR/By Region/USA" "USA region directory should be created" # Check that USA ROMs were moved assert_file_exists "$TEST_ROM_DIR/By Region/USA/Super Mario Bros (USA).nes" "USA ROM should be in USA directory" assert_file_exists "$TEST_ROM_DIR/By Region/USA/Legend of Zelda (USA).nes" "USA ROM should be in USA directory" # Check that non-USA ROMs remain in root assert_file_exists "$TEST_ROM_DIR/Super Mario Bros (Europe).nes" "Europe ROM should remain in root" assert_file_exists "$TEST_ROM_DIR/Super Mario Bros (Japan).nes" "Japan ROM should remain in root" } # Test organize_kind function test_organize_kind() { source "${PROJECT_ROOT}/organizers/organize-roms.sh" # Test organizing by Aftermarket kind organize_kind "$TEST_ROM_DIR" "Aftermarket" # Check that kind directory was created assert_file_exists "$TEST_ROM_DIR/By Kind/Aftermarket" "Aftermarket kind directory should be created" # Check that Aftermarket ROMs were moved assert_file_exists "$TEST_ROM_DIR/By Kind/Aftermarket/Super Mario Bros (Aftermarket - Unl).nes" "Aftermarket ROM should be in Aftermarket directory" # Test organizing by Pirate kind organize_kind "$TEST_ROM_DIR" "Pirate" # Check that Pirate directory was created and ROM moved assert_file_exists "$TEST_ROM_DIR/By Kind/Pirate" "Pirate kind directory should be created" assert_file_exists "$TEST_ROM_DIR/By Kind/Pirate/Super Mario Bros (Pirate).nes" "Pirate ROM should be in Pirate directory" } # Test organize_usa_preference function test_organize_usa_preference() { source "${PROJECT_ROOT}/organizers/organize-roms.sh" # Test USA preference organization organize_usa_preference "$TEST_ROM_DIR" # Check that USA preference directory was created assert_file_exists "$TEST_ROM_DIR/By Region/USA" "USA preference directory should be created" # Check that USA ROMs were moved to USA directory assert_file_exists "$TEST_ROM_DIR/By Region/USA/Super Mario Bros (USA).nes" "USA ROM should be prioritized" assert_file_exists "$TEST_ROM_DIR/By Region/USA/Legend of Zelda (USA).nes" "USA ROM should be prioritized" # Check that Europe directory was created for non-USA ROMs assert_file_exists "$TEST_ROM_DIR/By Region/Europe" "Europe directory should be created" assert_file_exists "$TEST_ROM_DIR/By Region/Europe/Super Mario Bros (Europe).nes" "Europe ROM should be in Europe directory" } # Test main organize script with different parameter combinations test_organize_main_script_language() { # Create fresh test directory local test_dir="$TEST_TEMP_DIR/organize_test_lang" create_test_dir "$test_dir" create_test_file "$test_dir/Game (En,Fr).nes" "test content" create_test_file "$test_dir/Game (USA).nes" "test content" # Test with language flag assert_exit_code "0" "${SCRIPT_DIR}/organize-roms.sh '$test_dir' -l" # Verify language organization occurred assert_file_exists "$test_dir/By Language" "By Language directory should be created" } test_organize_main_script_region() { # Create fresh test directory local test_dir="$TEST_TEMP_DIR/organize_test_region" create_test_dir "$test_dir" create_test_file "$test_dir/Game (USA).nes" "test content" create_test_file "$test_dir/Game (Europe).nes" "test content" # Test with region flag assert_exit_code "0" "${SCRIPT_DIR}/organize-roms.sh '$test_dir' -r" # Verify region organization occurred assert_file_exists "$test_dir/By Region" "By Region directory should be created" } test_organize_main_script_kind() { # Create fresh test directory local test_dir="$TEST_TEMP_DIR/organize_test_kind" create_test_dir "$test_dir" create_test_file "$test_dir/Game (Prototype).nes" "test content" create_test_file "$test_dir/Game (Aftermarket).nes" "test content" # Test with kind flag assert_exit_code "0" "${SCRIPT_DIR}/organize-roms.sh '$test_dir' -k" # Verify kind organization occurred assert_file_exists "$test_dir/By Kind" "By Kind directory should be created" } test_organize_main_script_usa() { # Create fresh test directory local test_dir="$TEST_TEMP_DIR/organize_test_usa" create_test_dir "$test_dir" create_test_file "$test_dir/Game (USA).nes" "test content" create_test_file "$test_dir/Game (Europe).nes" "test content" # Test with USA preference flag assert_exit_code "0" "${SCRIPT_DIR}/organize-roms.sh '$test_dir' -u" # Verify USA preference organization occurred assert_file_exists "$test_dir/By Region/USA" "USA directory should be created" } test_organize_main_script_combined() { # Create fresh test directory local test_dir="$TEST_TEMP_DIR/organize_test_combined" create_test_dir "$test_dir" create_test_file "$test_dir/Game (USA, En).nes" "test content" create_test_file "$test_dir/Game (Europe, Fr).nes" "test content" create_test_file "$test_dir/Game (Prototype).nes" "test content" # Test with multiple flags assert_exit_code "0" "${SCRIPT_DIR}/organize-roms.sh '$test_dir' -l -r -k -u" # Verify all organization types occurred assert_file_exists "$test_dir/By Language" "By Language directory should be created" assert_file_exists "$test_dir/By Region" "By Region directory should be created" assert_file_exists "$test_dir/By Kind" "By Kind directory should be created" } # Test error handling test_organize_invalid_directory() { # Test with non-existent directory assert_exit_code "1" "${PROJECT_ROOT}/organizers/organize-roms.sh '/nonexistent/directory' -l" } test_organize_no_parameters() { # Test with no parameters (should show usage) assert_exit_code "1" "${PROJECT_ROOT}/organizers/organize-roms.sh" } # Main test runner main() { echo "Running organize-roms.sh tests..." run_tests } # Run tests if this script is executed directly if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main "$@" fi