"""Tests for Coinbase exchange adapter."""

import pytest
from unittest.mock import Mock, AsyncMock, patch
from src.exchanges.coinbase import CoinbaseExchange


class TestCoinbaseExchange:
    """Tests for CoinbaseExchange."""
    
    @pytest.fixture
    def exchange(self):
        """Create Coinbase exchange instance."""
        return CoinbaseExchange(
            name="test_coinbase",
            api_key="test_key",
            secret_key="test_secret",
            permissions="read_only"
        )
    
    @pytest.mark.asyncio
    async def test_connect(self, exchange):
        """Test connection to Coinbase."""
        with patch.object(exchange.exchange, 'load_markets', new_callable=AsyncMock):
            await exchange.connect()
            assert exchange.is_connected
    
    @pytest.mark.asyncio
    async def test_fetch_balance(self, exchange):
        """Test fetching balance."""
        mock_balance = {'USD': {'free': 1000.0, 'used': 0.0, 'total': 1000.0}}
        exchange.exchange.fetch_balance = AsyncMock(return_value=mock_balance)
        exchange.is_connected = True
        
        balance = await exchange.fetch_balance()
        assert balance == mock_balance
    
    @pytest.mark.asyncio
    async def test_place_order_readonly(self, exchange):
        """Test placing order with read-only permissions."""
        exchange.permissions = "read_only"
        exchange.is_connected = True
        
        with pytest.raises(PermissionError):
            await exchange.place_order("BTC/USD", "buy", "market", 0.01)
    
    @pytest.mark.asyncio
    async def test_fetch_ohlcv(self, exchange):
        """Test fetching OHLCV data."""
        mock_ohlcv = [[1609459200000, 29000, 29500, 28800, 29300, 1000]]
        exchange.exchange.fetch_ohlcv = AsyncMock(return_value=mock_ohlcv)
        exchange.is_connected = True
        
        ohlcv = await exchange.fetch_ohlcv("BTC/USD", "1h")
        assert ohlcv == mock_ohlcv

