#!/usr/bin/env bash ################################################################################ # test-organize-ondemand.sh - Tests on-demand region/language/kind folder creation # Part of the rom-groomer project # # This test verifies that organize-roms.sh only creates subdirectories when # ROMs matching those criteria actually exist. ################################################################################ # Determine the directory of the current script SCRIPT_DIR="$(dirname "$(realpath "$0")")" # Determine the project root directory PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" # Load test framework and dependencies source "${PROJECT_ROOT}/tests/test-framework.sh" ################################################################################ # Test: Only creates region folders for regions that have ROMs ################################################################################ test_only_creates_needed_region_folders() { local test_dir test_dir=$(mktemp -d) # Create test ROMs with specific regions mkdir -p "${test_dir}/roms" touch "${test_dir}/roms/Super Mario Bros (USA).zip" touch "${test_dir}/roms/Final Fantasy (Japan).zip" touch "${test_dir}/roms/Zelda (USA).zip" # Note: No Europe ROMs # Run organization "${PROJECT_ROOT}/organizers/organize-roms.sh" "${test_dir}/roms" -r 2>/dev/null # Check that only USA and Japan folders were created assert_dir_exists "${test_dir}/roms/By Region/USA" "USA folder should exist" assert_dir_exists "${test_dir}/roms/By Region/Japan" "Japan folder should exist" # Verify Europe folder was NOT created if [[ -d "${test_dir}/roms/By Region/Europe" ]]; then test_fail "Europe folder should NOT exist (no Europe ROMs)" else test_pass "Europe folder not created (as expected)" fi # Verify ROMs are in correct folders assert_file_exists "${test_dir}/roms/By Region/USA/Super Mario Bros (USA).zip" "USA ROM should be organized" assert_file_exists "${test_dir}/roms/By Region/Japan/Final Fantasy (Japan).zip" "Japan ROM should be organized" rm -rf "$test_dir" } ################################################################################ # Test: No folders created when no matching ROMs exist ################################################################################ test_no_folders_when_no_matching_roms() { local test_dir test_dir=$(mktemp -d) # Create test ROMs without region tags mkdir -p "${test_dir}/roms" touch "${test_dir}/roms/Game1.zip" touch "${test_dir}/roms/Game2.zip" # Run organization by region "${PROJECT_ROOT}/organizers/organize-roms.sh" "${test_dir}/roms" -r 2>/dev/null # Check that By Region directory either doesn't exist or has no subdirectories if [[ -d "${test_dir}/roms/By Region" ]]; then local folder_count folder_count=$(find "${test_dir}/roms/By Region" -mindepth 1 -maxdepth 1 -type d 2>/dev/null | wc -l) assert_equals "0" "$folder_count" "No region folders should be created when no tagged ROMs exist" else test_pass "By Region directory not created (as expected)" fi rm -rf "$test_dir" } ################################################################################ # Test: Only creates language folders for languages that have ROMs ################################################################################ test_only_creates_needed_language_folders() { local test_dir test_dir=$(mktemp -d) # Create test ROMs with specific languages mkdir -p "${test_dir}/roms" touch "${test_dir}/roms/Game1 (En).zip" touch "${test_dir}/roms/Game2 (En).zip" touch "${test_dir}/roms/Game3 (Fr).zip" # Note: No Spanish ROMs # Run organization "${PROJECT_ROOT}/organizers/organize-roms.sh" "${test_dir}/roms" -l 2>/dev/null # Check that only En and Fr folders were created assert_dir_exists "${test_dir}/roms/By Language/En" "English folder should exist" assert_dir_exists "${test_dir}/roms/By Language/Fr" "French folder should exist" # Verify Spanish folder was NOT created if [[ -d "${test_dir}/roms/By Language/Es" ]]; then test_fail "Spanish folder should NOT exist (no Spanish ROMs)" else test_pass "Spanish folder not created (as expected)" fi rm -rf "$test_dir" } ################################################################################ # Test: Only creates kind folders for kinds that have ROMs ################################################################################ test_only_creates_needed_kind_folders() { local test_dir test_dir=$(mktemp -d) # Create test ROMs with specific kinds mkdir -p "${test_dir}/roms" touch "${test_dir}/roms/Game1 [Aftermarket].zip" touch "${test_dir}/roms/Game2 [Aftermarket].zip" # Note: No Pirate or other kinds # Run organization "${PROJECT_ROOT}/organizers/organize-roms.sh" "${test_dir}/roms" -k 2>/dev/null # Check that only Aftermarket folder was created assert_dir_exists "${test_dir}/roms/By Kind/Aftermarket" "Aftermarket folder should exist" # Verify Pirate folder was NOT created if [[ -d "${test_dir}/roms/By Kind/Pirate" ]]; then test_fail "Pirate folder should NOT exist (no Pirate ROMs)" else test_pass "Pirate folder not created (as expected)" fi rm -rf "$test_dir" } ################################################################################ # Test: Dry-run mode doesn't create any folders ################################################################################ test_dry_run_no_folders() { local test_dir test_dir=$(mktemp -d) # Create test ROMs mkdir -p "${test_dir}/roms" touch "${test_dir}/roms/Game (USA).zip" # Run organization in dry-run mode "${PROJECT_ROOT}/organizers/organize-roms.sh" "${test_dir}/roms" -r --dry-run 2>/dev/null # Check that no folders were created if [[ -d "${test_dir}/roms/By Region" ]]; then test_fail "By Region folder should NOT exist in dry-run mode" else test_pass "No folders created in dry-run mode (as expected)" fi # Original ROM should still be in place assert_file_exists "${test_dir}/roms/Game (USA).zip" "Original ROM should not be moved in dry-run mode" rm -rf "$test_dir" } ################################################################################ # Main test runner ################################################################################ main() { echo "Running on-demand folder creation tests..." run_tests } # Run tests if this script is executed directly if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main "$@" fi