lamarge 0.8.0

Client for the LaMarge API: sign-in, takes, transfers, withdrawals and market making, each request checked before it is signed.
Documentation
use super::*;

fn custom(api_url: &str, app_origin: &str) -> Result<Client> {
    Client::new(Environment::Custom { api_url: api_url.into(), app_origin: app_origin.into() })
}

#[test]
fn named_environments_build() {
    let client = Client::new(Environment::Production).unwrap();
    assert_eq!(client.inner().api, "https://api.lamarge.trade");
    assert_eq!(client.inner().origin, "https://app.lamarge.trade");
    assert_eq!(client.environment(), &Environment::Production);
    let client = Client::new(Environment::Development).unwrap();
    assert_eq!(client.inner().api, "https://api.dev.lamarge.trade");
    assert_eq!(client.inner().origin, "https://app.dev.lamarge.trade");
    assert_eq!(client.inner().lighter_api, transport::LIGHTER_API);
}

#[test]
fn https_guard() {
    assert!(custom("https://api.example.com/base/", "https://app.example.com").is_ok());
    for local in ["http://localhost:8080", "http://127.0.0.1:1", "http://[::1]:9"] {
        assert!(custom(local, local).is_ok(), "{local}");
    }
    for (api, origin) in [
        ("http://api.example.com", "https://app.example.com"),
        ("https://api.example.com", "http://app.example.com"),
        ("http://localhost.example.com", "https://app.example.com"),
        ("ftp://api.example.com", "https://app.example.com"),
        ("api.example.com", "https://app.example.com"),
        ("https://user:pass@api.example.com", "https://app.example.com"),
        ("https://api.example.com?x=1", "https://app.example.com"),
        ("https://api.example.com", "https://app.example.com/login"),
    ] {
        assert!(matches!(custom(api, origin), Err(Error::Config(_))), "{api} {origin}");
    }
}

#[test]
fn api_base_keeps_its_path_without_trailing_slash() {
    let client = custom("https://api.example.com/base/", "https://app.example.com/").unwrap();
    assert_eq!(client.inner().api, "https://api.example.com/base");
    assert_eq!(client.inner().origin, "https://app.example.com");
    let client = custom("https://api.example.com:8443", "https://app.example.com:8443").unwrap();
    assert_eq!(client.inner().api, "https://api.example.com:8443");
    assert_eq!(client.inner().origin, "https://app.example.com:8443");
}

#[test]
fn an_invalid_user_agent_suffix_is_refused() {
    let error = Client::builder(Environment::Production).user_agent_suffix("bad\nsuffix").build();
    assert!(matches!(error, Err(Error::Config(_))));
}

#[test]
fn retry_policy_counts_the_first_attempt() {
    assert_eq!(RetryPolicy::new(0, Duration::ZERO, Duration::ZERO).max_attempts(), 1);
    assert_eq!(RetryPolicy::none().max_attempts(), 1);
    let default = RetryPolicy::default();
    assert_eq!(default.max_attempts(), 3);
    assert_eq!(default.base_delay(), Duration::from_millis(500));
    assert_eq!(default.max_delay(), Duration::from_secs(5));
}

#[test]
fn injected_clock_and_websocket_tls_reach_the_transport() {
    let fixed = chrono::DateTime::from_timestamp(1_800_000_000, 0).unwrap();
    let client =
        Client::builder(Environment::Production).clock(Arc::new(move || fixed)).build().unwrap();
    assert_eq!(client.inner().now(), fixed);
    assert_eq!(client.inner().ws_tls.alpn_protocols, [b"http/1.1".to_vec()]);
}

#[test]
fn client_is_send_sync_and_debug_shows_no_internals() {
    fn check<T: Send + Sync + 'static>() {}
    check::<Client>();
    let client = Client::new(Environment::Production).unwrap();
    assert!(format!("{client:?}").contains("api.lamarge.trade"));
}