"""
Coverage tests for wallet/cached_key_deriver.py - untested branches.
"""

import pytest

from bsv.keys import PrivateKey

# ========================================================================
# Cached Key Deriver initialization branches
# ========================================================================


def test_cached_key_deriver_init():
    """Test CachedKeyDeriver initialization."""
    try:
        from bsv.wallet.cached_key_deriver import CachedKeyDeriver

        deriver = CachedKeyDeriver(root_key=PrivateKey())
        assert deriver  # Verify object creation succeeds
    except ImportError:
        pytest.skip("CachedKeyDeriver not available")


# ========================================================================
# Caching branches
# ========================================================================


def test_cached_key_deriver_cache_hit():
    """Test cache hit on repeated derivation."""
    try:
        from bsv.wallet.cached_key_deriver import CachedKeyDeriver

        deriver = CachedKeyDeriver(root_key=PrivateKey())

        if hasattr(deriver, "derive_child"):
            # First derivation - cache miss
            child1 = deriver.derive_child(0)
            # Second derivation - should hit cache
            child2 = deriver.derive_child(0)
            assert child1.key == child2.key
    except ImportError:
        pytest.skip("CachedKeyDeriver not available")


def test_cached_key_deriver_cache_different_indices():
    """Test cache with different indices."""
    try:
        from bsv.wallet.cached_key_deriver import CachedKeyDeriver

        deriver = CachedKeyDeriver(root_key=PrivateKey())

        if hasattr(deriver, "derive_child"):
            child1 = deriver.derive_child(0)
            child2 = deriver.derive_child(1)
            child3 = deriver.derive_child(0)  # Should hit cache

            assert child1.key == child3.key
            assert child1.key != child2.key
    except ImportError:
        pytest.skip("CachedKeyDeriver not available")


# ========================================================================
# Cache management branches
# ========================================================================


def test_cached_key_deriver_clear_cache():
    """Test clearing cache."""
    try:
        from bsv.wallet.cached_key_deriver import CachedKeyDeriver

        deriver = CachedKeyDeriver(root_key=PrivateKey())

        if hasattr(deriver, "derive_child") and hasattr(deriver, "clear_cache"):
            deriver.derive_child(0)
            deriver.clear_cache()
    except ImportError:
        pytest.skip("CachedKeyDeriver not available")


def test_cached_key_deriver_cache_size():
    """Test cache size limit."""
    try:
        from bsv.wallet.cached_key_deriver import CachedKeyDeriver

        deriver = CachedKeyDeriver(root_key=PrivateKey())

        if hasattr(deriver, "derive_child"):
            # Derive many keys to test cache limits
            for i in range(100):
                deriver.derive_child(i)
    except ImportError:
        pytest.skip("CachedKeyDeriver not available")


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


def test_cached_key_deriver_deterministic():
    """Test cached derivation is deterministic."""
    try:
        from bsv.wallet.cached_key_deriver import CachedKeyDeriver

        root = PrivateKey(b"\x02" * 32)
        deriver1 = CachedKeyDeriver(root_key=root)
        deriver2 = CachedKeyDeriver(root_key=root)

        if hasattr(deriver1, "derive_child"):
            child1 = deriver1.derive_child(5)
            child2 = deriver2.derive_child(5)
            assert child1.key == child2.key
    except ImportError:
        pytest.skip("CachedKeyDeriver not available")
