//! Golden vectors for the srun portal protocol primitives.
//!
//! These pin the byte output of `quirk_base64`, `xencode`, the digest helpers,
//! and `build_portal_params` so any future refactor that subtly changes encoding,
//! padding, word ordering, or parameter assembly is caught immediately.

use srunc::protocol::{
    build_portal_params, md5_hex, quirk_base64, sha1_hex, xencode, Action, Credentials,
};

const TOKEN: &str = "aa0edd0fff7dd9f1f0ae4e981ec0114c7b0bf6f67c4895bed4f4ac634e97ecf2";

#[test]
fn quirk_base64_golden_vectors() {
    assert_eq!(quirk_base64(b"1"), "9+==");
    assert_eq!(quirk_base64(b"2"), "9S==");
    assert_eq!(quirk_base64(b"34"), "9z+=");
    assert_eq!(quirk_base64(b"567"), "0FZ7");
    assert_eq!(quirk_base64(b"\x00"), "LL==");
    assert_eq!(quirk_base64(b"\x00\x00"), "LLL=");
    assert_eq!(quirk_base64(b"\xff\x00\x00"), "AvLL");
    assert_eq!(quirk_base64(b"\x01"), "L+==");
    assert_eq!(quirk_base64(b"\x01==!@#$%^&*()"), "LFt52HLkRourRX//8+==");
    assert_eq!(quirk_base64(b"\x01aAbB_+=-\x11"), "LaiVZYRs8ztfP+==");
}

#[test]
fn xencode_golden_vectors() {
    let z = "0000000000000000000000000000000000000000000000000000000000000000";
    assert_eq!(quirk_base64(&xencode(b"", TOKEN.as_bytes())), "");
    assert_eq!(
        quirk_base64(&xencode(b"1", TOKEN.as_bytes())),
        "NmsaR0fCm5H="
    );
    assert_eq!(
        quirk_base64(&xencode(b"agfawegwq12834eqrge", TOKEN.as_bytes())),
        "DAxHygvRUjlDyJjmvChIzuavMsjy7B9L"
    );
    assert_eq!(
        quirk_base64(&xencode(b"agfawegwq12834eqrge", z.as_bytes())),
        "TOdQ9ggF2y/mskS6Orkg+eUZIok9vqJr"
    );
    assert_eq!(
        quirk_base64(&xencode(b"9$02%8r89)(&22{}we[f]|s", TOKEN.as_bytes())),
        "kCG+xmvGAhCV717Y80Fk0o1YJ8SYvBdnUmQoqS=="
    );
}

#[test]
fn digests_golden_vectors() {
    assert_eq!(sha1_hex(b""), "da39a3ee5e6b4b0d3255bfef95601890afd80709");
    assert_eq!(sha1_hex(b"abc"), "a9993e364706816aba3e25717850c26c9cd0d89d");
    assert_eq!(md5_hex(b"pass"), "1a1dc91c907325c69271ddf0c944bc72");
}

// Pinned vector for inputs:
//   username="hello" password="pass" ip="" acid="1"
//   token="32f23b9c2229fd034f6d5160d8b4536496af550efc45113635689d2d8f12ffad"
#[test]
fn build_portal_params_login_golden_vector() {
    let creds = Credentials {
        username: "hello",
        password: "pass",
    };
    let token = "32f23b9c2229fd034f6d5160d8b4536496af550efc45113635689d2d8f12ffad";
    let p = build_portal_params(&creds, token, Action::Login, "", "1");

    assert_eq!(p.action, "login");
    assert_eq!(
        p.password.as_deref(),
        Some("{MD5}1a1dc91c907325c69271ddf0c944bc72")
    );
    assert_eq!(
        p.info,
        "{SRBX1}Btl+2HhG5Mr1GhsSBpa7Izl9M2U8Y846GmMzaSJ+xDYgR1GVXZRn67zxbQkaTXHtwBJ0E6H+ChJMyb9BM+qwni/EWJoXx0aMXpdUikuAjYmEKsHG"
    );
    assert_eq!(p.chksum, "8fd747a5b37c63f695b23fa2bd0339b1a961aa49");
}

#[test]
fn logout_omits_password() {
    let creds = Credentials {
        username: "hello",
        password: "pass",
    };
    let p = build_portal_params(&creds, TOKEN, Action::Logout, "", "1");
    assert_eq!(p.action, "logout");
    assert!(p.password.is_none());
}
