use super::*;
use std::net::{IpAddr, Ipv4Addr};

fn test_addr(port: u16) -> SocketAddr {
    SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), port)
}

#[test]
fn test_swim_config_defaults() {
    let config = SwimConfig::default();
    assert_eq!(config.gossip_interval, Duration::from_millis(200));
    assert_eq!(config.probe_interval, Duration::from_secs(1));
    assert_eq!(config.indirect_probe_count, 3);
}

#[test]
fn test_swim_config_builder() {
    let config = SwimConfig::new()
        .with_gossip_interval(Duration::from_millis(100))
        .with_probe_interval(Duration::from_millis(500));

    assert_eq!(config.gossip_interval, Duration::from_millis(100));
    assert_eq!(config.probe_interval, Duration::from_millis(500));
}

#[test]
fn test_sharding_config_defaults() {
    let config = ShardingConfig::default();
    assert_eq!(config.num_shards, 16);
    assert_eq!(config.replication_factor, 3);
    assert_eq!(config.split_threshold_bytes, 64 * 1024 * 1024);
}

#[test]
fn test_cluster_config_builder() {
    let config = ClusterConfig::builder()
        .with_rpc_addr(test_addr(9090))
        .with_api_addr(test_addr(8080))
        .with_seed_node(test_addr(9091))
        .with_datacenter("us-east-1")
        .build();

    assert!(config.is_ok());
    let config = config.unwrap();
    assert_eq!(config.rpc_addr.port(), 9090);
    assert_eq!(config.api_addr.port(), 8080);
    assert_eq!(config.seed_nodes.len(), 1);
    assert_eq!(config.datacenter, Some("us-east-1".to_string()));
}

#[test]
fn test_cluster_config_builder_missing_required() {
    let result = ClusterConfig::builder().build();
    assert!(result.is_err());
}

#[test]
fn test_swim_addr() {
    let config = ClusterConfig::builder()
        .with_rpc_addr(test_addr(9090))
        .with_api_addr(test_addr(8080))
        .build()
        .unwrap();

    let swim_addr = config.swim_addr();
    assert_eq!(swim_addr.port(), 18183); // Default swim port (181XX scheme)
}
