# Write Property-Based Tests for a Rust Library

An agent adds 19 proptest properties for semver's Version, VersionReq, Prerelease and BuildMetadata; all pass at 200,000 cases each, and no bug turned up.

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

## What the agent does

The agent lists the public API in `src/lib.rs` and `src/eval.rs` and the existing test files, then writes `tests/test_proptest.rs` and adds `proptest = "1"` to `Cargo.toml`, in the `cfg(not(miri))` dev-dependencies beside `criterion`.

The properties: display and parse round trips both ways for `Version`, `Comparator`, `VersionReq`, `Prerelease` and `BuildMetadata`; parsing never panics on arbitrary text and prints back what it accepts; ordering is total and `cmp_precedence` ignores build metadata; prerelease order matches a reference implementation of the SemVer 2.0.0 rules; and `*`, `=`, `>`, `>=`, `<`, `<=`, `~` and `^` match as documented, with several comparators acting as an AND.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Write property-based tests with proptest for the core parsing and transformation functions of this library: round trips, idempotence and invariants. Run them with a high example count. Open a pull request; if a property fails, include the shrunk counterexample and do not change the library.",
  "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 dtolnay/semver at 280ebcb: 2 files changed, 282 insertions(+), one line in `Cargo.toml` and the 281-line test file. `PROPTEST_CASES=200000 cargo test --release --test test_proptest` ended `test result: ok. 19 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 27.31s`, and `cargo test` exited 0 before and after. The sandbox had no C linker, so the agent installed `build-essential` with apt and Rust with rustup. No pull request was opened.

For the reviewer: accept `proptest` and its transitive dependencies, such as `rand` and `regex-syntax`. The default stays proptest's 256 cases, so the long run needs `PROPTEST_CASES`. `cargo fmt` failed because rustfmt was not installed, and clippy was not run.

## Run it on every night at 2:00 a.m. Pacific

An automation with a `schedule` trigger, `cron` `0 2 * * *` and `timezone` `America/Los_Angeles`, runs this request overnight in the Project's repository. The 200,000-case pass took 27 seconds in release mode, so a night run can afford a far higher `PROPTEST_CASES` than a pull request check would.

## Why run it on Rush?

No property failed, so there is no counterexample to report, and the library is unchanged. The one failure in the run was the agent's own: a `prop_assume!` that rejected too many generated prereleases made the first draft exit 101, and it rewrote the generator to build a non-empty prerelease directly.

## Frequently asked questions

### Did the change touch the library or Cargo.lock?

No. Only `Cargo.toml` and the new test file changed. `Cargo.lock` is gitignored in this repository; the run reports resolving proptest 1.12.0.

### Does Miri still run?

The run put `proptest` under `[target.'cfg(not(miri))'.dev-dependencies]`, the same table as `criterion`, and says Miri runs are unaffected. It did not run Miri itself.

## More Rust jobs

- [Add cargo-deny to a Rust Library Workspace](https://getrush.ai/run/add-cargo-deny/rust-library/)
- [Add cargo-semver-checks to CI for a Rust Library](https://getrush.ai/run/add-cargo-semver-checks-to-ci/rust-library/)
- [Label Feature-Gated Items on docs.rs for a Rust Library](https://getrush.ai/run/add-docs-rs-metadata/rust-library/)

## The same job on other stacks

- [Write Property-Based Tests for a TypeScript Library](https://getrush.ai/run/write-property-tests/typescript-library/)
- [Write Property-Based Tests for a Python Library](https://getrush.ai/run/write-property-tests/python/)
- [Write Property-Based Tests for a Rust Crate](https://getrush.ai/run/write-property-tests/rust/)

## 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/)
