"""
Coverage tests for spv/ modules - untested branches.
"""

import pytest

# Constants for skip messages
SKIP_SPV = "SPV module not available"


# ========================================================================
# SPV module branches
# ========================================================================


def test_spv_module_exists():
    """Test that SPV module exists."""
    try:
        import bsv.spv

        assert bsv.spv is not None
    except ImportError:
        pytest.skip(SKIP_SPV)


def test_spv_verify_merkle_proof():
    """Test verifying Merkle proof."""
    try:
        from bsv.spv import verify_merkle_proof

        txid = b"\x00" * 32
        merkle_root = b"\x01" * 32
        proof = []

        try:
            is_valid = verify_merkle_proof(txid, merkle_root, proof)
            assert isinstance(is_valid, bool)
        except (NameError, AttributeError):
            pytest.skip("verify_merkle_proof not available")
    except ImportError:
        pytest.skip(SKIP_SPV)


def test_spv_calculate_merkle_root():
    """Test calculating Merkle root."""
    try:
        from bsv.spv import calculate_merkle_root

        txids = [b"\x00" * 32, b"\x01" * 32]

        try:
            root = calculate_merkle_root(txids)
            assert isinstance(root, bytes)
            assert len(root) == 32
        except (NameError, AttributeError):
            pytest.skip("calculate_merkle_root not available")
    except ImportError:
        pytest.skip(SKIP_SPV)


# ========================================================================
# SPV header verification branches
# ========================================================================


def test_spv_verify_header():
    """Test verifying block header."""
    try:
        from bsv.spv import verify_header

        header = b"\x00" * 80  # Block header is 80 bytes

        try:
            is_valid = verify_header(header)
            assert isinstance(is_valid, bool)
        except (NameError, AttributeError):
            pytest.skip("verify_header not available")
    except ImportError:
        pytest.skip(SKIP_SPV)


# ========================================================================
# Edge cases
# ========================================================================


def test_spv_verify_merkle_proof_empty():
    """Test verifying Merkle proof with empty proof."""
    try:
        from bsv.spv import verify_merkle_proof

        txid = b"\x00" * 32
        merkle_root = b"\x00" * 32

        try:
            is_valid = verify_merkle_proof(txid, merkle_root, [])
            # With empty proof, txid should equal root for valid
            assert is_valid == (txid == merkle_root)
        except (NameError, AttributeError):
            pytest.skip("verify_merkle_proof not available")
    except ImportError:
        pytest.skip(SKIP_SPV)
