//! WASM SIMD128 verification tests.
//!
//! These tests verify that WASM SIMD128 is properly enabled and working.
//! Run with: RUSTFLAGS="-C target-feature=+simd128" cargo test --target wasm32-wasip1 --features wasm-simd

#![cfg(all(target_arch = "wasm32", feature = "wasm-simd"))]

#[allow(unused_imports)]
use jpegli::encoder::ChromaSubsampling;

/// Verify SIMD128 target feature is enabled at compile time.
#[test]
fn simd128_enabled() {
    // This will fail to compile if simd128 is not enabled
    #[cfg(not(target_feature = "simd128"))]
    compile_error!(
        "wasm-simd feature requires SIMD128. Build with: \
         RUSTFLAGS=\"-C target-feature=+simd128\" cargo test --target wasm32-wasip1"
    );

    #[cfg(target_feature = "simd128")]
    {
        // SIMD128 is enabled - test passes
        assert!(true, "SIMD128 is enabled");
    }
}

/// Verify wide crate operations work with SIMD128.
#[test]
#[cfg(target_feature = "simd128")]
fn wide_simd_operations() {
    use wide::i32x8;

    // Basic SIMD operations
    let a = i32x8::splat(10);
    let b = i32x8::splat(3);
    let sum = a + b;
    let expected = i32x8::splat(13);

    assert_eq!(sum.to_array(), expected.to_array());

    // Multiplication
    let product = a * b;
    let expected = i32x8::splat(30);
    assert_eq!(product.to_array(), expected.to_array());
}

/// Verify IDCT produces correct results with SIMD128.
#[test]
#[cfg(target_feature = "simd128")]
fn idct_parity_with_simd128() {
    use enough::Unstoppable;

    // Simple DCT coefficients (DC only)
    let mut input = [0i32; 64];
    input[0] = 1024; // DC coefficient

    // The IDCT should produce uniform output for DC-only input
    // Just verify encoder creation works with SIMD128
    let config = jpegli::encoder::EncoderConfig::new(90.0, ChromaSubsampling::Quarter);
    let mut enc = config
        .encode_from_bytes(8, 8, jpegli::encoder::PixelLayout::Gray8Srgb)
        .expect("encoder setup");
    enc.push_packed(&[128u8; 64], enough::Unstoppable)
        .expect("push");
    assert!(enc.finish().is_ok());
}
