# Tests
This directory contains test scripts and fixtures for xhs-mcp.
## π Directory Structure
```
tests/
βββ integration/ # Integration test scripts
β βββ image-downloader.test.js # Image URL download tests
β βββ title-validation.test.js # Title validation tests
βββ fixtures/ # Test data and fixtures
β βββ Bert.jpg # Sample image for testing
βββ README.md # This file
```
## π§ͺ Running Tests
### Run All Tests
```bash
npm test
```
### Run Individual Tests
```bash
# Test image downloader
node tests/integration/image-downloader.test.js
# Test title validation
node tests/integration/title-validation.test.js
```
### Enable Debug Logging
```bash
XHS_ENABLE_LOGGING=true node tests/integration/image-downloader.test.js
```
## π Test Coverage
### Integration Tests
#### 1. Image Downloader Tests (`image-downloader.test.js`)
Tests the image URL download and caching functionality:
- β Download single image from URL
- β Download multiple images concurrently
- β Process mixed paths (URLs + local files)
- β Cache mechanism (avoid duplicate downloads)
- β URL detection and validation
**Expected Output:**
```
π Testing image URL download feature...
β ImageDownloader created
π₯ Test 1: Downloading single image...
β Downloaded: ./temp_images/img_abc123_1234567890.jpg
File size: 45.67 KB
Cached: false
...
π All tests passed!
```
#### 2. Title Validation Tests (`title-validation.test.js`)
Tests the title width validation according to Xiaohongshu's display rules:
- β English-only titles
- β Chinese-only titles
- β Mixed language titles
- β Emoji handling
- β Width calculation (CJK: 2 units, ASCII: 1 unit)
- β Title truncation
- β Detailed character breakdown
- β Edge cases
**Expected Output:**
```
π§ͺ Testing Title Width Validation
XHS Title Constraints: Max 40 units
π Test Results:
Test 1: English only
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Title: "Hello World"
Length: 11 characters
Width: 11 units (max: 40)
Remaining: 29 units
Status: β Valid
...
```
## π§ Test Fixtures
### fixtures/Bert.jpg
Sample image file used for testing mixed path processing (URL + local file).
- **Format**: JPEG
- **Usage**: Integration tests for image downloader
- **Purpose**: Verify local file handling alongside URL downloads
## π Writing Tests
When adding new tests:
1. Place integration tests in `tests/integration/`
2. Add test data to `tests/fixtures/`
3. Follow the naming convention: `*.test.js`
4. Include clear test descriptions and expected outputs
5. Update this README with test coverage information
### Test Template
```javascript
#!/usr/bin/env node
/**
* Test script for [feature name]
*
* Usage:
* node tests/integration/[test-name].test.js
* npm test
*/
async function testFeature() {
console.log('π Testing [feature]...\n');
try {
// Test setup
// Test 1
console.log('π₯ Test 1: [description]...');
// ... test code ...
console.log('β Passed\n');
// Test 2
console.log('π₯ Test 2: [description]...');
// ... test code ...
console.log('β Passed\n');
console.log('π All tests passed!');
} catch (error) {
console.error('β Test failed:', error.message);
process.exit(1);
}
}
testFeature().catch(console.error);
```
## βοΈ Test Configuration
Tests rely on:
- **Built artifacts**: Tests run against `dist/` directory
- **Dependencies**: All dependencies from `package.json`
- **Environment**: Node.js runtime
Make sure to build the project before running tests:
```bash
npm run build
npm test
```
## π Troubleshooting
### Test Fails with Module Not Found
Make sure to build the project first:
```bash
npm run build
```
### Image Download Test Fails
Check your internet connection and ensure the test URLs are accessible:
```bash
curl -I https://picsum.photos/800/600
```
### Title Validation Test Fails
Ensure `string-width` package is installed:
```bash
npm install
```
## π Related Documentation
- [Usage Guide](../docs/USAGE_GUIDE.md) - How to use xhs-mcp
- [Project Structure](../docs/PROJECT_STRUCTURE.md) - Code organization
- [README](../README.md) - Main project documentation