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
1480 lines (1312 loc) · 50.7 KB
/
Copy pathgemini.test.ts
File metadata and controls
1480 lines (1312 loc) · 50.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import {
isGeminiModel,
isGemini3Model,
isGemini25Model,
isImageGenerationModel,
buildGemini3ThinkingConfig,
buildGemini25ThinkingConfig,
buildImageGenerationConfig,
normalizeGeminiTools,
applyGeminiTransforms,
toGeminiSchema,
wrapToolsAsFunctionDeclarations,
} 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 gemini-3.1-pro", () => {
expect(isGemini3Model("gemini-3.1-pro")).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(2);
});
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(1);
});
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");
});
describe("Google Search (Grounding)", () => {
it("injects googleSearch tool when mode is 'auto'", () => {
const payload: RequestPayload = { contents: [], tools: [] };
applyGeminiTransforms(payload, {
model: "gemini-3-pro",
googleSearch: { mode: "auto" },
});
const tools = payload.tools as unknown[];
expect(tools).toHaveLength(1);
expect(tools[0]).toEqual({
googleSearch: {},
});
});
it("ignores threshold value (deprecated in new API)", () => {
const payload: RequestPayload = { contents: [] };
applyGeminiTransforms(payload, {
model: "gemini-3-flash",
googleSearch: { mode: "auto", threshold: 0.7 },
});
const tools = payload.tools as unknown[];
const searchTool = tools[0] as Record<string, unknown>;
// New API uses simple googleSearch: {} without threshold
expect(searchTool).toEqual({ googleSearch: {} });
});
it("works without threshold specified", () => {
const payload: RequestPayload = { contents: [] };
applyGeminiTransforms(payload, {
model: "gemini-3-pro",
googleSearch: { mode: "auto" },
});
const tools = payload.tools as unknown[];
const searchTool = tools[0] as Record<string, unknown>;
expect(searchTool).toEqual({ googleSearch: {} });
});
it("does not inject search tool when mode is 'off'", () => {
const payload: RequestPayload = { contents: [], tools: [] };
applyGeminiTransforms(payload, {
model: "gemini-3-pro",
googleSearch: { mode: "off" },
});
const tools = payload.tools as unknown[];
expect(tools).toHaveLength(0);
});
it("does not inject search tool when googleSearch is undefined", () => {
const payload: RequestPayload = { contents: [], tools: [] };
applyGeminiTransforms(payload, {
model: "gemini-3-pro",
});
const tools = payload.tools as unknown[];
expect(tools).toHaveLength(0);
});
it("appends search tool to existing tools array", () => {
const payload: RequestPayload = {
contents: [],
tools: [
{ function: { name: "existing_tool", input_schema: { type: "object" } } },
],
};
applyGeminiTransforms(payload, {
model: "gemini-3-pro",
googleSearch: { mode: "auto" },
});
const tools = payload.tools as unknown[];
expect(tools).toHaveLength(2);
const lastTool = tools[1] as Record<string, unknown>;
expect(lastTool).toHaveProperty("googleSearch");
});
it("search tool is not normalized (skipped by normalizeGeminiTools)", () => {
const payload: RequestPayload = { contents: [] };
applyGeminiTransforms(payload, {
model: "gemini-3-pro",
googleSearch: { mode: "auto" },
});
const tools = payload.tools as unknown[];
const searchTool = tools[0] as Record<string, unknown>;
expect(searchTool).toHaveProperty("googleSearch");
expect(searchTool).not.toHaveProperty("function");
expect(searchTool).not.toHaveProperty("custom");
});
});
});
describe("isImageGenerationModel", () => {
it("returns true for gemini-3-pro-image", () => {
expect(isImageGenerationModel("gemini-3-pro-image")).toBe(true);
});
it("returns true for gemini-3-pro-image-preview", () => {
expect(isImageGenerationModel("gemini-3-pro-image-preview")).toBe(true);
});
it("returns true for gemini-2.5-flash-image", () => {
expect(isImageGenerationModel("gemini-2.5-flash-image")).toBe(true);
});
it("returns true for imagen-3", () => {
expect(isImageGenerationModel("imagen-3")).toBe(true);
});
it("returns true for uppercase GEMINI-3-PRO-IMAGE", () => {
expect(isImageGenerationModel("GEMINI-3-PRO-IMAGE")).toBe(true);
});
it("returns false for gemini-3-pro", () => {
expect(isImageGenerationModel("gemini-3-pro")).toBe(false);
});
it("returns false for gemini-2.5-flash", () => {
expect(isImageGenerationModel("gemini-2.5-flash")).toBe(false);
});
it("returns false for claude-sonnet-4-6", () => {
expect(isImageGenerationModel("claude-sonnet-4-6")).toBe(false);
});
});
describe("buildImageGenerationConfig", () => {
const originalEnv = process.env;
beforeEach(() => {
// Reset environment before each test
vi.resetModules();
process.env = { ...originalEnv };
});
afterEach(() => {
process.env = originalEnv;
});
it("returns default 1:1 aspect ratio when no env var set", () => {
delete process.env.OPENCODE_IMAGE_ASPECT_RATIO;
const config = buildImageGenerationConfig();
expect(config).toEqual({ aspectRatio: "1:1" });
});
it("uses OPENCODE_IMAGE_ASPECT_RATIO env var when set to valid value", () => {
process.env.OPENCODE_IMAGE_ASPECT_RATIO = "16:9";
const config = buildImageGenerationConfig();
expect(config).toEqual({ aspectRatio: "16:9" });
});
it("accepts all valid aspect ratios", () => {
const validRatios = ["1:1", "2:3", "3:2", "3:4", "4:3", "4:5", "5:4", "9:16", "16:9", "21:9"];
for (const ratio of validRatios) {
process.env.OPENCODE_IMAGE_ASPECT_RATIO = ratio;
const config = buildImageGenerationConfig();
expect(config.aspectRatio).toBe(ratio);
}
});
it("falls back to 1:1 for invalid aspect ratio", () => {
process.env.OPENCODE_IMAGE_ASPECT_RATIO = "invalid";
const config = buildImageGenerationConfig();
expect(config).toEqual({ aspectRatio: "1:1" });
});
it("falls back to 1:1 for unsupported aspect ratio", () => {
process.env.OPENCODE_IMAGE_ASPECT_RATIO = "5:3";
const config = buildImageGenerationConfig();
expect(config).toEqual({ aspectRatio: "1:1" });
});
});
describe("toGeminiSchema", () => {
it("returns null/undefined as-is", () => {
expect(toGeminiSchema(null)).toBe(null);
expect(toGeminiSchema(undefined)).toBe(undefined);
});
it("returns primitives as-is", () => {
expect(toGeminiSchema("string")).toBe("string");
expect(toGeminiSchema(123)).toBe(123);
expect(toGeminiSchema(true)).toBe(true);
});
it("returns arrays as-is", () => {
const arr = [1, 2, 3];
expect(toGeminiSchema(arr)).toBe(arr);
});
it("converts type to uppercase", () => {
expect(toGeminiSchema({ type: "object" })).toEqual({ type: "OBJECT" });
expect(toGeminiSchema({ type: "string" })).toEqual({ type: "STRING" });
expect(toGeminiSchema({ type: "boolean" })).toEqual({ type: "BOOLEAN" });
expect(toGeminiSchema({ type: "number" })).toEqual({ type: "NUMBER" });
expect(toGeminiSchema({ type: "integer" })).toEqual({ type: "INTEGER" });
expect(toGeminiSchema({ type: "array" })).toEqual({ type: "ARRAY", items: { type: "STRING" } });
});
it("removes additionalProperties field", () => {
const schema = {
type: "object",
properties: { foo: { type: "string" } },
additionalProperties: false,
};
const result = toGeminiSchema(schema) as Record<string, unknown>;
expect(result).not.toHaveProperty("additionalProperties");
expect(result.type).toBe("OBJECT");
});
it("removes $schema field", () => {
const schema = {
$schema: "http://json-schema.org/draft-07/schema#",
type: "object",
};
const result = toGeminiSchema(schema) as Record<string, unknown>;
expect(result).not.toHaveProperty("$schema");
expect(result.type).toBe("OBJECT");
});
it("removes $id and $comment fields", () => {
const schema = {
$id: "my-schema",
$comment: "This is a comment",
type: "object",
};
const result = toGeminiSchema(schema) as Record<string, unknown>;
expect(result).not.toHaveProperty("$id");
expect(result).not.toHaveProperty("$comment");
expect(result.type).toBe("OBJECT");
});
it("recursively transforms properties", () => {
const schema = {
type: "object",
properties: {
name: { type: "string" },
age: { type: "number" },
active: { type: "boolean" },
},
};
const result = toGeminiSchema(schema) as Record<string, unknown>;
const props = result.properties as Record<string, Record<string, string>>;
expect(props["name"]!.type).toBe("STRING");
expect(props["age"]!.type).toBe("NUMBER");
expect(props["active"]!.type).toBe("BOOLEAN");
});
it("transforms nested objects recursively", () => {
const schema = {
type: "object",
properties: {
user: {
type: "object",
properties: {
email: { type: "string" },
},
additionalProperties: false,
},
},
};
const result = toGeminiSchema(schema) as Record<string, unknown>;
const props = result.properties as Record<string, Record<string, unknown>>;
expect(props["user"]!.type).toBe("OBJECT");
expect(props["user"]).not.toHaveProperty("additionalProperties");
const userProps = props["user"]!.properties as Record<string, Record<string, string>>;
expect(userProps["email"]!.type).toBe("STRING");
});
it("transforms array items schema", () => {
const schema = {
type: "array",
items: {
type: "object",
properties: {
id: { type: "number" },
},
},
};
const result = toGeminiSchema(schema) as Record<string, unknown>;
expect(result.type).toBe("ARRAY");
const items = result.items as Record<string, unknown>;
expect(items.type).toBe("OBJECT");
const itemProps = items.properties as Record<string, Record<string, string>>;
expect(itemProps["id"]!.type).toBe("NUMBER");
});
it("transforms anyOf schemas", () => {
const schema = {
anyOf: [
{ type: "string" },
{ type: "number" },
],
};
const result = toGeminiSchema(schema) as Record<string, unknown>;
const anyOf = result.anyOf as Array<Record<string, string>>;
expect(anyOf[0]!.type).toBe("STRING");
expect(anyOf[1]!.type).toBe("NUMBER");
});
it("transforms oneOf schemas", () => {
const schema = {
oneOf: [
{ type: "boolean" },
{ type: "string" },
],
};
const result = toGeminiSchema(schema) as Record<string, unknown>;
const oneOf = result.oneOf as Array<Record<string, string>>;
expect(oneOf[0]!.type).toBe("BOOLEAN");
expect(oneOf[1]!.type).toBe("STRING");
});
it("transforms allOf schemas", () => {
const schema = {
allOf: [
{ type: "object", properties: { a: { type: "string" } } },
{ properties: { b: { type: "number" } } },
],
};
const result = toGeminiSchema(schema) as Record<string, unknown>;
const allOf = result.allOf as Array<Record<string, unknown>>;
expect(allOf[0]!.type).toBe("OBJECT");
const props0 = allOf[0]!.properties as Record<string, Record<string, string>>;
expect(props0["a"]!.type).toBe("STRING");
const props1 = allOf[1]!.properties as Record<string, Record<string, string>>;
expect(props1["b"]!.type).toBe("NUMBER");
});
it("preserves enum values", () => {
const schema = {
type: "string",
enum: ["low", "medium", "high"],
};
const result = toGeminiSchema(schema) as Record<string, unknown>;
expect(result.type).toBe("STRING");
expect(result.enum).toEqual(["low", "medium", "high"]);
});
it("preserves required array when all properties exist", () => {
const schema = {
type: "object",
properties: {
name: { type: "string" },
},
required: ["name"],
};
const result = toGeminiSchema(schema) as Record<string, unknown>;
expect(result.required).toEqual(["name"]);
});
it("filters required array to only include existing properties", () => {
// This fixes: "parameters.required[X]: property is not defined"
const schema = {
type: "object",
properties: {
name: { type: "string" },
age: { type: "number" },
},
required: ["name", "nonexistent", "age", "alsoMissing"],
};
const result = toGeminiSchema(schema) as Record<string, unknown>;
expect(result.required).toEqual(["name", "age"]);
});
it("omits required field when no valid properties remain", () => {
const schema = {
type: "object",
properties: {
name: { type: "string" },
},
required: ["nonexistent", "alsoMissing"],
};
const result = toGeminiSchema(schema) as Record<string, unknown>;
expect(result).not.toHaveProperty("required");
});
it("handles MCP tool with missing properties in required (issue #161)", () => {
// Simulates the group_execute_tool schema from issue #161
const schema = {
type: "object",
properties: {
mcp_name: { type: "string", enum: ["exa-mcp-server", "context7"] },
tool_name: { type: "string" },
// Note: "arguments" is missing from properties but present in required
},
required: ["mcp_name", "tool_name", "arguments"],
};
const result = toGeminiSchema(schema) as Record<string, unknown>;
// Should filter out "arguments" since it doesn't exist in properties
expect(result.required).toEqual(["mcp_name", "tool_name"]);
expect(result.type).toBe("OBJECT");
});
it("preserves description", () => {
const schema = {
type: "string",
description: "User's full name",
};
const result = toGeminiSchema(schema) as Record<string, unknown>;
expect(result.description).toBe("User's full name");
});
it("preserves default value", () => {
const schema = {
type: "number",
default: 42,
};
const result = toGeminiSchema(schema) as Record<string, unknown>;
expect(result.default).toBe(42);
});
it("handles complex real-world MCP schema", () => {
// Simulates a PostHog-like complex schema with enums and nested types
const schema = {
$schema: "http://json-schema.org/draft-07/schema#",
type: "object",
properties: {
event_name: {
type: "string",
description: "Event name to track",
},
properties: {
type: "object",
additionalProperties: true,
description: "Event properties",
},
level: {
type: "string",
enum: ["info", "warning", "error"],
},
items: {
type: "array",
items: {
type: "object",
properties: {
id: { type: "string" },
value: { type: "number" },
},
additionalProperties: false,
},
},
},
required: ["event_name"],
additionalProperties: false,
};
const result = toGeminiSchema(schema) as Record<string, unknown>;
// Should remove unsupported fields
expect(result).not.toHaveProperty("$schema");
expect(result).not.toHaveProperty("additionalProperties");
// Should uppercase types
expect(result.type).toBe("OBJECT");
const props = result.properties as Record<string, Record<string, unknown>>;
expect(props["event_name"]!.type).toBe("STRING");
expect(props["properties"]!.type).toBe("OBJECT");
expect(props["properties"]).not.toHaveProperty("additionalProperties");
expect(props["level"]!.type).toBe("STRING");
expect(props["level"]!.enum).toEqual(["info", "warning", "error"]);
expect(props["items"]!.type).toBe("ARRAY");
const itemsSchema = props["items"]!.items as Record<string, unknown>;
expect(itemsSchema.type).toBe("OBJECT");
expect(itemsSchema).not.toHaveProperty("additionalProperties");
const itemProps = itemsSchema.properties as Record<string, Record<string, string>>;
expect(itemProps["id"]!.type).toBe("STRING");
expect(itemProps["value"]!.type).toBe("NUMBER");
// Should preserve required
expect(result.required).toEqual(["event_name"]);
});
});
describe("normalizeGeminiTools schema transformation", () => {
it("transforms tool schemas to Gemini format with uppercase types", () => {
const payload: RequestPayload = {
contents: [],
tools: [
{
function: {
name: "test_tool",
description: "A test tool",
input_schema: {
type: "object",
properties: {
name: { type: "string" },
count: { type: "number" },
},
},
},
},
],
};
normalizeGeminiTools(payload);
const tool = (payload.tools as unknown[])[0] as Record<string, unknown>;
const func = tool.function as Record<string, unknown>;
const schema = func.input_schema as Record<string, unknown>;
expect(schema.type).toBe("OBJECT");
const props = schema.properties as Record<string, Record<string, string>>;
expect(props["name"]!.type).toBe("STRING");
expect(props["count"]!.type).toBe("NUMBER");
});