use trazaeo::reliability::{
    enforce_monotonic_sequence, retry_with_backoff, InMemoryIdempotencyStore, RetryPolicy,
};

/// Tests that integration retry and idempotency are deterministic under partial failures.
#[test]
fn integration_retry_and_idempotency_are_deterministic_under_partial_failures() {
    let policy = RetryPolicy {
        max_attempts: 5,
        initial_backoff_ms: 1,
        backoff_multiplier: 2,
    };
    let mut attempts = 0u32;
    let result: Result<&str, &str> = retry_with_backoff(policy, |_attempt| {
        attempts += 1;
        if attempts < 4 {
            Err("transient")
        } else {
            Ok("ok")
        }
    });
    assert_eq!(result.expect("operation should eventually succeed"), "ok");
    assert_eq!(attempts, 4);

    let mut store = InMemoryIdempotencyStore::default();
    let first = store.process_once("evt-42", || "processed");
    let second = store.process_once("evt-42", || "processed-again");
    assert_eq!(first, Some("processed"));
    assert_eq!(second, None);
}

/// Tests that integration detects reordered event sequences.
#[test]
fn integration_detects_reordered_event_sequences() {
    assert!(enforce_monotonic_sequence(&[10, 20, 30, 40]).is_ok());
    let err = enforce_monotonic_sequence(&[10, 30, 20, 40]).expect_err("reorder must fail");
    assert!(err.to_string().contains("non-monotonic event ordering"));
}
