# Write the missing tests

Returns: A PR with passing checks. Starts: on an event.

A nightly schedule starts an agent that finds untested code, writes the tests and opens a pull request by morning.

## Best for

- Codebases with thin coverage on code that changes often
- Teams that review tests in the morning

## Uses

- A connected GitHub repository
- A Project
- An automation with a schedule trigger

## The automation

Prompt: Find the least-tested module changed in the last week, write tests for its untested paths, and open a pull request. Keep production code unchanged.

Trigger:

```json
{
  "kind": "schedule",
  "config": {
    "cron": "0 2 * * *",
    "timezone": "America/Los_Angeles"
  }
}
```

## While it works

Live stream events (`GET /o/{org}/sessions/{session}/events`):

- `tool_use`: bash · git log --since=7.days --stat
- `thinking`: refunds.ts changed 9 times and has no tests for partial refunds.
- `tool_use`: write · refunds.test.ts
- `tool_result`: 18 passed
- `message`: Opened a pull request with 6 new tests.

## What comes back

- `status`: completed
- `branch`: rush/tests-refunds
- `pr_url`: github.com/acme/payments/pull/…
- `total_cost_usd`: 0.52

## Steps

1. **Pick the Project.** The schedule runs in the Project's repository, with its skills and connections.
2. **Add a schedule.** A `schedule` trigger takes a `cron` expression and a `timezone`, or `every` for a fixed interval.
3. **Read the morning pull request.** Each night opens one pull request on its own branch. Failing checks end the run `needs_review`.

Field names are the API's; the values are an example.
