# Integration Testing Guide

This guide explains how to test the Legnext Python SDK with real API calls.

## Quick Start

1. **Install dev dependencies with uv:**
   ```bash
   uv pip install -e ".[dev]"
   ```

2. **Create `.env` file:**
   ```bash
   cp .env.example .env
   ```

3. **Add your API key to `.env`:**
   ```bash
   LEGNEXT_API_KEY=your_actual_api_key_here
   ```

4. **Run integration tests with uv:**
   ```bash
   # Run all integration tests
   uv run pytest tests/test_integration.py -v -m integration
   
   # Run specific test (balance)
   uv run pytest tests/test_integration.py::test_account_balance -v -m integration
   
   # Run specific test (diffusion)
   uv run pytest tests/test_integration.py::test_diffusion_generate_image -v -m integration
   ```

## Test Files

### Unit Tests (Default)
- Use mocked data, no real API calls
- Run with: `pytest` (no API key needed)
- Located in: `test_client.py`, `test_types.py`, `test_webhook.py`, `test_errors.py`

### Integration Tests
- Make real API calls
- Require valid API key in `.env` file
- Run with: `pytest tests/test_integration.py -v -m integration`
- Located in: `test_integration.py`

## Available Integration Tests

1. **`test_account_balance`** - Tests account balance API
   - Verifies API key authentication
   - Checks response structure
   - Prints balance information
   - **Run with:** `uv run pytest tests/test_integration.py::test_account_balance -v -m integration`

2. **`test_account_balance_async`** - Tests async account balance API
   - Same as above but with async client

3. **`test_diffusion_generate_image`** - Tests image generation (diffusion)
   - Creates a real image generation task
   - Waits for completion (may take 1-5 minutes)
   - Verifies generated images
   - **⚠️ Consumes API quota/points**
   - **Run with:** `uv run pytest tests/test_integration.py::test_diffusion_generate_image -v -m integration`

4. **`test_diffusion_generate_image_async`** - Tests async image generation
   - Same as above but with async client

5. **`test_client_initialization_with_real_key`** - Tests client initialization
   - Verifies API key is set correctly
   - Checks all resources are available

6. **`test_task_get`** - Tests task status retrieval
   - Requires `LEGNEXT_TEST_JOB_ID` in `.env` (optional)
   - Skips automatically if not set

## Environment Variables

### Required
- `LEGNEXT_API_KEY` - Your Legnext API key (required for integration tests)

### Optional
- `LEGNEXT_BASE_URL` - Custom API base URL (defaults to `https://api.legnext.ai/api`)
- `LEGNEXT_TEST_JOB_ID` - Job ID for testing task retrieval (optional)

## Running Tests with uv

### Run All Tests (Unit + Integration)
```bash
uv run pytest -v
```

### Run Only Unit Tests
```bash
uv run pytest -m "not integration" -v
```

### Run Only Integration Tests
```bash
uv run pytest tests/test_integration.py -v -m integration
```

### Run Specific Integration Tests

**Test account balance:**
```bash
uv run pytest tests/test_integration.py::test_account_balance -v -m integration
```

**Test diffusion image generation:**
```bash
uv run pytest tests/test_integration.py::test_diffusion_generate_image -v -m integration
```

**Test async diffusion:**
```bash
uv run pytest tests/test_integration.py::test_diffusion_generate_image_async -v -m integration
```

### Run Multiple Specific Tests
```bash
# Test both balance and diffusion
uv run pytest tests/test_integration.py::test_account_balance tests/test_integration.py::test_diffusion_generate_image -v -m integration
```

## Troubleshooting

### Integration tests are skipped
- Make sure `.env` file exists in project root
- Verify `LEGNEXT_API_KEY` is set in `.env`
- Check that `python-dotenv` is installed: `pip install python-dotenv`

### API authentication errors
- Verify your API key is correct
- Check that API key has proper permissions
- Ensure API key is not expired

### Import errors
- Make sure dev dependencies are installed: `uv pip install -e ".[dev]"`
- Verify you're in the project root directory

## Notes

- Integration tests make real API calls and may consume your API quota
- `.env` file is gitignored and will not be committed to the repository
- Unit tests run by default and don't require an API key
- Integration tests automatically skip if API key is not configured
