# /quality:coverage - Generate Test Coverage Report

Generate comprehensive test coverage report with recommendations for improving coverage.

## Parameters
- `library-name` (optional): Specific library to analyze. If omitted, analyzes all libraries.

## Tasks

### 1. Run Coverage Analysis
```bash
# Single library
npx nx test [library-name] --skip-nx-cache --coverage

# All libraries (if no library specified)
npm run ci # Runs all tests with coverage
```

### 2. Parse Coverage Report
Coverage output typically in:
- `coverage/libs/[domain]/[layer]/[name]/`
- Look for `coverage-summary.json` or text output

Extract metrics:
- **Line coverage**: % of executable lines tested
- **Branch coverage**: % of conditional branches tested
- **Function coverage**: % of functions called in tests
- **Statement coverage**: % of statements executed

### 3. Identify Uncovered Code
Parse coverage report to find:
- **Uncovered files**: Files with 0% coverage
- **Partially covered files**: < 80% coverage
- **Uncovered lines**: Specific line numbers not tested
- **Uncovered branches**: Conditional paths not tested

```bash
# List files with coverage below 80%
# (Parse from coverage JSON output)
```

### 4. Categorize Coverage Gaps

**Critical (High Risk):**
- Service methods handling business logic
- Data transformation functions
- Error handling code paths
- Security-related functions
- State management store actions

**Important (Medium Risk):**
- Component public methods
- Utility functions
- Validators
- Pipes and filters
- Guard functions

**Low Priority:**
- Getters/setters
- Simple property assignments
- Console logging
- Type definitions

### 5. Generate Recommendations

For each coverage gap, provide:
- **File and line numbers**
- **Risk level** (Critical/Important/Low)
- **Suggested test type** (unit/integration)
- **Test approach** (example test scenario)

Example:
```
📍 libs/oms/data-access/src/lib/services/order.service.ts:45-52
🔴 Critical - Business Logic
❌ 0% coverage - Error handling path

Recommended test:
it('should handle API error when fetching order', async () => {
  // Mock API to return error
  // Call method
  // Verify error handling
});
```

### 6. Calculate Coverage Trends
If historical data available:
- Compare with previous coverage percentage
- Show improvement/regression
- Identify files with declining coverage

### 7. Generate HTML Report
```bash
# Open coverage report in browser (if available)
open coverage/libs/[domain]/[layer]/[name]/index.html
```

### 8. Create Coverage Summary Report

**Overall Metrics:**
```
Coverage Summary for [library-name]
====================================

Line Coverage:     XX.X% (XXX/XXX lines)
Branch Coverage:   XX.X% (XXX/XXX branches)
Function Coverage: XX.X% (XXX/XXX functions)
Statement Coverage: XX.X% (XXX/XXX statements)

Target: 80% (Recommended minimum)
Status: ✅ Met / ⚠️ Below Target / 🔴 Critical
```

**Files Needing Attention:**
```
🔴 Critical (< 50% coverage):
  1. service-name.service.ts - 35% (business logic)
  2. data-processor.ts - 42% (transformations)

⚠️ Below Target (50-79% coverage):
  3. component-name.component.ts - 68%
  4. validator.ts - 72%

✅ Well Covered (≥ 80% coverage):
  - Other files maintaining good coverage
```

**Top Priority Tests to Add:**
1. [File:Line] - [Description] - [Risk Level]
2. ...

### 9. Framework-Specific Notes

**Vitest:**
- Coverage provider: v8 or istanbul
- Config in `vitest.config.ts`
- Coverage thresholds configurable

**Jest:**
- Coverage collected via `--coverage` flag
- Config in `jest.config.ts`
- Coverage directory: `coverage/`

### 10. Set Coverage Thresholds (Optional)
Suggest adding to test config:
```typescript
// vitest.config.ts
coverage: {
  thresholds: {
    lines: 80,
    functions: 80,
    branches: 80,
    statements: 80
  }
}
```

## Output Format
```
Test Coverage Report
====================

Library: [name]
Test Framework: [Vitest/Jest]
Generated: [timestamp]

📊 Coverage Metrics
-------------------
Lines:      XX.X% ████████░░ (XXX/XXX)
Branches:   XX.X% ███████░░░ (XXX/XXX)
Functions:  XX.X% █████████░ (XXX/XXX)
Statements: XX.X% ████████░░ (XXX/XXX)

🎯 Target: 80% | Status: [✅/⚠️/🔴]

🔍 Coverage Gaps
----------------
[Categorized list with priorities]

💡 Recommendations
------------------
[Prioritized list of tests to add]

📈 Next Steps
-------------
1. Focus on critical coverage gaps first
2. Add tests for business logic in [files]
3. Consider setting coverage thresholds
4. Re-run: npx nx test [library-name] --skip-nx-cache --coverage
```

## Error Handling
- No coverage data: Ensure `--coverage` flag used
- Missing library: Verify library name is correct
- Coverage tool not configured: Check test config for coverage setup

## References
- docs/guidelines/testing.md
- CLAUDE.md Testing Framework section
- Vitest coverage: https://vitest.dev/guide/coverage
- Jest coverage: https://jestjs.io/docs/configuration#collectcoverage-boolean
