# Write node:test Tests for a Fastify API

An agent adds 32 node:test tests for a Fastify API's error paths, using fastify.inject against a real MariaDB; npm test goes from 57 to 89 passing.

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

## What the agent does

The agent reads the auth, users and tasks routes under `src/routes/api/`, the `authorization`, `password-manager` and `tasks-file-manager` plugins, the schemas and the existing tests. The baseline already reports 100% line coverage, so the gaps are untested behaviors, not lines.

It writes one file, `test/routes/api/errors/error-paths.test.ts`, with the repository's `build` helper and `inject`: 401 on every `/api/tasks` route and on `PUT /api/users/update-password` without a session or with a forged cookie, 403 for a non-admin delete and a non-moderator assign with the task left unchanged, 400 on bad query strings, ids and login bodies, and upload failures.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Find the routes and plugins in this Fastify API whose error paths have no tests and write node:test tests for them using fastify.inject against a real MySQL in the sandbox. Run npm test and open a pull request.",
  "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 fastify/demo at 895908e: 1 file changed, 294 insertions(+). `npm test` exited 0 before and after, going from `# tests 57` to `# tests 89` with `# fail 0`, and `npm run lint` exited 0 after the change (not run before). The repository has no lockfile, so the agent used `npm install --no-package-lock`, and the MySQL was MariaDB 10.11 from apt because Docker was unavailable. It wrote a `.env` from `.env.example` with the three `CAN_*` flags changed from 0 to 1 and a `COOKIE_SECRET` filled in. No pull request was opened.

For the reviewer: two tests lock in a 500 when `assigned_user_id` or `userId` names an unknown user, from the foreign-key violation, where a 400 or 404 would be more correct. The non-multipart upload test asserts only a 4xx. `npx tsc --noEmit -p test/tsconfig.json` fails on a deprecated `baseUrl` in `test/tsconfig.json`, a file the change does not touch.

## 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. Its sandbox needs a MySQL where it can create `test_db` and `test_user`, and the same `.env`, because `npm test` seeds the database before the suite starts.

## Why run it on Rush?

Line coverage was already 100%, so a coverage report would have shown nothing to write. Asking for error paths turned up 32 cases no test asserted, including that an unknown user id on `POST /api/tasks` and `POST /api/tasks/:id/assign` answers 500 rather than a client error. The tests pin that 500.

## Frequently asked questions

### Do the new tests share the database with the existing ones?

Yes. The `tasks` tests use the same `test_db`, so the new tests create their own rows and delete them in `finally`. The first draft cleaned up in `t.after` and failed with `Unable to acquire a connection`, because `build(t)` had already closed the app.

### What did the run leave untested?

`passwordManager.compare` with a stored hash that has no `.` separator. The run thinks login would answer 500 there but did not check it.

## More Fastify jobs

- [Add an Audit Log to a Fastify API](https://getrush.ai/run/add-audit-log/fastify/)
- [Add Response Compression to a Fastify App](https://getrush.ai/run/add-compression/fastify/)
- [Add CSRF Protection to a Fastify App](https://getrush.ai/run/add-csrf-protection/fastify/)

## More jobs like this: Write the missing tests

- [Write Vitest Tests for Nuxt Server Routes](https://getrush.ai/run/write-nuxt-tests/nuxt/)
- [Write pgTAP Tests for a Supabase Database](https://getrush.ai/run/write-pgtap-tests/supabase/)
- [Write PHPUnit Tests for a CakePHP App](https://getrush.ai/run/write-phpunit-tests/cakephp/)

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