# Code Coverage

Generate and view code coverage reports for Wasot. For the full testing strategy, see [testing](./testing.md). For build optimization, see [build release](./build_release.md).

## Quick Start

```bash
# Run coverage with fast tests only (default)
./scripts/coverage.sh

# Run coverage with all tests (including slow/ignored tests)
./scripts/coverage.sh --all

# Run coverage with only slow tests
./scripts/coverage.sh --slow
```

## Prerequisites

The script will auto-install these if missing:

```bash
cargo install cargo-llvm-cov
cargo install --locked cargo-nextest
```

## Output Files

After running, coverage reports are generated in `coverage/`:

| File | Format | Description |
|------|--------|-------------|
| `html/index.html` | HTML | Interactive browser-based report |
| `summary.txt` | Text | Quick overview in terminal |
| `lcov.info` | LCOV | For IDE integrations and CI tools |
| `coverage.json` | JSON | Machine-readable for scripting |

## Viewing Results

### HTML Report (Recommended)

```bash
# Open in browser
xdg-open coverage/html/index.html   # Linux
open coverage/html/index.html       # macOS
```

The HTML report shows:
- Line-by-line coverage highlighting
- Per-file coverage percentages
- Uncovered code paths in red

### Terminal Summary

```bash
cat coverage/summary.txt
```

Shows a table with regions, functions, and line coverage per file.

### Current Coverage

As of the last run:
- **Line coverage**: 91.29%
- **Function coverage**: 94.05%

## Test Modes

| Mode | Command | Tests Run | Duration |
|------|---------|-----------|----------|
| Fast (default) | `./scripts/coverage.sh` | Regular tests | ~1 min |
| All | `./scripts/coverage.sh --all` | Fast + slow tests | ~5+ min |
| Slow only | `./scripts/coverage.sh --slow` | Ignored/fuzz tests | ~2+ min |

See [testing](./testing.md) for details on test categories.

## IDE Integration

### VS Code

Install the Coverage Gutters extension, then:
1. Run `./scripts/coverage.sh`
2. The `coverage/lcov.info` file is auto-detected
3. Use `Coverage Gutters: Display Coverage` command

### JetBrains IDEs

1. Run `./scripts/coverage.sh`
2. Import `coverage/lcov.info` via Run > Show Coverage Data

## CI Integration

The `coverage/lcov.info` file can be uploaded to:
- Codecov
- Coveralls
- SonarQube

Example for GitHub Actions:
```yaml
- name: Generate coverage
  run: ./scripts/coverage.sh
- name: Upload coverage
  uses: codecov/codecov-action@v3
  with:
    files: coverage/lcov.info
```

## Excluding Files

Some files may show 0% coverage (demo binaries, unused tools):
- `bin/wasot_demo.rs`
- `tools/grpc_library_client.rs`

These are expected and can be excluded from coverage tracking if needed.

## Troubleshooting

### llvm-cov not found
```bash
cargo install cargo-llvm-cov
```

### nextest not found
```bash
cargo install --locked cargo-nextest
```

### Tests fail during coverage
Coverage runs with `--retries 3` to handle flaky tests. If tests consistently fail:
```bash
# Run tests directly to see errors
cargo nextest run
```
