Skip to content

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-1
other: recorded-other-1 | recorded-other-2 | recorded-other-1
  • The client always uses https://replay.invalid/v1. During a recording, fetchLocal sends that call to the local server. The cassette keeps the placeholder URL, so the port is not part of the request. Replay uses replayFetch and does not use the network.
  • Each prompt and each new replayFetch keeps its own count. A missing prompt gets HTTP 404 { "error": { "message" } }. The client throws NotFoundError, and runEval ends that case in error. The message names the request and ends with the hint. maxRetries is 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 record writes that JSON to cassettes/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.