#[test]
#[cfg(feature = "coolprop")]
fn test_co2_damping_near_critical() {
    let inner = CoolPropBackend::new();
    let damped = CoolPropBackend::with_damping();
    let fluid = FluidId::new("CO2");

    // Near critical point of CO2: Tc=304.13K, Pc=7.3773 MPa
    let tc = 304.13;
    let pc = 7.3773e6;

    // Query points approaching the critical point
    let temperatures = [tc * 0.95, tc * 0.99, tc * 0.999, tc, tc * 1.001, tc * 1.05];

    for t_k in temperatures {
        let state = FluidState::from_pt(Pressure::from_pascals(pc), Temperature::from_kelvin(t_k));

        // Raw CoolProp might return very large values or NaN for Cp near critical
        let cp_inner = inner
            .property(fluid.clone(), Property::Cp, state.clone())
            .unwrap_or(f64::NAN);
        let cp_damped = damped.property(fluid.clone(), Property::Cp, state).unwrap();

        // Damped value must be finite and respect cp_max (default 1e6)
        assert!(cp_damped.is_finite());
        assert!(cp_damped <= 2e6); // Some margin over default cp_max

        if cp_inner.is_finite() && cp_inner < 1e4 {
            // Far from critical, they should be identical
            assert_relative_eq!(cp_damped, cp_inner, max_relative = 0.01);
        }
    }
}

#[test]
#[cfg(feature = "coolprop")]
fn test_damping_smoothness() {
    let damped = CoolPropBackend::with_damping();
    let fluid = FluidId::new("CO2");
    let tc = 304.13;
    let pc = 7.3773e6;

    // Small step near critical to check for discontinuities
    let t1 = tc - 0.001;
    let t2 = tc + 0.001;

    let state1 = FluidState::from_pt(Pressure::from_pascals(pc), Temperature::from_kelvin(t1));
    let state2 = FluidState::from_pt(Pressure::from_pascals(pc), Temperature::from_kelvin(t2));

    let cp1 = damped
        .property(fluid.clone(), Property::Cp, state1)
        .unwrap();
    let cp2 = damped
        .property(fluid.clone(), Property::Cp, state2)
        .unwrap();

    // Sigmoid damping should ensure finite delta
    assert!((cp1 - cp2).abs() < 50000.0);
}
