forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgemini.test.ts
More file actions
490 lines (432 loc) · 15.7 KB
/
Copy pathgemini.test.ts
File metadata and controls
490 lines (432 loc) · 15.7 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
import { describe, it, expect } from "vitest";
import {
isGeminiModel,
isGemini3Model,
isGemini25Model,
buildGemini3ThinkingConfig,
buildGemini25ThinkingConfig,
normalizeGeminiTools,
applyGeminiTransforms,
} from "./gemini";
import type { RequestPayload } from "./types";
describe("transform/gemini", () => {
describe("isGeminiModel", () => {
it("returns true for gemini-pro", () => {
expect(isGeminiModel("gemini-pro")).toBe(true);
});
it("returns true for gemini-1.5-pro", () => {
expect(isGeminiModel("gemini-1.5-pro")).toBe(true);
});
it("returns true for gemini-2.5-flash", () => {
expect(isGeminiModel("gemini-2.5-flash")).toBe(true);
});
it("returns true for gemini-3-pro-high", () => {
expect(isGeminiModel("gemini-3-pro-high")).toBe(true);
});
it("returns true for uppercase GEMINI-PRO", () => {
expect(isGeminiModel("GEMINI-PRO")).toBe(true);
});
it("returns true for mixed case Gemini-Pro", () => {
expect(isGeminiModel("Gemini-Pro")).toBe(true);
});
it("returns false for claude-3-opus", () => {
expect(isGeminiModel("claude-3-opus")).toBe(false);
});
it("returns false for gpt-4", () => {
expect(isGeminiModel("gpt-4")).toBe(false);
});
it("returns false for gemini-claude hybrid (contains both)", () => {
expect(isGeminiModel("gemini-claude-hybrid")).toBe(false);
});
it("returns false for claude-on-gemini", () => {
expect(isGeminiModel("claude-on-gemini")).toBe(false);
});
it("returns false for empty string", () => {
expect(isGeminiModel("")).toBe(false);
});
});
describe("isGemini3Model", () => {
it("returns true for gemini-3-pro", () => {
expect(isGemini3Model("gemini-3-pro")).toBe(true);
});
it("returns true for gemini-3-pro-high", () => {
expect(isGemini3Model("gemini-3-pro-high")).toBe(true);
});
it("returns true for gemini-3-flash", () => {
expect(isGemini3Model("gemini-3-flash")).toBe(true);
});
it("returns true for uppercase GEMINI-3-PRO", () => {
expect(isGemini3Model("GEMINI-3-PRO")).toBe(true);
});
it("returns false for gemini-2.5-pro", () => {
expect(isGemini3Model("gemini-2.5-pro")).toBe(false);
});
it("returns false for gemini-pro", () => {
expect(isGemini3Model("gemini-pro")).toBe(false);
});
it("returns false for claude-3-opus", () => {
expect(isGemini3Model("claude-3-opus")).toBe(false);
});
it("returns false for empty string", () => {
expect(isGemini3Model("")).toBe(false);
});
});
describe("isGemini25Model", () => {
it("returns true for gemini-2.5-pro", () => {
expect(isGemini25Model("gemini-2.5-pro")).toBe(true);
});
it("returns true for gemini-2.5-flash", () => {
expect(isGemini25Model("gemini-2.5-flash")).toBe(true);
});
it("returns true for gemini-2.5-pro-preview", () => {
expect(isGemini25Model("gemini-2.5-pro-preview")).toBe(true);
});
it("returns true for uppercase GEMINI-2.5-PRO", () => {
expect(isGemini25Model("GEMINI-2.5-PRO")).toBe(true);
});
it("returns false for gemini-3-pro", () => {
expect(isGemini25Model("gemini-3-pro")).toBe(false);
});
it("returns false for gemini-2.0-flash", () => {
expect(isGemini25Model("gemini-2.0-flash")).toBe(false);
});
it("returns false for gemini-pro", () => {
expect(isGemini25Model("gemini-pro")).toBe(false);
});
it("returns false for empty string", () => {
expect(isGemini25Model("")).toBe(false);
});
});
describe("buildGemini3ThinkingConfig", () => {
it("builds config with includeThoughts true and low tier", () => {
const config = buildGemini3ThinkingConfig(true, "low");
expect(config).toEqual({
includeThoughts: true,
thinkingLevel: "low",
});
});
it("builds config with includeThoughts true and medium tier", () => {
const config = buildGemini3ThinkingConfig(true, "medium");
expect(config).toEqual({
includeThoughts: true,
thinkingLevel: "medium",
});
});
it("builds config with includeThoughts true and high tier", () => {
const config = buildGemini3ThinkingConfig(true, "high");
expect(config).toEqual({
includeThoughts: true,
thinkingLevel: "high",
});
});
it("builds config with includeThoughts false", () => {
const config = buildGemini3ThinkingConfig(false, "high");
expect(config).toEqual({
includeThoughts: false,
thinkingLevel: "high",
});
});
});
describe("buildGemini25ThinkingConfig", () => {
it("builds config with includeThoughts true and budget", () => {
const config = buildGemini25ThinkingConfig(true, 8192);
expect(config).toEqual({
includeThoughts: true,
thinkingBudget: 8192,
});
});
it("builds config with includeThoughts false and budget", () => {
const config = buildGemini25ThinkingConfig(false, 16384);
expect(config).toEqual({
includeThoughts: false,
thinkingBudget: 16384,
});
});
it("builds config without budget when undefined", () => {
const config = buildGemini25ThinkingConfig(true, undefined);
expect(config).toEqual({
includeThoughts: true,
});
expect(config).not.toHaveProperty("thinkingBudget");
});
it("builds config without budget when zero", () => {
const config = buildGemini25ThinkingConfig(true, 0);
expect(config).toEqual({
includeThoughts: true,
});
expect(config).not.toHaveProperty("thinkingBudget");
});
it("builds config without budget when negative", () => {
const config = buildGemini25ThinkingConfig(true, -1000);
expect(config).toEqual({
includeThoughts: true,
});
expect(config).not.toHaveProperty("thinkingBudget");
});
it("builds config with large budget", () => {
const config = buildGemini25ThinkingConfig(true, 100000);
expect(config).toEqual({
includeThoughts: true,
thinkingBudget: 100000,
});
});
});
describe("normalizeGeminiTools", () => {
it("returns empty debug info when tools is not an array", () => {
const payload: RequestPayload = { contents: [] };
const result = normalizeGeminiTools(payload);
expect(result).toEqual({
toolDebugMissing: 0,
toolDebugSummaries: [],
});
});
it("returns empty debug info when tools is undefined", () => {
const payload: RequestPayload = { contents: [], tools: undefined };
const result = normalizeGeminiTools(payload);
expect(result).toEqual({
toolDebugMissing: 0,
toolDebugSummaries: [],
});
});
it("normalizes tool with function.input_schema", () => {
const payload: RequestPayload = {
contents: [],
tools: [
{
function: {
name: "test_tool",
description: "A test tool",
input_schema: { type: "object", properties: { foo: { type: "string" } } },
},
},
],
};
const result = normalizeGeminiTools(payload);
expect(result.toolDebugMissing).toBe(0);
expect(result.toolDebugSummaries).toHaveLength(1);
expect((payload.tools as unknown[])[0]).not.toHaveProperty("custom");
});
it("normalizes tool with function.parameters", () => {
const payload: RequestPayload = {
contents: [],
tools: [
{
function: {
name: "test_tool",
description: "A test tool",
parameters: { type: "object", properties: { bar: { type: "number" } } },
},
},
],
};
const result = normalizeGeminiTools(payload);
expect(result.toolDebugMissing).toBe(0);
});
it("creates custom from function and strips it for Gemini", () => {
const payload: RequestPayload = {
contents: [],
tools: [
{
function: {
name: "my_func",
description: "My function",
input_schema: { type: "object" },
},
},
],
};
normalizeGeminiTools(payload);
expect((payload.tools as unknown[])[0]).not.toHaveProperty("custom");
expect((payload.tools as unknown[])[0]).toHaveProperty("function");
});
it("creates custom when both function and custom are missing", () => {
const payload: RequestPayload = {
contents: [],
tools: [
{
name: "standalone_tool",
description: "A standalone tool",
parameters: { type: "object", properties: {} },
},
],
};
normalizeGeminiTools(payload);
expect((payload.tools as unknown[])[0]).not.toHaveProperty("custom");
});
it("counts missing schemas", () => {
const payload: RequestPayload = {
contents: [],
tools: [
{ name: "tool1" },
{ name: "tool2" },
{ function: { name: "tool3", input_schema: { type: "object" } } },
],
};
const result = normalizeGeminiTools(payload);
expect(result.toolDebugMissing).toBe(0);
});
it("generates debug summaries for each tool", () => {
const payload: RequestPayload = {
contents: [],
tools: [
{ function: { name: "t1", input_schema: { type: "object" } } },
{ function: { name: "t2", input_schema: { type: "object" } } },
],
};
const result = normalizeGeminiTools(payload);
expect(result.toolDebugSummaries).toHaveLength(2);
expect(result.toolDebugSummaries[0]).toContain("idx=0");
expect(result.toolDebugSummaries[1]).toContain("idx=1");
});
it("uses default tool name when name is missing", () => {
const payload: RequestPayload = {
contents: [],
tools: [{}],
};
const result = normalizeGeminiTools(payload);
expect(result.toolDebugSummaries[0]).toContain("idx=0");
});
it("extracts schema from custom.input_schema", () => {
const payload: RequestPayload = {
contents: [],
tools: [
{
custom: {
name: "custom_tool",
input_schema: { type: "object", properties: { x: { type: "string" } } },
},
},
],
};
normalizeGeminiTools(payload);
expect((payload.tools as unknown[])[0]).not.toHaveProperty("custom");
});
it("extracts schema from inputSchema (camelCase)", () => {
const payload: RequestPayload = {
contents: [],
tools: [
{
name: "camel_tool",
inputSchema: { type: "object", properties: { y: { type: "boolean" } } },
},
],
};
normalizeGeminiTools(payload);
expect((payload.tools as unknown[])[0]).not.toHaveProperty("custom");
});
});
describe("applyGeminiTransforms", () => {
it("applies Gemini 3 thinking config with thinkingLevel", () => {
const payload: RequestPayload = { contents: [] };
applyGeminiTransforms(payload, {
model: "gemini-3-pro-high",
tierThinkingLevel: "high",
normalizedThinking: { includeThoughts: true },
});
const genConfig = payload.generationConfig as Record<string, unknown>;
expect(genConfig.thinkingConfig).toEqual({
includeThoughts: true,
thinkingLevel: "high",
});
});
it("applies Gemini 2.5 thinking config with thinkingBudget", () => {
const payload: RequestPayload = { contents: [] };
applyGeminiTransforms(payload, {
model: "gemini-2.5-flash",
tierThinkingBudget: 8192,
normalizedThinking: { includeThoughts: true },
});
const genConfig = payload.generationConfig as Record<string, unknown>;
expect(genConfig.thinkingConfig).toEqual({
includeThoughts: true,
thinkingBudget: 8192,
});
});
it("prefers tierThinkingBudget over normalizedThinking.thinkingBudget", () => {
const payload: RequestPayload = { contents: [] };
applyGeminiTransforms(payload, {
model: "gemini-2.5-pro",
tierThinkingBudget: 16384,
normalizedThinking: { includeThoughts: true, thinkingBudget: 8192 },
});
const genConfig = payload.generationConfig as Record<string, unknown>;
expect((genConfig.thinkingConfig as Record<string, unknown>).thinkingBudget).toBe(16384);
});
it("falls back to normalizedThinking.thinkingBudget when tierThinkingBudget is undefined", () => {
const payload: RequestPayload = { contents: [] };
applyGeminiTransforms(payload, {
model: "gemini-2.5-pro",
normalizedThinking: { includeThoughts: true, thinkingBudget: 4096 },
});
const genConfig = payload.generationConfig as Record<string, unknown>;
expect((genConfig.thinkingConfig as Record<string, unknown>).thinkingBudget).toBe(4096);
});
it("does not apply thinking config when normalizedThinking is undefined", () => {
const payload: RequestPayload = { contents: [] };
applyGeminiTransforms(payload, {
model: "gemini-3-pro",
});
expect(payload.generationConfig).toBeUndefined();
});
it("preserves existing generationConfig properties", () => {
const payload: RequestPayload = {
contents: [],
generationConfig: { temperature: 0.7, maxOutputTokens: 1000 },
};
applyGeminiTransforms(payload, {
model: "gemini-3-pro-medium",
tierThinkingLevel: "medium",
normalizedThinking: { includeThoughts: true },
});
const genConfig = payload.generationConfig as Record<string, unknown>;
expect(genConfig.temperature).toBe(0.7);
expect(genConfig.maxOutputTokens).toBe(1000);
expect(genConfig.thinkingConfig).toBeDefined();
});
it("normalizes tools and returns debug info", () => {
const payload: RequestPayload = {
contents: [],
tools: [
{ function: { name: "tool1", input_schema: { type: "object" } } },
{ name: "tool2" },
],
};
const result = applyGeminiTransforms(payload, {
model: "gemini-2.5-flash",
});
expect(result.toolDebugSummaries).toHaveLength(2);
expect(result.toolDebugMissing).toBe(0);
});
it("defaults includeThoughts to true when not specified", () => {
const payload: RequestPayload = { contents: [] };
applyGeminiTransforms(payload, {
model: "gemini-3-pro-low",
tierThinkingLevel: "low",
normalizedThinking: {},
});
const genConfig = payload.generationConfig as Record<string, unknown>;
expect((genConfig.thinkingConfig as Record<string, unknown>).includeThoughts).toBe(true);
});
it("respects includeThoughts false", () => {
const payload: RequestPayload = { contents: [] };
applyGeminiTransforms(payload, {
model: "gemini-3-pro-high",
tierThinkingLevel: "high",
normalizedThinking: { includeThoughts: false },
});
const genConfig = payload.generationConfig as Record<string, unknown>;
expect((genConfig.thinkingConfig as Record<string, unknown>).includeThoughts).toBe(false);
});
it("handles Gemini 2.5 without tierThinkingBudget or normalizedThinking.thinkingBudget", () => {
const payload: RequestPayload = { contents: [] };
applyGeminiTransforms(payload, {
model: "gemini-2.5-pro",
normalizedThinking: { includeThoughts: true },
});
const genConfig = payload.generationConfig as Record<string, unknown>;
const thinkingConfig = genConfig.thinkingConfig as Record<string, unknown>;
expect(thinkingConfig.includeThoughts).toBe(true);
expect(thinkingConfig).not.toHaveProperty("thinkingBudget");
});
});
});