forked from ericc-ch/copilot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnative-passthrough.test.ts
More file actions
257 lines (230 loc) · 9.44 KB
/
native-passthrough.test.ts
File metadata and controls
257 lines (230 loc) · 9.44 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
import { describe, test, expect, beforeEach, afterEach } from "bun:test"
import type { AnthropicMessagesPayload } from "~/routes/messages/anthropic-types"
import { state } from "~/lib/state"
import { buildUpstreamPayload } from "~/services/copilot/create-messages-native"
import { isNativeAnthropicModel } from "~/services/copilot/native-models"
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/** Minimal valid payload base — only the fields required by the type. */
function basePayload(
overrides: Partial<AnthropicMessagesPayload>,
): AnthropicMessagesPayload {
return {
model: "claude-sonnet-4-5",
messages: [{ role: "user", content: "hi" }],
max_tokens: 1024,
...overrides,
}
}
// ---------------------------------------------------------------------------
// buildUpstreamPayload tests
// ---------------------------------------------------------------------------
describe("buildUpstreamPayload", () => {
// T1 — output_config present but thinking absent → output_config stripped
test("T1: strips output_config when thinking is absent", () => {
const payload = basePayload({
output_config: { effort: "high" },
})
const result = buildUpstreamPayload(payload)
expect(result).not.toHaveProperty("output_config")
expect(result).not.toHaveProperty("thinking")
})
// T2 — adaptive upgrade preserves explicit output_config: { effort: "high" }
test("T2: adaptive upgrade preserves explicit output_config effort", () => {
const payload = basePayload({
model: "claude-opus-4.7",
thinking: { type: "enabled" },
output_config: { effort: "high" },
} as Partial<AnthropicMessagesPayload>)
const result = buildUpstreamPayload(payload)
expect(result.thinking).toEqual({ type: "adaptive" })
// Should keep caller's "high", not override to "medium"
expect(result.output_config).toEqual({ effort: "high" })
})
// T3 — already adaptive → forwarded as-is
test("T3: already-adaptive thinking forwarded as-is", () => {
const payload = basePayload({
model: "claude-opus-4.7",
thinking: { type: "adaptive" },
output_config: { effort: "low" },
} as Partial<AnthropicMessagesPayload>)
const result = buildUpstreamPayload(payload)
expect(result.thinking).toEqual({ type: "adaptive" })
expect(result.output_config).toEqual({ effort: "low" })
})
// T4 — legacy model with enabled thinking → kept as-is, no adaptive upgrade
test("T4: legacy model with enabled thinking kept as-is", () => {
const payload = basePayload({
model: "claude-sonnet-4-5",
thinking: { type: "enabled", budget_tokens: 1024 },
} as Partial<AnthropicMessagesPayload>)
const result = buildUpstreamPayload(payload)
expect(result.thinking).toEqual({ type: "enabled", budget_tokens: 1024 })
expect(result).not.toHaveProperty("output_config")
})
// T5 — adaptive upgrade with no output_config → defaults to effort:medium
test("T5: adaptive upgrade with no output_config defaults to effort:medium", () => {
const payload = basePayload({
model: "claude-opus-4.7",
thinking: { type: "enabled", budget_tokens: 1024 },
// output_config intentionally absent
} as Partial<AnthropicMessagesPayload>)
const result = buildUpstreamPayload(payload)
expect(result.thinking).toEqual({ type: "adaptive" })
expect(result.output_config).toEqual({ effort: "medium" })
})
// T6 — output_config: {} also triggers default (not bypassed)
test("T6: empty output_config triggers medium effort default", () => {
const payload = basePayload({
model: "claude-opus-4.7",
thinking: { type: "enabled" },
output_config: {},
} as Partial<AnthropicMessagesPayload>)
const result = buildUpstreamPayload(payload)
expect(result.thinking).toEqual({ type: "adaptive" })
expect(result.output_config).toEqual({ effort: "medium" })
})
})
// ---------------------------------------------------------------------------
// isNativeAnthropicModel tests
// ---------------------------------------------------------------------------
// Per-test state isolation
let savedModels: typeof state.models
beforeEach(() => {
savedModels = state.models
})
afterEach(() => {
state.models = savedModels
})
describe("isNativeAnthropicModel", () => {
// T5 — model in loaded list with vendor "Anthropic" → true
test("T5: model with vendor Anthropic in loaded list → true", () => {
state.models = {
object: "list",
data: [
{
id: "claude-sonnet-4-5",
vendor: "Anthropic",
name: "Claude Sonnet 4.5",
object: "model",
version: "1",
preview: false,
model_picker_enabled: true,
capabilities: {
family: "claude",
limits: {},
object: "model_capabilities",
supports: {},
tokenizer: "cl100k_base",
type: "chat",
},
},
],
}
expect(isNativeAnthropicModel("claude-sonnet-4-5")).toBe(true)
})
// T6 — model in loaded list with vendor "OpenAI" → false
test("T6: model with vendor OpenAI in loaded list → false", () => {
state.models = {
object: "list",
data: [
{
id: "gpt-4o",
vendor: "OpenAI",
name: "GPT-4o",
object: "model",
version: "1",
preview: false,
model_picker_enabled: true,
capabilities: {
family: "gpt",
limits: {},
object: "model_capabilities",
supports: {},
tokenizer: "cl100k_base",
type: "chat",
},
},
],
}
expect(isNativeAnthropicModel("gpt-4o")).toBe(false)
})
// T7 — model NOT in loaded list, starts with "claude-" → true (heuristic)
test("T7: model not in loaded list but starts with claude- → true", () => {
state.models = { object: "list", data: [] }
expect(isNativeAnthropicModel("claude-future-1")).toBe(true)
})
// T8 — model NOT in loaded list, starts with "gpt-" → false
test("T8: model not in loaded list and starts with gpt- → false", () => {
state.models = { object: "list", data: [] }
expect(isNativeAnthropicModel("gpt-5")).toBe(false)
})
// T9 — state.models undefined → heuristic
test("T9: state.models undefined → heuristic (claude- prefix → true)", () => {
state.models = undefined
expect(isNativeAnthropicModel("claude-something")).toBe(true)
})
})
// ---------------------------------------------------------------------------
// isAdaptiveThinkingModel boundary tests (via buildUpstreamPayload)
// ---------------------------------------------------------------------------
describe("isAdaptiveThinkingModel boundaries (via buildUpstreamPayload)", () => {
// B1 — claude-opus-4.6 is NOT upgraded (one below threshold)
test("B1: claude-opus-4.6 does NOT get adaptive upgrade", () => {
const payload = basePayload({
model: "claude-opus-4.6",
thinking: { type: "enabled", budget_tokens: 2048 },
} as Partial<AnthropicMessagesPayload>)
const result = buildUpstreamPayload(payload)
expect(result.thinking).toEqual({ type: "enabled", budget_tokens: 2048 })
expect(result).not.toHaveProperty("output_config")
})
// B2 — claude-opus-4.7 IS upgraded (exact threshold)
test("B2: claude-opus-4.7 (dot separator) IS upgraded to adaptive", () => {
const payload = basePayload({
model: "claude-opus-4.7",
thinking: { type: "enabled" },
} as Partial<AnthropicMessagesPayload>)
const result = buildUpstreamPayload(payload)
expect(result.thinking).toEqual({ type: "adaptive" })
})
// B3 — claude-opus-4-7 (dash separator) IS upgraded
test("B3: claude-opus-4-7 (dash separator) IS upgraded to adaptive", () => {
const payload = basePayload({
model: "claude-opus-4-7",
thinking: { type: "enabled" },
} as Partial<AnthropicMessagesPayload>)
const result = buildUpstreamPayload(payload)
expect(result.thinking).toEqual({ type: "adaptive" })
})
// B4 — claude-opus-4-6 (dash separator) is NOT upgraded
test("B4: claude-opus-4-6 (dash separator) NOT upgraded", () => {
const payload = basePayload({
model: "claude-opus-4-6",
thinking: { type: "enabled", budget_tokens: 512 },
} as Partial<AnthropicMessagesPayload>)
const result = buildUpstreamPayload(payload)
expect(result.thinking).toEqual({ type: "enabled", budget_tokens: 512 })
expect(result).not.toHaveProperty("output_config")
})
// B5 — claude-opus-4.8 (one above threshold) IS upgraded
test("B5: claude-opus-4.8 (one above threshold) IS upgraded", () => {
const payload = basePayload({
model: "claude-opus-4.8",
thinking: { type: "enabled" },
} as Partial<AnthropicMessagesPayload>)
const result = buildUpstreamPayload(payload)
expect(result.thinking).toEqual({ type: "adaptive" })
})
// B6 — claude-sonnet-4.7 (non-opus) is NOT upgraded
test("B6: claude-sonnet-4.7 (non-opus) NOT upgraded to adaptive", () => {
const payload = basePayload({
model: "claude-sonnet-4.7",
thinking: { type: "enabled", budget_tokens: 1024 },
} as Partial<AnthropicMessagesPayload>)
const result = buildUpstreamPayload(payload)
expect(result.thinking).toEqual({ type: "enabled", budget_tokens: 1024 })
expect(result).not.toHaveProperty("output_config")
})
})