<commit_before>extern crate screeps_api;
extern crate hyper;
extern crate hyper_rustls;
extern crate dotenv;

use hyper::client::Client;
use hyper::net::HttpsConnector;
use screeps_api::error::{Error, ErrorType, ApiError};

/// Set up dotenv and retrieve a specific variable, informatively panicking if it does not exist.
fn env(var: &str) -> String {
    dotenv::dotenv().ok();
    match ::std::env::var(var) {
        Ok(value) => value,
        Err(e) => panic!("must have `{}` defined (err: {:?})", var, e),
    }
}

fn create_secure_client() -> hyper::Client {
    Client::with_connector(HttpsConnector::new(hyper_rustls::TlsClient::new()))
}

fn logged_in() -> screeps_api::API<hyper::Client> {
    let client = create_secure_client();

    let username = env("SCREEPS_API_USERNAME");
    let password = env("SCREEPS_API_PASSWORD");
    let mut api = screeps_api::API::new(client);

    if let Err(err) = api.login(username, password) {
        panic!("Error logging in: {:?}\nTo disable login tests, use `cargo test -- --skip auth`",
               err);
    }

    api
}

#[test]
fn test_auth_my_info() {
    let mut api = logged_in();

    let _ = api.my_info().unwrap();
}

#[test]
fn test_auth_map_stats() {
    let mut api = logged_in();

    let result = api.map_stats(&["E15N52", "E19S79", "E14S78", "E19S81", "W6S67", "InvalidRoomName"]).unwrap();

    assert_eq!(result.rooms.len(), 5);
}

#[test]
fn test_auth_token_reretrieval() {
    let mut api = logged_in();

    let _ = api.my_info().unwrap();

    let _ = api.my_info().unwrap();

    let _ = api.my_info().unwrap();
}

#[test]
fn test_shared_token_storage() {
    let shared = std::rc::Rc::new(std::cell::RefCell::new(None));
    let shared_client = std::rc::Rc::new(create_secure_client());

    let username = env("SCREEPS_API_USERNAME");
    let password = env("SCREEPS_API_PASSWORD");

    screeps_api::API::with_token(shared_client.clone(), shared.clone()).login(username, password).unwrap();

    screeps_api::API::with_token(shared_client.clone(), shared.clone()).my_info().unwrap();

    screeps_api::API::with_token(shared_client.clone(), shared.clone()).my_info().unwrap();

    screeps_api::API::with_token(shared_client.clone(), shared.clone()).my_info().unwrap();

    screeps_api::API::with_token(shared_client.clone(), shared.clone()).my_info().unwrap();
}

#[test]
fn test_auth_room_overview() {
    let mut api = logged_in();<commit_msg>Fix the new auth test name to include 'auth', remove unneeded 'let _ = ' statements.<commit_after>extern crate screeps_api;
extern crate hyper;
extern crate hyper_rustls;
extern crate dotenv;

use hyper::client::Client;
use hyper::net::HttpsConnector;
use screeps_api::error::{Error, ErrorType, ApiError};

/// Set up dotenv and retrieve a specific variable, informatively panicking if it does not exist.
fn env(var: &str) -> String {
    dotenv::dotenv().ok();
    match ::std::env::var(var) {
        Ok(value) => value,
        Err(e) => panic!("must have `{}` defined (err: {:?})", var, e),
    }
}

fn create_secure_client() -> hyper::Client {
    Client::with_connector(HttpsConnector::new(hyper_rustls::TlsClient::new()))
}

fn logged_in() -> screeps_api::API<hyper::Client> {
    let client = create_secure_client();

    let username = env("SCREEPS_API_USERNAME");
    let password = env("<PASSWORD>_API_PASSWORD");
    let mut api = screeps_api::API::new(client);

    if let Err(err) = api.login(username, password) {
        panic!("Error logging in: {:?}\nTo disable login tests, use `cargo test -- --skip auth`",
               err);
    }

    api
}

#[test]
fn test_auth_my_info() {
    let mut api = logged_in();

    let _ = api.my_info().unwrap();
}

#[test]
fn test_auth_map_stats() {
    let mut api = logged_in();

    let result = api.map_stats(&["E15N52", "E19S79", "E14S78", "E19S81", "W6S67", "InvalidRoomName"]).unwrap();

    assert_eq!(result.rooms.len(), 5);
}

#[test]
fn test_auth_token_reretrieval() {
    let mut api = logged_in();

    api.my_info().unwrap();

    api.my_info().unwrap();

    api.my_info().unwrap();
}

#[test]
fn test_auth_shared_token_storage() {
    let shared = std::rc::Rc::new(std::cell::RefCell::new(None));
    let shared_client = std::rc::Rc::new(create_secure_client());

    let username = env("SCREEPS_API_USERNAME");
    let password = env("<PASSWORD>");

    screeps_api::API::with_token(shared_client.clone(), shared.clone()).login(username, password).unwrap();

    screeps_api::API::with_token(shared_client.clone(), shared.clone()).my_info().unwrap();

    screeps_api::API::with_token(shared_client.clone(), shared.clone()).my_info().unwrap();

    screeps_api::API::with_token(shared_client.clone(), shared.clone()).my_info().unwrap();

    screeps_api::API::with_token(shared_client.clone(), shared.clone()).my_info().unwrap();
}

#[test]
fn test_auth_room_overview() {
    let mut api = logged_in();
{'fraction_non_alphanumeric': 0.17866612278197022, 'fraction_numerical': 0.008158270446665308, 'mean_word_length': 4.5225225225225225, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2107732', 'n_tokens_mistral': 1735, 'n_tokens_neox': 1705, 'n_words': 332}