#!/bin/bash ################################################################################ # test-sort2folders.sh # Unit tests for the sort2folders.sh utility script # # Tests the alphabetical organization functionality for ROM files, # ensuring proper directory creation and file movement for flashcart navigation. # # Usage: ${PROJECT_ROOT}/test-sort2folders.sh ################################################################################ # 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" ################################################################################ # TEST: Script exists and is executable ################################################################################ test_sort2folders_exists() { local script_path="${PROJECT_ROOT}/organizers/sort2folders.sh" assert_file_exists "$script_path" "sort2folders.sh should exist" assert_file_executable "$script_path" "sort2folders.sh should be executable" } ################################################################################ # TEST: Script shows usage when no arguments provided ################################################################################ test_sort2folders_usage() { local script_path="${PROJECT_ROOT}/organizers/sort2folders.sh" local output output=$("$script_path" 2>&1) local exit_code=$? assert_equals 1 $exit_code "Script should exit with code 1 when no arguments provided" assert_contains "$output" "Usage:" "Should display usage information" assert_contains "$output" "flashcart navigation" "Should mention flashcart navigation" } ################################################################################ # TEST: Script validates input directory ################################################################################ test_sort2folders_validation() { local script_path="${PROJECT_ROOT}/organizers/sort2folders.sh" local output # Test with non-existent directory output=$("$script_path" "/nonexistent/directory" 2>&1) local exit_code=$? assert_equals 1 $exit_code "Script should exit with code 1 for non-existent directory" assert_contains "$output" "Directory does not exist" "Should report non-existent directory" } ################################################################################ # TEST: Script organizes files correctly with smart grouping ################################################################################ test_sort2folders_organization() { local test_dir="${TEST_TEMP_DIR}/sort_test" local script_path="${PROJECT_ROOT}/organizers/sort2folders.sh" # Create test directory and sample files mkdir -p "$test_dir" # Create test files with various first characters touch "$test_dir/Advance Wars.gba" touch "$test_dir/Bomberman.gba" touch "$test_dir/Castlevania.gba" touch "$test_dir/1942.gba" touch "$test_dir/007 - Everything or Nothing.gba" touch "$test_dir/@#$%Test.gba" touch "$test_dir/zelda - Link's Awakening.gba" # Run the script with small grouping to force multiple directories local output output=$("$script_path" "$test_dir" 3 2>&1) local exit_code=$? assert_equals 0 $exit_code "Script should exit successfully" # Check that some directories were created (exact structure depends on smart grouping) local total_dirs total_dirs=$(find "$test_dir" -maxdepth 1 -type d ! -path "$test_dir" | wc -l) assert_greater_than 0 "$total_dirs" "Should create at least one directory" # Check that all original files are gone from root assert_file_not_exists "$test_dir/Advance Wars.gba" "Original file should be moved" assert_file_not_exists "$test_dir/Bomberman.gba" "Original file should be moved" assert_file_not_exists "$test_dir/Castlevania.gba" "Original file should be moved" assert_file_not_exists "$test_dir/1942.gba" "Original file should be moved" assert_file_not_exists "$test_dir/007 - Everything or Nothing.gba" "Original file should be moved" assert_file_not_exists "$test_dir/@#$%Test.gba" "Original file should be moved" assert_file_not_exists "$test_dir/zelda - Link's Awakening.gba" "Original file should be moved" # Check that all files were moved somewhere local total_files_after total_files_after=$(find "$test_dir" -type f | wc -l) assert_equals 7 "$total_files_after" "All 7 files should still exist after organization" } ################################################################################ # TEST: Script handles empty directory gracefully ################################################################################ test_sort2folders_empty_directory() { local test_dir="${TEST_TEMP_DIR}/empty_test" local script_path="${PROJECT_ROOT}/organizers/sort2folders.sh" # Create empty test directory mkdir -p "$test_dir" # Run the script local output output=$("$script_path" "$test_dir" 2>&1) local exit_code=$? assert_equals 0 $exit_code "Script should handle empty directory gracefully" assert_contains "$output" "No files to organize" "Should report no files to organize" } ################################################################################ # TEST: Script follows ROM groomer coding standards ################################################################################ test_sort2folders_coding_standards() { local script_path="${PROJECT_ROOT}/organizers/sort2folders.sh" # Check for required header elements assert_contains "$(head -20 "$script_path")" "Script:" "Should have Script header" assert_contains "$(head -20 "$script_path")" "Description:" "Should have Description header" assert_contains "$(head -20 "$script_path")" "Usage:" "Should have Usage header" assert_contains "$(head -20 "$script_path")" "Dependencies:" "Should have Dependencies header" # Check for required variables and functions assert_contains "$(cat "$script_path")" "SCRIPT_NAME=" "Should have SCRIPT_NAME variable" assert_contains "$(cat "$script_path")" "umask 000" "Should set umask 000" assert_contains "$(cat "$script_path")" 'source "${PROJECT_ROOT}/core/config.sh"' "Should source config.sh" assert_contains "$(cat "$script_path")" 'source "${PROJECT_ROOT}/core/logger.sh"' "Should source logger.sh" # Check for proper function structure assert_contains "$(cat "$script_path")" "main()" "Should have main function" assert_contains "$(cat "$script_path")" "show_usage()" "Should have show_usage function" } ################################################################################ # TEST: Script has valid bash syntax ################################################################################ test_sort2folders_syntax() { local script_path="${PROJECT_ROOT}/organizers/sort2folders.sh" # Test bash syntax bash -n "$script_path" assert_equals 0 $? "Script should have valid bash syntax" } ################################################################################ # TEST: Smart grouping with default max files (40) ################################################################################ test_sort2folders_smart_grouping_default() { local test_dir="${TEST_TEMP_DIR}/smart_default_test" local script_path="${PROJECT_ROOT}/organizers/sort2folders.sh" # Create test directory with files that should fit in one group mkdir -p "$test_dir" # Create 15 test files across different letters touch "$test_dir/Alien.rom" "$test_dir/Batman.rom" "$test_dir/Contra.rom" touch "$test_dir/Doom.rom" "$test_dir/Ecco.rom" "$test_dir/Final Fantasy.rom" touch "$test_dir/Golden Axe.rom" "$test_dir/Halo.rom" "$test_dir/Ice Climber.rom" touch "$test_dir/Jet Force.rom" "$test_dir/Kirby.rom" "$test_dir/Legend of Zelda.rom" touch "$test_dir/Mario.rom" "$test_dir/Ninja Gaiden.rom" "$test_dir/Pac-Man.rom" # Run with default grouping (40 files) local output output=$("$script_path" "$test_dir" 2>&1) local exit_code=$? assert_equals 0 $exit_code "Script should complete successfully" assert_contains "$output" "Smart Grouping Strategy:" "Should display grouping strategy" # With 15 files and default 40 max, should create one combined group local group_dirs group_dirs=$(find "$test_dir" -maxdepth 1 -type d ! -path "$test_dir" | wc -l) assert_equals 1 "$group_dirs" "Should create exactly 1 directory with default grouping" # Should have all files moved local files_in_root files_in_root=$(find "$test_dir" -maxdepth 1 -type f | wc -l) assert_equals 0 "$files_in_root" "All files should be moved from root directory" } ################################################################################ # TEST: Smart grouping with small max files (5) ################################################################################ test_sort2folders_smart_grouping_small() { local test_dir="${TEST_TEMP_DIR}/smart_small_test" local script_path="${PROJECT_ROOT}/organizers/sort2folders.sh" # Create test directory with files requiring multiple groups mkdir -p "$test_dir" # Create files that will require multiple groups with max 5 touch "$test_dir/Alien.rom" "$test_dir/Batman.rom" "$test_dir/Contra.rom" touch "$test_dir/Doom.rom" "$test_dir/Ecco.rom" "$test_dir/Final Fantasy.rom" touch "$test_dir/Golden Axe.rom" "$test_dir/Halo.rom" "$test_dir/Ice Climber.rom" touch "$test_dir/Jet Force.rom" "$test_dir/Kirby.rom" "$test_dir/Legend of Zelda.rom" touch "$test_dir/Mario.rom" "$test_dir/Sonic.rom" "$test_dir/Tetris.rom" # Run with small grouping (5 files) local output output=$("$script_path" "$test_dir" 5 2>&1) local exit_code=$? assert_equals 0 $exit_code "Script should complete successfully" assert_contains "$output" "max 5 files per group" "Should report max 5 files per group" # With 15 files and max 5, should create 3 groups local group_dirs group_dirs=$(find "$test_dir" -maxdepth 1 -type d ! -path "$test_dir" | wc -l) assert_greater_than 1 "$group_dirs" "Should create multiple directories with small grouping" # Verify no single directory has more than 5 files for dir in "$test_dir"/*; do if [[ -d "$dir" ]]; then local files_in_dir files_in_dir=$(find "$dir" -type f | wc -l) assert_less_than_or_equal "$files_in_dir" 5 "No directory should have more than 5 files" fi done } ################################################################################ # TEST: Smart grouping handles numbers and symbols correctly (no "Other" fallback) ################################################################################ test_sort2folders_numbers_symbols_no_other() { local test_dir="${TEST_TEMP_DIR}/no_other_test" local script_path="${PROJECT_ROOT}/organizers/sort2folders.sh" # Create test directory with problematic files that used to cause "Other" fallback mkdir -p "$test_dir" # Create files that were previously falling back to "Other" directory touch "$test_dir/'89 Dennou Kyuusei Uranai (Japan).zip" touch "$test_dir/8 Eyes (USA).zip" touch "$test_dir/8 Eyes (Japan).zip" touch "$test_dir/[BIOS] Demo Vision (USA) (Rev 2) (Program).zip" touch "$test_dir/240p Test Suite (World) (v0.20) (Program).zip" touch "$test_dir/4 Nin Shogi (Japan).zip" touch "$test_dir/007 - Everything or Nothing.gba" touch "$test_dir/@#$%Test.gba" # Run with grouping local output output=$("$script_path" "$test_dir" 50 2>&1) local exit_code=$? assert_equals 0 $exit_code "Script should complete successfully" # Critical test: should NOT create an "Other" directory if [[ -d "$test_dir/Other" ]]; then assert_false true "Should NOT create fallback 'Other' directory" else assert_true true "Correctly avoided creating 'Other' directory" fi # Should have created appropriate # groups instead local has_hash_groups=false for dir in "$test_dir"/*; do if [[ -d "$dir" ]]; then local dirname dirname=$(basename "$dir") if [[ "$dirname" == "#"* ]]; then has_hash_groups=true break fi fi done assert_true "$has_hash_groups" "Should create # groups for numbers/symbols instead of Other" # Verify all files were moved (none left in root) local files_in_root files_in_root=$(find "$test_dir" -maxdepth 1 -type f | wc -l) assert_equals 0 "$files_in_root" "All files should be moved from root directory" # Verify all files still exist somewhere local total_files_after total_files_after=$(find "$test_dir" -type f | wc -l) assert_equals 8 "$total_files_after" "All 8 files should still exist after organization" } ################################################################################ # TEST: Smart grouping handles numbers and symbols correctly ################################################################################ test_sort2folders_smart_grouping_symbols() { local test_dir="${TEST_TEMP_DIR}/smart_symbols_test" local script_path="${PROJECT_ROOT}/organizers/sort2folders.sh" # Create test directory with mix of letters, numbers, and symbols mkdir -p "$test_dir" # Create files with various starting characters touch "$test_dir/Alien.rom" "$test_dir/Batman.rom" touch "$test_dir/1942.rom" "$test_dir/007 Bond.rom" touch "$test_dir/@Special.rom" "$test_dir/#Hash.rom" touch "$test_dir/Zelda.rom" # Run with grouping that should merge symbols local output output=$("$script_path" "$test_dir" 10 2>&1) local exit_code=$? assert_equals 0 $exit_code "Script should complete successfully" # Check that symbols/numbers are handled appropriately local has_symbols_group=false local has_symbols_range=false for dir in "$test_dir"/*; do if [[ -d "$dir" ]]; then local dirname dirname=$(basename "$dir") if [[ "$dirname" == "#" ]]; then has_symbols_group=true elif [[ "$dirname" == "#-"* ]]; then has_symbols_range=true fi fi done # Should have either a dedicated symbols group (#) or a range starting with # (e.g., #-A) local symbols_handled=false if [[ "$has_symbols_group" == true || "$has_symbols_range" == true ]]; then symbols_handled=true fi assert_true "$symbols_handled" "Numbers and symbols should be correctly grouped in # directories" } ################################################################################ # TEST: Smart grouping parameter validation ################################################################################ test_sort2folders_smart_grouping_validation() { local test_dir="${TEST_TEMP_DIR}/validation_test" local script_path="${PROJECT_ROOT}/organizers/sort2folders.sh" mkdir -p "$test_dir" touch "$test_dir/test.rom" # Test invalid max_files_per_group values local output # Test zero output=$("$script_path" "$test_dir" 0 2>&1) assert_equals 1 $? "Should reject max_files_per_group of 0" assert_contains "$output" "Invalid argument" "Should report invalid parameter" # Test negative number output=$("$script_path" "$test_dir" -5 2>&1) assert_equals 1 $? "Should reject negative max_files_per_group" # Test non-numeric output=$("$script_path" "$test_dir" "abc" 2>&1) assert_equals 1 $? "Should reject non-numeric max_files_per_group" # Test valid number output=$("$script_path" "$test_dir" 25 2>&1) assert_equals 0 $? "Should accept valid max_files_per_group" assert_contains "$output" "max 25 files per group" "Should use specified max files" } ################################################################################ # TEST: Smart grouping preserves file content and permissions ################################################################################ test_sort2folders_smart_grouping_preservation() { local test_dir="${TEST_TEMP_DIR}/preservation_test" local script_path="${PROJECT_ROOT}/organizers/sort2folders.sh" mkdir -p "$test_dir" # Create test file with specific content and permissions echo "test content" > "$test_dir/Test File.rom" chmod 644 "$test_dir/Test File.rom" # Get original file info local original_content original_content=$(cat "$test_dir/Test File.rom") local original_perms original_perms=$(stat -c "%a" "$test_dir/Test File.rom") # Run the script "$script_path" "$test_dir" 2>&1 >/dev/null # Find the moved file local moved_file moved_file=$(find "$test_dir" -name "Test File.rom" -type f) assert_file_exists "$moved_file" "File should exist after move" # Check content preservation local new_content new_content=$(cat "$moved_file") assert_equals "$original_content" "$new_content" "File content should be preserved" # Check permissions preservation - just verify file is readable and writable local new_perms new_perms=$(stat -c "%a" "$moved_file") if [[ "$new_perms" == "666" || "$new_perms" == "644" ]]; then test_pass "File has appropriate permissions ($new_perms)" else test_fail "File should have reasonable permissions, got: $new_perms" fi } ################################################################################ # Main test execution ################################################################################ main() { # Initialize test framework init_test_framework # Run all tests run_test "test_sort2folders_exists" run_test "test_sort2folders_usage" run_test "test_sort2folders_validation" run_test "test_sort2folders_organization" run_test "test_sort2folders_empty_directory" run_test "test_sort2folders_coding_standards" run_test "test_sort2folders_syntax" run_test "test_sort2folders_smart_grouping_default" run_test "test_sort2folders_smart_grouping_small" run_test "test_sort2folders_numbers_symbols_no_other" run_test "test_sort2folders_smart_grouping_symbols" run_test "test_sort2folders_smart_grouping_validation" run_test "test_sort2folders_smart_grouping_preservation" print_test_summary cleanup_test_framework # Exit with failure code if any tests failed [ $TESTS_FAILED -eq 0 ] } # Run main function main