Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import type { GeminiBudgetBody } from './gemini-features.ts'
4import { geminiRequestMap, parseGeminiWire } from './gemini-request.ts'
5
6/**
7 * Slice of the Generative Language discovery document (v1beta, 2026-10-08).
8 * `ThinkingConfig.thinkingLevel` is the uppercase enum. `Content.role`
9 * allows only user and model. Permission.role is a different field.
10 */
11const discovery = {
12 schemas: {
13 ThinkingConfig: {
14 properties: {
15 thinkingLevel: {
16 description: 'Recommended for Gemini 3 or later models.',
17 enum: [
18 'THINKING_LEVEL_UNSPECIFIED',
19 'MINIMAL',
20 'LOW',
21 'MEDIUM',
22 'HIGH',
23 ],
24 },
25 thinkingBudget: {
26 description:
27 'The number of thoughts tokens that the model should generate.',
28 },
29 },
30 },
31 Content: {
32 properties: {
33 role: {
34 description:
35 "Optional. The producer of the content. Must be either 'user' or 'model'. Useful to set for multi-turn conversations, otherwise can be left blank or unset.",
36 },
37 },
38 },
39 Permission: {
40 properties: {
41 role: { description: 'Required. The role granted by this permission.' },
42 },
43 },
44 },
45}
46
47const budget = (off: number | null): GeminiBudgetBody => ({ on: -1, off })
48
49describe('gemini discovery request map', () => {
50 it('reads the thinking enum and the user-or-model role', () => {
51 expect(parseGeminiWire(discovery)).toEqual({
52 thinkingLevels: ['MINIMAL', 'LOW', 'MEDIUM', 'HIGH'],
53 hasThinkingBudget: true,
54 developerRole: false,
55 })
56 })
57
58 it('throws when the document states none of the request-map fields', () => {
59 expect(() => parseGeminiWire({ schemas: {} })).toThrow(
60 'gemini discovery: no request-map fields',
61 )
62 })
63
64 it('maps an effort model onto thinkingLevel HIGH and leaves max tokens unset', () => {
65 const wire = parseGeminiWire(discovery)
66 expect(
67 geminiRequestMap(
68 wire,
69 { mode: 'effort', mandatory: true, efforts: ['low', 'medium', 'high'] },
70 null,
71 ),
72 ).toEqual({
73 thinking: {
74 on: {
75 generationConfig: { thinkingConfig: { thinkingLevel: 'HIGH' } },
76 },
77 off: null,
78 levels: {
79 off: null,
80 minimal: null,
81 low: 'LOW',
82 medium: 'MEDIUM',
83 high: 'HIGH',
84 xhigh: null,
85 max: null,
86 },
87 },
88 maxTokensField: null,
89 developerRole: false,
90 replayReasoningContent: null,
91 store: null,
92 strictTools: null,
93 sessionAffinity: null,
94 cacheControl: null,
95 toolStream: null,
96 reasoningEffort: null,
97 })
98 })
99
100 it('maps a budget model onto the published dynamic and disable numbers', () => {
101 const wire = parseGeminiWire(discovery)
102 expect(
103 geminiRequestMap(wire, { mode: 'budget', mandatory: false }, budget(0))
104 ?.thinking,
105 ).toEqual({
106 on: { generationConfig: { thinkingConfig: { thinkingBudget: -1 } } },
107 off: { generationConfig: { thinkingConfig: { thinkingBudget: 0 } } },
108 levels: null,
109 })
110 expect(
111 geminiRequestMap(wire, { mode: 'budget', mandatory: true }, budget(null))
112 ?.thinking?.off,
113 ).toBeNull()
114 })
115
116 it('stays a map with thinking unset when only the role is verified', () => {
117 const wire = parseGeminiWire(discovery)
118 const map = geminiRequestMap(wire, null, null)
119 expect(map?.developerRole).toBe(false)
120 expect(map?.thinking).toBeNull()
121 expect(map?.maxTokensField).toBeNull()
122 })
123
124 it('returns null when neither the role nor a thinking body is verified', () => {
125 const wire = parseGeminiWire({
126 schemas: {
127 ThinkingConfig: {
128 properties: {
129 thinkingLevel: { enum: ['LOW', 'HIGH'] },
130 },
131 },
132 },
133 })
134 expect(wire.developerRole).toBeNull()
135 expect(
136 geminiRequestMap(
137 wire,
138 { mode: 'effort', mandatory: null, efforts: ['low'] },
139 null,
140 ),
141 ).toBeNull()
142 })
143})