You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 82aae14
Browse filesBrowse the repository at this point in the historyBrowse files
Rewrite README with MSW comparison, real E2E examples, and Responses API docs
Move MSW comparison above the fold so readers immediately understand when
to use this vs MSW. Add E2E test patterns section with real-world examples
from the CopilotKit Dojo test suite: global setup/teardown, JSON fixture
files, fixture load ordering, predicate-based routing for supervisor agents,
tool result catch-all, and universal fallback. Document /v1/responses endpoint.
Deterministic mock OpenAI server for testing. Streams SSE responses in real OpenAI chat completion format, driven entirely by fixtures. Zero runtime dependencies — built on Node.js builtins only.
3
+
Deterministic mock OpenAI server for testing. Streams SSE responses in real OpenAI Chat Completions and Responses API format, driven entirely by fixtures. Zero runtime dependencies — built on Node.js builtins only.
4
+
5
+
Supports both streaming (SSE) and non-streaming JSON responses, text completions, tool calls, and error injection. Point any process at it via `OPENAI_BASE_URL` and get reproducible, instant responses.
4
6
5
7
## Install
6
8
7
9
```bash
8
10
npm install @copilotkit/mock-openai
9
11
```
10
12
13
+
## When to Use This vs MSW
14
+
15
+
[MSW (Mock Service Worker)](https://mswjs.io/) is a popular API mocking library, but it solves a different problem.
16
+
17
+
**The key difference is architecture.** mock-openai runs a real HTTP server on a port. MSW patches `http`/`https`/`fetch` modules inside a single Node.js process. MSW can only intercept requests from the process that calls `server.listen()` — child processes, separate services, and workers are unaffected.
18
+
19
+
This matters for E2E tests where multiple processes make OpenAI calls:
The Next.js app (or any other service) just needs:
110
+
111
+
```env
112
+
OPENAI_BASE_URL=http://localhost:5555/v1
113
+
OPENAI_API_KEY=mock-key
114
+
```
115
+
116
+
### JSON Fixture Files
117
+
118
+
Define fixtures as JSON — one file per feature, loaded with `loadFixtureFile` or `loadFixtureDir`.
119
+
120
+
**Text responses** — match on a substring of the last user message:
121
+
122
+
```json
123
+
{
124
+
"fixtures": [
125
+
{
126
+
"match": { "userMessage": "stock price of AAPL" },
127
+
"response": { "content": "The current stock price of Apple Inc. (AAPL) is $150.25." }
128
+
},
129
+
{
130
+
"match": { "userMessage": "capital of France" },
131
+
"response": { "content": "The capital of France is Paris." }
132
+
}
133
+
]
134
+
}
135
+
```
136
+
137
+
**Tool call responses** — the agent framework receives these as tool calls and executes them:
138
+
139
+
```json
140
+
{
141
+
"fixtures": [
142
+
{
143
+
"match": { "userMessage": "one step with eggs" },
144
+
"response": {
145
+
"toolCalls": [
146
+
{
147
+
"name": "generate_task_steps",
148
+
"arguments": "{\"steps\":[{\"description\":\"Crack eggs into bowl\",\"status\":\"enabled\"},{\"description\":\"Preheat oven to 350F\",\"status\":\"enabled\"}]}"
149
+
}
150
+
]
151
+
}
152
+
},
153
+
{
154
+
"match": { "userMessage": "background color to blue" },
155
+
"response": {
156
+
"toolCalls": [
157
+
{
158
+
"name": "change_background",
159
+
"arguments": "{\"background\":\"blue\"}"
160
+
}
161
+
]
162
+
}
163
+
}
164
+
]
165
+
}
166
+
```
167
+
168
+
### Fixture Load Order Matters
169
+
170
+
Fixtures are evaluated first-match-wins. When two fixtures could match the same message, load the more specific one first:
171
+
172
+
```typescript
173
+
// Load HITL fixtures first — "one step with eggs" is more specific than
174
+
// "plan to make brownies" which also appears in the HITL user message
// Then load everything else — earlier matches take priority
178
+
mockServer.loadFixtureDir(FIXTURES_DIR);
179
+
```
180
+
181
+
### Predicate-Based Routing
182
+
183
+
When substring matching isn't enough — for example, when the last user message is the same across multiple requests but the system prompt differs — use predicates:
184
+
185
+
```typescript
186
+
// Supervisor agent: same user message every time, but system prompt
187
+
// contains state flags like "Flights found: false"
Load fixtures from JSON files on disk. See [Fixture Files](#fixture-files)below.
333
+
Load fixtures from JSON files on disk. See [Fixture Files](#json-fixture-files)above.
101
334
102
335
#### `clearFixtures()`
103
336
@@ -121,7 +354,7 @@ mock.nextRequestError(429, {
121
354
122
355
### Request Journal
123
356
124
-
Every request to `/v1/chat/completions` is recorded in a journal.
357
+
Every request to `/v1/chat/completions`and `/v1/responses`is recorded in a journal.
125
358
126
359
#### Programmatic Access
127
360
@@ -183,7 +416,7 @@ Fixtures are evaluated in registration order (first match wins). A fixture match
183
416
}
184
417
```
185
418
186
-
Streams as SSE chunks, splitting `content` by `chunkSize`.
419
+
Streams as SSE chunks, splitting `content` by `chunkSize`. With `stream: false`, returns a standard `chat.completion` JSON object.
187
420
188
421
### Tool Calls
189
422
@@ -202,33 +435,12 @@ Streams as SSE chunks, splitting `content` by `chunkSize`.
202
435
}
203
436
```
204
437
205
-
## Fixture Files
438
+
## API Endpoints
206
439
207
-
Fixtures can be defined in JSON files for use with the CLI or `loadFixtureFile`/`loadFixtureDir`.
440
+
The server handles:
208
441
209
-
```json
210
-
{
211
-
"fixtures": [
212
-
{
213
-
"match": { "userMessage": "hello" },
214
-
"response": { "content": "Hello! How can I help you today?" }
215
-
},
216
-
{
217
-
"match": { "toolName": "get_weather" },
218
-
"response": {
219
-
"toolCalls": [
220
-
{
221
-
"name": "get_weather",
222
-
"arguments": "{\"location\":\"San Francisco\"}"
223
-
}
224
-
]
225
-
}
226
-
}
227
-
]
228
-
}
229
-
```
230
-
231
-
Each entry can also include `latency` and `chunkSize` overrides.
442
+
-**POST `/v1/chat/completions`** — OpenAI Chat Completions API (streaming and non-streaming)
443
+
-**POST `/v1/responses`** — OpenAI Responses API (streaming and non-streaming). Requests are translated to the Chat Completions fixture format internally, so the same fixtures work for both endpoints.
0 commit comments