README.mdā¢6.05 kB
# Testing Suite
This directory contains comprehensive tests for the Low Cost Browsing MCP Server.
## Test Structure
```
tests/
āāā unit/ # Unit tests for individual components
ā āāā browser-manager.test.ts # BrowserManager tests
ā āāā llm-provider.test.ts # LLM provider tests
ā āāā navigation.test.ts # Navigation tools tests
ā āāā extraction.test.ts # Extraction tools tests
ā āāā llm-tools.test.ts # LLM transformation tests
ā āāā error-scenarios.test.ts # Error handling tests
ā āāā llm-errors.test.ts # LLM-specific error tests
āāā e2e/ # End-to-end tests
ā āāā basic-navigation.spec.ts # Basic navigation flows
ā āāā interaction-flow.spec.ts # Complex interaction scenarios
āāā integration/ # Integration tests
ā āāā llm-integration.test.ts # LLM integration tests
ā āāā real-website.test.ts # Real website tests
āāā mocks/ # Mock implementations
ā āāā llm-providers.ts # Mock LLM providers and browser
āāā fixtures/ # Test data and configurations
ā āāā test-config.yaml # Test configuration
āāā setup.ts # Global test setup
```
## Test Types
### Unit Tests
- Test individual components in isolation
- Use mocks for external dependencies
- Fast execution, no network calls
- Coverage: BrowserManager, LLM providers, all tools
### E2E Tests
- Test complete user scenarios
- Use real browser automation
- Test actual tool interactions
- Scenarios: navigation, form filling, data extraction
### Integration Tests
- Test with real external services
- Require actual LLM providers
- May require API keys or local models
- Test real website interactions
### Error Tests
- Test all error scenarios
- Verify proper error codes and messages
- Cover timeouts, network issues, invalid inputs
- Test graceful degradation
## Running Tests
### All Tests
```bash
npm test # Run unit tests only
npm run test:e2e # Run E2E tests only
npm run test:all # Run all tests
npm run test:coverage # Run with coverage report
```
### Specific Test Categories
```bash
# Unit tests
npx jest tests/unit/
# E2E tests
npx playwright test
# Integration tests (requires setup)
npx jest tests/integration/
# Error scenarios
npx jest tests/unit/error-scenarios.test.ts
```
### Watch Mode
```bash
npm run test:watch # Watch unit tests
npm run test:e2e:ui # Playwright UI mode
```
## Test Configuration
### Jest (Unit Tests)
- Configuration: `jest.config.js`
- TypeScript support with `ts-jest`
- ESM module support
- Coverage threshold: 80%
- Timeout: 30 seconds
### Playwright (E2E Tests)
- Configuration: `playwright.config.ts`
- Browsers: Chromium, Firefox, WebKit
- Parallel execution
- Trace collection on failures
## Environment Setup
### Required Environment Variables
For integration tests that use real LLM providers:
```bash
# Optional - enables LLM integration tests
OPENAI_API_KEY=sk-your-key-here
ANTHROPIC_API_KEY=sk-ant-your-key-here
OLLAMA_AVAILABLE=true # Set to true if Ollama is running
```
### Local Model Setup
For Ollama tests:
```bash
# Install and start Ollama
ollama serve
# Pull test model
ollama pull llama3.1
```
## Coverage Reports
Coverage reports are generated in the `coverage/` directory:
- `coverage/lcov-report/index.html` - HTML report
- `coverage/lcov.info` - LCOV format
- `coverage/coverage-final.json` - JSON format
### Coverage Targets
- Statements: >80%
- Branches: >80%
- Functions: >80%
- Lines: >80%
## Mock Strategy
### External Dependencies
- **OpenAI API**: Mocked to return predictable responses
- **Anthropic API**: Mocked for consistent testing
- **Ollama/JAN APIs**: Mocked HTTP requests
- **Playwright Browser**: Mocked for unit tests, real for E2E
### Mock Data
- Predefined responses for various scenarios
- Error conditions for testing edge cases
- Large content for testing limits
- CAPTCHA detection scenarios
## Test Scenarios
### Navigation Tests
- ā
Basic URL opening
- ā
Page navigation
- ā
Session persistence
- ā
Timeout handling
- ā
Invalid URLs
### Interaction Tests
- ā
Form filling
- ā
Button clicking
- ā
Text input
- ā
Wait conditions
- ā
Dynamic content
### Extraction Tests
- ā
HTML content extraction
- ā
Text content extraction
- ā
Table extraction
- ā
Attribute extraction
- ā
Screenshot capture
- ā
Size limits
### LLM Tests
- ā
Text transformation
- ā
JSON schema validation
- ā
Preprocessing
- ā
Multiple providers
- ā
Error handling
### Error Scenarios
- ā
Navigation timeouts
- ā
Element not found
- ā
CAPTCHA detection
- ā
Content too large
- ā
LLM failures
- ā
API errors
## Continuous Integration
Tests are designed to run in CI environments:
- Headless browser mode
- No external dependencies for unit tests
- Optional integration tests with proper env vars
- Coverage reporting
- Parallel execution support
## Debugging Tests
### Failed Tests
```bash
# Run specific test
npx jest navigation.test.ts
# Run with debug output
npx jest --verbose navigation.test.ts
# Playwright debug mode
npx playwright test --debug
```
### Coverage Analysis
```bash
# Generate detailed coverage
npm run test:coverage
# Open HTML report
open coverage/lcov-report/index.html
```
### Logs and Traces
- E2E test traces in `test-results/`
- Playwright screenshots on failures
- Jest verbose output for debugging
- Browser console logs captured
## Contributing
When adding new features:
1. Add unit tests for new components
2. Add E2E tests for new user scenarios
3. Test error conditions
4. Maintain >80% coverage
5. Update this README if needed
### Test Naming Convention
- Unit tests: `component-name.test.ts`
- E2E tests: `scenario-name.spec.ts`
- Integration tests: `integration-name.test.ts`
- Error tests: `error-type.test.ts`