Add comprehensive test suite with 97% code coverage

Implemented full test coverage for core bot components:

Test Coverage:
- MentionDetector: 98% (28 tests) - mention detection, nickname handling, content extraction
- Logger: 98% (15 tests) - file logging, log levels, unicode support
- ConversationMemory: 91% (25 tests) - history management, time filtering, JSON persistence
- PerplexityProvider: 100% (22 tests) - API calls, error handling, statistics
- Config: 100% (26 tests) - env loading, validation, JSON config

Infrastructure:
- Added pytest, pytest-asyncio, pytest-mock, pytest-cov to requirements.txt
- Created pytest.ini with coverage configuration
- Created .coveragerc to exclude non-production files
- Added conftest.py with shared fixtures and test isolation

Test Features:
- 116 total tests, all passing
- Isolated test environment (clean env vars, logging handlers)
- Async testing support for PerplexityProvider
- Mocked HTTP requests to avoid real API calls
- Comprehensive edge case coverage
- Unicode/German character support testing

Total: 97% code coverage across 273 statements
This commit is contained in:
Claude 2026-01-02 22:10:14 +00:00
parent db467d774c
commit f6813b5fa5
No known key found for this signature in database
10 changed files with 1760 additions and 0 deletions

111
tests/README.md Normal file
View file

@ -0,0 +1,111 @@
# Test Suite for Eugen Twitch Bot
Comprehensive test coverage for the Eugen bot components.
## Test Coverage
**Overall: 97% code coverage**
### Components Tested
- **MentionDetector** (utils.py) - 98% coverage
- Explicit mentions (@Eugen, Eugen:, etc.)
- Nickname detection and generation
- Ambiguous greeting detection
- Content extraction
- Edge cases and unicode support
- **Logger** (utils.py) - 98% coverage
- File-based logging
- Log levels (INFO, DEBUG, ERROR, WARNING)
- API call logging
- Unicode character support
- **ConversationMemory** (memory.py) - 91% coverage
- User history storage and retrieval
- Time-based message filtering
- Message limits enforcement
- JSON persistence
- Error handling
- **PerplexityProvider** (ai_provider.py) - 100% coverage
- API request/response handling
- Error handling (timeouts, network errors, HTTP errors)
- Statistics tracking
- API key validation
- **Config** (config.py) - 100% coverage
- Environment variable loading
- JSON configuration
- Validation
- Default values
## Running Tests
### Run all tests
```bash
pytest tests/
```
### Run with coverage report
```bash
pytest tests/ --cov=. --cov-report=term-missing
```
### Run specific test file
```bash
pytest tests/test_utils.py
pytest tests/test_memory.py
pytest tests/test_ai_provider.py
pytest tests/test_config.py
```
### Run specific test
```bash
pytest tests/test_utils.py::TestMentionDetector::test_mention_with_at_symbol
```
## Test Structure
```
tests/
├── __init__.py # Package marker
├── conftest.py # Shared fixtures and test configuration
├── test_utils.py # Tests for MentionDetector and Logger
├── test_memory.py # Tests for ConversationMemory
├── test_ai_provider.py # Tests for PerplexityProvider
└── test_config.py # Tests for Config
```
## Key Features
- **Test Isolation**: Each test runs in a clean environment with:
- Isolated environment variables
- Clean logging handlers
- Temporary directories for file operations
- **Async Testing**: Full support for async/await with pytest-asyncio
- **Mocking**: HTTP requests mocked using pytest-mock to avoid real API calls
- **Fixtures**: Reusable test data in conftest.py:
- `temp_dir`: Temporary directory with automatic cleanup
- `mock_env_file`: Sample .env file
- `mock_config_json`: Sample config.json
- `sample_conversation_history`: Test chat history
- `mock_perplexity_response`: Sample API response
## Test Statistics
- **Total Tests**: 116
- **Passing**: 116 (100%)
- **Code Coverage**: 97%
- **Test Execution Time**: ~1.2 seconds
## Coverage Gaps
The 3% uncovered code consists of:
- Error handling edge cases in memory.py (lines 92-94, 111-112, 143-144)
- Rarely-used utility functions in utils.py (lines 101, 165)
These are defensive code paths that are difficult to trigger in tests.