forked from Zoo-Code-Org/Zoo-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencode-go.spec.ts
More file actions
178 lines (158 loc) · 4.99 KB
/
Copy pathopencode-go.spec.ts
File metadata and controls
178 lines (158 loc) · 4.99 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
// npx vitest run src/api/providers/__tests__/opencode-go.spec.ts
// Mock vscode first to avoid import errors
vitest.mock("vscode", () => ({}))
import { Anthropic } from "@anthropic-ai/sdk"
import OpenAI from "openai"
import { opencodeGoDefaultModelId } from "@roo-code/types"
import { OpencodeGoHandler } from "../opencode-go"
import { ApiHandlerOptions } from "../../../shared/api"
vitest.mock("openai")
vitest.mock("delay", () => ({ default: vitest.fn(() => Promise.resolve()) }))
vitest.mock("../fetchers/modelCache", () => ({
getModels: vitest.fn().mockImplementation(() =>
Promise.resolve({
"glm-5.1": {
maxTokens: 32768,
contextWindow: 200000,
supportsImages: false,
supportsPromptCache: false,
description: "GLM 5.1",
},
}),
),
getModelsFromCache: vitest.fn().mockReturnValue(undefined),
}))
const mockCreate = vitest.fn()
;(OpenAI as any).mockImplementation(() => ({
chat: { completions: { create: mockCreate } },
}))
describe("OpencodeGoHandler", () => {
const mockOptions: ApiHandlerOptions = {
opencodeGoApiKey: "test-key",
opencodeGoModelId: "glm-5.1",
}
beforeEach(() => {
vitest.clearAllMocks()
mockCreate.mockClear()
})
it("initializes the OpenAI client with the Opencode Go base URL and key", () => {
const handler = new OpencodeGoHandler(mockOptions)
expect(handler).toBeInstanceOf(OpencodeGoHandler)
expect(OpenAI).toHaveBeenCalledWith(
expect.objectContaining({
baseURL: "https://opencode.ai/zen/go/v1",
apiKey: "test-key",
}),
)
})
describe("fetchModel", () => {
it("returns the configured model info", async () => {
const handler = new OpencodeGoHandler(mockOptions)
const result = await handler.fetchModel()
expect(result.id).toBe("glm-5.1")
expect(result.info.maxTokens).toBe(32768)
expect(result.info.contextWindow).toBe(200000)
expect(result.info.supportsPromptCache).toBe(false)
})
it("falls back to the default model id when none is configured", async () => {
const handler = new OpencodeGoHandler({ opencodeGoApiKey: "test-key" })
const result = await handler.fetchModel()
expect(result.id).toBe(opencodeGoDefaultModelId)
})
})
describe("createMessage", () => {
beforeEach(() => {
mockCreate.mockImplementation(async () => ({
[Symbol.asyncIterator]: async function* () {
yield {
choices: [
{
delta: {
content: "Hello",
reasoning_content: "thinking…",
tool_calls: [
{
index: 0,
id: "call_1",
function: { name: "read_file", arguments: '{"path":' },
},
],
},
index: 0,
},
],
usage: null,
}
yield {
choices: [{ delta: {}, index: 0 }],
usage: {
prompt_tokens: 12,
completion_tokens: 7,
total_tokens: 19,
prompt_tokens_details: { cached_tokens: 4 },
},
}
},
}))
})
it("streams text, reasoning, tool-call and usage chunks", async () => {
const handler = new OpencodeGoHandler(mockOptions)
const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }]
const chunks = []
for await (const chunk of handler.createMessage("You are helpful.", messages)) {
chunks.push(chunk)
}
expect(chunks).toContainEqual({ type: "text", text: "Hello" })
expect(chunks).toContainEqual({ type: "reasoning", text: "thinking…" })
expect(chunks).toContainEqual({
type: "tool_call_partial",
index: 0,
id: "call_1",
name: "read_file",
arguments: '{"path":',
})
expect(chunks).toContainEqual({
type: "usage",
inputTokens: 12,
outputTokens: 7,
cacheReadTokens: 4,
})
})
it("requests a streaming completion with usage included", async () => {
const handler = new OpencodeGoHandler(mockOptions)
const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }]
for await (const _chunk of handler.createMessage("sys", messages)) {
void _chunk // drain
}
expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({
model: "glm-5.1",
stream: true,
stream_options: { include_usage: true },
max_completion_tokens: 32768,
temperature: expect.any(Number),
}),
{ signal: undefined },
)
})
})
describe("completePrompt", () => {
it("returns the message content for a non-streaming completion", async () => {
mockCreate.mockResolvedValue({ choices: [{ message: { content: "the answer" } }] })
const handler = new OpencodeGoHandler(mockOptions)
expect(await handler.completePrompt("ping")).toBe("the answer")
expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({
model: "glm-5.1",
stream: false,
max_completion_tokens: 32768,
}),
)
})
it("wraps errors with an Opencode Go-specific message", async () => {
mockCreate.mockRejectedValue(new Error("boom"))
const handler = new OpencodeGoHandler(mockOptions)
await expect(handler.completePrompt("ping")).rejects.toThrow("Opencode Go completion error: boom")
})
})
})