# Write Unit Tests for a Rust Project

An agent writes inline Rust tests for five fd source files; 209 unit tests pass and line coverage rises from 87.64% to 92.75%.

[Run this on Rush](https://cloud.getrush.ai/run/write-unit-tests/rust/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent ran `cargo llvm-cov --summary-only` on the untouched clone of sharkdp/fd, where `cargo test` already passed 122 integration tests, and ranked files by line coverage. It skipped `main.rs` (74.7%) as binary wiring the integration suite exercises, and took `output.rs` (58.9%), `dir_entry.rs` (70.6%), `exec/job.rs` (75.6%), `error.rs` (76.9%) and `fmt/mod.rs` (83.2%).

The tests go in each file's own `#[cfg(test)]` module. To build a real `Config` and a real walker entry it added two test-only constructors, `Config::for_test()` in `src/config.rs` and `DirEntry::for_test()`, so release builds are unchanged. The `exec/job.rs` tests run `sh` and `test`, so they are gated to `cfg(unix)`.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Run the test suite with coverage, pick the five least-covered files that hold real logic, and write Rust tests for them that exercise behaviour, not implementation details. Do not change production code unless a test reveals a bug; if it does, say so in the description. Open a pull request with the coverage before and after.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

Tried on sharkdp/fd at cc4e40f: 6 files changed, 592 insertions(+), 1 deletion(-), all in `src/`. `cargo test --bin fd` ended `test result: ok. 209 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.04s`, the 122 integration tests still passed, and `cargo clippy --all-targets` and `cargo fmt --check` exited 0. Per file, `output.rs` reached 98.0% and `exec/job.rs` and `error.rs` 100%. The run copied the diff to `/workspace/.rush/artifacts/`, but capture keeps no `.diff` file, so it did not come back; the change is in the run's answer. No pull request was opened.

For the reviewer: `FormatTemplate::parse("{{{}}}")` yields `{{}}` where Rust's `format!` gives `{x}`. The run pinned only the clear case, `a{}}b` to `a{}b`, and left the parser alone. Decide too whether the two helpers belong inside each test module. Coverage was not re-measured after the last clippy fix, a one-line test change.

## Run it on every night

An automation with a `schedule` trigger, `cron` `0 2 * * *` and your `timezone`, runs this prompt in the Project's repository each night, each run on its own branch. The sandbox had no Rust toolchain, linker or coverage tool, so expect each night's run to install rustup, gcc and `cargo-llvm-cov` as this trial did.

## Why run it on Rush?

With 122 integration tests green, fd's remaining gaps sat in units: output writing at 58.9%, the placeholder template parser, error reporting. Testing them directly surfaced an ambiguity in how `fmt/mod.rs` reads `{{{}}}`, which the run reported for a decision instead of changing parser behaviour on its own.

## Frequently asked questions

### Did production code change?

Only to add `Config::for_test()` and `DirEntry::for_test()`, both behind `#[cfg(test)]`, so they compile into test builds alone. No new test exposed a bug.

### How honest is the 92.75% total?

The run says it slightly flatters the result, because the new test code sits in the same files and counts toward the total. The per-file numbers, such as `dir_entry.rs` from 70.6% to 97.6%, are the fairer comparison.

## More Rust jobs

- [Add cargo-deny to a Rust Workspace](https://getrush.ai/run/add-cargo-deny/rust/)
- [Add Test Coverage Reporting to Rust CI](https://getrush.ai/run/add-coverage-to-ci/rust/)
- [Add Criterion Benchmarks to a Rust Workspace](https://getrush.ai/run/add-criterion-benchmarks/rust/)

## The same job on other stacks

- [Write Rack::Test Tests for a Sinatra Service That Has None](https://getrush.ai/run/write-unit-tests/sinatra/)
- [Write Unit Tests for a Slack Bolt App](https://getrush.ai/run/write-unit-tests/slack-bolt/)
- [Write the Missing PHPUnit Tests for a Slim 4 API](https://getrush.ai/run/write-unit-tests/slim/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
