#![allow(
    clippy::assertions_on_constants,
    clippy::assertions_on_result_states,
    clippy::clone_on_copy,
    clippy::decimal_literal_representation,
    clippy::doc_markdown,
    clippy::empty_line_after_doc_comments,
    clippy::field_reassign_with_default,
    clippy::get_unwrap,
    clippy::identity_op,
    clippy::inconsistent_digit_grouping,
    clippy::indexing_slicing,
    clippy::integer_division,
    clippy::len_zero,
    clippy::let_underscore_must_use,
    clippy::manual_div_ceil,
    clippy::manual_let_else,
    clippy::manual_range_contains,
    clippy::modulo_arithmetic,
    clippy::needless_range_loop,
    clippy::non_ascii_literal,
    clippy::redundant_clone,
    clippy::shadow_reuse,
    clippy::shadow_same,
    clippy::shadow_unrelated,
    clippy::single_match_else,
    clippy::str_to_string,
    clippy::string_slice,
    clippy::tests_outside_test_module,
    clippy::too_many_lines,
    clippy::unnecessary_wraps,
    clippy::unseparated_literal_suffix,
    clippy::use_debug,
    clippy::useless_vec,
    clippy::wildcard_enum_match_arm,
    clippy::else_if_without_else,
    clippy::expect_used,
    clippy::missing_const_for_fn,
    clippy::similar_names,
    clippy::type_complexity,
    clippy::collapsible_else_if,
    clippy::doc_lazy_continuation,
    clippy::items_after_test_module,
    clippy::map_clone,
    clippy::multiple_unsafe_ops_per_block,
    clippy::unwrap_or_default,
    clippy::assign_op_pattern,
    clippy::needless_borrow,
    clippy::println_empty_string,
    clippy::unnecessary_cast,
    clippy::used_underscore_binding,
    clippy::create_dir,
    clippy::implicit_saturating_sub,
    clippy::exit,
    clippy::expect_fun_call,
    clippy::too_many_arguments,
    clippy::unnecessary_map_or,
    clippy::unwrap_used,
    dead_code,
    unused_imports,
    unused_variables,
    clippy::cloned_ref_to_slice_refs,
    clippy::neg_multiply,
    clippy::while_let_loop,
    clippy::bool_assert_comparison,
    clippy::excessive_precision,
    clippy::trivially_copy_pass_by_ref,
    clippy::op_ref,
    clippy::redundant_closure,
    clippy::unnecessary_lazy_evaluations,
    clippy::if_then_some_else_none,
    clippy::unnecessary_to_owned,
    clippy::single_component_path_imports,
)]
//! Smoke tests for action masking functionality (Wave 9 Agent 2)
//!
//! Validates position limit enforcement via action masking.
//! With branching DQN, the mask has 5 elements (one per exposure level):
//! [Short100, Short50, Flat, Long50, Long100]

use ml::dqn::action_space::{get_valid_action_mask, ExposureLevel, FactoredAction};

#[test]
fn test_action_masking_at_neutral_position() {
    // At position 0.0 with max_position=2.0, all 5 exposure levels valid
    let mask = get_valid_action_mask(0.0, 2.0);

    assert_eq!(mask.len(), 5, "Mask should have 5 elements (one per exposure level)");
    assert_eq!(
        mask.iter().filter(|&&v| v).count(),
        5,
        "All exposure levels should be valid at position 0.0 with max_position=2.0"
    );
}

#[test]
fn test_action_masking_very_restrictive_limit() {
    // With max_position=0.6, only Flat and ±50% exposures should be valid
    let mask = get_valid_action_mask(0.0, 0.6);

    let valid_count = mask.iter().filter(|&&v| v).count();
    assert_eq!(
        valid_count, 3,
        "Only Flat, Short50, and Long50 should be valid (3/5 exposures)"
    );

    // Short100 (index 0) invalid: |exposure|=1.0 > 0.6
    assert!(!mask[0], "Short100 should be INVALID (exposure=-1.0 > 0.6)");

    // Short50 (index 1) valid: |exposure|=0.5 <= 0.6
    assert!(mask[1], "Short50 should be VALID (exposure=-0.5 <= 0.6)");

    // Flat (index 2) valid: |exposure|=0.0 <= 0.6
    assert!(mask[2], "Flat should be VALID (exposure=0.0 <= 0.6)");

    // Long50 (index 3) valid: |exposure|=0.5 <= 0.6
    assert!(mask[3], "Long50 should be VALID (exposure=+0.5 <= 0.6)");

    // Long100 (index 4) invalid: |exposure|=1.0 > 0.6
    assert!(!mask[4], "Long100 should be INVALID (exposure=+1.0 > 0.6)");
}

#[test]
fn test_action_masking_preserves_all_action_variants() {
    // With masking at max_position=1.0, all 5 exposure levels valid
    let mask = get_valid_action_mask(0.0, 1.0);
    assert_eq!(mask.len(), 5);

    assert!(mask[0], "Short100 should be valid");
    assert!(mask[1], "Short50 should be valid");
    assert!(mask[2], "Flat should be valid");
    assert!(mask[3], "Long50 should be valid");
    assert!(mask[4], "Long100 should be valid");
}

#[test]
fn test_action_masking_index_mapping_correctness() {
    let mask = get_valid_action_mask(0.0, 0.6);
    assert_eq!(mask.len(), 5);

    // Exposure order: Short100, Short50, Flat, Long50, Long100
    assert!(!mask[0], "Short100 (idx 0) should be masked");
    assert!(mask[1], "Short50 (idx 1) should be valid");
    assert!(mask[2], "Flat (idx 2) should be valid");
    assert!(mask[3], "Long50 (idx 3) should be valid");
    assert!(!mask[4], "Long100 (idx 4) should be masked");

    // Verify FactoredAction mapping still works for the 45-action space
    let action_0 = FactoredAction::from_index(0).unwrap();
    assert_eq!(action_0.exposure, ExposureLevel::Short100);

    let action_18 = FactoredAction::from_index(18).unwrap();
    assert_eq!(action_18.exposure, ExposureLevel::Flat);
}

#[test]
fn test_action_masking_boundary_conditions() {
    // At max_position=1.0, all exposure levels valid (max |exp| = 1.0 <= 1.0)
    let mask = get_valid_action_mask(0.0, 1.0);
    let valid_count = mask.iter().filter(|&&v| v).count();
    assert_eq!(valid_count, 5, "All exposures valid when max |exposure| equals max_position");

    // At max_position=0.99, Short100/Long100 masked (|1.0| > 0.99)
    let mask_below = get_valid_action_mask(0.0, 0.99);
    let valid_below = mask_below.iter().filter(|&&v| v).count();
    assert_eq!(valid_below, 3, "Only 3 exposures valid when max_position < 1.0");
}

#[test]
fn test_action_masking_flat_always_valid() {
    // Flat (exposure=0.0) should ALWAYS be valid
    for max_pos in &[0.1, 0.5, 1.0, 2.0, 10.0] {
        let mask = get_valid_action_mask(0.0, *max_pos);
        assert!(
            mask[2], // Flat is index 2 in the 5-element mask
            "Flat should always be valid (max_position={})",
            max_pos
        );
    }
}

#[test]
fn test_action_masking_edge_case_zero_max_position() {
    // max_position=0.0 should only allow Flat (|0.0| <= 0.0)
    let mask = get_valid_action_mask(0.0, 0.0);

    let valid_count = mask.iter().filter(|&&v| v).count();
    assert_eq!(valid_count, 1, "Only Flat should be valid when max_position=0.0");

    assert!(!mask[0], "Short100 invalid at max_position=0.0");
    assert!(!mask[1], "Short50 invalid at max_position=0.0");
    assert!(mask[2], "Flat valid at max_position=0.0");
    assert!(!mask[3], "Long50 invalid at max_position=0.0");
    assert!(!mask[4], "Long100 invalid at max_position=0.0");
}
