-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathunbound.spec.ts
More file actions
204 lines (175 loc) · 5.17 KB
/
Copy pathunbound.spec.ts
File metadata and controls
204 lines (175 loc) · 5.17 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
import { Anthropic } from "@anthropic-ai/sdk"
import OpenAI from "openai"
import { UnboundHandler } from "../unbound"
vi.mock("openai", () => {
const createMock = vi.fn()
return {
default: vi.fn(function () {
return {
chat: {
completions: {
create: createMock,
},
},
}
}),
}
})
vi.mock("../fetchers/modelCache", () => ({
getModels: vi.fn().mockResolvedValue({
"openai/gpt-4o": {
maxTokens: 4096,
contextWindow: 128000,
supportsImages: true,
supportsPromptCache: false,
inputPrice: 2.5,
outputPrice: 10,
description: "GPT-4o",
},
}),
}))
describe("UnboundHandler", () => {
beforeEach(() => {
vi.clearAllMocks()
})
it("identifies itself as Zoo Code in the Unbound request headers", () => {
new UnboundHandler({
unboundApiKey: "test-key",
unboundModelId: "openai/gpt-4o",
})
expect(OpenAI).toHaveBeenCalledWith(
expect.objectContaining({
defaultHeaders: expect.objectContaining({
"X-Unbound-Metadata": JSON.stringify({ labels: [{ key: "app", value: "zoo-code" }] }),
}),
}),
)
})
it("streams reasoning chunks from delta.reasoning_content", async () => {
const mockCreate = (OpenAI as unknown as any)().chat.completions.create
mockCreate.mockResolvedValue({
async *[Symbol.asyncIterator]() {
yield { choices: [{ delta: { reasoning_content: "thinking..." } }] }
yield { choices: [{ delta: { content: "answer" } }] }
yield { choices: [{ delta: {} }], usage: { prompt_tokens: 1, completion_tokens: 1 } }
},
})
const handler = new UnboundHandler({
unboundApiKey: "test-key",
unboundModelId: "openai/gpt-4o",
})
const chunks: any[] = []
for await (const chunk of handler.createMessage("system", [{ role: "user", content: "hi" }], {
taskId: "t",
tools: [],
})) {
chunks.push(chunk)
}
expect(chunks).toContainEqual({ type: "reasoning", text: "thinking..." })
})
it("falls back to delta.reasoning when reasoning_content is absent", async () => {
const mockCreate = (OpenAI as unknown as any)().chat.completions.create
mockCreate.mockResolvedValue({
async *[Symbol.asyncIterator]() {
yield { choices: [{ delta: { reasoning: "router-style thought" } }] }
yield { choices: [{ delta: {} }], usage: { prompt_tokens: 1, completion_tokens: 1 } }
},
})
const handler = new UnboundHandler({
unboundApiKey: "test-key",
unboundModelId: "openai/gpt-4o",
})
const chunks: any[] = []
for await (const chunk of handler.createMessage("system", [{ role: "user", content: "hi" }], {
taskId: "t",
tools: [],
})) {
chunks.push(chunk)
}
expect(chunks).toContainEqual({ type: "reasoning", text: "router-style thought" })
})
it("prefers delta.reasoning_content over delta.reasoning when both are present", async () => {
const mockCreate = (OpenAI as unknown as any)().chat.completions.create
mockCreate.mockResolvedValue({
async *[Symbol.asyncIterator]() {
yield {
choices: [
{
delta: {
reasoning_content: "primary thought",
reasoning: "fallback thought",
},
},
],
}
yield { choices: [{ delta: {} }], usage: { prompt_tokens: 1, completion_tokens: 1 } }
},
})
const handler = new UnboundHandler({
unboundApiKey: "test-key",
unboundModelId: "openai/gpt-4o",
})
const chunks: any[] = []
for await (const chunk of handler.createMessage("system", [{ role: "user", content: "hi" }], {
taskId: "t",
tools: [],
})) {
chunks.push(chunk)
}
const reasoningChunks = chunks.filter((chunk) => chunk.type === "reasoning")
expect(reasoningChunks).toEqual([{ type: "reasoning", text: "primary thought" }])
})
it("identifies itself as Zoo Code in per-request Unbound metadata", async () => {
const mockCreate = (OpenAI as unknown as any)().chat.completions.create
mockCreate.mockResolvedValue({
async *[Symbol.asyncIterator]() {
yield {
choices: [{ delta: { content: "ok" } }],
}
yield {
choices: [{ delta: {} }],
usage: { prompt_tokens: 1, completion_tokens: 1 },
}
},
})
const handler = new UnboundHandler({
unboundApiKey: "test-key",
unboundModelId: "openai/gpt-4o",
})
const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "hello" }]
const stream = handler.createMessage("system", messages, {
taskId: "task-123",
mode: "architect",
tools: [],
})
for await (const _chunk of stream) {
// drain stream
}
expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({
unbound_metadata: {
originApp: "zoo-code",
taskId: "task-123",
mode: "architect",
},
}),
)
})
it("completePrompt returns the response text", async () => {
const mockCreate = (OpenAI as unknown as any)().chat.completions.create
mockCreate.mockResolvedValue({
choices: [{ message: { content: "completed text" } }],
})
const handler = new UnboundHandler({
unboundApiKey: "test-key",
unboundModelId: "openai/gpt-4o",
})
const result = await handler.completePrompt("Write a haiku")
expect(result).toBe("completed text")
expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({
messages: [{ role: "system", content: "Write a haiku" }],
}),
)
})
})