use solana_cli::cli::{process_command, CliCommand, CliConfig};
use solana_client::rpc_client::RpcClient;
use solana_core::test_validator::TestValidator;
use solana_faucet::faucet::run_local_faucet;
use solana_sdk::{commitment_config::CommitmentConfig, signature::Keypair};
use std::sync::mpsc::channel;

#[test]
fn test_cli_request_airdrop() {
    let test_validator = TestValidator::with_no_fees();

    let (sender, receiver) = channel();
    run_local_faucet(test_validator.mint_keypair(), sender, None);
    let faucet_addr = receiver.recv().unwrap();

    let mut bob_config = CliConfig::recent_for_tests();
    bob_config.json_rpc_url = test_validator.rpc_url();
    bob_config.command = CliCommand::Airdrop {
        faucet_host: None,
        faucet_port: faucet_addr.port(),
        pubkey: None,
        lamports: 50,
    };
    let keypair = Keypair::new();
    bob_config.signers = vec![&keypair];

    let sig_response = process_command(&bob_config);
    sig_response.unwrap();

    let rpc_client = RpcClient::new(test_validator.rpc_url());

    let balance = rpc_client
        .get_balance_with_commitment(&bob_config.signers[0].pubkey(), CommitmentConfig::recent())
        .unwrap()
        .value;
    assert_eq!(balance, 50);

    test_validator.close();
}
