//! Helper functions for creating test nodes and data.

use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use stemedb_cluster::membership::{NodeId, NodeInfo};

/// Creates a test socket address with the given port.
pub(crate) fn test_addr(port: u16) -> SocketAddr {
    SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), port)
}

/// Creates a test node ID from a single byte (repeated 16 times).
pub(crate) fn test_node_id(n: u8) -> NodeId {
    NodeId::from_bytes([n; 16])
}

/// Creates a test NodeInfo for the given index.
pub(crate) fn test_node_info(n: u8) -> NodeInfo {
    let id = test_node_id(n);
    NodeInfo::new(id, test_addr(9090 + n as u16), test_addr(8080 + n as u16))
}

/// Generate a random hash for test assertions.
pub(crate) fn rand_hash() -> [u8; 32] {
    use std::time::{SystemTime, UNIX_EPOCH};

    let nanos = SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_nanos()).unwrap_or(0);

    let mut hash = [0u8; 32];
    hash[..16].copy_from_slice(&nanos.to_le_bytes());

    // Add some randomness
    let random_bytes: [u8; 16] = rand::random();
    hash[16..32].copy_from_slice(&random_bytes);

    hash
}
