Example: replay
Copy this to record the HTTP calls of the OpenAI client and replay them with the server closed. pnpm test and pnpm replay stay on this machine. A local server writes the replies. No hosted model and no API key.
Run these in this directory. Node.js 22.23.4 or later.
| Command | What it does |
|---|---|
pnpm test |
Records, then checks order, cycling, and a missing prompt. |
pnpm replay |
Prints the cycled replies and the 404 message. |
pnpm record |
Does what pnpm replay does, and writes cassettes/chat.json. |
pnpm replay prints:
cycle: recorded-cycle-1 | recorded-cycle-2 | recorded-cycle-1other: recorded-other-1 | recorded-other-2 | recorded-other-1- The client always uses
https://replay.invalid/v1. During a recording,fetchLocalsends that call to the local server. The cassette keeps the placeholder URL, so the port is not part of the request. Replay usesreplayFetchand does not use the network. - Each prompt and each new
replayFetchkeeps its own count. A missing prompt gets HTTP 404{ "error": { "message" } }. The client throwsNotFoundError, andrunEvalends that case inerror. The message names the request and ends with the hint.maxRetriesis 0: a retry would get the same 404. - The assistant text is the fixed strings in
src/replies.ts, served once each. A further call is an HTTP error, and the cassette does not keep it.pnpm recordwrites that JSON tocassettes/chat.json. That file is not in the repository until the command runs.
| File | Role |
|---|---|
src/session.ts |
createClient. API_KEY is not-a-real-key. Recording and replay pass different fetch wrappers. |
src/backend.ts |
Local server for POST /v1/chat/completions. |
src/replies.ts |
Fixed assistant strings. Not a hosted model. |
scripts/replay.ts |
pnpm replay and pnpm record. |
replay.test.ts |
Records, then replays. |