mod common;
use std::str::FromStr;

use fvm_shared::address::Address;
use lotus_rs::{
    helpers::marshal::marshal_cbor_address,
    types::{
        chain::{epoch::ChainEpoch, randomness::DomainSeparationTag},
        miner::policy::CHAIN_FINALITY,
    },
};

#[tokio::test]
async fn test_head() {
    let client = common::setup_client();
    let head = client.chain_head().await.unwrap();
    assert_eq!(head.Height > 0, true)
}

#[tokio::test]
async fn test_randomness() {
    let client = common::setup_client();
    let head = client.chain_head().await.unwrap();
    let ticket_epoch: ChainEpoch = head.Height - CHAIN_FINALITY;
    let addr = Address::from_str("t0001");
    let entropy = marshal_cbor_address(&addr.unwrap());
    let rand = client
        .state_get_randomness_from_tickets(
            DomainSeparationTag::SealRandomness,
            ticket_epoch,
            entropy,
            head.Cids,
        )
        .await
        .unwrap();

    assert_eq!(DomainSeparationTag::SealRandomness.to_u8(), 5);
    assert_eq!(rand.len(), 32)
}

#[test_log::test(tokio::test)]
async fn test_state_miner_info() {
    let client = common::setup_client();
    let mut head = client.chain_head().await.unwrap();
    let cid = head.Cids.remove(0);

    let state_miner_info = client
        .state_miner_info("t01001".to_string(), Some(cid))
        .await;
    assert!(
        state_miner_info.is_ok(),
        "Error {}",
        state_miner_info.unwrap_err()
    )
}

#[tokio::test]
async fn test_state_network_version() {
    let client = common::setup_client();
    let mut head = client.chain_head().await.unwrap();
    let cid = head.Cids.remove(0);

    let state_network_version = client.state_network_version(Some(cid)).await;
    assert!(state_network_version.is_ok())
}
