# Write Unit Tests for a Fiber v3 App

Yes. An agent writes the first Go tests for a Fiber v3 app with app.Test, runs them under the race detector your CI already uses, and opens a PR with what it found.

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

## What the agent does

The agent reads what the test workflow expects before it writes a test, because in a repository like gofiber/boilerplate there is a `Test` workflow and no test at all. `.github/workflows/test.yml` runs `go test ./... -v -race` on Go 1.26.x and 1.27.x, on an Ubuntu and a Windows runner, for every push to `master` and every pull request; `lint.yml` runs golangci-lint v2.14.0 with `--tests=false`, so test files are not linted; `security.yml` runs gosec; `.dockerignore` excludes `*_test.go` from the image. `go.mod` declares `go 1.26.0` with `github.com/gofiber/fiber/v3` v3.7.0 and `github.com/gofiber/utils/v2`. The code is four files: `app.go` parses `-port` and `-prod` flags, calls `database.Connect()`, builds the app with `recover` and `logger` middleware, registers `GET` and `POST /api/v1/users` on a `/api/v1` group, serves `./static/public` with the `static` middleware on `/*`, and ends with `app.Use(handlers.NotFound)`, which sends `./static/private/404.html` with status 404. `handlers/handlers.go` has `UserList`, `UserCreate` (which copies `c.FormValue("user")` with `utils.CopyString` into a `models.User`) and `NotFound`; `database/database.go` is a package-level slice of `*models.User` with a mutex that `Insert` takes and `Get` does not.

The tests go beside the code they test, in the style Fiber v3 expects: build an `*fiber.App` in the test, register the handler under test, and drive it with `app.Test(httptest.NewRequest(...))`, which in v3 takes an optional `fiber.TestConfig` and defaults to a one-second timeout. `handlers/handlers_test.go` covers `UserCreate` with a URL-encoded form and asserts the `{"success":true,"user":{"name":"alice"}}` body, then `UserList` returning what was inserted; it also pins a behaviour worth a question, that a `POST` with no `user` field is accepted and stores a user with an empty name. `database/database_test.go` checks that `Connect` resets the slice, that `Insert` and `Get` keep order, and runs fifty concurrent `Insert` and `Get` calls, which under `-race` reports a data race between the write in `Insert` and the unlocked read in `Get`.

Two details decide where the `NotFound` test lives. `handlers.NotFound` sends a file by a path relative to the working directory, and `go test` runs each package from its own directory, so from `handlers/` the file is not found and the response is a bare 404 with an empty body; the test that asserts the HTML page therefore lives in the root package as `app_test.go`, where the working directory is the repository root, and builds the same route order `app.go` does: static first, then the 404 handler. The second is that the CI matrix includes Windows, so the test opens the expected file with `filepath.Join` and compares bodies after normalising line endings rather than byte for byte.

The agent runs `go vet ./...` and `go test ./... -race -v` in its sandbox on the toolchain `go.mod` names, keeps the output, and writes the race report into the pull request as a finding rather than fixing `database.Get`, since the prompt says not to change production code: the test that triggers it is marked with `t.Skip` and a one-line reason until the lock is added, so the workflow stays green while the report stays visible. It runs `gofmt -l` over the new files, since golangci-lint skips tests here, and adds nothing to `test.yml`, which already runs the suite with the race detector on both platforms.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Write unit tests for this Fiber v3 app. Read go.mod, app.go, handlers/handlers.go, database/database.go, models/user.go, .github/workflows/test.yml, .github/workflows/lint.yml, .golangci.yml and .dockerignore first. Use app.Test with httptest requests. Write handlers/handlers_test.go for UserCreate with a form body and the JSON it returns, UserList after inserts, and the empty-form case as it behaves today; database/database_test.go for Connect, Insert and Get order, and a concurrent Insert and Get test; and app_test.go in the root package for NotFound serving static/private/404.html, since the handler's path is relative to the working directory. Keep the tests portable to the Windows runner in test.yml. Do not change production code; if the race detector reports database.Get, keep the test, skip it with a reason, and write the report into the pull request as a finding. Run go vet ./..., gofmt -l on the new files and go test ./... -race -v on the toolchain go.mod names, and open a pull request that pastes the output.",
  "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

What comes back is a pull request that adds `handlers/handlers_test.go`, `database/database_test.go` and `app_test.go`, and changes nothing under `handlers/`, `database/`, `models/` or in `app.go`. The description pastes the `go vet`, `gofmt -l` and `go test ./... -race -v` output from the sandbox, with the race detector's report quoted under a heading of its own.

That report is the finding: `database.Insert` writes the slice under the mutex and `database.Get` reads it without one, so the `Test` workflow, which already runs with `-race`, will fail the moment a test exercises both at once. The pull request shows the two-line fix, taking the lock in `Get` and returning a copy, and leaves it to the reviewer. The empty-name user is listed as a question, not a bug.

Before merging, a reviewer decides whether to apply the lock in the same pull request or a follow-up, un-skips the concurrent test once it is applied, and checks that the Windows job in `test.yml` passes the root-package file test, which is why the test normalises line endings.

## Run it on every night

An automation with a `schedule` trigger, `cron` `0 2 * * *` in the Project's `timezone`, runs this prompt in the repository each night. The run reads what the previous pull request covered before it writes anything, so once the four packages have tests, the next night turns to whatever the week's commits added, and a night with nothing uncovered ends with a short note and no pull request.

Each night opens at most one pull request on its own branch, and the automation's `max_runs_per_hour` keeps a mis-set schedule from starting more than one. Pull requests from a fork never start a run; the schedule does.

## Why run it on Rush?

Writing the first tests for a Fiber app is mostly learning the house rules: the v3 `app.Test` signature, a 404 handler whose file path depends on the working directory, a CI matrix that includes Windows, a linter that skips test files, and a race detector already on in CI that the first concurrent test will trip. Working each of those out by hand is an afternoon; the agent reads the workflows and the four source files, writes the tests to fit them, and runs the same `go test ./... -race` the workflow does in its own sandbox before anyone opens the diff.

Every command and its output stays on the run, so the reviewer reads the race report and the test log without checking out the branch. Several Go services can get the same treatment at once, each in its own sandbox, and a test that exposes a real problem is reported rather than quietly worked around.

## Frequently asked questions

### Why is the 404 test in the root package?

`handlers.NotFound` calls `SendFile("./static/private/404.html")`, a path relative to the process's working directory. `go test` runs each package in its own directory, so from `handlers/` the file is missing and the response is a 404 with an empty body. The root package runs from the repository root, where the file is, so that is where the test that reads the page belongs.

### What does the race detector report?

A write in `database.Insert`, under the mutex, racing a read in `database.Get`, which takes no lock and returns the slice itself. `test.yml` runs with `-race`, so the concurrent test is skipped with a reason until the lock is added; the pull request quotes the report and the two-line fix.

### Does the race run need cgo?

Yes, `-race` needs a C compiler. The sandbox has one, and so do the workflow's Ubuntu and Windows runners, which is why `test.yml` can run `go test ./... -v -race` on both. The agent runs the same command.

### Will golangci-lint complain about the new files?

No. `lint.yml` passes `--tests=false`, so `*_test.go` files are excluded from the lint run, and `.golangci.yml` enables only the `modernize` linter besides the defaults. The agent runs `gofmt -l` on the tests anyway so they match the rest of the code.

### Which Go version runs the tests?

The one `go.mod` names, 1.26.0, which the `go` command downloads when the installed toolchain is older. The CI matrix runs 1.26.x and 1.27.x, so a test that uses a newer standard-library function than 1.26 has would fail one of the two jobs; the agent sticks to what both have.

## More Fiber jobs

- [Add API Key Authentication to a Fiber App](https://getrush.ai/run/add-api-key-auth/fiber/)
- [Add Response Compression to a Fiber App](https://getrush.ai/run/add-compression/fiber/)
- [Add CORS to a Fiber API](https://getrush.ai/run/add-cors/fiber/)

## The same job on other stacks

- [Write Unit Tests for a Gatsby Blog](https://getrush.ai/run/write-unit-tests/gatsby/)
- [Write Unit Tests for a Gin Project](https://getrush.ai/run/write-unit-tests/gin/)
- [Write Unit Tests for a Go Project](https://getrush.ai/run/write-unit-tests/go/)

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