mod common;

use randomorg_rpc::{
    BlobFormat, BlobsParams, BoolOrArray, DecimalFractionsParams, GaussiansParams, IntOrArray,
    IntegerSequencesMultiformParams, IntegerSequencesUniformParams, IntegersParams, StringsParams,
    UuidsParams,
};

#[tokio::test]
async fn usage() {
    let c = common::client();
    let r = c.get_usage().await.unwrap();
    assert!(r.requests_left > 0, "no requests left: {r:?}");
    assert!(r.bits_left > 0);
    assert!(!r.status.is_empty());
}

#[tokio::test]
async fn integers() {
    let c = common::client();
    let r = c
        .generate_integers(IntegersParams::new(5, 1, 100))
        .await
        .unwrap();
    assert_eq!(r.random.data.len(), 5);
}

#[tokio::test]
async fn integer_sequences_uniform() {
    let c = common::client();
    let r = c
        .generate_integer_sequences_uniform(IntegerSequencesUniformParams::new(2, 4, 1, 10))
        .await
        .unwrap();
    assert_eq!(r.random.data.len(), 2);
    assert!(r.random.data.iter().all(|seq| seq.len() == 4));
}

#[tokio::test]
async fn integer_sequences_multiform() {
    let c = common::client();
    let p = IntegerSequencesMultiformParams::new(
        3,
        vec![2, 3, 4],
        IntOrArray::Many(vec![0, 0, 0]),
        IntOrArray::Many(vec![10, 20, 30]),
    )
    .replacement(BoolOrArray::One(true));
    let r = c.generate_integer_sequences_multiform(p).await.unwrap();
    assert_eq!(r.random.data.len(), 3);
    assert_eq!(r.random.data[0].len(), 2);
    assert_eq!(r.random.data[1].len(), 3);
    assert_eq!(r.random.data[2].len(), 4);
}

#[tokio::test]
async fn decimal_fractions() {
    let c = common::client();
    let r = c
        .generate_decimal_fractions(DecimalFractionsParams::new(4, 8))
        .await
        .unwrap();
    assert_eq!(r.random.data.len(), 4);
    assert!(r.random.data.iter().all(|x| (0.0..=1.0).contains(x)));
}

#[tokio::test]
async fn gaussians() {
    let c = common::client();
    let r = c
        .generate_gaussians(GaussiansParams::new(5, 0.0, 1.0, 5))
        .await
        .unwrap();
    assert_eq!(r.random.data.len(), 5);
}

#[tokio::test]
async fn strings() {
    let c = common::client();
    let r = c
        .generate_strings(StringsParams::new(3, 6, "abcdef0123456789"))
        .await
        .unwrap();
    assert_eq!(r.random.data.len(), 3);
    assert!(r.random.data.iter().all(|s| s.len() == 6));
}

#[tokio::test]
async fn uuids() {
    let c = common::client();
    let r = c.generate_uuids(UuidsParams::new(2)).await.unwrap();
    assert_eq!(r.random.data.len(), 2);
    assert!(r.random.data.iter().all(|s| s.len() == 36));
}

#[tokio::test]
async fn blobs() {
    let c = common::client();
    let r = c.generate_blobs(BlobsParams::new(2, 64)).await.unwrap();
    assert_eq!(r.random.data.len(), 2);

    let hex = c
        .generate_blobs(BlobsParams::new(1, 64).format(BlobFormat::Hex))
        .await
        .unwrap();
    assert_eq!(hex.random.data.len(), 1);
    assert!(hex.random.data[0].chars().all(|ch| ch.is_ascii_hexdigit()));
}
