lamarge 0.9.0

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

use super::*;

#[test]
fn client_id_charset_and_length() {
    assert!(ClientId::new("take-1:retry_2.a").is_ok());
    assert!(ClientId::new("a".repeat(64)).is_ok());
    assert_eq!(ClientId::new("a".repeat(65)), Err(InvalidClientId));
    assert_eq!(ClientId::new(""), Err(InvalidClientId));
    for bad in ["a b", "a/b", "é", "a\n", "a#b"] {
        assert_eq!(ClientId::new(bad), Err(InvalidClientId), "{bad:?}");
    }
    assert!(serde_json::from_str::<ClientId>("\"a/b\"").is_err());
    assert_eq!(serde_json::from_str::<ClientId>("\"ok\"").unwrap().as_str(), "ok");
}

#[test]
fn generated_client_ids_are_valid_and_distinct() {
    let (a, b) = (ClientId::generate(), ClientId::generate());
    assert_ne!(a, b);
    assert_eq!(a.as_str().len(), 32);
    assert_eq!(ClientId::new(a.as_str()).unwrap(), a);
}

#[test]
fn cursors_are_lossless_decimal_strings() {
    let cursor = Cursor::new(u64::MAX);
    let json = serde_json::to_string(&cursor).unwrap();
    assert_eq!(json, "\"18446744073709551615\"");
    assert_eq!(serde_json::from_str::<Cursor>(&json).unwrap(), cursor);
    assert!(serde_json::from_str::<Cursor>("7").is_err(), "a JSON number loses precision");
    assert!("-7".parse::<Revision>().is_err());
    assert!(Revision::new(2) > Revision::new(1));
}

#[test]
fn portfolio_revisions_order_by_count_then_time() {
    let parse = |text: &str| text.parse::<PortfolioRevision>().unwrap();
    assert_eq!(parse("12:1731585600000000").to_string(), "12:1731585600000000");
    assert!(parse("12:5") > parse("12:4"));
    assert!(parse("13:0") > parse("12:9"));
    for bad in ["12", "12:", ":5", "a:1", "1:2:3"] {
        assert!(bad.parse::<PortfolioRevision>().is_err(), "{bad:?}");
    }
}

#[test]
fn uuid_ids_are_transparent() {
    let text = "0192f1c2-3d4e-7f00-8a1b-2c3d4e5f6a7b";
    let id: TransferId = serde_json::from_str(&format!("\"{text}\"")).unwrap();
    assert_eq!(id.to_string(), text);
    assert_eq!(serde_json::to_string(&id).unwrap(), format!("\"{text}\""));
    assert!(serde_json::from_str::<TakeId>("\"not-a-uuid\"").is_err());
}

#[test]
fn order_ids_are_prefixed_hex() {
    let text = format!("0x{}", "ab".repeat(32));
    let id: OrderId = text.parse().unwrap();
    assert_eq!(id.to_string(), text);
    assert_eq!(serde_json::to_string(&id).unwrap(), format!("\"{text}\""));
    assert!("0xabcd".parse::<OrderId>().is_err());
}

fn signature(v: u8) -> [u8; 65] {
    let mut bytes = [0x11; 65];
    bytes[64] = v;
    bytes
}

#[test]
fn signature_v_is_normalized_and_bounded() {
    assert_eq!(SignatureBytes::new(signature(0)).unwrap().as_bytes()[64], 27);
    assert_eq!(SignatureBytes::new(signature(1)).unwrap().as_bytes()[64], 28);
    assert_eq!(SignatureBytes::new(signature(28)).unwrap().as_bytes()[64], 28);
    assert!(SignatureBytes::new(signature(2)).is_none());
    assert!(SignatureBytes::new(signature(29)).is_none());
}

#[test]
fn signature_wire_form_and_redacted_debug() {
    let signature = SignatureBytes::new(signature(27)).unwrap();
    let json = serde_json::to_string(&signature).unwrap();
    assert_eq!(json, format!("\"0x{}1b\"", "11".repeat(64)));
    assert_eq!(serde_json::from_str::<SignatureBytes>(&json).unwrap(), signature);
    assert!(!format!("{signature:?}").contains("1111"));
    assert!(serde_json::from_str::<SignatureBytes>(&format!("\"{}1b\"", "11".repeat(64))).is_err());
    assert!(serde_json::from_str::<SignatureBytes>(&format!("\"0x{}\"", "11".repeat(64))).is_err());
}

proptest! {
    #[test]
    fn client_id_accepts_exactly_its_pattern(id in "[A-Za-z0-9_.:-]{1,64}") {
        prop_assert!(ClientId::new(id).is_ok());
    }

    #[test]
    fn client_id_refuses_other_characters(prefix in "[a-z]{0,10}", bad in "[^A-Za-z0-9_.:-]") {
        let id = format!("{prefix}{bad}");
        prop_assert!(ClientId::new(id).is_err());
    }

    #[test]
    fn revision_text_round_trips(value: u64) {
        prop_assert_eq!(Revision::new(value).to_string().parse::<Revision>().unwrap().get(), value);
    }
}