Tests for Kiro Gateway
A comprehensive set of unit and integration tests for Kiro Gateway, providing full coverage of all system components.
Testing Philosophy: Complete Network Isolation
The key principle of this test suite is 100% isolation from real network requests.
This is achieved through a global, automatically applied fixture block_all_network_calls in tests/conftest.py. It intercepts and blocks any attempts by httpx.AsyncClient to establish connections at the application level.
Benefits:
- Reliability: Tests don't depend on external API availability or network state.
- Speed: Absence of real network delays makes test execution instant.
- Security: Guarantees that test runs never use real credentials.
Any attempt to make an unauthorized network call will result in immediate test failure with an error, ensuring strict isolation control.
Running Tests
Installing Dependencies
# Main project dependencies
pip install -r requirements.txt
# Additional testing dependencies
pip install pytest pytest-asyncio hypothesis
Running All Tests
# Run the entire test suite
pytest
# Run with verbose output
pytest -v
# Run with verbose output and coverage
pytest -v -s --tb=short
# Run only unit tests
pytest tests/unit/ -v
# Run only integration tests
pytest tests/integration/ -v
# Run a specific file
pytest tests/unit/test_auth_manager.py -v
# Run a specific test
pytest tests/unit/test_auth_manager.py::TestKiroAuthManagerInitialization::test_initialization_stores_credentials -v
pytest Options
# Stop on first failure
pytest -x
# Show local variables on errors
pytest -l
# Run in parallel mode (requires pytest-xdist)
pip install pytest-xdist
pytest -n auto
Test Structure
tests/
βββ conftest.py # Shared fixtures and utilities
βββ unit/ # Unit tests for individual components
β βββ test_auth_manager.py # KiroAuthManager tests
β βββ test_cache.py # ModelInfoCache tests (is_valid_model, add_hidden_model)
β βββ test_config.py # Configuration tests (SERVER_HOST, SERVER_PORT, LOG_LEVEL, etc.)
β βββ test_converters_anthropic.py # Anthropic Messages API β Kiro converter tests
β βββ test_converters_core.py # Shared conversion logic tests (UnifiedMessage, merging, etc.)
β βββ test_converters_openai.py # OpenAI Chat API β Kiro converter tests
β βββ test_debug_logger.py # DebugLogger tests (off/errors/all modes)
β βββ test_debug_middleware.py # DebugLoggerMiddleware tests (endpoint filtering, mode handling)
β βββ test_exceptions.py # Exception handlers tests (validation_exception_handler, sanitize_validation_errors)
β βββ test_http_client.py # KiroHttpClient tests
β βββ test_main_cli.py # CLI argument parsing tests (--host, --port)
β βββ test_model_resolver.py # Dynamic Model Resolution System tests
β βββ test_models_anthropic.py # Anthropic Pydantic models tests (all content blocks, tools, streaming)
β βββ test_models_openai.py # OpenAI Pydantic models tests (messages, tools, responses, streaming)
β βββ test_parsers.py # AwsEventStreamParser tests (including JSON truncation diagnostics)
β βββ test_routes_anthropic.py # Anthropic API endpoint tests (/v1/messages)
β βββ test_routes_openai.py # OpenAI API endpoint tests (/v1/chat/completions)
β βββ test_streaming_anthropic.py # Anthropic streaming response tests
β βββ test_streaming_core.py # Shared streaming logic tests
β βββ test_streaming_openai.py # OpenAI streaming response tests
β βββ test_thinking_parser.py # ThinkingParser tests (FSM for thinking blocks)
β βββ test_tokenizer.py # Tokenizer tests (tiktoken)
β βββ test_vpn_proxy.py # VPN/Proxy configuration tests (environment variables, URL normalization, NO_PROXY)
βββ integration/ # Integration tests for full flow
β βββ test_full_flow.py # End-to-end tests
βββ README.md # This file
Testing Philosophy
Principles
- Isolation: Each test is completely isolated from external services through mocks
- Detail: Abundant print() for understanding test flow during debugging
- Coverage: Tests cover not only happy path, but also edge cases and errors
- Security: All tests use mock credentials, never real ones
Test Structure (Arrange-Act-Assert)
Each test follows the pattern:
- Arrange (Setup): Prepare mocks and data
- Act (Action): Execute the tested action
- Assert (Verify): Verify result with explicit comparison
Test Types
- Unit tests: Test individual functions/classes in isolation
- Integration tests: Verify component interactions
- Security tests: Verify security system
- Edge case tests: Paranoid edge case checks
Adding New Tests
When adding new tests:
- Follow existing class structure (
Test*Success,Test*Errors,Test*EdgeCases) - Use descriptive names:
test_<what_it_does>_<expected_result> - Add docstring with "What it does" and "Purpose"
- Use print() for logging test steps
Troubleshooting
Tests fail with ImportError
# Make sure you're in project root
cd /path/to/kiro-gateway
# pytest.ini already contains pythonpath = .
# Just run pytest
pytest
Tests pass locally but fail in CI
- Check dependency versions in requirements.txt
- Ensure all mocks correctly isolate external calls
Async tests don't work
# Make sure pytest-asyncio is installed
pip install pytest-asyncio
# Check for @pytest.mark.asyncio decorator
Coverage Metrics
To check code coverage:
# Install coverage
pip install pytest-cov
# Run with coverage report
pytest --cov=kiro --cov-report=html
# View report
open htmlcov/index.html # macOS/Linux
start htmlcov/index.html # Windows
Contacts and Support
If you find bugs or have suggestions for test improvements, create an issue in the project repository.