forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclaude.test.ts
More file actions
798 lines (649 loc) · 25.7 KB
/
Copy pathclaude.test.ts
File metadata and controls
798 lines (649 loc) · 25.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
import { describe, it, expect } from "vitest";
import {
isClaudeModel,
isClaudeThinkingModel,
configureClaudeToolConfig,
buildClaudeThinkingConfig,
ensureClaudeMaxOutputTokens,
appendClaudeThinkingHint,
normalizeClaudeTools,
applyClaudeTransforms,
CLAUDE_THINKING_MAX_OUTPUT_TOKENS,
CLAUDE_INTERLEAVED_THINKING_HINT,
} from "./claude";
import type { RequestPayload } from "./types";
describe("isClaudeModel", () => {
it("returns true for claude model names", () => {
expect(isClaudeModel("claude-sonnet-4-5")).toBe(true);
expect(isClaudeModel("claude-opus-4-5")).toBe(true);
expect(isClaudeModel("claude-3-opus")).toBe(true);
expect(isClaudeModel("claude-3-5-sonnet")).toBe(true);
});
it("returns true for case-insensitive matches", () => {
expect(isClaudeModel("CLAUDE-SONNET-4-5")).toBe(true);
expect(isClaudeModel("Claude-Opus-4-5")).toBe(true);
expect(isClaudeModel("cLaUdE-3-opus")).toBe(true);
});
it("returns true for prefixed claude models", () => {
expect(isClaudeModel("antigravity-claude-sonnet-4-5")).toBe(true);
expect(isClaudeModel("google/claude-opus-4-5")).toBe(true);
});
it("returns false for non-claude models", () => {
expect(isClaudeModel("gemini-3-pro")).toBe(false);
expect(isClaudeModel("gpt-4")).toBe(false);
expect(isClaudeModel("llama-3")).toBe(false);
expect(isClaudeModel("")).toBe(false);
});
it("returns false for similar but non-claude names", () => {
expect(isClaudeModel("claudia-model")).toBe(false);
expect(isClaudeModel("clade-model")).toBe(false);
});
});
describe("isClaudeThinkingModel", () => {
it("returns true for claude thinking models", () => {
expect(isClaudeThinkingModel("claude-sonnet-4-5-thinking")).toBe(true);
expect(isClaudeThinkingModel("claude-opus-4-5-thinking")).toBe(true);
expect(isClaudeThinkingModel("claude-sonnet-4-5-thinking-high")).toBe(true);
expect(isClaudeThinkingModel("claude-opus-4-5-thinking-low")).toBe(true);
});
it("returns true for case-insensitive matches", () => {
expect(isClaudeThinkingModel("CLAUDE-SONNET-4-5-THINKING")).toBe(true);
expect(isClaudeThinkingModel("Claude-Opus-4-5-Thinking")).toBe(true);
});
it("returns true for prefixed thinking models", () => {
expect(isClaudeThinkingModel("antigravity-claude-sonnet-4-5-thinking")).toBe(true);
expect(isClaudeThinkingModel("google/claude-opus-4-5-thinking-high")).toBe(true);
});
it("returns false for non-thinking claude models", () => {
expect(isClaudeThinkingModel("claude-sonnet-4-5")).toBe(false);
expect(isClaudeThinkingModel("claude-opus-4-5")).toBe(false);
expect(isClaudeThinkingModel("claude-3-opus")).toBe(false);
});
it("returns false for non-claude models", () => {
expect(isClaudeThinkingModel("gemini-3-pro-thinking")).toBe(false);
expect(isClaudeThinkingModel("gpt-4-thinking")).toBe(false);
});
it("requires both claude and thinking keywords", () => {
expect(isClaudeThinkingModel("thinking-model")).toBe(false);
expect(isClaudeThinkingModel("claude-model")).toBe(false);
});
});
describe("configureClaudeToolConfig", () => {
it("creates toolConfig if not present", () => {
const payload: RequestPayload = {};
configureClaudeToolConfig(payload);
expect(payload.toolConfig).toBeDefined();
expect((payload.toolConfig as any).functionCallingConfig).toBeDefined();
expect((payload.toolConfig as any).functionCallingConfig.mode).toBe("VALIDATED");
});
it("adds functionCallingConfig to existing toolConfig", () => {
const payload: RequestPayload = {
toolConfig: { someOtherConfig: true },
};
configureClaudeToolConfig(payload);
expect((payload.toolConfig as any).someOtherConfig).toBe(true);
expect((payload.toolConfig as any).functionCallingConfig.mode).toBe("VALIDATED");
});
it("sets mode to VALIDATED on existing functionCallingConfig", () => {
const payload: RequestPayload = {
toolConfig: {
functionCallingConfig: { existingKey: "value" },
},
};
configureClaudeToolConfig(payload);
expect((payload.toolConfig as any).functionCallingConfig.existingKey).toBe("value");
expect((payload.toolConfig as any).functionCallingConfig.mode).toBe("VALIDATED");
});
it("overwrites existing mode", () => {
const payload: RequestPayload = {
toolConfig: {
functionCallingConfig: { mode: "AUTO" },
},
};
configureClaudeToolConfig(payload);
expect((payload.toolConfig as any).functionCallingConfig.mode).toBe("VALIDATED");
});
it("handles null toolConfig gracefully", () => {
const payload: RequestPayload = { toolConfig: null };
configureClaudeToolConfig(payload);
expect(payload.toolConfig).toBeDefined();
});
});
describe("buildClaudeThinkingConfig", () => {
it("builds config with include_thoughts only", () => {
const config = buildClaudeThinkingConfig(true);
expect(config).toEqual({ include_thoughts: true });
});
it("builds config with include_thoughts false", () => {
const config = buildClaudeThinkingConfig(false);
expect(config).toEqual({ include_thoughts: false });
});
it("includes thinking_budget when provided and positive", () => {
const config = buildClaudeThinkingConfig(true, 8192);
expect(config).toEqual({
include_thoughts: true,
thinking_budget: 8192,
});
});
it("excludes thinking_budget when zero", () => {
const config = buildClaudeThinkingConfig(true, 0);
expect(config).toEqual({ include_thoughts: true });
});
it("excludes thinking_budget when negative", () => {
const config = buildClaudeThinkingConfig(true, -100);
expect(config).toEqual({ include_thoughts: true });
});
it("excludes thinking_budget when undefined", () => {
const config = buildClaudeThinkingConfig(true, undefined);
expect(config).toEqual({ include_thoughts: true });
});
it("handles various budget values", () => {
expect(buildClaudeThinkingConfig(true, 8192)).toHaveProperty("thinking_budget", 8192);
expect(buildClaudeThinkingConfig(true, 16384)).toHaveProperty("thinking_budget", 16384);
expect(buildClaudeThinkingConfig(true, 32768)).toHaveProperty("thinking_budget", 32768);
});
});
describe("ensureClaudeMaxOutputTokens", () => {
it("sets maxOutputTokens when not present", () => {
const config: Record<string, unknown> = {};
ensureClaudeMaxOutputTokens(config, 8192);
expect(config.maxOutputTokens).toBe(CLAUDE_THINKING_MAX_OUTPUT_TOKENS);
});
it("sets maxOutputTokens when current is less than budget", () => {
const config: Record<string, unknown> = { maxOutputTokens: 4096 };
ensureClaudeMaxOutputTokens(config, 8192);
expect(config.maxOutputTokens).toBe(CLAUDE_THINKING_MAX_OUTPUT_TOKENS);
});
it("sets maxOutputTokens when current equals budget", () => {
const config: Record<string, unknown> = { maxOutputTokens: 8192 };
ensureClaudeMaxOutputTokens(config, 8192);
expect(config.maxOutputTokens).toBe(CLAUDE_THINKING_MAX_OUTPUT_TOKENS);
});
it("does not change maxOutputTokens when current is greater than budget", () => {
const config: Record<string, unknown> = { maxOutputTokens: 100000 };
ensureClaudeMaxOutputTokens(config, 8192);
expect(config.maxOutputTokens).toBe(100000);
});
it("handles snake_case max_output_tokens", () => {
const config: Record<string, unknown> = { max_output_tokens: 4096 };
ensureClaudeMaxOutputTokens(config, 8192);
expect(config.maxOutputTokens).toBe(CLAUDE_THINKING_MAX_OUTPUT_TOKENS);
expect(config.max_output_tokens).toBeUndefined();
});
it("removes max_output_tokens when setting maxOutputTokens", () => {
const config: Record<string, unknown> = {
max_output_tokens: 4096,
maxOutputTokens: 4096,
};
ensureClaudeMaxOutputTokens(config, 8192);
expect(config.maxOutputTokens).toBe(CLAUDE_THINKING_MAX_OUTPUT_TOKENS);
expect(config.max_output_tokens).toBeUndefined();
});
it("prefers maxOutputTokens over max_output_tokens for comparison", () => {
const config: Record<string, unknown> = {
maxOutputTokens: 100000,
max_output_tokens: 4096,
};
ensureClaudeMaxOutputTokens(config, 8192);
expect(config.maxOutputTokens).toBe(100000);
});
});
describe("appendClaudeThinkingHint", () => {
describe("with string systemInstruction", () => {
it("appends hint to existing string instruction", () => {
const payload: RequestPayload = {
systemInstruction: "You are a helpful assistant.",
};
appendClaudeThinkingHint(payload);
expect(payload.systemInstruction).toBe(
`You are a helpful assistant.\n\n${CLAUDE_INTERLEAVED_THINKING_HINT}`
);
});
it("uses hint alone when existing instruction is empty", () => {
const payload: RequestPayload = {
systemInstruction: "",
};
appendClaudeThinkingHint(payload);
expect(payload.systemInstruction).toBe(CLAUDE_INTERLEAVED_THINKING_HINT);
});
it("uses hint alone when existing instruction is whitespace", () => {
const payload: RequestPayload = {
systemInstruction: " ",
};
appendClaudeThinkingHint(payload);
expect(payload.systemInstruction).toBe(CLAUDE_INTERLEAVED_THINKING_HINT);
});
it("accepts custom hint", () => {
const payload: RequestPayload = {
systemInstruction: "Base instruction.",
};
appendClaudeThinkingHint(payload, "Custom hint.");
expect(payload.systemInstruction).toBe("Base instruction.\n\nCustom hint.");
});
});
describe("with object systemInstruction (parts array)", () => {
it("appends hint to last text part", () => {
const payload: RequestPayload = {
systemInstruction: {
parts: [{ text: "First part." }, { text: "Last part." }],
},
};
appendClaudeThinkingHint(payload);
const sys = payload.systemInstruction as any;
expect(sys.parts[0].text).toBe("First part.");
expect(sys.parts[1].text).toBe(`Last part.\n\n${CLAUDE_INTERLEAVED_THINKING_HINT}`);
});
it("appends hint to single text part", () => {
const payload: RequestPayload = {
systemInstruction: {
parts: [{ text: "Only part." }],
},
};
appendClaudeThinkingHint(payload);
const sys = payload.systemInstruction as any;
expect(sys.parts[0].text).toBe(`Only part.\n\n${CLAUDE_INTERLEAVED_THINKING_HINT}`);
});
it("creates new text part when no text parts exist", () => {
const payload: RequestPayload = {
systemInstruction: {
parts: [{ image: "base64data" }],
},
};
appendClaudeThinkingHint(payload);
const sys = payload.systemInstruction as any;
expect(sys.parts).toHaveLength(2);
expect(sys.parts[1].text).toBe(CLAUDE_INTERLEAVED_THINKING_HINT);
});
it("creates parts array when not present", () => {
const payload: RequestPayload = {
systemInstruction: { role: "system" },
};
appendClaudeThinkingHint(payload);
const sys = payload.systemInstruction as any;
expect(sys.parts).toEqual([{ text: CLAUDE_INTERLEAVED_THINKING_HINT }]);
});
});
describe("with no systemInstruction", () => {
it("creates systemInstruction when contents array exists", () => {
const payload: RequestPayload = {
contents: [{ role: "user", parts: [{ text: "Hello" }] }],
};
appendClaudeThinkingHint(payload);
expect(payload.systemInstruction).toEqual({
parts: [{ text: CLAUDE_INTERLEAVED_THINKING_HINT }],
});
});
it("does not create systemInstruction when no contents", () => {
const payload: RequestPayload = {};
appendClaudeThinkingHint(payload);
expect(payload.systemInstruction).toBeUndefined();
});
});
});
describe("normalizeClaudeTools", () => {
const identityClean = (schema: unknown) => schema as Record<string, unknown>;
const realClean = (schema: unknown): Record<string, unknown> => {
if (!schema || typeof schema !== "object") return {};
const cleaned = { ...schema as Record<string, unknown> };
delete cleaned.$schema;
delete cleaned.$id;
return cleaned;
};
it("returns empty result when no tools", () => {
const payload: RequestPayload = {};
const result = normalizeClaudeTools(payload, identityClean);
expect(result.toolDebugMissing).toBe(0);
expect(result.toolDebugSummaries).toEqual([]);
});
it("returns empty result when tools is not an array", () => {
const payload: RequestPayload = { tools: "not an array" };
const result = normalizeClaudeTools(payload, identityClean);
expect(result.toolDebugMissing).toBe(0);
expect(result.toolDebugSummaries).toEqual([]);
});
describe("functionDeclarations format", () => {
it("normalizes tools with functionDeclarations array", () => {
const payload: RequestPayload = {
tools: [{
functionDeclarations: [{
name: "get_weather",
description: "Get weather for a location",
parameters: {
type: "object",
properties: {
location: { type: "string" },
},
required: ["location"],
},
}],
}],
};
const result = normalizeClaudeTools(payload, identityClean);
expect(result.toolDebugMissing).toBe(0);
expect(result.toolDebugSummaries).toContain("decl=get_weather,src=functionDeclarations,hasSchema=y");
const tools = payload.tools as any[];
expect(tools).toHaveLength(1);
expect(tools[0].functionDeclarations).toHaveLength(1);
expect(tools[0].functionDeclarations[0].name).toBe("get_weather");
});
it("handles multiple functionDeclarations", () => {
const payload: RequestPayload = {
tools: [{
functionDeclarations: [
{ name: "tool1", description: "First tool" },
{ name: "tool2", description: "Second tool" },
],
}],
};
normalizeClaudeTools(payload, identityClean);
const tools = payload.tools as any[];
expect(tools[0].functionDeclarations).toHaveLength(2);
});
});
describe("function/custom format", () => {
it("normalizes OpenAI-style function tools", () => {
const payload: RequestPayload = {
tools: [{
type: "function",
function: {
name: "search",
description: "Search the web",
parameters: {
type: "object",
properties: {
query: { type: "string" },
},
},
},
}],
};
const result = normalizeClaudeTools(payload, identityClean);
expect(result.toolDebugSummaries).toContain("decl=search,src=function/custom,hasSchema=y");
const tools = payload.tools as any[];
expect(tools[0].functionDeclarations[0].name).toBe("search");
});
it("normalizes custom-style tools", () => {
const payload: RequestPayload = {
tools: [{
custom: {
name: "custom_tool",
description: "A custom tool",
input_schema: {
type: "object",
properties: { arg: { type: "string" } },
},
},
}],
};
const result = normalizeClaudeTools(payload, identityClean);
expect(result.toolDebugSummaries).toContain("decl=custom_tool,src=function/custom,hasSchema=y");
});
it("normalizes tools with top-level name/parameters", () => {
const payload: RequestPayload = {
tools: [{
name: "direct_tool",
description: "Direct definition",
parameters: {
type: "object",
properties: { value: { type: "number" } },
},
}],
};
normalizeClaudeTools(payload, identityClean);
const tools = payload.tools as any[];
expect(tools[0].functionDeclarations[0].name).toBe("direct_tool");
});
});
describe("schema normalization", () => {
it("adds placeholder when schema is missing", () => {
const payload: RequestPayload = {
tools: [{
function: {
name: "no_schema_tool",
description: "Tool without schema",
},
}],
};
const result = normalizeClaudeTools(payload, identityClean);
expect(result.toolDebugMissing).toBe(1);
const tools = payload.tools as any[];
const params = tools[0].functionDeclarations[0].parameters;
expect(params.type).toBe("object");
expect(params.properties._placeholder).toBeDefined();
expect(params.required).toContain("_placeholder");
});
it("adds placeholder when schema has no properties", () => {
const payload: RequestPayload = {
tools: [{
function: {
name: "empty_schema_tool",
parameters: { type: "object" },
},
}],
};
normalizeClaudeTools(payload, identityClean);
const tools = payload.tools as any[];
const params = tools[0].functionDeclarations[0].parameters;
expect(params.properties._placeholder).toBeDefined();
});
it("preserves existing properties", () => {
const payload: RequestPayload = {
tools: [{
function: {
name: "has_props_tool",
parameters: {
type: "object",
properties: {
existingProp: { type: "string" },
},
},
},
}],
};
normalizeClaudeTools(payload, identityClean);
const tools = payload.tools as any[];
const params = tools[0].functionDeclarations[0].parameters;
expect(params.properties.existingProp).toBeDefined();
expect(params.properties._placeholder).toBeUndefined();
});
it("cleans schema using provided function", () => {
const payload: RequestPayload = {
tools: [{
function: {
name: "needs_cleaning",
parameters: {
$schema: "http://json-schema.org/draft-07/schema#",
type: "object",
properties: { arg: { type: "string" } },
},
},
}],
};
normalizeClaudeTools(payload, realClean);
const tools = payload.tools as any[];
const params = tools[0].functionDeclarations[0].parameters;
expect(params.$schema).toBeUndefined();
expect(params.properties.arg).toBeDefined();
});
});
describe("tool name sanitization", () => {
it("removes special characters from tool names", () => {
const payload: RequestPayload = {
tools: [{
function: {
name: "tool@with#special$chars!",
parameters: { type: "object", properties: { x: { type: "string" } } },
},
}],
};
normalizeClaudeTools(payload, identityClean);
const tools = payload.tools as any[];
expect(tools[0].functionDeclarations[0].name).toBe("tool_with_special_chars_");
});
it("truncates long tool names to 64 characters", () => {
const longName = "a".repeat(100);
const payload: RequestPayload = {
tools: [{
function: {
name: longName,
parameters: { type: "object", properties: { x: { type: "string" } } },
},
}],
};
normalizeClaudeTools(payload, identityClean);
const tools = payload.tools as any[];
expect(tools[0].functionDeclarations[0].name).toHaveLength(64);
});
it("generates name when missing", () => {
const payload: RequestPayload = {
tools: [{
function: {
description: "Nameless tool",
parameters: { type: "object", properties: { x: { type: "string" } } },
},
}],
};
normalizeClaudeTools(payload, identityClean);
const tools = payload.tools as any[];
expect(tools[0].functionDeclarations[0].name).toBe("tool-0");
});
});
describe("passthrough tools", () => {
it("preserves non-function tools like codeExecution", () => {
const payload: RequestPayload = {
tools: [
{ codeExecution: {} },
{
function: {
name: "regular_tool",
parameters: { type: "object", properties: { x: { type: "string" } } },
},
},
],
};
normalizeClaudeTools(payload, identityClean);
const tools = payload.tools as any[];
expect(tools).toHaveLength(2);
expect(tools[0].functionDeclarations).toBeDefined();
expect(tools[1].codeExecution).toBeDefined();
});
});
});
describe("applyClaudeTransforms", () => {
const mockCleanJSONSchema = (schema: unknown) => schema as Record<string, unknown>;
it("applies tool config for all Claude models", () => {
const payload: RequestPayload = {};
applyClaudeTransforms(payload, {
model: "claude-sonnet-4-6",
cleanJSONSchema: mockCleanJSONSchema,
});
expect((payload.toolConfig as any)?.functionCallingConfig?.mode).toBe("VALIDATED");
});
it("applies thinking config for thinking models", () => {
const payload: RequestPayload = {};
applyClaudeTransforms(payload, {
model: "claude-opus-4-6-thinking",
normalizedThinking: { includeThoughts: true, thinkingBudget: 8192 },
cleanJSONSchema: mockCleanJSONSchema,
});
const genConfig = payload.generationConfig as any;
expect(genConfig.thinkingConfig.include_thoughts).toBe(true);
expect(genConfig.thinkingConfig.thinking_budget).toBe(8192);
});
it("uses tierThinkingBudget over normalizedThinking.thinkingBudget", () => {
const payload: RequestPayload = {};
applyClaudeTransforms(payload, {
model: "claude-opus-4-6-thinking",
tierThinkingBudget: 32768,
normalizedThinking: { includeThoughts: true, thinkingBudget: 8192 },
cleanJSONSchema: mockCleanJSONSchema,
});
const genConfig = payload.generationConfig as any;
expect(genConfig.thinkingConfig.thinking_budget).toBe(32768);
});
it("ensures maxOutputTokens for thinking models with budget", () => {
const payload: RequestPayload = {
generationConfig: { maxOutputTokens: 4096 },
};
applyClaudeTransforms(payload, {
model: "claude-opus-4-6-thinking",
normalizedThinking: { includeThoughts: true, thinkingBudget: 8192 },
cleanJSONSchema: mockCleanJSONSchema,
});
const genConfig = payload.generationConfig as any;
expect(genConfig.maxOutputTokens).toBe(CLAUDE_THINKING_MAX_OUTPUT_TOKENS);
});
it("does not apply thinking config for non-thinking models", () => {
const payload: RequestPayload = {};
applyClaudeTransforms(payload, {
model: "claude-sonnet-4-6",
normalizedThinking: { includeThoughts: true, thinkingBudget: 8192 },
cleanJSONSchema: mockCleanJSONSchema,
});
const genConfig = payload.generationConfig as any;
expect(genConfig?.thinkingConfig).toBeUndefined();
});
it("appends thinking hint for thinking models with tools", () => {
const payload: RequestPayload = {
systemInstruction: "You are helpful.",
tools: [{ function: { name: "test", parameters: { type: "object", properties: { x: { type: "string" } } } } }],
};
applyClaudeTransforms(payload, {
model: "claude-opus-4-6-thinking",
cleanJSONSchema: mockCleanJSONSchema,
});
expect((payload.systemInstruction as string)).toContain(CLAUDE_INTERLEAVED_THINKING_HINT);
});
it("does not append thinking hint for thinking models without tools", () => {
const payload: RequestPayload = {
systemInstruction: "You are helpful.",
};
applyClaudeTransforms(payload, {
model: "claude-opus-4-6-thinking",
cleanJSONSchema: mockCleanJSONSchema,
});
expect((payload.systemInstruction as string)).not.toContain(CLAUDE_INTERLEAVED_THINKING_HINT);
});
it("does not append thinking hint for non-thinking models with tools", () => {
const payload: RequestPayload = {
systemInstruction: "You are helpful.",
tools: [{ function: { name: "test", parameters: { type: "object", properties: { x: { type: "string" } } } } }],
};
applyClaudeTransforms(payload, {
model: "claude-sonnet-4-6",
cleanJSONSchema: mockCleanJSONSchema,
});
expect((payload.systemInstruction as string)).not.toContain(CLAUDE_INTERLEAVED_THINKING_HINT);
});
it("normalizes tools and returns debug info", () => {
const payload: RequestPayload = {
tools: [{ function: { name: "my_tool" } }],
};
const result = applyClaudeTransforms(payload, {
model: "claude-sonnet-4-6",
cleanJSONSchema: mockCleanJSONSchema,
});
expect(result.toolDebugMissing).toBe(1);
expect(result.toolDebugSummaries).toContain("decl=my_tool,src=function/custom,hasSchema=n");
});
it("converts stop_sequences in generationConfig", () => {
const payload: RequestPayload = {
generationConfig: { stop_sequences: ["END"] },
};
applyClaudeTransforms(payload, {
model: "claude-sonnet-4-6",
cleanJSONSchema: mockCleanJSONSchema,
});
const genConfig = payload.generationConfig as any;
expect(genConfig.stopSequences).toEqual(["END"]);
expect(genConfig.stop_sequences).toBeUndefined();
});
});
describe("constants", () => {
it("exports CLAUDE_THINKING_MAX_OUTPUT_TOKENS", () => {
expect(CLAUDE_THINKING_MAX_OUTPUT_TOKENS).toBe(64_000);
});
it("exports CLAUDE_INTERLEAVED_THINKING_HINT", () => {
expect(CLAUDE_INTERLEAVED_THINKING_HINT).toContain("Interleaved thinking is enabled");
});
});