# Write Tests for a Ktor Server

An agent writes 16 testApplication tests for Ktor's kweet sample against a real H2 database; they exposed two bugs, both fixed, and the run flagged a third.

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

## What the agent does

The agent reads `KweetApplication.kt`, `Register.kt` and `dao/DAOFacadeDatabase.kt`, and notes that the 4 existing tests in `KweetApplicationTest` pass because they mock the DAO. Before changing production code, `./gradlew run` on an empty `build/db` failed with `Constraint "USERS_EMAIL_UNIQUE" already exists`.

The new `KweetFlowsTest.kt` runs the real app against a fresh in-memory H2 database per test, with no mocks. Its 16 tests cover registration with invalid and duplicate input, login and four failure cases, posting with and without a login or a valid CSRF code, deleting as the owner, another user or anonymously, and 404, 400 and 500 responses.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "In the kweet sample, write tests with Ktor's testApplication for registration, login, posting and deleting a kweet, and the error responses. Run ./gradlew 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 ktorio/ktor-samples at e9763d6: 3 files changed, 319 insertions(+), 4 deletions(-). Two files are outside the tests: `kweet/build.gradle.kts` pins H2 from `2.4.240` to `2.3.232` for the fresh-database failure, and `DAOFacadeDatabase.kt` switches `user()`, `userByEmail()` and `getKweet()` to `selectAll()`, because, the run reports, they read columns they never selected and returned 500s. `./gradlew test --no-daemon` ended `BUILD SUCCESSFUL in 23s`, with 16 tests in `KweetFlowsTest` and 4 in `KweetApplicationTest`, no failures. The agent installed JDK 17 and did not re-run `./gradlew run` after the fix. No pull request was opened.

For the reviewer: the run flagged a possible fall-through it did not fix or test: in `Register.kt`, it reports, a failed `createUser` still sets the session and redirects a second time. Decide whether the H2 pin and DAO fix ride with the tests or go in their own pull request.

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

The nightly tests automation has a `schedule` trigger on cron `0 2 * * *` in America/Los_Angeles, with this request as its prompt. Each night the same flows run against a fresh H2 database rather than the mocked DAO, so a schema or DAO regression like this trial's shows up by morning.

## Why run it on Rush?

Four passing tests said kweet worked, but they mocked the DAO. Against the real database the sample could not create its schema on an empty `build/db`, and three DAO lookups failed. The run fixed those and flagged a possible registration fall-through for a person.

## Frequently asked questions

### Why did a test-writing run change production code?

Without the H2 pin and the `selectAll()` fix the real-database tests could not pass, and the sample could not start on an empty database. The run kept those changes to two files and suggests splitting them into their own pull request.

### Why does a missing kweet id return 500?

That is the sample's behaviour today, so `missingKweetIsServerError` asserts it as is. The run notes that adding Ktor's StatusPages to answer 404 would mean updating that test.

## More Ktor jobs

- [Add StatusPages Error Handling to a Ktor API](https://getrush.ai/run/add-api-error-handling/ktor/)
- [Add HTTP Caching to a Ktor API](https://getrush.ai/run/add-caching/ktor/)
- [Add Response Compression to a Ktor API](https://getrush.ai/run/add-compression/ktor/)

## More jobs like this: Write the missing tests

- [Write Tests for AWS Lambda Functions in a Serverless Service](https://getrush.ai/run/write-lambda-tests/serverless/)
- [Write LiveView Tests for a Phoenix App](https://getrush.ai/run/write-liveview-tests/phoenix-liveview/)
- [Write k6 Load Tests for an Axum API](https://getrush.ai/run/write-load-tests/axum/)

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