<?php
/**
 * Test script to verify the WebP Express bulk conversion fix
 * This script tests the file existence validation and error handling
 */

// Include the plugin's autoloader
require_once __DIR__ . '/lib/classes/Convert.php';

// Mock the necessary WordPress functions and classes
if (!function_exists('wp_send_json_error')) {
    function wp_send_json_error($message) {
        echo json_encode(['success' => false, 'data' => $message]);
        exit;
    }
}

if (!function_exists('wp_die')) {
    function wp_die() {
        exit;
    }
}

// Test cases
echo "=== WebP Express Bulk Conversion Fix Test ===\n";

// Test 1: Non-existent file handling
echo "\nTest 1: Testing non-existent file handling...\n";

try {
    // This should fail gracefully now instead of causing an exception
    $result = \WebPExpress\Convert::convertFile('/non/existent/file.jpg');

    if ($result['success'] === false && strpos($result['msg'], 'does not exist') !== false) {
        echo "✓ PASS: Non-existent file handled correctly\n";
        echo "  Message: " . $result['msg'] . "\n";
    } else {
        echo "✗ FAIL: Non-existent file not handled properly\n";
        echo "  Result: " . print_r($result, true) . "\n";
    }
} catch (Exception $e) {
    echo "✗ FAIL: Exception thrown for non-existent file: " . $e->getMessage() . "\n";
}

// Test 2: Test ConvertHelperIndependent as well
echo "\nTest 2: Testing ConvertHelperIndependent with non-existent file...\n";

try {
    $result = \WebPExpress\ConvertHelperIndependent::convert(
        '/non/existent/file.jpg',
        '/tmp/test.webp',
        ['quality' => 80]
    );

    if ($result['success'] === false && strpos($result['msg'], 'does not exist') !== false) {
        echo "✓ PASS: ConvertHelperIndependent handles non-existent files correctly\n";
        echo "  Message: " . $result['msg'] . "\n";
    } else {
        echo "✗ FAIL: ConvertHelperIndependent did not handle non-existent file properly\n";
        echo "  Result: " . print_r($result, true) . "\n";
    }
} catch (Exception $e) {
    echo "✗ FAIL: Exception thrown in ConvertHelperIndependent: " . $e->getMessage() . "\n";
}

echo "\n=== Test Complete ===\n";
echo "\nSummary of fixes applied:\n";
echo "1. ✓ Added file existence validation in Convert::convertFile() before SanityCheck\n";
echo "2. ✓ Added file existence validation in ConvertHelperIndependent::convert() before SanityCheck\n";
echo "3. ✓ Added file existence check in BulkConvert::getListRecursively() to filter out missing files\n";
echo "4. ✓ Added 30-second timeout to AJAX requests in bulk-convert.js\n";
echo "5. ✓ Improved error handling in bulk-convert.js to continue on failures\n";
echo "6. ✓ Modified error handling to only stop on critical security errors, not file-specific errors\n";

echo "\nThese fixes should resolve the issue where bulk conversion gets stuck on missing files.\n";
echo "The plugin will now:\n";
echo "- Skip missing files during bulk conversion listing\n";
echo "- Handle missing files gracefully during conversion\n";
echo "- Continue processing other files when one fails\n";
echo "- Timeout after 30 seconds per file to prevent hanging\n";
echo "- Only stop the process for critical security errors\n";