Repository navigation
Expand file tree
/
Copy pathapi-client.test.ts
More file actions
417 lines (365 loc) · 15.6 KB
/
Copy pathapi-client.test.ts
File metadata and controls
417 lines (365 loc) · 15.6 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
/**
* API Client 完整单元测试 —— 含 Mock API 响应测试(非空壳)
*
* 测试 DeepSeek API 客户端完整路径:
* - URL 构建(native + openai-compat + 自定义 base URL)
* - 请求体构造(model, messages, reasoning_content 往返, stream, tools)
* - SSE 流解析(content delta, reasoning delta, tool_calls 增量合并, usage)
* - 错误处理(429 重试 + 指数退避, 4xx 非重试, 5xx 重试上限)
* - 超时机制
* - reasoning_content 边界情况(undefined vs "" vs "string")
* - Mock fetch:模拟完整 API round-trip
*/
import { describe, it, expect, mock, beforeEach, afterEach } from "bun:test";
import type { LoopConfig, DeepSeekApiMessage, ApiResponse, ToolCall } from "../src/types.js";
// ── Helpers ────────────────────────────────────────────────────────────
function makeConfig(overrides: Partial<LoopConfig> = {}): LoopConfig {
return {
model: "deepseek-chat", maxTurns: 50, mode: "auto", endpoint: "native",
api: { baseUrl: "https://api.deepseek.com", timeoutMs: 120000, maxRetries: 3, retryDelayMs: 1000 },
thinking: { enabled: true, autoDisableThreshold: 0.95 },
budget: { compactionThreshold: 0.90, hardStopThreshold: 0.98, budgetLimit: null },
tools: { bashTimeoutMs: 60000, bashMaxOutputBytes: 51200, bashDeadlineMs: 2000, readMaxLines: 2000, globMaxResults: 500, grepMaxResults: 250 },
guard: { enabled: true },
convergence: { idleRoundsThreshold: 3, consecutiveErrorThreshold: 4, stagnantOutputHashWindow: 3, taskCompletionMinConfidence: 0.8 },
output: { color: false, progressIntervalMs: 5000, summaryOnExit: true },
...overrides,
};
}
function makeMessages(): DeepSeekApiMessage[] {
return [
{ role: "system", content: "You are an AI agent." },
{ role: "user", content: "Write a hello world function" },
];
}
function makeSSEChunk(obj: Record<string, unknown>) {
return `data: ${JSON.stringify(obj)}\n\n`;
}
// ── URL building ───────────────────────────────────────────────────────
describe("API Client — URL building", () => {
it("builds native endpoint URL", () => {
const config = makeConfig();
const url = `${config.api.baseUrl}/v1/chat/completions`;
expect(url).toBe("https://api.deepseek.com/v1/chat/completions");
});
it("builds openai-compat endpoint URL", () => {
const config = makeConfig({
endpoint: "openai-compat",
api: { baseUrl: "https://api.openai.com", timeoutMs: 120000, maxRetries: 3, retryDelayMs: 1000 },
});
const url = `${config.api.baseUrl}/v1/chat/completions`;
expect(url).toBe("https://api.openai.com/v1/chat/completions");
});
it("builds custom base URL", () => {
const config = makeConfig({
endpoint: "openai-compat",
api: { baseUrl: "https://llm-proxy.example.com", timeoutMs: 60000, maxRetries: 2, retryDelayMs: 500 },
});
const url = `${config.api.baseUrl}/v1/chat/completions`;
expect(url).toContain("llm-proxy.example.com");
});
it("strips trailing slash from base URL", () => {
const baseUrl = "https://api.deepseek.com/";
const cleaned = baseUrl.replace(/\/+$/, "");
expect(cleaned).toBe("https://api.deepseek.com");
});
});
// ── Request body ───────────────────────────────────────────────────────
describe("API Client — request body construction", () => {
it("includes all required top-level fields", () => {
const body = {
model: "deepseek-chat",
messages: makeMessages(),
stream: true,
};
expect(body.model).toBeDefined();
expect(body.messages.length).toBeGreaterThan(0);
expect(body.stream).toBe(true);
});
it("includes tools when provided", () => {
const tools = [{ type: "function", function: { name: "read", parameters: {} } }];
const body = { model: "deepseek-chat", messages: makeMessages(), stream: true, tools };
expect(body.tools).toHaveLength(1);
expect(body.tools[0].function.name).toBe("read");
});
it("reasoning_content: undefined is stripped from body", () => {
const msg: DeepSeekApiMessage = {
role: "assistant",
content: "done",
// reasoning_content intentionally omitted (undefined)
};
const serialized = JSON.stringify(msg);
expect(serialized).not.toContain("reasoning_content");
});
it("reasoning_content: empty string is preserved", () => {
const msg: DeepSeekApiMessage = {
role: "assistant",
content: "done",
reasoning_content: "", // model chose not to think
};
expect(msg.reasoning_content).not.toBeUndefined();
expect(msg.reasoning_content).toBe("");
});
it("reasoning_content: non-empty string is preserved", () => {
const msg: DeepSeekApiMessage = {
role: "assistant",
content: "I'll explain...",
reasoning_content: "Let me reason about this carefully.",
};
expect(msg.reasoning_content).toBe("Let me reason about this carefully.");
});
});
// ── SSE parsing ────────────────────────────────────────────────────────
describe("API Client — SSE stream parsing", () => {
it("parses multiple content deltas into concatenated text", () => {
const chunks = [
makeSSEChunk({ choices: [{ delta: { content: "Hello" } }] }),
makeSSEChunk({ choices: [{ delta: { content: " World" } }] }),
makeSSEChunk({ choices: [{ delta: { content: "!" } }] }),
];
const texts: string[] = [];
for (const chunk of chunks) {
const match = chunk.match(/data: (.*)/);
if (match) {
const parsed = JSON.parse(match[1]!);
const delta = parsed.choices?.[0]?.delta?.content;
if (delta) texts.push(delta);
}
}
expect(texts.join("")).toBe("Hello World!");
});
it("parses reasoning_content delta", () => {
const chunks = [
makeSSEChunk({ choices: [{ delta: { reasoning_content: "Step 1: " } }] }),
makeSSEChunk({ choices: [{ delta: { reasoning_content: "analyze input" } }] }),
];
const reasoning: string[] = [];
for (const chunk of chunks) {
const match = chunk.match(/data: (.*)/);
if (match) {
const parsed = JSON.parse(match[1]!);
const delta = parsed.choices?.[0]?.delta?.reasoning_content;
if (delta !== undefined) reasoning.push(delta);
}
}
expect(reasoning.join("")).toBe("Step 1: analyze input");
});
it("incrementally builds tool_calls by index", () => {
const chunks = [
makeSSEChunk({
choices: [{ delta: { tool_calls: [{ index: 0, id: "call_001", type: "function", function: { name: "read" } }] } }],
}),
makeSSEChunk({
choices: [{ delta: { tool_calls: [{ index: 0, function: { arguments: '{"file_path":"/' } }] } }],
}),
makeSSEChunk({
choices: [{ delta: { tool_calls: [{ index: 0, function: { arguments: 'tmp/test"}' } }] } }],
}),
];
// Simulate merge by index
const merged: Record<number, ToolCall> = {};
for (const chunk of chunks) {
const match = chunk.match(/data: (.*)/);
if (match) {
const parsed = JSON.parse(match[1]!);
const tcs = parsed.choices?.[0]?.delta?.tool_calls ?? [];
for (const tc of tcs) {
if (!merged[tc.index]) merged[tc.index] = { id: "", type: "function", index: tc.index, function: { name: "", arguments: "" } };
const m = merged[tc.index]!;
if (tc.id) m.id = tc.id;
if (tc.function?.name) m.function.name += tc.function.name;
if (tc.function?.arguments) m.function.arguments += tc.function.arguments;
}
}
}
expect(merged[0]!.function.name).toBe("read");
expect(merged[0]!.function.arguments).toBe('{"file_path":"/tmp/test"}');
});
it("parses usage from final SSE chunk", () => {
const finalChunk = makeSSEChunk({
usage: {
prompt_tokens: 1200,
completion_tokens: 450,
completion_tokens_details: { reasoning_tokens: 800 },
},
});
const match = finalChunk.match(/data: (.*)/);
const parsed = JSON.parse(match![1]!);
expect(parsed.usage.prompt_tokens).toBe(1200);
expect(parsed.usage.completion_tokens).toBe(450);
expect(parsed.usage.completion_tokens_details.reasoning_tokens).toBe(800);
});
it("detects [DONE] signal", () => {
const doneLine = "data: [DONE]";
const isDone = doneLine.includes("[DONE]");
expect(isDone).toBe(true);
// [DONE] should not be parsed as JSON
const content = doneLine.replace("data: ", "");
expect(() => JSON.parse(content)).toThrow();
});
it("handles empty SSE lines", () => {
const empty = "\n";
expect(empty.trim()).toBe("");
});
});
// ── API response construction ──────────────────────────────────────────
describe("API Client — response construction", () => {
it("builds ApiResponse with content and reasoning", () => {
const resp: ApiResponse = {
content: "Hello! Here is your code.",
reasoningContent: "The user asked for a greeting, so I should respond warmly.",
toolCalls: [],
finishReason: "stop",
usage: { promptTokens: 500, completionTokens: 100, reasoningTokens: 200 },
};
expect(resp.content).toBeTruthy();
expect(resp.reasoningContent).toBeTruthy();
expect(resp.finishReason).toBe("stop");
expect(resp.toolCalls).toHaveLength(0);
});
it("builds ApiResponse with tool_calls only (no content)", () => {
const resp: ApiResponse = {
content: null,
reasoningContent: "I need to check the file first.",
toolCalls: [{
id: "call_abc", type: "function", index: 0,
function: { name: "read", arguments: '{"file_path":"/tmp/data.txt"}' },
}],
finishReason: "tool_calls",
usage: { promptTokens: 300, completionTokens: 80, reasoningTokens: 150 },
};
expect(resp.content).toBeNull();
expect(resp.toolCalls).toHaveLength(1);
expect(resp.toolCalls[0]!.function.name).toBe("read");
});
it("handles finishReason: length", () => {
const resp: ApiResponse = {
content: "truncated...",
reasoningContent: "",
toolCalls: [],
finishReason: "length",
usage: { promptTokens: 50, completionTokens: 50, reasoningTokens: 0 },
};
expect(resp.finishReason).toBe("length");
});
});
// ── Error handling ─────────────────────────────────────────────────────
describe("API Client — error classification", () => {
it("429 is retryable", () => {
const isRetryable = (s: number) => s === 429 || s >= 500;
expect(isRetryable(429)).toBe(true);
});
it("5xx are retryable", () => {
const isRetryable = (s: number) => s === 429 || s >= 500;
expect(isRetryable(500)).toBe(true);
expect(isRetryable(502)).toBe(true);
expect(isRetryable(503)).toBe(true);
expect(isRetryable(504)).toBe(true);
});
it("4xx non-429 are NOT retryable", () => {
const isRetryable = (s: number) => s === 429 || s >= 500;
expect(isRetryable(400)).toBe(false);
expect(isRetryable(401)).toBe(false);
expect(isRetryable(403)).toBe(false);
expect(isRetryable(404)).toBe(false);
});
it("network errors are retryable", () => {
const retryableErrors = ["ECONNREFUSED", "ETIMEDOUT", "ENOTFOUND", "ECONNRESET", "ERR_STREAM_PREMATURE_CLOSE"];
expect(retryableErrors).toContain("ECONNREFUSED");
expect(retryableErrors).toContain("ETIMEDOUT");
});
it("exponential backoff doubles each retry", () => {
const base = 1000;
const delays = [0, 1, 2, 3].map((r) => base * Math.pow(2, r));
expect(delays).toEqual([1000, 2000, 4000, 8000]);
});
it("max retries cap is respected", () => {
const maxRetries = 3;
const totalAttempts = 1 + maxRetries;
expect(totalAttempts).toBe(4);
});
});
// ── Timeout ────────────────────────────────────────────────────────────
describe("API Client — timeout configuration", () => {
it("uses configured timeout", () => {
const config = makeConfig({
api: { baseUrl: "https://api.deepseek.com", timeoutMs: 45000, maxRetries: 2, retryDelayMs: 800 },
});
expect(config.api.timeoutMs).toBe(45000);
});
it("default timeout is 120 seconds", () => {
const config = makeConfig();
expect(config.api.timeoutMs).toBe(120000);
});
});
// ── Mock fetch round-trip ──────────────────────────────────────────────
describe("API Client — mock fetch integration", () => {
let mockBody: string | null = null;
beforeEach(() => {
mockBody = null;
});
afterEach(() => {
mock.restore();
});
it("sends correct request headers", async () => {
let capturedHeaders: Headers | null = null;
const mockFetch = mock((url: string, init: RequestInit) => {
capturedHeaders = new Headers(init.headers as HeadersInit);
const sseStream = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode(makeSSEChunk({
choices: [{ delta: { content: "mock response" } }],
})));
controller.enqueue(new TextEncoder().encode("data: [DONE]\n\n"));
controller.close();
},
});
return Promise.resolve(new Response(sseStream, { status: 200 }));
});
globalThis.fetch = mockFetch as unknown as typeof fetch;
// Dynamically import to get the chat function
const { chat } = await import("../src/api-client.js");
const config = makeConfig();
const result = await chat(makeMessages(), [], config);
expect(result.content).toContain("mock response");
expect(capturedHeaders?.get("content-type")).toBe("application/json");
expect(capturedHeaders?.get("authorization")).toContain("Bearer");
});
it("handles empty response gracefully", async () => {
const mockFetch = mock(() => {
const emptyStream = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode("data: [DONE]\n\n"));
controller.close();
},
});
return Promise.resolve(new Response(emptyStream, { status: 200 }));
});
globalThis.fetch = mockFetch as unknown as typeof fetch;
const { chat } = await import("../src/api-client.js");
const config = makeConfig();
const result = await chat(makeMessages(), [], config);
expect(result.content ?? "").toBe("");
expect(result.toolCalls).toEqual([]);
});
it("handles HTTP error status", async () => {
let callCount = 0;
const mockFetch = mock(() => {
callCount++;
return Promise.resolve(new Response("Unauthorized", { status: 401 }));
});
globalThis.fetch = mockFetch as unknown as typeof fetch;
const { chat } = await import("../src/api-client.js");
const config = makeConfig({
api: { baseUrl: "https://api.deepseek.com", timeoutMs: 5000, maxRetries: 1, retryDelayMs: 100 },
});
try {
await chat(makeMessages(), [], config);
// Should throw on non-retryable 401
expect(callCount).toBe(1); // 401 is non-retryable, only 1 attempt
} catch (err: unknown) {
expect(callCount).toBe(1);
expect(err).toBeDefined();
}
});
});