Repository navigation
Expand file tree
/
Copy pathintegration.test.ts
More file actions
503 lines (461 loc) · 19.4 KB
/
Copy pathintegration.test.ts
File metadata and controls
503 lines (461 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/**
* Integration tests — end-to-end proxy tests with mock upstream.
* @see .omo/plans/zcode-proxy.md Task 13
*/
import { describe, it, expect, beforeAll, afterAll } from "bun:test";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { loadConfig } from "./config/loader.js";
import { AuthManager } from "./auth/manager.js";
import { startServer, type ProxyServer } from "./server/server.js";
let proxyServer: ProxyServer;
let mockUpstreamServer: ReturnType<typeof Bun.serve>;
let proxyPort: number;
let mockPort: number;
let capturedUpstreamBodies: string[] = [];
function findFreePort(): number {
return 18000 + Math.floor(Math.random() * 1000);
}
// The former config.test.yaml fixture (removed in f6aa147) inlined as a temp
// file so the suite stays self-contained.
let configDir: string;
function writeTestConfig(): string {
configDir = mkdtempSync(join(tmpdir(), "zcode-int-"));
const yaml = [
"server:",
' port: 19090',
' host: "127.0.0.1"',
"auth:",
' proxyApiKey: "test-proxy-key"',
"provider: zai",
"defaultModel: glm-4.6",
"models:",
" - glm-4.6",
"logging:",
" level: info",
"",
].join("\n");
const path = join(configDir, "config.test.yaml");
writeFileSync(path, yaml);
return path;
}
beforeAll(async () => {
mockPort = findFreePort();
proxyPort = findFreePort();
mockUpstreamServer = Bun.serve({
port: mockPort,
hostname: "127.0.0.1",
async fetch(req) {
const url = new URL(req.url);
const rawBody = await req.text();
let parsed: { stream?: boolean; model?: string } = {};
try { parsed = JSON.parse(rawBody); } catch {}
if (url.pathname.includes("/v1/messages")) {
capturedUpstreamBodies.push(rawBody);
let hasToolResult = false;
let hasToolsDefined = false;
try {
const parsedAny = JSON.parse(rawBody) as {
messages?: Array<{ content?: unknown }>;
tools?: unknown[];
};
hasToolResult = (parsedAny.messages ?? []).some(
(m) => Array.isArray(m.content) && m.content.some((b: any) => b?.type === "tool_result"),
);
hasToolsDefined = (parsedAny.tools?.length ?? 0) > 0;
} catch {}
if (hasToolResult) {
return new Response(JSON.stringify({
id: "msg_after_tool",
type: "message",
role: "assistant",
content: [{ type: "text", text: "Tool result acknowledged" }],
model: "glm-4.6",
stop_reason: "end_turn",
stop_sequence: null,
usage: { input_tokens: 25, output_tokens: 4 },
}), { status: 200, headers: { "content-type": "application/json" } });
}
if (hasToolsDefined && !parsed.stream) {
return new Response(JSON.stringify({
id: "msg_tool_call",
type: "message",
role: "assistant",
content: [
{ type: "text", text: "Calling tool." },
{ type: "tool_use", id: "toolu_http_1", name: "get_weather", input: { city: "SF" } },
],
model: "glm-4.6",
stop_reason: "tool_use",
stop_sequence: null,
usage: { input_tokens: 15, output_tokens: 12 },
}), { status: 200, headers: { "content-type": "application/json" } });
}
if (parsed.stream) {
const sse = [
'event: message_start\ndata: {"type":"message_start","message":{"id":"msg_int","model":"glm-4.6","usage":{"input_tokens":33,"output_tokens":1}}}\n\n',
'event: content_block_delta\ndata: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Integration stream"}}\n\n',
'event: message_delta\ndata: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":4}}\n\n',
'event: message_stop\ndata: {"type":"message_stop"}\n\n',
].join("");
return new Response(sse, { status: 200, headers: { "content-type": "text/event-stream" } });
}
return new Response(JSON.stringify({
id: "msg_int_test",
type: "message",
role: "assistant",
content: [{ type: "text", text: "Integration test response" }],
model: "glm-4.6",
stop_reason: "end_turn",
stop_sequence: null,
usage: { input_tokens: 10, output_tokens: 8 },
}), { status: 200, headers: { "content-type": "application/json" } });
}
if (url.pathname.includes("/chat/completions")) {
capturedUpstreamBodies.push(rawBody);
let hasToolResult = false;
let hasToolsDefined = false;
try {
const parsedAny = JSON.parse(rawBody) as {
messages?: Array<{ role?: string }>;
tools?: unknown[];
};
hasToolResult = (parsedAny.messages ?? []).some((m) => m.role === "tool");
hasToolsDefined = (parsedAny.tools?.length ?? 0) > 0;
} catch {}
if (hasToolResult) {
return new Response(JSON.stringify({
id: "chatcmpl-after-tool",
object: "chat.completion",
created: Date.now(),
model: "glm-4.6",
choices: [{
index: 0,
message: { role: "assistant", content: "Tool result acknowledged" },
finish_reason: "stop",
}],
usage: { prompt_tokens: 25, completion_tokens: 4, total_tokens: 29 },
}), { status: 200, headers: { "content-type": "application/json" } });
}
if (hasToolsDefined && !parsed.stream) {
return new Response(JSON.stringify({
id: "chatcmpl-tool-call",
object: "chat.completion",
created: Date.now(),
model: "glm-4.6",
choices: [{
index: 0,
message: {
role: "assistant",
content: "Calling tool.",
tool_calls: [{
id: "call_http_1",
type: "function",
function: { name: "get_weather", arguments: "{\"city\":\"SF\"}" },
}],
},
finish_reason: "tool_calls",
}],
usage: { prompt_tokens: 15, completion_tokens: 12, total_tokens: 27 },
}), { status: 200, headers: { "content-type": "application/json" } });
}
if (parsed.stream) {
const sse = [
'data: {"id":"chatcmpl-int-stream","object":"chat.completion.chunk","created":1,"model":"glm-4.6","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}',
'',
'data: {"id":"chatcmpl-int-stream","object":"chat.completion.chunk","created":1,"model":"glm-4.6","choices":[{"index":0,"delta":{"content":"Integration stream"},"finish_reason":null}]}',
'',
'data: {"id":"chatcmpl-int-stream","object":"chat.completion.chunk","created":1,"model":"glm-4.6","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}',
'',
'data: {"id":"chatcmpl-int-stream","object":"chat.completion.chunk","created":1,"model":"glm-4.6","choices":[],"usage":{"prompt_tokens":33,"completion_tokens":4,"total_tokens":37}}',
'',
'data: [DONE]',
'',
].join("\n");
return new Response(sse, { status: 200, headers: { "content-type": "text/event-stream" } });
}
return new Response(JSON.stringify({
id: "chatcmpl-int-test",
object: "chat.completion",
created: Date.now(),
model: "glm-4.6",
choices: [{
index: 0,
message: { role: "assistant", content: "OpenAI integration response" },
finish_reason: "stop",
}],
usage: { prompt_tokens: 12, completion_tokens: 6, total_tokens: 18 },
}), { status: 200, headers: { "content-type": "application/json" } });
}
return new Response("not found", { status: 404 });
},
});
const config = loadConfig(writeTestConfig());
config.server.port = proxyPort;
config.server.host = "127.0.0.1";
config.auth.proxyApiKey = "integration-test-key";
config.providers.zai.anthropicBase = `http://127.0.0.1:${mockPort}/anthropic`;
config.providers.zai.openaiBase = `http://127.0.0.1:${mockPort}/coding`;
const auth = new AuthManager();
auth.setOAuthCredential({ apiKey: "integrationTestKey", secret: "integrationTestSecret", provider: "zai" });
proxyServer = await startServer({ config, auth });
});
afterAll(() => {
// Use stop(false) (not stop(true)) — stop(true) calls process.exit(0) which
// terminates test discovery early and masks failures in other test files.
proxyServer?.stop(false);
mockUpstreamServer?.stop(false);
if (configDir) rmSync(configDir, { recursive: true, force: true });
});
function proxyUrl(path: string): string {
return `http://127.0.0.1:${proxyPort}${path}`;
}
function authHeader(): Record<string, string> {
return { "Authorization": "Bearer integration-test-key", "Content-Type": "application/json" };
}
describe("integration: OpenAI clients (translated Anthropic upstream)", () => {
it("POST /v1/chat/completions returns 200 with a translated OpenAI response", async () => {
const resp = await fetch(proxyUrl("/v1/chat/completions"), {
method: "POST",
headers: authHeader(),
body: JSON.stringify({
model: "glm-4.6",
messages: [{ role: "user", content: "Hello" }],
}),
});
expect(resp.status).toBe(200);
const body = await resp.json();
expect(body.object).toBe("chat.completion");
expect(body.choices[0].message.content).toBe("Integration test response");
expect(body.model).toBe("glm-4.6");
});
it("gzips the translated response when the client advertises gzip", async () => {
const resp = await fetch(proxyUrl("/v1/chat/completions"), {
method: "POST",
headers: { ...authHeader(), "accept-encoding": "gzip" },
body: JSON.stringify({ model: "glm-4.6", messages: [{ role: "user", content: "Hi" }] }),
});
expect(resp.status).toBe(200);
expect(resp.headers.get("content-encoding")).toBe("gzip");
const body = await resp.json();
expect(body.object).toBe("chat.completion");
expect(body.choices[0].message.content).toBe("Integration test response");
});
});
describe("integration: OpenAI streaming passthrough", () => {
it("passes OpenAI-compatible SSE chunks through", async () => {
const resp = await fetch(proxyUrl("/v1/chat/completions"), {
method: "POST",
headers: authHeader(),
body: JSON.stringify({
model: "glm-4.6",
messages: [{ role: "user", content: "Stream test" }],
stream: true,
}),
});
expect(resp.status).toBe(200);
expect(resp.headers.get("content-type")).toBe("text/event-stream");
const text = await resp.text();
expect(text).toContain("chat.completion.chunk");
expect(text).toContain("Integration stream");
expect(text).toContain("data: [DONE]");
});
});
describe("integration: Anthropic streaming usage", () => {
it("passes upstream usage through verbatim (input_tokens on message_start)", async () => {
const resp = await fetch(proxyUrl("/v1/messages"), {
method: "POST",
headers: authHeader(),
body: JSON.stringify({
model: "glm-4.6",
max_tokens: 100,
stream: true,
messages: [{ role: "user", content: "Stream test" }],
}),
});
expect(resp.status).toBe(200);
const text = await resp.text();
// passthrough: the upstream's message_start usage (input_tokens 33) and
// message_delta usage (output_tokens 4) must arrive unmodified
const startLine = text.split("\n").find((l) => l.startsWith("data: ") && l.includes('"message_start"'));
expect(startLine).toBeDefined();
const start = JSON.parse(startLine!.slice(6));
expect(start.message.usage.input_tokens).toBe(33);
const deltaLine = text.split("\n").find((l) => l.startsWith("data: ") && l.includes('"message_delta"'));
expect(deltaLine).toBeDefined();
const delta = JSON.parse(deltaLine!.slice(6));
expect(delta.usage.output_tokens).toBe(4);
expect(text).toContain("event: message_stop");
});
});
describe("integration: OpenAI tool-call roundtrip (HTTP layer)", () => {
it("returns OpenAI tool_calls on turn 1, accepts tool results on turn 2, upstream receives Anthropic shape", async () => {
const tools = [{ type: "function", function: { name: "get_weather", parameters: { type: "object", properties: { city: { type: "string" } } } } }];
const resp1 = await fetch(proxyUrl("/v1/chat/completions"), {
method: "POST",
headers: authHeader(),
body: JSON.stringify({
model: "glm-4.6",
messages: [{ role: "user", content: "weather in SF?" }],
tools,
tool_choice: "auto",
}),
});
expect(resp1.status).toBe(200);
const body1 = await resp1.json();
expect(body1.choices[0].finish_reason).toBe("tool_calls");
const toolCall = body1.choices[0].message.tool_calls?.[0];
expect(toolCall).toBeDefined();
expect(toolCall.id).toBe("toolu_http_1");
expect(toolCall.function.name).toBe("get_weather");
expect(JSON.parse(toolCall.function.arguments)).toEqual({ city: "SF" });
const resp2 = await fetch(proxyUrl("/v1/chat/completions"), {
method: "POST",
headers: authHeader(),
body: JSON.stringify({
model: "glm-4.6",
messages: [
{ role: "user", content: "weather in SF?" },
{ role: "assistant", content: null, tool_calls: [toolCall] },
{ role: "tool", tool_call_id: toolCall.id, content: "62°F" },
],
tools,
}),
});
expect(resp2.status).toBe(200);
const body2 = await resp2.json();
expect(body2.choices[0].finish_reason).toBe("stop");
expect(body2.choices[0].message.content).toBe("Tool result acknowledged");
const toolResultBody = capturedUpstreamBodies
.map((b) => JSON.parse(b))
.filter((b) => (b.messages ?? []).some(
(m: any) => Array.isArray(m.content) && m.content.some((blk: any) => blk?.type === "tool_result" && blk?.tool_use_id === "toolu_http_1"),
));
expect(toolResultBody.length).toBeGreaterThanOrEqual(1);
const upstreamReq = toolResultBody.at(-1);
expect(upstreamReq.messages).toHaveLength(3);
expect(upstreamReq.messages[0].role).toBe("user");
expect(upstreamReq.messages[1].role).toBe("assistant");
expect(upstreamReq.messages[1].content.some((b: any) => b.type === "tool_use" && b.id === "toolu_http_1")).toBeTrue();
expect(upstreamReq.messages[2].content.some((b: any) => b.type === "tool_result" && b.content === "62°F")).toBeTrue();
expect(upstreamReq.tools).toHaveLength(1);
expect(upstreamReq.tools[0].name).toBe("get_weather");
});
});
describe("integration: Anthropic clients (native passthrough)", () => {
it("POST /v1/messages returns 200 with the upstream Anthropic response verbatim", async () => {
const resp = await fetch(proxyUrl("/v1/messages"), {
method: "POST",
headers: authHeader(),
body: JSON.stringify({
model: "glm-4.6",
max_tokens: 100,
messages: [{ role: "user", content: "Hello" }],
}),
});
expect(resp.status).toBe(200);
const body = await resp.json();
expect(body.content[0].text).toBe("Integration test response");
expect(body.stop_reason).toBe("end_turn");
});
it("round-trips Anthropic tool_use → tool_result with the native shape upstream", async () => {
const tools = [{ name: "get_weather", description: "Get weather", input_schema: { type: "object", properties: { city: { type: "string" } } } }];
// Turn 1: Anthropic client asks for a tool call.
const resp1 = await fetch(proxyUrl("/v1/messages"), {
method: "POST",
headers: authHeader(),
body: JSON.stringify({
model: "glm-4.6",
max_tokens: 100,
messages: [{ role: "user", content: "weather in SF?" }],
tools,
tool_choice: { type: "auto" },
}),
});
expect(resp1.status).toBe(200);
const body1 = await resp1.json();
expect(body1.stop_reason).toBe("tool_use");
const toolUse = body1.content.find((b: any) => b.type === "tool_use");
expect(toolUse).toBeDefined();
expect(toolUse.name).toBe("get_weather");
expect(toolUse.input).toEqual({ city: "SF" });
// Turn 2: Anthropic client replays tool_result history; the upstream must
// see the native Anthropic shape (assistant tool_use + user tool_result).
const resp2 = await fetch(proxyUrl("/v1/messages"), {
method: "POST",
headers: authHeader(),
body: JSON.stringify({
model: "glm-4.6",
max_tokens: 100,
messages: [
{ role: "user", content: "weather in SF?" },
{ role: "assistant", content: [toolUse] },
{ role: "user", content: [{ type: "tool_result", tool_use_id: toolUse.id, content: "62°F" }] },
],
tools,
}),
});
expect(resp2.status).toBe(200);
const body2 = await resp2.json();
expect(body2.stop_reason).toBe("end_turn");
expect(body2.content[0].text).toBe("Tool result acknowledged");
const upstreamReq = capturedUpstreamBodies
.map((b) => JSON.parse(b))
.filter((b) => (b.messages ?? []).some(
(m: any) => Array.isArray(m.content) && m.content.some((blk: any) => blk?.type === "tool_result" && blk?.tool_use_id === toolUse.id),
))
.at(-1);
expect(upstreamReq).toBeDefined();
expect(upstreamReq.messages.map((m: any) => m.role)).toEqual(["user", "assistant", "user"]);
expect(upstreamReq.messages[1].content.some((b: any) => b.type === "tool_use" && b.name === "get_weather")).toBeTrue();
expect(upstreamReq.messages[2].content.some((b: any) => b.type === "tool_result" && b.content === "62°F")).toBeTrue();
expect(upstreamReq.tools[0].name).toBe("get_weather");
});
});
describe("integration: Models endpoint", () => {
it("GET /v1/models returns model list", async () => {
const resp = await fetch(proxyUrl("/v1/models"), {
headers: { Authorization: "Bearer integration-test-key" },
});
expect(resp.status).toBe(200);
const body = await resp.json();
expect(body.object).toBe("list");
expect(body.data.length).toBeGreaterThan(0);
});
});
describe("integration: Auth", () => {
it("rejects request without proxy key", async () => {
const resp = await fetch(proxyUrl("/v1/models"));
expect(resp.status).toBe(401);
});
it("rejects request with wrong proxy key", async () => {
const resp = await fetch(proxyUrl("/v1/models"), {
headers: { Authorization: "Bearer wrong-key" },
});
expect(resp.status).toBe(401);
});
});
describe("integration: Health", () => {
it("GET /health returns ok", async () => {
const resp = await fetch(proxyUrl("/health"), {
headers: { Authorization: "Bearer integration-test-key" },
});
expect(resp.status).toBe(200);
const body = await resp.json();
expect(body.status).toBe("ok");
});
});
describe("integration: Error handling", () => {
it("unknown route returns 404", async () => {
const resp = await fetch(proxyUrl("/unknown"), {
headers: { Authorization: "Bearer integration-test-key" },
});
expect(resp.status).toBe(404);
});
it("CORS preflight returns 204", async () => {
const resp = await fetch(proxyUrl("/v1/models"), { method: "OPTIONS" });
expect(resp.status).toBe(204);
expect(resp.headers.get("access-control-allow-origin")).toBe("*");
});
});