from cryptography.hazmat.primitives.asymmetric import ec

from aliro_harness.issuer.keys import (
    generate_p256_keypair,
    load_private_key_pem,
    save_private_key_pem,
)


def test_generate_p256_keypair_returns_p256_private_key():
    priv = generate_p256_keypair()
    assert isinstance(priv, ec.EllipticCurvePrivateKey)
    assert priv.curve.name == "secp256r1"


def test_pem_roundtrip_preserves_private_key(tmp_path):
    original = generate_p256_keypair()
    path = tmp_path / "key.pem"

    save_private_key_pem(path, original)
    loaded = load_private_key_pem(path)

    original_bytes = original.private_numbers().private_value.to_bytes(32, "big")
    loaded_bytes = loaded.private_numbers().private_value.to_bytes(32, "big")
    assert original_bytes == loaded_bytes
