# Legnext SDK Tests

This directory contains tests for the Legnext Python SDK.

## Test Types

- **Unit Tests**: Test SDK components with mocked data (no real API calls)
  - `test_client.py` - Client initialization and configuration
  - `test_types.py` - Type models and validation
  - `test_webhook.py` - Webhook functionality
  - `test_errors.py` - Error handling

- **Integration Tests**: Test SDK with real API calls (requires API key)
  - `test_integration.py` - Real API integration tests

## Running Tests

### Unit Tests (Default - No API Key Required)

Run all unit tests:
```bash
pytest
```

Run with coverage:
```bash
pytest --cov=legnext --cov-report=html
```

Run specific test file:
```bash
pytest tests/test_client.py
```

Run tests matching a pattern:
```bash
pytest -k "test_client"
```

### Integration Tests (Requires API Key)

**Setup:**

1. Install dev dependencies (includes `python-dotenv`):
   ```bash
   uv pip install -e ".[dev]"
   # or
   pip install -e ".[dev]"
   ```

2. Create `.env` file from template:
   ```bash
   cp .env.example .env
   ```

3. Edit `.env` and add your API key:
   ```bash
   LEGNEXT_API_KEY=your_actual_api_key_here
   ```

**Run Integration Tests:**

Run only integration tests:
```bash
pytest tests/test_integration.py -v -m integration
```

Run all tests (unit + integration):
```bash
pytest -v
```

Skip integration tests (unit tests only):
```bash
pytest -m "not integration" -v
```

**Note:** Integration tests will automatically skip if `LEGNEXT_API_KEY` is not set.

## Test Structure

**Unit Tests (Mocked - No API Key Required):**
- `conftest.py` - Pytest configuration and shared fixtures
- `test_client.py` - Tests for Client and AsyncClient
- `test_types.py` - Tests for type models and validation
- `test_webhook.py` - Tests for webhook functionality
- `test_errors.py` - Tests for error handling

**Integration Tests (Real API Calls - Requires API Key):**
- `test_integration.py` - Real API integration tests

## Writing Tests

### Using Fixtures

```python
def test_something(api_key, mock_task_response):
    client = Client(api_key=api_key)
    # Use mock_task_response...
```

### Mocking HTTP Requests

```python
from unittest.mock import Mock, patch

def test_with_mock(api_key):
    with patch('legnext._internal.http_client.httpx.Client') as mock_client:
        mock_response = Mock()
        mock_response.json.return_value = {"job_id": "test"}
        mock_client.return_value.request.return_value = mock_response

        client = Client(api_key=api_key)
        # Test...
```

### Async Tests

```python
import pytest

@pytest.mark.asyncio
async def test_async_feature():
    # Test async code...
```

## Coverage

We aim for >80% code coverage. Check coverage with:

```bash
pytest --cov=legnext --cov-report=term-missing
```

View detailed HTML report:
```bash
open htmlcov/index.html
```
