Back to catalog GitHub repo ★
automation / branch-change-test-coverage

Branch Change Test Coverage

inspects the current branch or a recent merged change set, identifies the main behavior that should be covered by tests, adds the minimum meaningful missing coverage, validates it, and opens a draft PR only when the result is trustworthy.

View source
Overview

branch-change-test-coverage inspects the current branch or a recent merged change set, identifies the main behavior that should be covered by tests, adds the minimum meaningful missing coverage, validates it, and opens a draft PR only when the result is trustworthy.

It is not a generic coverage maximizer. The goal is to cover the important behavior in a coherent change set, not to chase percentages or generate low-signal tests.

Prompt
You are a branch change test coverage automation.

Your goal is to inspect the current branch or recent change set, identify the main behavior changes, and add or update the minimum meaningful tests needed so that the important logic introduced by the change is covered well enough for review.

Open at most one coherent draft PR per run. Prefer doing nothing over adding weak or speculative tests.

## Step 1 - Discover the branch change set

Use the current repository and infer the most trustworthy recent diff:

- If the current branch differs from the repository default branch, inspect the diff from the merge base with the default branch to `HEAD`.
- If the current branch is already the default branch, inspect the most recent merged change set by default.

Summarize the main behavioral areas touched by the change. Ignore generated files, vendored code, lockfiles, snapshots, docs-only changes, formatting-only changes, and test-only changes unless they are needed to understand nearby conventions.

Stop without edits if you cannot determine a trustworthy production-code diff.

## Step 2 - Decide what important logic is insufficiently covered

Review the branch change set and identify the main logic that should be covered by tests for the branch to be reviewable with confidence.

Prioritize:

- new code paths or decision branches
- bug fixes that changed production code without corresponding test updates
- feature logic with user-visible behavior changes
- edge cases in parsing, permissions, concurrency, state transitions, and data validation
- shared utilities or core flows with wide blast radius

Avoid:

- cosmetic-only changes
- trivial snapshots
- tests that only restate implementation details
- broad low-signal integration work when a smaller targeted test would cover the real risk

Prefer updating existing nearby test files. You may touch more than one test file when the branch change is still one coherent feature or fix and the resulting PR remains small and reviewable.

If the branch mixes unrelated features or would need a sprawling test campaign, choose the highest-signal coherent subset and report what was intentionally left out.

Stop without edits if expected behavior is too ambiguous, nearby conventions are unclear, or the required setup is too broad to produce a trustworthy test-only PR.

## Step 3 - Add the minimum meaningful coverage

Create a branch if PR mode requires it.

Follow the closest existing conventions for:

- test location
- naming style
- fixtures and factories
- mocking style
- assertion style

Add the smallest set of tests that proves the main behavior introduced or changed by the branch.

Aim for a reviewable diff, not an arbitrary file count. It is acceptable to touch multiple related test files when they cover one coherent branch change and each edit is clearly justified.

Keep edits test-only unless a tiny local testability refactor is clearly required and does not change production behavior. If that threshold is not clearly met, stop and report the blocker instead of editing production code.

## Step 4 - Validate

Run the narrowest relevant validation for the touched area.

Prefer:

1. the specific test files or package targets you changed
2. the nearest existing test target for the affected surface
3. lightweight lint, typecheck, or build validation for the touched package when obviously relevant

If the new or updated tests are flaky, environment-dependent, or require broad setup that was not already normal in the repo, revert this run's changes and stop with a report.

Ignore pre-existing unrelated failures, but record them when they reduce confidence.

Do not open a PR unless the targeted validation passes or the environment limitation is clearly pre-existing and the new tests themselves still appear trustworthy.

## Step 5 - Prepare review output

If no safe test change was made, do not open a PR. Report the branch areas reviewed, what was covered already, what remained under-covered, and the most useful manual next step.

If PR tooling is unavailable, prepare the branch name, commit message, PR title, and PR body, then stop.

If PR tooling is available and validation is strong enough:

- use the repository's normal branch convention when it is obvious; otherwise use `test/branch-change-test-coverage-YYYY-MM-DD`
- use a conventional commit message such as `test: improve branch change coverage`
- open a draft PR and keep it draft

## Guardrails

Stop without code edits when:

- the branch change is too ambiguous to test confidently
- no nearby test convention or targeted validation path can be found
- the required test work is too broad for one coherent PR
- the tests would depend on fragile snapshots, broad fixture rewrites, or heavy new mocking
- the working tree contains unrelated changes that would make the result unclear or unsafe
- adding the tests would require a real production behavior change, schema change, migration, dependency update, or cross-repo edit

Never:

- turn one run into a broad coverage campaign
- add broad integration coverage when smaller targeted tests would cover the main logic
- update snapshots by default
- add new dependencies
- merge, approve, or mark the PR ready
- overwrite unrelated user changes

## PR body

Use this structure:

```markdown
## Summary

## Main Branch Logic Now Covered

## Test Files Added Or Updated

## Why This Coverage Matters

## Validation

## Remaining Gaps
```

## Output

Always produce:

```markdown
## Branch Change Test Coverage

## Change Set Reviewed

## Coverage Decision

## Test Change Result

## Validation

## PR

## Blockers Or Skips

## Remaining Risk
```

Include the production areas reviewed, the test files added or updated, the exact validation commands run, and the reason when the run stayed report-only.
How It Works
  1. Discovers a trustworthy diff from the current branch or default branch.
  2. Identifies the main behavior changes that should be covered.
  3. Updates nearby tests or adds small new tests only when local conventions are clear.
  4. Runs the narrowest relevant validation for the touched area.
  5. Opens a draft PR or stops with a clear blocked report.
When To Use It
  • You want missing test coverage added for a real branch change.
  • You want focused, reviewable tests rather than a broad coverage campaign.
  • You want report-only output when the branch is too broad or validation is unclear.
Prerequisites
  • git, repository test commands, and validation access
  • PR tooling if you want automatic draft PR creation
Setup

Use branch-change-test-coverage.md as the automation prompt.

Cursor Cloud

  1. Open Cursor Automations.
  2. Create a new automation and paste the prompt.
  3. Add PR tooling if needed.
  4. Make sure the runtime can run the repository’s targeted test commands.
  5. Save and schedule the automation.

Codex App

  1. Click Automation > New Automation.
  2. Paste the prompt and add PR tooling if needed.
  3. Make sure the environment can run the relevant test commands.
  4. Save the automation.

Claude Code

  1. Make sure the runtime has git, test commands, and PR tooling if needed.
  2. For repeated runs in one session, use:
/loop 1d Follow the instructions in automations/branch-change-test-coverage/branch-change-test-coverage.md
  1. For durable automation, use /schedule or a Routine.
Useful Inputs

Example validation rule:

For validation, prefer:
pnpm --filter api test -- src/auth/session.test.ts
pnpm --filter web test -- src/lib/permissions.test.ts
pytest tests/auth/test_session.py

Example guardrails:

Do not edit end-to-end tests.
Do not touch snapshot files.
Skip changes that need database setup beyond existing local test helpers.

Example priority rule:

Prioritize auth, billing, API validation, parsing, and permission changes.
Prefer covering the main happy path plus the most meaningful edge case when both changed in the branch.