api_gemini 0.7.1

Gemini's API for accessing large language models (LLMs).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# api_gemini Testing Guide

This document explains the testing philosophy, policies, and organization structure for the `api_gemini` crate.

## 🚫 NO MOCKUP TESTS POLICY

This crate follows a **strict no-mockup policy** for all testing:

### Core Principles

- **Real Integration Tests Only**: All API functionality is tested against the actual Gemini API
- **No Mock Servers**: Tests use real HTTP calls to Google's production endpoints
- **No Mock Objects**: No synthetic test doubles or stub implementations
- **No Test Ignoring**: Tests are never marked with `#[ignore]` or conditionally skipped
- **No Silent Skipping**: Tests NEVER silently skip when API keys are unavailable
- **Missing API Key = Test Failure**: When `GEMINI_API_KEY` is unavailable, tests MUST fail explicitly (not skip, not pass)
- **Explicit Failures Only**: All authentication errors and missing keys result in test failures, never graceful skips
- **Confidence in Reality**: Tests validate actual production behavior, not simulated responses

### Rationale

**Why we don't use mocks:**
1. **Hide Integration Failures**: Mocks can hide real-world integration issues
2. **Miss API Changes**: Real API changes aren't caught by mock tests
3. **False Confidence**: Passing mock tests don't guarantee production functionality
4. **Edge Case Blind Spots**: Real services have edge cases that mocks don't simulate
5. **Maintenance Overhead**: Mock data becomes stale and requires constant updates

**Why we don't silently skip tests:**
1. **Masks Configuration Issues**: Silent skips hide missing API keys in CI/CD
2. **False Green Builds**: Tests appear to pass when they didn't actually run
3. **Deployment Risks**: Code reaches production without proper integration validation
4. **Debug Difficulty**: Developers don't know if tests ran or were skipped

**Benefits of real API testing with explicit failures:**
1. **Production Confidence**: Tests prove the client works with the actual service
2. **Immediate Feedback**: API changes and configuration issues detected immediately
3. **Real Error Handling**: Tests encounter actual error conditions
4. **Performance Insights**: Tests reveal actual latency and timing characteristics
5. **Authentication Validation**: Tests verify actual API key and auth flows
6. **CI/CD Integrity**: Build failures force proper configuration before deployment

## πŸ“ Test Organization Structure

```
tests/
β”œβ”€β”€ readme.md                               # This file - testing guide and policies
β”œβ”€β”€ docs/                                   # Behavioral spec files mirroring docs/ entity instances
β”œβ”€β”€ api_key_failure_tests.rs               # API key validation and loud failure behavior
β”œβ”€β”€ audio_processing_tests.rs              # Audio content processing tests
β”œβ”€β”€ buffered_streaming_tests.rs            # Buffered streaming feature tests
β”œβ”€β”€ cache_tests.rs                         # Request caching feature tests
β”œβ”€β”€ code_execution_tests.rs               # Code execution feature integration tests
β”œβ”€β”€ comprehensive_integration_tests.rs    # Extended real API testing scenarios
β”œβ”€β”€ compression_tests.rs                  # HTTP compression feature integration tests
β”œβ”€β”€ config_optimization_tests.rs          # Client configuration optimization tests
β”œβ”€β”€ cost_quota_tests.rs                   # Enterprise cost quota management tests
β”œβ”€β”€ count_tokens_tests.rs                 # Real count tokens API functionality
β”œβ”€β”€ dynamic_configuration_tests.rs        # Hot-reload config change tests
β”œβ”€β”€ enhanced_circuit_breaker_tests.rs     # Circuit breaker enterprise feature tests
β”œβ”€β”€ enhanced_rate_limiting_tests.rs       # Rate limiting enterprise feature tests
β”œβ”€β”€ enhanced_retry_logic_tests.rs         # Retry logic with exponential backoff tests
β”œβ”€β”€ enterprise_quota_management_tests.rs  # Quota enforcement tests
β”œβ”€β”€ example_validation_tests.rs           # Documentation example validation
β”œβ”€β”€ failover_tests.rs                     # Multi-endpoint failover tests
β”œβ”€β”€ health_checks_tests.rs                # Periodic health monitoring tests
β”œβ”€β”€ integration_tests.rs                  # Core real API integration tests
β”œβ”€β”€ model_comparison_tests.rs             # Model comparison feature tests
β”œβ”€β”€ streaming_control_tests.rs            # Stream pause/resume/cancel tests
β”œβ”€β”€ streaming_optimization_tests.rs       # Streaming performance tests
β”œβ”€β”€ structured_logging_tests.rs           # Logging and diagnostics tests
β”œβ”€β”€ sync_api_tests.rs                     # Synchronous API wrapper tests
β”œβ”€β”€ system_instructions_tests.rs          # System prompt configuration tests
β”œβ”€β”€ templates_tests.rs                    # Request template feature tests
β”œβ”€β”€ websocket_streaming_tests.rs          # WebSocket bidirectional streaming tests
β”œβ”€β”€ common/                               # Shared test infrastructure
β”œβ”€β”€ deployment/                           # Deployment scenario tests
β”œβ”€β”€ media_api/                            # Media file management tests
└── safety/                               # Safety settings and filtering tests
```

## 🎯 Test Categories

### 1. Integration Tests (`integration_tests.rs`)
**Purpose**: Core API functionality validation
**Type**: Real API calls
**Requirements**: Valid `GEMINI_API_KEY`

- List models and get model details
- Single-turn content generation
- Multi-turn conversations
- Multimodal content (text + images)
- Function calling and tool use
- Text embeddings
- Safety settings
- Error handling for invalid inputs

### 2. Comprehensive Integration Tests (`comprehensive_integration_tests.rs`)
**Purpose**: Extended real-world scenarios
**Type**: Real API calls
**Requirements**: Valid `GEMINI_API_KEY`

- Advanced feature combinations
- Edge cases and error conditions
- Performance and timeout scenarios
- Large request handling
- Concurrent operation testing

### 3. Count Tokens Tests (`count_tokens_tests.rs`)
**Purpose**: Token counting functionality
**Type**: Real API calls
**Requirements**: Valid `GEMINI_API_KEY`

- Simple text token counting
- Multimodal content token counting
- Conversation context token counting
- Different model token counting
- Error handling for invalid requests

### 4. Synchronous API Tests (`sync_api_tests.rs`)
**Purpose**: Blocking wrapper validation
**Type**: Real API calls (via sync wrapper)
**Requirements**: Valid `GEMINI_API_KEY`

- Sync client construction
- Thread safety validation
- Runtime management
- Performance overhead measurement

### 5. Unit Tests (embedded in implementation files)
**Purpose**: Internal logic validation
**Type**: Pure unit tests (no API calls)
**Requirements**: None

- Builder pattern validation
- Error type construction
- Data structure serialization
- Configuration parsing

### 6. Example Validation Tests (`example_validation_test.rs`)
**Purpose**: Documentation example verification
**Type**: Real API calls
**Requirements**: Valid `GEMINI_API_KEY`

- Ensure README examples work
- Validate API patterns in docs
- Verify example code compiles and runs

## πŸ”§ Running Tests

### Run All Tests (Default - Requires API Key)
```bash
cargo test
```

### Run Only Unit Tests (No API Key Required)
```bash
cargo test --no-default-features
```

### Run Specific Test Categories
```bash
# Core integration tests
cargo test --test integration_tests

# Count tokens functionality
cargo test --test count_tokens_tests

# Synchronous API tests
cargo test --test sync_api_tests

# Example validation
cargo test --test example_validation_test
```

### Debug Individual Tests
```bash
# Run with output capture disabled
cargo test test_generate_content_simple -- --nocapture

# Run specific test with features
cargo test --features logging test_structured_logging
```

## πŸ”‘ API Key Requirements

### How Tests Load API Keys

Tests use `Client::new()` which relies on **workspace_tools** to load the API key in this order:

1. **Workspace Secrets File** (Primary): `secret/-secrets.sh`
   - Uses workspace_tools 0.6.0 to locate the workspace root
   - **Important**: workspace_tools 0.6.0 uses `secret/` (visible directory, NO dot prefix)
   - Follows the [Secret Directory Policy]../../../secret/readme.md

2. **Environment Variable** (Fallback): `GEMINI_API_KEY`
   - Standard environment variable
   - Used if workspace secrets unavailable

### Setup Options

**Option 1: Workspace Secrets File (Recommended)**
```bash
# workspace_tools 0.6.0 uses secret/ directory (visible, NO dot prefix)
echo 'export GEMINI_API_KEY="your-key-here"' >> secret/-secrets.sh
chmod 600 secret/-secrets.sh
```

**Option 2: Environment Variable**
```bash
export GEMINI_API_KEY="your-key-here"
```

### Test Behavior and Error Messages

- **With Valid API Key**: All tests run and validate against real API

- **Without API Key**: Integration tests FAIL EXPLICITLY with detailed error messages showing:
  - Workspace secrets path tried (`secret/-secrets.sh`)
  - Specific error from workspace_tools (e.g., "key not found or file unreadable")
  - Environment variable status (e.g., "not set or empty")
  - Clear setup instructions with exact commands

- **Invalid API Key**: Tests FAIL with authentication errors (this is correct and expected behavior)

- **Silent Skips Prohibited**: Tests never silently skip - all missing keys result in explicit test failures with actionable error messages listing all paths tried

## ⚠️ Important Testing Insights

### API Response Timing (Real-World Data)

Based on actual API testing, different request types have significantly different response times:

- **Simple text generation**: ~0.5 seconds (fast)
- **Safety settings requests**: ~15-17 seconds (slow due to content analysis)
- **Function calling**: ~2-4 seconds (moderate)
- **Multimodal requests**: ~3-8 seconds (varies by image complexity)

### Test Timeout Strategy

Tests use appropriate timeouts based on actual API behavior:

```rust
// Safety settings require longer timeouts
let result = tokio::time::timeout
(
  Duration::from_secs( 25 ), // Accommodate safety processing
  client.models().by_name( "gemini-1.5-pro-latest" )
    .generate_content( &safety_request )
).await;
```

### Common Pitfalls to Avoid

❌ **Don't do this:**
- Silent test skipping on failures or missing keys
- Graceful skips when API keys are unavailable
- Generic short timeouts for all request types
- Environment variable race conditions in parallel tests
- Assuming all API calls have same performance characteristics
- Using `.expect()` to hide authentication errors

βœ… **Do this instead:**
- Explicit test failures with actionable error messages when keys are missing
- Let authentication errors propagate as test failures (NOT skips)
- Request-type-specific timeouts
- Proper test isolation
- Clear panic messages that indicate missing API key configuration

## πŸ—οΈ Test Development Guidelines

### When Adding New Tests

1. **Follow the no-mock policy**: Use real API calls for all functionality tests
2. **Never skip on missing keys**: Tests MUST fail explicitly when API keys are missing (use `.expect()` with clear messages)
3. **No graceful skip patterns**: Don't use `match` patterns that return `Ok(())` on authentication failures
4. **Use appropriate timeouts**: Different request types need different timeout values
5. **Test error conditions**: Validate error handling with real API error responses
6. **Document test purpose**: Include clear comments about what each test validates
7. **Explicit failure messages**: Use `.expect("GEMINI_API_KEY not found...")` for clear error reporting

### Test Naming Conventions

- `integration_test_*`: Real API integration tests
- `test_*_real_api`: Explicit real API testing
- `test_*_error_handling`: Error condition validation
- `test_*_authentication_*`: Auth-related testing

### Error Handling Patterns

**βœ… CORRECT: Explicit failure on missing API key**
```rust
// Use shared helper from tests/common/mod.rs
// Panics with clear diagnostic message if key missing
#[ tokio::test ]
async fn test_generate_content() -> Result< (), Box< dyn std::error::Error > >
{
  let client = create_integration_client();

  let request = GenerateContentRequest { /* ... */ };
  let response = client.models().by_name( "gemini-1.5-pro" ).generate_content( &request ).await?;

  assert!( !response.candidates.is_empty() );
  Ok( () )
}
```

**❌ WRONG: Graceful skip pattern (DO NOT USE)**
```rust
// This is PROHIBITED - never do this
let client = match create_test_client()
{
  Ok( client ) => client,
  Err( _ ) =>
  {
    println!( "⏸️  Skipping test - no API key available" );
    return Ok( () ); // WRONG - this hides missing configuration
  }
};
```

## πŸ“Š Test Metrics

### Current Test Coverage

- **Total Tests**: ~265 test functions across 35 test files
- **Integration Tests**: real API endpoint tests (require `GEMINI_API_KEY`)
- **Unit Tests**: pure unit tests (no API key required)
- **Success Rate**: 100% (when API key is available)

### Test Categories Breakdown

| Category | File(s) | Count | Type | API Required |
|----------|---------|-------|------|--------------|
| Core Integration | `integration_tests.rs` | 19 | Real API + Validation | Mix |
| Comprehensive Integration | `comprehensive_integration_tests.rs` | 16 | Real API | Yes |
| Count Tokens | `count_tokens_tests.rs` | 8 | Real API | Yes |
| Sync API | `sync_api_tests.rs` | 8 | Real API | Yes |
| Enterprise Cost Quota | `cost_quota_tests.rs` | 26 | Real API | Yes |
| Enterprise Quota Mgmt | `enterprise_quota_management_tests.rs` | 16 | Real API | Yes |
| Example Validation | `example_validation_tests.rs` | 14 | Real API | Yes |
| Audio Processing | `audio_processing_tests.rs` | 7 | Real API | Yes |
| Code Execution | `code_execution_tests.rs` | 8 | Real API | Yes |
| Compression | `compression_tests.rs` | 18 | Real API | Yes |
| Dynamic Config | `dynamic_configuration_tests.rs` | 15 | Real API | Yes |
| Failover | `failover_tests.rs` | 9 | Real API | Yes |
| Health Checks | `health_checks_tests.rs` | 6 | Real API | Yes |
| Model Comparison | `model_comparison_tests.rs` | 8 | Real API | Yes |
| Streaming Control | `streaming_control_tests.rs` | 14 | Real API | Yes |
| Streaming Optimization | `streaming_optimization_tests.rs` | 4 | Real API | Yes |
| Structured Logging | `structured_logging_tests.rs` | 9 | Real API | Yes |
| System Instructions | `system_instructions_tests.rs` | 5 | Real API | Yes |
| WebSocket Streaming | `websocket_streaming_tests.rs` | 12 | Real API | Yes |
| Circuit Breaker | `enhanced_circuit_breaker_tests.rs` | 7 | Unit | No |
| Rate Limiting | `enhanced_rate_limiting_tests.rs` | 7 | Unit | No |
| Retry Logic | `enhanced_retry_logic_tests.rs` | 7 | Unit | No |
| Caching | `cache_tests.rs` | 6 | Unit | No |
| API Key Failure | `api_key_failure_tests.rs` | 5 | Unit | No |
| Buffered Streaming | `buffered_streaming_tests.rs` | 5 | Unit | No |
| Templates | `templates_tests.rs` | 8 | Unit | No |
| Deployment | `deployment/` | 26 | Mixed | Mixed |
| Media API | `media_api/` | 20 | Real API | Yes |
| Safety | `safety/` | 16 | Mixed | Mixed |

## πŸ”„ Continuous Integration

### CI/CD Strategy

**IMPORTANT**: Tests themselves NEVER gracefully skip. The CI pipeline must decide whether to run integration tests or not.

```bash
# CI pipeline example - decision happens at CI level, NOT in tests
if [ -z "$GEMINI_API_KEY" ]; then
    # Explicitly skip integration tests at CI level
    cargo test --no-default-features
    echo "⚠️ Running unit tests only - no GEMINI_API_KEY configured"
    echo "⚠️ Integration tests were NOT run - configure key for full validation"
else
    # Run full test suite including integration tests
    cargo test
    echo "βœ… All tests passed including integration tests"
fi
```

**Key Points**:
- Tests themselves use `.expect()` and fail when keys are missing
- The CI pipeline decides whether to run integration tests or not via `--no-default-features`
- This ensures developers are always aware when integration tests didn't run
- No false confidence from silently skipped tests

### External Service Dependencies

Integration tests require real API access:
- All integration tests make real API calls to Google Gemini API
- Timeouts are set appropriately for each request type
- Authentication failures result in test failures (not skips)
- Clear error messages indicate missing API key configuration

## πŸ“š References

- [Main README Testing Section]../readme.md#testing
- [Google Gemini API Documentation]https://ai.google.dev/api/rest

---

**Last Updated**: 2025-01-28
**Maintainer**: Development Team
**Policy Version**: 1.0