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

import pytest

# Constants for skip messages
SKIP_MEMORY_STORAGE = "MemoryStorage operations not available"


# ========================================================================
# Storage interface branches
# ========================================================================


def test_storage_interface_exists():
    """Test that Storage interface exists."""
    try:
        from bsv.storage import Storage

        assert Storage is not None
    except ImportError:
        pytest.skip("Storage interface not available")


def test_memory_storage_init():
    """Test MemoryStorage initialization."""
    try:
        from bsv.storage import MemoryStorage

        storage = MemoryStorage()
        assert storage is not None
    except (ImportError, AttributeError):
        pytest.skip("MemoryStorage not available")


# ========================================================================
# Storage operations branches
# ========================================================================


def test_storage_set_get():
    """Test setting and getting value."""
    try:
        from bsv.storage import MemoryStorage

        storage = MemoryStorage()

        if hasattr(storage, "set") and hasattr(storage, "get"):
            storage.set("key", "value")
            result = storage.get("key")
            assert result == "value"
    except (ImportError, AttributeError):
        pytest.skip(SKIP_MEMORY_STORAGE)


def test_storage_delete():
    """Test deleting value."""
    try:
        from bsv.storage import MemoryStorage

        storage = MemoryStorage()

        if hasattr(storage, "set") and hasattr(storage, "delete"):
            storage.set("key", "value")
            storage.delete("key")

            if hasattr(storage, "get"):
                try:
                    result = storage.get("key")
                    assert result is None
                except KeyError:
                    # Expected
                    pass
    except (ImportError, AttributeError):
        pytest.skip(SKIP_MEMORY_STORAGE)


def test_storage_exists():
    """Test checking if key exists."""
    try:
        from bsv.storage import MemoryStorage

        storage = MemoryStorage()

        if hasattr(storage, "set") and hasattr(storage, "exists"):
            storage.set("key", "value")
            assert storage.exists("key")
            assert not storage.exists("nonexistent")
    except (ImportError, AttributeError):
        pytest.skip(SKIP_MEMORY_STORAGE)


# ========================================================================
# File storage branches
# ========================================================================


def test_file_storage_init():
    """Test FileStorage initialization."""
    try:
        from bsv.storage import FileStorage

        try:
            # Using /tmp for test purposes only, not production code
            storage = FileStorage(path="/tmp/test_storage")  # NOSONAR
            assert storage is not None
        except (TypeError, OSError):
            # May require different parameters
            pytest.skip("FileStorage initialization different")
    except (ImportError, AttributeError):
        pytest.skip("FileStorage not available")


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


def test_storage_get_nonexistent():
    """Test getting non-existent key."""
    try:
        from bsv.storage import MemoryStorage

        storage = MemoryStorage()

        if hasattr(storage, "get"):
            try:
                result = storage.get("nonexistent")
                assert result is None
            except KeyError:
                # Expected
                pass
    except (ImportError, AttributeError):
        pytest.skip("MemoryStorage not available")


def test_storage_overwrite():
    """Test overwriting value."""
    try:
        from bsv.storage import MemoryStorage

        storage = MemoryStorage()

        if hasattr(storage, "set") and hasattr(storage, "get"):
            storage.set("key", "value1")
            storage.set("key", "value2")
            result = storage.get("key")
            assert result == "value2"
    except (ImportError, AttributeError):
        pytest.skip(SKIP_MEMORY_STORAGE)
