# 33.6 Test it the hard way (/affordable-self-improving-ai-workers/building-a-durable-ai-worker/test-it-the-hard-way)

---
type: Document
title: "33.6 Test it the hard way"
description: "Tests that break the worker on purpose: a crash right after a payment, a restart during a wait, two messages at once, and a check that a waiting object really sleeps. All from recorded model answers, with no API key."
status: stable
order: 433.06
ksor:
  owner: team:panaversity
  audience: [ public ]
  approval:
    by: process:panaversity
    at: 2026-11-30T08:18:55Z
chapter: "33"
part: IV
expert_status: manager
concepts: [ "33.6" ]
last_verified: 2026-11-29
generated:
  at: 2026-11-30T08:18:55Z
  by: human:zia
trust_tier: unverified
build_id: sha256:d6ce3f28a2cf8e492a60bae1b426b7cc6466e35e83d70c3b9763db2c55009f4b
dirty: true
ksor_version: 0.0.60
---

**In everyday life.** A fire drill is not a talk about fire. Someone pulls the alarm, and you see who leaves by the right door. The building is only safe if the drill worked.

**What you will learn.** How to test a durable worker by breaking it, how recorded model answers make the tests free and repeatable, and how to see that a waiting object sleeps.

**Why it matters.** A worker that has never crashed in a test will crash for the first time in front of a client.

### Recorded answers, not a live model

Every test in this part runs without an API key. Instead of a live model, the tests use **recorded answers**: a small script that reads the conversation and gives the reply a real model gave when the test was recorded. "Last result was the invoice: ask for approval." "Last result was approved: post it."

This has three benefits. The tests cost nothing. They give the same result every time, so a failure means the code changed, not the model's mood. And they run in seconds, so Claude Code can run them after every change.

One lesson from our own testing: make the recorded answers depend on the **conversation**, not on IDs. The test model's internal IDs changed on every start. A script that reads "what was the last tool result?" kept working across crashes. A script that counted calls would not have, because after a crash the count starts again.

Recorded answers test *your* code: the harness, the tools, the waits. They do not test whether a model makes good decisions. That is the job of the golden set ([24.2](../../building-ai-workers/evaluating-ai-workers/golden-sets.md)), the cases whose right answers a person wrote down, in the lab's port.

### Test 1: crash right after the payment

This is the test the chapter is built around.

> Show me how test 1 kills the object, and exactly when.

The lab's crash helper sets a flag. `post_invoice` checks it right after the ledger call, and if it is set, the object aborts itself, before Pi can save the tool's result. That is the worst moment: the outside world has changed, and the checkpoint does not know.

What should happen next, and what we saw:

1. The object dies. The request fails.
2. With no new request, the object's alarm wakes it. `Lifecycle` asks Pi to carry on.
3. Pi loads the last checkpoint. `post_invoice` had not finished, so Pi runs it again.
4. `api.memo` returns the saved key. The ledger sees it, and answers "already posted".
5. The model gets the result and finishes the conversation.

The ledger: **one post, two calls.** The test reads that from the ledger, not from the worker.

**Ask Claude Code one more question.** "What if the crash comes *before* the ledger call?" Then the rerun makes the first real call, with the saved key. One post, one call. Ask for that test too. A test that only crashes in one place can hide a bug in another.

### Test 2: restart while waiting

The test starts a conversation, lets it reach the approval, then restarts the whole local runtime, not just one object. Afterwards it checks that the approval is still listed, sends the answer and checks that the invoice is posted once.

In our run this passed: after the restart the tool was still waiting, and the signal was `delivered`.

### Test 6: two messages at once

Send two messages to the same conversation, at the same moment, with different operation IDs. Both should be accepted, and the second should start only after the first has finished. Then send one of them again with the same operation ID. It should be refused as already accepted.

If the two runs interleave, so that the second message's tool calls appear between the first's, something bypassed Pi's queue. Most often, Claude Code has added a second way into the object that calls the model directly.

### Watch it sleep

A test cannot prove that you are not billed. You can see it on the dashboard after you deploy.

1. Start one invoice that needs approval, and do not answer it.
2. Open the Durable Objects page for your Worker in the Cloudflare dashboard.
3. Look at the object's active time over the next hour. It should rise for a few seconds while the worker reads the invoice, then stay flat.

If it keeps rising, something is keeping the object awake. 33.8 lists what to look for.

> **The invariant.** Test the moment between acting and saving. That is where durable workers fail.

**Check yourself.** A test crashes the worker after the payment and checks that the final message says "posted". It passes. What has it not proved? *(That the invoice was posted once. A worker that posted twice can still end with "posted". Only the ledger's count proves it.)*
