|
| 1 | +# Codex Task: Improve Test Coverage |
| 2 | + |
| 3 | +## Objective |
| 4 | + |
| 5 | +Incrementally improve test coverage toward 90%. Each run should target **one package**, add tests, and open a PR. |
| 6 | + |
| 7 | +## Steps |
| 8 | + |
| 9 | +1. **Measure current coverage per package:** |
| 10 | + |
| 11 | +```bash |
| 12 | +go test -coverprofile=coverage.out ./... |
| 13 | +go tool cover -func=coverage.out | awk '/^total:/ {print "TOTAL:", $3}' |
| 14 | +# Per-package breakdown: |
| 15 | +go test -cover ./... 2>&1 | sort -t'%' -k2 -n |
| 16 | +``` |
| 17 | + |
| 18 | +2. **Pick the lowest-coverage package** from this priority list (skip packages already above 80%): |
| 19 | + |
| 20 | +| Priority | Package | Notes | |
| 21 | +|----------|---------|-------| |
| 22 | +| 1 | `scanner/` | Core scanning logic, high impact | |
| 23 | +| 2 | `watch/` | Daemon/event system | |
| 24 | +| 3 | `render/` | Output rendering | |
| 25 | +| 4 | `handoff/` | Handoff artifact building | |
| 26 | +| 5 | `cmd/` | CLI commands | |
| 27 | +| 6 | `config/` | Configuration loading | |
| 28 | +| 7 | `limits/` | Budget enforcement | |
| 29 | + |
| 30 | +**Skip these packages** (hard to test in isolation): |
| 31 | +- `mcp/` — MCP server, requires full stdio lifecycle |
| 32 | +- Root `main.go` — thin entry point |
| 33 | + |
| 34 | +3. **Write tests** following the project's existing patterns (see below). |
| 35 | + |
| 36 | +4. **Check if the CI coverage floor can be bumped.** If total coverage is now ≥ current floor + 5 points, update the `min=` value in `.github/workflows/ci.yml` (the `Enforce coverage floor` step). |
| 37 | + |
| 38 | +5. **Open a PR** with: |
| 39 | + - Title: `test: improve <pkg> coverage from X% to Y%` |
| 40 | + - Body: before/after per-package and total coverage numbers |
| 41 | + - Max **500 lines** of new test code per PR |
| 42 | + |
| 43 | +## Test Patterns to Follow |
| 44 | + |
| 45 | +All existing tests in this repo use these conventions. Follow them exactly. |
| 46 | + |
| 47 | +### Table-driven tests |
| 48 | + |
| 49 | +```go |
| 50 | +func TestFoo(t *testing.T) { |
| 51 | + tests := []struct { |
| 52 | + name string |
| 53 | + // inputs |
| 54 | + // expected outputs |
| 55 | + }{ |
| 56 | + {name: "descriptive case name", /* ... */}, |
| 57 | + } |
| 58 | + for _, tt := range tests { |
| 59 | + t.Run(tt.name, func(t *testing.T) { |
| 60 | + // test body |
| 61 | + }) |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +### Standard library only |
| 67 | + |
| 68 | +- Use `testing` package only — no testify, no gomock, no external test deps. |
| 69 | +- Use `t.Errorf` / `t.Fatalf` for assertions. |
| 70 | +- Use `t.Helper()` in test helper functions. |
| 71 | + |
| 72 | +### Temp directories |
| 73 | + |
| 74 | +```go |
| 75 | +dir := t.TempDir() // auto-cleaned up |
| 76 | +``` |
| 77 | + |
| 78 | +### File setup in tests |
| 79 | + |
| 80 | +```go |
| 81 | +err := os.WriteFile(filepath.Join(dir, "file.go"), []byte(content), 0o644) |
| 82 | +if err != nil { |
| 83 | + t.Fatal(err) |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +### Subtests for variants |
| 88 | + |
| 89 | +Use `t.Run(tt.name, ...)` — never run test cases in a bare loop. |
| 90 | + |
| 91 | +## Functions to Skip |
| 92 | + |
| 93 | +These are difficult to unit test and should be skipped: |
| 94 | + |
| 95 | +- Anything requiring a running subprocess (`exec.Command` wrappers) |
| 96 | +- Terminal UI rendering (lipgloss/bubbletea output) |
| 97 | +- `main()` and top-level CLI entry points |
| 98 | +- Functions that require network access or a running MCP server |
| 99 | +- Functions that depend on git operations against real repos (unless using `t.TempDir()` with `git init`) |
| 100 | + |
| 101 | +## Rules |
| 102 | + |
| 103 | +- **Never modify source code** — only add `_test.go` files. |
| 104 | +- Add tests to the **existing** `_test.go` file for the package if one exists; create a new one only if needed. |
| 105 | +- Keep tests deterministic — no sleep, no real network, no time-dependent assertions. |
| 106 | +- Run `go test -race ./...` before opening the PR. |
| 107 | +- Run `go vet ./...` and `gofmt -l .` — the PR must pass CI. |
| 108 | +- Each test function should test **one behavior** clearly described by its name. |
| 109 | + |
| 110 | +## PR Checklist |
| 111 | + |
| 112 | +- [ ] `go test -race -coverprofile=coverage.out ./...` passes |
| 113 | +- [ ] `go vet ./...` clean |
| 114 | +- [ ] `gofmt -l .` shows no files |
| 115 | +- [ ] No source files modified, only `_test.go` files added/changed |
| 116 | +- [ ] Coverage floor bumped in CI if warranted (≥ floor + 5) |
| 117 | +- [ ] PR title follows format: `test: improve <pkg> coverage from X% to Y%` |
| 118 | +- [ ] PR body includes before/after coverage table |
0 commit comments