|
| 1 | +# Test Coverage Reporting Guide |
| 2 | + |
| 3 | +This document explains how to run, analyze, and maintain test coverage for OpenCoder. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +OpenCoder uses Bun's built-in test coverage tools integrated with CI/CD to ensure code quality. Coverage reports are automatically generated on each commit and uploaded to Codecov for tracking over time. |
| 8 | + |
| 9 | +### Current Coverage Status |
| 10 | + |
| 11 | +- **Function Coverage**: ~79% |
| 12 | +- **Line Coverage**: ~72% |
| 13 | +- **Quality Gate**: 70% minimum (enforced via codecov.yml) |
| 14 | + |
| 15 | +## Running Coverage Reports |
| 16 | + |
| 17 | +### Quick Coverage Check |
| 18 | + |
| 19 | +Display coverage summary in terminal: |
| 20 | + |
| 21 | +```bash |
| 22 | +bun test --coverage |
| 23 | +``` |
| 24 | + |
| 25 | +### Generate LCOV Report |
| 26 | + |
| 27 | +Create LCOV format report (used by Codecov): |
| 28 | + |
| 29 | +```bash |
| 30 | +bun test --coverage --coverage-reporter=lcov |
| 31 | +``` |
| 32 | + |
| 33 | +Or using npm scripts: |
| 34 | + |
| 35 | +```bash |
| 36 | +npm run test:coverage:report |
| 37 | +# or |
| 38 | +make test-coverage-report |
| 39 | +``` |
| 40 | + |
| 41 | +### Generate HTML Report |
| 42 | + |
| 43 | +Create an interactive HTML coverage report: |
| 44 | + |
| 45 | +```bash |
| 46 | +npm run test:coverage:html |
| 47 | +``` |
| 48 | + |
| 49 | +The HTML report will be in `coverage/index.html` and can be opened in a browser for detailed line-by-line coverage analysis. |
| 50 | + |
| 51 | +### View Text Summary |
| 52 | + |
| 53 | +Display coverage as formatted text table: |
| 54 | + |
| 55 | +```bash |
| 56 | +bun test --coverage --coverage-reporter=text |
| 57 | +``` |
| 58 | + |
| 59 | +Or use the make target: |
| 60 | + |
| 61 | +```bash |
| 62 | +make test-coverage-check |
| 63 | +``` |
| 64 | + |
| 65 | +### Using Codecov for HTML Reports |
| 66 | + |
| 67 | +For detailed line-by-line coverage analysis, use the Codecov dashboard: |
| 68 | + |
| 69 | +1. Visit https://codecov.io |
| 70 | +2. View interactive HTML reports with file-by-file coverage |
| 71 | +3. See historical trends and pull request analysis |
| 72 | + |
| 73 | +Or generate LCOV format locally and view with your IDE: |
| 74 | + |
| 75 | +```bash |
| 76 | +npm run test:coverage:report |
| 77 | +# Then open coverage/lcov.info in your IDE or use the Codecov dashboard |
| 78 | +``` |
| 79 | + |
| 80 | +## CI/CD Integration |
| 81 | + |
| 82 | +### Automatic Coverage Reporting |
| 83 | + |
| 84 | +On every push or pull request to `main`: |
| 85 | + |
| 86 | +1. Tests run with coverage enabled |
| 87 | +2. LCOV report is generated |
| 88 | +3. Report is uploaded to Codecov |
| 89 | +4. Status badge is added to PR |
| 90 | + |
| 91 | +### Quality Gates |
| 92 | + |
| 93 | +Coverage thresholds are configured in `codecov.yml`: |
| 94 | + |
| 95 | +- **Project Coverage**: Minimum 70% |
| 96 | + - Failure threshold: 5% drop |
| 97 | + - Status: Reported but not blocking |
| 98 | + |
| 99 | +- **Patch Coverage**: Minimum 70% |
| 100 | + - Failure threshold: 5% drop |
| 101 | + - Applies to changed code only |
| 102 | + |
| 103 | +To view quality gate status: |
| 104 | + |
| 105 | +```bash |
| 106 | +# Check local coverage |
| 107 | +bun test --coverage --coverage-reporter=text |
| 108 | + |
| 109 | +# Coverage status is reported in: |
| 110 | +# - Codecov dashboard (https://codecov.io) |
| 111 | +# - PR comments (automatic) |
| 112 | +# - GitHub status checks |
| 113 | +``` |
| 114 | + |
| 115 | +## Coverage Reports |
| 116 | + |
| 117 | +### Local Reports |
| 118 | + |
| 119 | +Reports are stored in the `coverage/` directory (gitignored): |
| 120 | + |
| 121 | +- `lcov.info` - LCOV format (for Codecov) |
| 122 | +- `coverage.txt` - Text summary |
| 123 | +- `index.html` - Interactive HTML report |
| 124 | +- Raw coverage data files |
| 125 | + |
| 126 | +### Codecov Dashboard |
| 127 | + |
| 128 | +View historical coverage trends: |
| 129 | + |
| 130 | +1. Go to https://codecov.io |
| 131 | +2. Search for "opencoder" project |
| 132 | +3. View: |
| 133 | + - Coverage trends over time |
| 134 | + - File-by-file coverage |
| 135 | + - Pull request analysis |
| 136 | + - Coverage commitments |
| 137 | + |
| 138 | +## Best Practices |
| 139 | + |
| 140 | +### Writing Testable Code |
| 141 | + |
| 142 | +1. Keep functions small and focused |
| 143 | +2. Minimize side effects |
| 144 | +3. Use dependency injection for easier mocking |
| 145 | +4. Avoid deep nesting of conditions |
| 146 | + |
| 147 | +### Writing Good Tests |
| 148 | + |
| 149 | +1. **Test behavior, not implementation** |
| 150 | + - Focus on inputs and outputs |
| 151 | + - Don't test private methods |
| 152 | + |
| 153 | +2. **Cover multiple scenarios** |
| 154 | + - Success cases |
| 155 | + - Error cases |
| 156 | + - Edge cases |
| 157 | + |
| 158 | +3. **Use descriptive test names** |
| 159 | + - `test("parses --model option", () => { ... })` |
| 160 | + - Not: `test("test 1", () => { ... })` |
| 161 | + |
| 162 | +4. **Keep tests isolated** |
| 163 | + - Each test should be independent |
| 164 | + - Clean up after each test (use afterEach) |
| 165 | + |
| 166 | +### Coverage Targets by File Type |
| 167 | + |
| 168 | +- **Utilities & Helpers**: 90%+ coverage |
| 169 | +- **Core Logic**: 80%+ coverage |
| 170 | +- **Integration Code**: 70%+ coverage |
| 171 | +- **Error Handlers**: 70%+ coverage |
| 172 | + |
| 173 | +## Troubleshooting |
| 174 | + |
| 175 | +### Coverage Report Not Generated |
| 176 | + |
| 177 | +```bash |
| 178 | +# Ensure bun is installed |
| 179 | +bun --version |
| 180 | + |
| 181 | +# Run tests with explicit options |
| 182 | +bun test --coverage --coverage-reporter=text |
| 183 | + |
| 184 | +# Check if coverage directory exists |
| 185 | +ls -la coverage/ |
| 186 | +``` |
| 187 | + |
| 188 | +### Coverage Seems Low |
| 189 | + |
| 190 | +1. Check if all test files are being discovered |
| 191 | +2. Verify test files match `tests/**/*.test.ts` pattern |
| 192 | +3. Ensure code under test is in `src/**/*.ts` |
| 193 | +4. Run `bun test --coverage` to see actual numbers |
| 194 | + |
| 195 | +### LCOV Report Upload Fails |
| 196 | + |
| 197 | +1. Verify `codecov.yml` exists |
| 198 | +2. Check GitHub Actions logs for errors |
| 199 | +3. Ensure repository is public (for free Codecov tier) |
| 200 | +4. Check Codecov status in PR |
| 201 | + |
| 202 | +## Scripts Reference |
| 203 | + |
| 204 | +| Script | Command | Purpose | |
| 205 | +|--------|---------|---------| |
| 206 | +| `npm run test` | `bun test` | Run tests without coverage | |
| 207 | +| `npm run test:coverage` | `bun test --coverage` | Show coverage summary | |
| 208 | +| `npm run test:coverage:report` | `bun test --coverage --coverage-reporter=lcov` | Generate LCOV report | |
| 209 | +| `npm run test:coverage:check` | `bun test --coverage --coverage-reporter=text` | Display text summary | |
| 210 | + |
| 211 | +## Make Targets |
| 212 | + |
| 213 | +| Target | Purpose | |
| 214 | +|--------|---------| |
| 215 | +| `make test` | Run all tests | |
| 216 | +| `make test-coverage` | Run tests with coverage | |
| 217 | +| `make test-coverage-report` | Generate LCOV report | |
| 218 | +| `make test-coverage-check` | Display coverage text summary | |
| 219 | + |
| 220 | +## Coverage Goals |
| 221 | + |
| 222 | +### Current Target |
| 223 | + |
| 224 | +- **Overall**: 75%+ line coverage |
| 225 | +- **Core modules**: 80%+ line coverage |
| 226 | +- **Utilities**: 90%+ line coverage |
| 227 | + |
| 228 | +### Future Goals |
| 229 | + |
| 230 | +- Increase overall coverage to 85% |
| 231 | +- Achieve 90%+ for utility modules |
| 232 | +- Maintain consistency across all source files |
| 233 | + |
| 234 | +## Additional Resources |
| 235 | + |
| 236 | +- [Bun Test Coverage Documentation](https://bun.sh/docs/test/coverage) |
| 237 | +- [Codecov Documentation](https://docs.codecov.io) |
| 238 | +- [LCOV Format](https://github.com/linux-test-project/lcov) |
| 239 | +- [Istanbul Coverage Tool](https://istanbul.js.org/) |
| 240 | + |
| 241 | +## Questions or Issues? |
| 242 | + |
| 243 | +For coverage-related questions: |
| 244 | + |
| 245 | +1. Check this guide first |
| 246 | +2. Run `bun test --coverage` to verify local setup |
| 247 | +3. Compare with `coverage/index.html` for detailed view |
| 248 | +4. Check GitHub Actions logs for CI failures |
0 commit comments