#![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,
)]
//! Test suite for PPO CircuitBreaker
//!
//! TDD Red Phase: Tests written FIRST before implementation
//! These tests define the expected behavior of the CircuitBreaker module

use std::thread;
use std::time::Duration;

// Import from PPO module (will fail until implementation exists)
use ml::ppo::circuit_breaker::{CircuitBreaker, CircuitBreakerConfig, CircuitState};

#[test]
fn test_circuit_breaker_closed_to_open() {
    // Test that circuit opens after exceeding failure threshold
    let config = CircuitBreakerConfig {
        failure_threshold: 3,
        success_threshold: 2,
        timeout_duration: Duration::from_secs(60),
        half_open_max_calls: 2,
    };
    let breaker = CircuitBreaker::new(config);

    // Initial state: Closed
    assert_eq!(breaker.current_state(), CircuitState::Closed);
    assert!(breaker.allow_request());

    // Record 2 failures - should stay closed
    breaker.record_failure();
    breaker.record_failure();
    assert_eq!(breaker.current_state(), CircuitState::Closed);
    assert!(breaker.allow_request());

    // Third failure - should open
    breaker.record_failure();
    assert_eq!(breaker.current_state(), CircuitState::Open);
    assert!(!breaker.allow_request()); // Blocked when open
}

#[test]
fn test_circuit_breaker_half_open_recovery() {
    // Test transition from HalfOpen to Closed after successful recoveries
    let config = CircuitBreakerConfig {
        failure_threshold: 2,
        success_threshold: 2,
        timeout_duration: Duration::from_millis(100),
        half_open_max_calls: 3,
    };
    let breaker = CircuitBreaker::new(config);

    // Open the circuit
    breaker.record_failure();
    breaker.record_failure();
    assert_eq!(breaker.current_state(), CircuitState::Open);

    // Wait for timeout to transition to half-open
    thread::sleep(Duration::from_millis(150));
    assert!(breaker.allow_request());
    assert_eq!(breaker.current_state(), CircuitState::HalfOpen);

    // Record successes to close the circuit
    breaker.record_success();
    assert_eq!(breaker.current_state(), CircuitState::HalfOpen);

    breaker.record_success();
    assert_eq!(breaker.current_state(), CircuitState::Closed);
    assert!(breaker.allow_request());
}

#[test]
fn test_circuit_breaker_rejects_in_open() {
    // Test that training requests are rejected when circuit is open
    let config = CircuitBreakerConfig {
        failure_threshold: 2,
        success_threshold: 2,
        timeout_duration: Duration::from_secs(10), // Long timeout
        half_open_max_calls: 2,
    };
    let breaker = CircuitBreaker::new(config);

    // Open the circuit
    breaker.record_failure();
    breaker.record_failure();
    assert_eq!(breaker.current_state(), CircuitState::Open);

    // Repeated requests should be rejected (timeout not elapsed)
    assert!(!breaker.allow_request());
    assert!(!breaker.allow_request());
    assert!(!breaker.allow_request());

    // Circuit should still be open
    assert_eq!(breaker.current_state(), CircuitState::Open);
}
