#![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 7-level ExposureLevel, the mask has 7 elements:
//! [ShortSmall, ShortHalf, ShortFull, Flat, LongSmall, LongHalf, LongFull]

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 7 exposure levels valid
    let mask = get_valid_action_mask(0.0, 2.0);

    assert_eq!(mask.len(), 7, "Mask should have 7 elements (one per exposure level)");
    assert_eq!(
        mask.iter().filter(|&&v| v).count(),
        7,
        "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, ShortFull and LongFull should be masked (|1.0| > 0.6)
    let mask = get_valid_action_mask(0.0, 0.6);

    let valid_count = mask.iter().filter(|&&v| v).count();
    assert_eq!(
        valid_count, 5,
        "ShortSmall, ShortHalf, Flat, LongSmall, LongHalf valid (5/7 exposures)"
    );

    assert!(mask[0], "ShortSmall (0.25) should be VALID");
    assert!(mask[1], "ShortHalf (0.50) should be VALID");
    assert!(!mask[2], "ShortFull (1.0) should be INVALID");
    assert!(mask[3], "Flat (0.0) should be VALID");
    assert!(mask[4], "LongSmall (0.25) should be VALID");
    assert!(mask[5], "LongHalf (0.50) should be VALID");
    assert!(!mask[6], "LongFull (1.0) should be INVALID");
}

#[test]
fn test_action_masking_preserves_all_action_variants() {
    // With masking at max_position=1.0, all 7 exposure levels valid
    let mask = get_valid_action_mask(0.0, 1.0);
    assert_eq!(mask.len(), 7);
    assert!(mask.iter().all(|&v| v), "All 7 exposures should be valid at max_position=1.0");
}

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

    // ShortFull (idx 2) and LongFull (idx 6) should be masked
    assert!(mask[0], "ShortSmall should be valid");
    assert!(mask[1], "ShortHalf should be valid");
    assert!(!mask[2], "ShortFull should be masked");
    assert!(mask[3], "Flat should be valid");
    assert!(mask[4], "LongSmall should be valid");
    assert!(mask[5], "LongHalf should be valid");
    assert!(!mask[6], "LongFull should be masked");

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

    let action_27 = FactoredAction::from_index(27).unwrap();
    assert_eq!(action_27.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, 7, "All exposures valid when max |exposure| equals max_position");

    // At max_position=0.99, ShortFull/LongFull 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, 5, "Only 5 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[3], // Flat is index 3 in the 7-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], "ShortSmall invalid at max_position=0.0");
    assert!(!mask[1], "ShortHalf invalid at max_position=0.0");
    assert!(!mask[2], "ShortFull invalid at max_position=0.0");
    assert!(mask[3], "Flat valid at max_position=0.0");
    assert!(!mask[4], "LongSmall invalid at max_position=0.0");
    assert!(!mask[5], "LongHalf invalid at max_position=0.0");
    assert!(!mask[6], "LongFull invalid at max_position=0.0");
}
