|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | +) |
| 8 | + |
| 9 | +func newAntigravityAccountWithMapping(mapping map[string]string) *Account { |
| 10 | + mappingAny := make(map[string]any, len(mapping)) |
| 11 | + for k, v := range mapping { |
| 12 | + mappingAny[k] = v |
| 13 | + } |
| 14 | + return &Account{ |
| 15 | + ID: 1, |
| 16 | + Name: "ag", |
| 17 | + Platform: PlatformAntigravity, |
| 18 | + Credentials: map[string]any{ |
| 19 | + "model_mapping": mappingAny, |
| 20 | + }, |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +func TestGeminiThinkingLevelFromBody(t *testing.T) { |
| 25 | + tests := []struct { |
| 26 | + name string |
| 27 | + body string |
| 28 | + want string |
| 29 | + }{ |
| 30 | + {"empty body", ``, "high"}, |
| 31 | + {"no thinkingConfig", `{"contents":[]}`, "high"}, |
| 32 | + {"budget -1 dynamic", `{"generationConfig":{"thinkingConfig":{"thinkingBudget":-1}}}`, "high"}, |
| 33 | + {"budget 0 off", `{"generationConfig":{"thinkingConfig":{"thinkingBudget":0}}}`, "low"}, |
| 34 | + {"budget 1000 (agy low)", `{"generationConfig":{"thinkingConfig":{"thinkingBudget":1000}}}`, "low"}, |
| 35 | + {"budget 4000 (agy medium)", `{"generationConfig":{"thinkingConfig":{"thinkingBudget":4000}}}`, "medium"}, |
| 36 | + {"budget 24576", `{"generationConfig":{"thinkingConfig":{"thinkingBudget":24576}}}`, "high"}, |
| 37 | + {"thinkingLevel wins over budget", `{"generationConfig":{"thinkingConfig":{"thinkingBudget":24576,"thinkingLevel":"low"}}}`, "low"}, |
| 38 | + {"thinkingLevel medium", `{"generationConfig":{"thinkingConfig":{"thinkingLevel":"MEDIUM"}}}`, "medium"}, |
| 39 | + {"invalid json", `{"generationConfig":`, "high"}, |
| 40 | + } |
| 41 | + for _, tt := range tests { |
| 42 | + t.Run(tt.name, func(t *testing.T) { |
| 43 | + require.Equal(t, tt.want, geminiThinkingLevelFromBody([]byte(tt.body))) |
| 44 | + }) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestResolveGeminiThinkingVariant(t *testing.T) { |
| 49 | + // 与生产 Antigravity 账号从上游同步来的目录一致:只有带后缀的变体,没有裸名。 |
| 50 | + catalog := map[string]string{ |
| 51 | + "gemini-3.8-flash-low": "gemini-3.8-flash-low", |
| 52 | + "gemini-3.8-flash-medium": "gemini-3.8-flash-medium", |
| 53 | + "gemini-3.8-flash-high": "gemini-3.8-flash-high", |
| 54 | + "gemini-3.8-flash-tiered": "gemini-3.8-flash-tiered", |
| 55 | + "gemini-3.6-flash-high": "gemini-3.6-flash-high", |
| 56 | + "gemini-3.1-pro-high": "gemini-3.1-pro-high", |
| 57 | + "gemini-2.5-flash": "gemini-2.5-flash", |
| 58 | + } |
| 59 | + budget := func(v string) []byte { |
| 60 | + return []byte(`{"contents":[],"generationConfig":{"thinkingConfig":{"thinkingBudget":` + v + `}}}`) |
| 61 | + } |
| 62 | + |
| 63 | + tests := []struct { |
| 64 | + name string |
| 65 | + mapping map[string]string |
| 66 | + model string |
| 67 | + body []byte |
| 68 | + want string |
| 69 | + wantMatch bool |
| 70 | + }{ |
| 71 | + {"agy high (-1) -> -high", catalog, "gemini-3.8-flash", budget("-1"), "gemini-3.8-flash-high", true}, |
| 72 | + {"agy medium (4000) -> -medium", catalog, "gemini-3.8-flash", budget("4000"), "gemini-3.8-flash-medium", true}, |
| 73 | + {"agy low (1000) -> -low", catalog, "gemini-3.8-flash", budget("1000"), "gemini-3.8-flash-low", true}, |
| 74 | + {"models/ prefix stripped", catalog, "models/gemini-3.8-flash", budget("1000"), "gemini-3.8-flash-low", true}, |
| 75 | + {"no thinkingConfig -> -high", catalog, "gemini-3.8-flash", []byte(`{"contents":[]}`), "gemini-3.8-flash-high", true}, |
| 76 | + // gemini-3.6/3.7/3.8-flash 的四个变体会被 resolveModelMapping 自动补齐,所以降级用一个不在默认表里的型号 |
| 77 | + {"only -high exists: low request degrades to high", map[string]string{ |
| 78 | + "gemini-3.5-flash-high": "gemini-3.5-flash-high", |
| 79 | + }, "gemini-3.5-flash", budget("1000"), "gemini-3.5-flash-high", true}, |
| 80 | + {"injected default variants are usable", catalog, "gemini-3.6-flash", budget("1000"), "gemini-3.6-flash-low", true}, |
| 81 | + {"already suffixed: untouched", catalog, "gemini-3.8-flash-low", budget("-1"), "", false}, |
| 82 | + {"bare name explicitly mapped: untouched", catalog, "gemini-2.5-flash", budget("-1"), "", false}, |
| 83 | + {"no variants in mapping: untouched", catalog, "gemini-9.9-flash", budget("-1"), "", false}, |
| 84 | + {"non-gemini model: untouched", catalog, "claude-sonnet-4-6", budget("-1"), "", false}, |
| 85 | + {"explicit bare alias wins over variants", map[string]string{ |
| 86 | + "gemini-3.8-flash": "gemini-3.8-flash-tiered", |
| 87 | + "gemini-3.8-flash-high": "gemini-3.8-flash-high", |
| 88 | + }, "gemini-3.8-flash", budget("-1"), "", false}, |
| 89 | + {"variant value is itself a mapping target", map[string]string{ |
| 90 | + "gemini-3.8-flash-high": "gemini-3.8-flash-tiered", |
| 91 | + }, "gemini-3.8-flash", budget("-1"), "gemini-3.8-flash-tiered", true}, |
| 92 | + {"user-written bare identity passthrough: respected", map[string]string{ |
| 93 | + "gemini-3.8-flash": "gemini-3.8-flash", |
| 94 | + "gemini-3.8-flash-high": "gemini-3.8-flash-high", |
| 95 | + }, "gemini-3.8-flash", budget("1000"), "", false}, |
| 96 | + {"runtime-injected bare passthrough (not in credentials): still resolved", map[string]string{ |
| 97 | + // resolveModelMapping 会补 gemini-3.7-flash → gemini-3.7-flash 的默认透传, |
| 98 | + // 但 credentials 里没有这条,应当继续推导变体。 |
| 99 | + "gemini-3.7-flash-medium": "gemini-3.7-flash-medium", |
| 100 | + }, "gemini-3.7-flash", budget("4000"), "gemini-3.7-flash-medium", true}, |
| 101 | + // 空 credentials 映射 → 走 DefaultAntigravityModelMapping,其中同样只有带后缀的 3.8 flash |
| 102 | + {"empty mapping falls back to default catalog", map[string]string{}, "gemini-3.8-flash", budget("-1"), "gemini-3.8-flash-high", true}, |
| 103 | + } |
| 104 | + for _, tt := range tests { |
| 105 | + t.Run(tt.name, func(t *testing.T) { |
| 106 | + account := newAntigravityAccountWithMapping(tt.mapping) |
| 107 | + got, matched := resolveGeminiThinkingVariant(account, tt.model, tt.body) |
| 108 | + require.Equal(t, tt.wantMatch, matched) |
| 109 | + require.Equal(t, tt.want, got) |
| 110 | + }) |
| 111 | + } |
| 112 | + |
| 113 | + t.Run("nil account", func(t *testing.T) { |
| 114 | + got, matched := resolveGeminiThinkingVariant(nil, "gemini-3.8-flash", budget("-1")) |
| 115 | + require.False(t, matched) |
| 116 | + require.Empty(t, got) |
| 117 | + }) |
| 118 | +} |
0 commit comments