|
| 1 | +# Numbers-LE Testing Guidelines |
| 2 | + |
| 3 | +This document outlines testing practices and policies for Numbers-LE development. |
| 4 | + |
| 5 | +## Core Principle |
| 6 | + |
| 7 | +**No broken or failed tests are allowed in commits.** |
| 8 | + |
| 9 | +All tests must pass before code can be committed or merged. This ensures code quality and prevents regressions. |
| 10 | + |
| 11 | +## Running Tests |
| 12 | + |
| 13 | +### Run All Tests |
| 14 | + |
| 15 | +```bash |
| 16 | +bun run test |
| 17 | +``` |
| 18 | + |
| 19 | +### Run Tests with Coverage |
| 20 | + |
| 21 | +```bash |
| 22 | +bun run test:coverage |
| 23 | +``` |
| 24 | + |
| 25 | +### Run Tests in Watch Mode |
| 26 | + |
| 27 | +```bash |
| 28 | +bun run test:watch |
| 29 | +``` |
| 30 | + |
| 31 | +### Run Tests for Specific File |
| 32 | + |
| 33 | +```bash |
| 34 | +bun x vitest run src/extraction/formats/csv.test.ts |
| 35 | +``` |
| 36 | + |
| 37 | +## Test Structure |
| 38 | + |
| 39 | +### Unit Tests |
| 40 | + |
| 41 | +Located in `src/**/*.test.ts` and `src/**/*.spec.ts`: |
| 42 | + |
| 43 | +- **Pure function tests** - Test extraction logic in isolation |
| 44 | +- **Format tests** - Test JSON, YAML, CSV, TOML, INI, ENV extraction |
| 45 | +- **Utility tests** - Test helper functions, statistics |
| 46 | +- **Configuration tests** - Test config validation |
| 47 | + |
| 48 | +### Integration Tests |
| 49 | + |
| 50 | +Located in `src/**/*.spec.ts`: |
| 51 | + |
| 52 | +- **Sample file tests** - Test extraction against real file formats |
| 53 | +- **Cross-platform tests** - Ensure case-sensitivity compatibility |
| 54 | +- **End-to-end workflows** - Test complete command flows |
| 55 | + |
| 56 | +## Test Coverage Requirements |
| 57 | + |
| 58 | +- **Minimum Coverage**: Maintain reasonable coverage across core functionality |
| 59 | +- **Critical Paths**: All extraction logic must be tested |
| 60 | +- **Error Handling**: All error paths must be covered |
| 61 | +- **Edge Cases**: Boundary conditions must be tested |
| 62 | +- **Security Tests**: CSV injection, ENV/INI injection prevention |
| 63 | + |
| 64 | +## Before Committing |
| 65 | + |
| 66 | +### Checklist |
| 67 | + |
| 68 | +- [ ] All tests pass (`bun run test`) |
| 69 | +- [ ] No broken tests |
| 70 | +- [ ] No skipped tests (unless intentionally) |
| 71 | +- [ ] Type checking passes (`bun x tsc -p ./`) |
| 72 | +- [ ] Linting passes (`bun run lint`) |
| 73 | + |
| 74 | +### CI/CD Validation |
| 75 | + |
| 76 | +The CI pipeline automatically: |
| 77 | + |
| 78 | +1. Runs all tests on Ubuntu, macOS, and Windows |
| 79 | +2. Generates coverage reports |
| 80 | +3. Verifies all tests pass |
| 81 | +4. Fails the build if any tests fail |
| 82 | + |
| 83 | +## Fixing Failed Tests |
| 84 | + |
| 85 | +### When a Test Fails |
| 86 | + |
| 87 | +1. **Don't commit the failure** - Fix the test or the code |
| 88 | +2. **Run locally first** - Verify fix works before pushing |
| 89 | +3. **Check all platforms** - Ensure fix works on Linux/Windows (case sensitivity, etc.) |
| 90 | +4. **Update test if needed** - If behavior changed intentionally, update test |
| 91 | + |
| 92 | +### Common Issues |
| 93 | + |
| 94 | +- **Case sensitivity** - Use exact case for file references (`README.md` not `readme.md`) |
| 95 | +- **Mock issues** - Ensure mocks are properly reset in `beforeEach` |
| 96 | +- **Timing issues** - Avoid `async/await` in tests when possible, use static imports |
| 97 | + |
| 98 | +## Test Best Practices |
| 99 | + |
| 100 | +### 1. Use Descriptive Test Names |
| 101 | + |
| 102 | +```typescript |
| 103 | +// ✅ Good |
| 104 | +it('should extract numbers from JSON files', () => { |
| 105 | + // ... |
| 106 | +}); |
| 107 | + |
| 108 | +// ❌ Bad |
| 109 | +it('works', () => { |
| 110 | + // ... |
| 111 | +}); |
| 112 | +``` |
| 113 | + |
| 114 | +### 2. Test One Thing Per Test |
| 115 | + |
| 116 | +```typescript |
| 117 | +// ✅ Good - separate tests |
| 118 | +it('should extract integers', () => { /* ... */ }); |
| 119 | +it('should extract floats', () => { /* ... */ }); |
| 120 | + |
| 121 | +// ❌ Bad - multiple concerns |
| 122 | +it('should extract integers and floats', () => { /* ... */ }); |
| 123 | +``` |
| 124 | + |
| 125 | +### 3. Use Arrange-Act-Assert Pattern |
| 126 | + |
| 127 | +```typescript |
| 128 | +it('should calculate statistics', () => { |
| 129 | + // Arrange |
| 130 | + const numbers = [1, 2, 3, 4, 5]; |
| 131 | + |
| 132 | + // Act |
| 133 | + const stats = calculateStatistics(numbers); |
| 134 | + |
| 135 | + // Assert |
| 136 | + expect(stats.average).toBe(3); |
| 137 | + expect(stats.median).toBe(3); |
| 138 | +}); |
| 139 | +``` |
| 140 | + |
| 141 | +### 4. Clean Up Mocks |
| 142 | + |
| 143 | +```typescript |
| 144 | +beforeEach(() => { |
| 145 | + vi.clearAllMocks(); |
| 146 | + // Reset mocks to default state |
| 147 | +}); |
| 148 | +``` |
| 149 | + |
| 150 | +## Cross-Platform Testing |
| 151 | + |
| 152 | +### Case Sensitivity |
| 153 | + |
| 154 | +Always use exact case for file references: |
| 155 | + |
| 156 | +```typescript |
| 157 | +// ✅ Good - works on all platforms |
| 158 | +const content = readSampleFile('README.md'); |
| 159 | + |
| 160 | +// ❌ Bad - fails on Linux |
| 161 | +const content = readSampleFile('readme.md'); |
| 162 | +``` |
| 163 | + |
| 164 | +### Path Separators |
| 165 | + |
| 166 | +Use platform-agnostic path handling: |
| 167 | + |
| 168 | +```typescript |
| 169 | +import { join } from 'path'; |
| 170 | +const filePath = join(SAMPLE_DIR, filename); |
| 171 | +``` |
| 172 | + |
| 173 | +## Coverage Reports |
| 174 | + |
| 175 | +Coverage reports are generated automatically: |
| 176 | + |
| 177 | +- **Location**: `coverage/index.html` |
| 178 | +- **Format**: HTML, LCOV, JSON |
| 179 | +- **CI/CD**: Coverage uploaded as artifact |
| 180 | + |
| 181 | +## Continuous Integration |
| 182 | + |
| 183 | +### GitHub Actions |
| 184 | + |
| 185 | +Tests run automatically on: |
| 186 | + |
| 187 | +- **Ubuntu** (latest) |
| 188 | +- **macOS** (latest) |
| 189 | +- **Windows** (latest) |
| 190 | + |
| 191 | +All platforms must pass for the build to succeed. |
| 192 | + |
| 193 | +### Pre-commit Hooks |
| 194 | + |
| 195 | +Consider setting up pre-commit hooks to run tests before commits: |
| 196 | + |
| 197 | +```bash |
| 198 | +# Install husky (if needed) |
| 199 | +bun add -d husky |
| 200 | + |
| 201 | +# Add pre-commit hook |
| 202 | +echo "bun run test" > .husky/pre-commit |
| 203 | +``` |
| 204 | + |
| 205 | +## Reporting Test Issues |
| 206 | + |
| 207 | +If you encounter test failures: |
| 208 | + |
| 209 | +1. **Run locally** - Verify it fails consistently |
| 210 | +2. **Check CI logs** - See platform-specific errors |
| 211 | +3. **Reproduce** - Document steps to reproduce |
| 212 | +4. **Fix or report** - Either fix or create an issue |
| 213 | + |
| 214 | +## Related Documentation |
| 215 | + |
| 216 | +- [Performance Monitoring](PERFORMANCE.md) - Performance testing and benchmarks |
| 217 | +- [Architecture](ARCHITECTURE.md) - Code structure |
| 218 | +- [Commands](COMMANDS.md) - Command testing guidelines |
| 219 | + |
0 commit comments