|
| 1 | +# Testing |
| 2 | + |
| 3 | +The `@pivanov/claude-wire/testing` subpath ships in-process `IClaudeProcess` fakes you can swap in for the real `spawnClaude()` during unit tests. Living behind a subpath means production installs that never import from `/testing` skip the module entirely. |
| 4 | + |
| 5 | +## When to Use |
| 6 | + |
| 7 | +- Tests that exercise SDK behavior (parsing, sessions, retries, tool dispatch) without spawning the real `claude` binary. |
| 8 | +- CI environments where the binary is unavailable or authentication isn't set up. |
| 9 | +- Deterministic regression tests that pin a specific NDJSON transcript. |
| 10 | + |
| 11 | +For end-to-end coverage against the real CLI, spawn `claude` normally and consume `claude.ask()` as usual. |
| 12 | + |
| 13 | +## `createMockProcess(options)` |
| 14 | + |
| 15 | +One-shot mock. Pre-supply the NDJSON lines the mock should emit; the stream emits them in order, closes stdout, and resolves `exited` with the configured exit code. |
| 16 | + |
| 17 | +```ts |
| 18 | +import { createMockProcess } from "@pivanov/claude-wire/testing"; |
| 19 | + |
| 20 | +const proc = createMockProcess({ |
| 21 | + lines: [ |
| 22 | + '{"type":"system","subtype":"init","session_id":"s1","model":"haiku","tools":[]}', |
| 23 | + '{"type":"assistant","message":{"role":"assistant","content":[{"type":"text","text":"hi"}]}}', |
| 24 | + '{"type":"result","subtype":"success","session_id":"s1","result":"hi","is_error":false,"total_cost_usd":0.001,"duration_ms":100,"modelUsage":{}}', |
| 25 | + ], |
| 26 | + exitCode: 0, |
| 27 | +}); |
| 28 | + |
| 29 | +// Inspect what the SDK wrote to stdin: |
| 30 | +console.log(proc.writes); |
| 31 | +console.log(proc.killed); |
| 32 | +``` |
| 33 | + |
| 34 | +A positional overload is also accepted for ergonomics: `createMockProcess(lines, exitCode?)`. |
| 35 | + |
| 36 | +### `IMockProcess` |
| 37 | + |
| 38 | +Extends `IClaudeProcess` with two read-only inspection fields: |
| 39 | + |
| 40 | +| Field | Type | Description | |
| 41 | +|-------|------|-------------| |
| 42 | +| `writes` | `readonly string[]` | Every line written to stdin via `write()`, in order. | |
| 43 | +| `killed` | `boolean` | True after `kill()` has been called at least once. | |
| 44 | + |
| 45 | +## `createMultiTurnMockProcess()` |
| 46 | + |
| 47 | +Long-lived mock for tests that need to react to SDK input. The stdout stream stays open until `closeStdout()` or `kill()`. Push events with `emitLines()` (raw NDJSON) or `emitEvent()` (typed `TClaudeEvent`). |
| 48 | + |
| 49 | +```ts |
| 50 | +import { createMultiTurnMockProcess } from "@pivanov/claude-wire/testing"; |
| 51 | + |
| 52 | +const proc = createMultiTurnMockProcess(); |
| 53 | + |
| 54 | +// First turn: |
| 55 | +proc.emitEvent({ type: "system", subtype: "init", session_id: "s1", model: "haiku", tools: [] }); |
| 56 | +proc.emitEvent({ type: "assistant", message: { role: "assistant", content: [{ type: "text", text: "first" }] } }); |
| 57 | +proc.emitEvent({ type: "result", subtype: "success", session_id: "s1", result: "first", is_error: false, total_cost_usd: 0.001 }); |
| 58 | + |
| 59 | +// React to a stdin write before emitting the next turn: |
| 60 | +await waitFor(() => proc.writes.some((w) => w.includes("follow-up"))); |
| 61 | +proc.emitEvent({ type: "result", subtype: "success", session_id: "s1", result: "second", is_error: false, total_cost_usd: 0.002 }); |
| 62 | + |
| 63 | +proc.kill(); |
| 64 | +``` |
| 65 | + |
| 66 | +### `IMultiTurnMockProcess` |
| 67 | + |
| 68 | +Extends `IMockProcess` with three control methods: |
| 69 | + |
| 70 | +| Method | Description | |
| 71 | +|--------|-------------| |
| 72 | +| `emitLines(lines: string[])` | Push raw NDJSON lines into stdout. Each gets a trailing `\n`. | |
| 73 | +| `emitEvent(event: TClaudeEvent)` | JSON-stringify a typed event and emit it as one line. | |
| 74 | +| `closeStdout()` | Close the stdout stream so the reader sees EOF. Idempotent. | |
| 75 | + |
| 76 | +## Wiring Into Bun Tests |
| 77 | + |
| 78 | +Use `mock.module` to redirect `spawnClaude` at the module boundary: |
| 79 | + |
| 80 | +```ts |
| 81 | +import { beforeEach, mock, test, expect } from "bun:test"; |
| 82 | +import { createMockProcess, type IMockProcess } from "@pivanov/claude-wire/testing"; |
| 83 | + |
| 84 | +let mockProc: IMockProcess; |
| 85 | + |
| 86 | +beforeEach(() => { |
| 87 | + mockProc = createMockProcess({ |
| 88 | + lines: [/* fixture lines */], |
| 89 | + exitCode: 0, |
| 90 | + }); |
| 91 | + mock.module("@pivanov/claude-wire", async () => { |
| 92 | + const real = await import("@pivanov/claude-wire"); |
| 93 | + return { ...real, spawnClaude: () => mockProc }; |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +test("session reads a turn from the mock", async () => { |
| 98 | + const { createSession } = await import("@pivanov/claude-wire"); |
| 99 | + const session = createSession(); |
| 100 | + const result = await session.ask("hi"); |
| 101 | + expect(result.text).toBe("hi"); |
| 102 | +}); |
| 103 | +``` |
| 104 | + |
| 105 | +For Vitest, Jest, or other runners, use the equivalent module-mock primitive (`vi.mock`, `jest.mock`). |
| 106 | + |
| 107 | +## Fuzz Testing the Parser |
| 108 | + |
| 109 | +The `@pivanov/claude-wire/parser` subpath exposes `parseLine` and `createTranslator`, both deterministic given the same input. The internal test suite ships a seeded fuzz harness over these; if you build adapters or alternative pipelines, the same harness pattern works for your translator. See `tests/parser/translator.fuzz.test.ts` in the repo for a reference implementation. |
0 commit comments