| |
|
|
| package handler |
|
|
| import ( |
| "testing" |
|
|
| "github.com/Wei-Shaw/sub2api/internal/service" |
| "github.com/stretchr/testify/require" |
| ) |
|
|
| |
| |
| func TestGeminiV1BetaHandler_PlatformRoutingInvariant(t *testing.T) { |
| tests := []struct { |
| name string |
| platform string |
| expectedService string |
| description string |
| }{ |
| { |
| name: "Gemini平台使用ForwardNative", |
| platform: service.PlatformGemini, |
| expectedService: "GeminiMessagesCompatService.ForwardNative", |
| description: "Gemini OAuth 账户直接调用 Google API", |
| }, |
| { |
| name: "Antigravity平台使用ForwardGemini", |
| platform: service.PlatformAntigravity, |
| expectedService: "AntigravityGatewayService.ForwardGemini", |
| description: "Antigravity 账户通过 CRS 中转,支持 Gemini 协议", |
| }, |
| } |
|
|
| for _, tt := range tests { |
| t.Run(tt.name, func(t *testing.T) { |
| |
| var routedService string |
| if tt.platform == service.PlatformAntigravity { |
| routedService = "AntigravityGatewayService.ForwardGemini" |
| } else { |
| routedService = "GeminiMessagesCompatService.ForwardNative" |
| } |
|
|
| require.Equal(t, tt.expectedService, routedService, |
| "平台 %s 应该路由到 %s: %s", |
| tt.platform, tt.expectedService, tt.description) |
| }) |
| } |
| } |
|
|
| |
| |
| func TestGeminiV1BetaHandler_ListModelsAntigravityFallback(t *testing.T) { |
| tests := []struct { |
| name string |
| hasGeminiAccount bool |
| hasAntigravity bool |
| expectedBehavior string |
| }{ |
| { |
| name: "有Gemini账户-调用ForwardAIStudioGET", |
| hasGeminiAccount: true, |
| hasAntigravity: false, |
| expectedBehavior: "forward_to_upstream", |
| }, |
| { |
| name: "无Gemini有Antigravity-返回静态列表", |
| hasGeminiAccount: false, |
| hasAntigravity: true, |
| expectedBehavior: "static_fallback", |
| }, |
| { |
| name: "无任何账户-返回503", |
| hasGeminiAccount: false, |
| hasAntigravity: false, |
| expectedBehavior: "service_unavailable", |
| }, |
| } |
|
|
| for _, tt := range tests { |
| t.Run(tt.name, func(t *testing.T) { |
| |
| var behavior string |
|
|
| if tt.hasGeminiAccount { |
| behavior = "forward_to_upstream" |
| } else if tt.hasAntigravity { |
| behavior = "static_fallback" |
| } else { |
| behavior = "service_unavailable" |
| } |
|
|
| require.Equal(t, tt.expectedBehavior, behavior) |
| }) |
| } |
| } |
|
|
| |
| func TestGeminiV1BetaHandler_GetModelAntigravityFallback(t *testing.T) { |
| tests := []struct { |
| name string |
| hasGeminiAccount bool |
| hasAntigravity bool |
| expectedBehavior string |
| }{ |
| { |
| name: "有Gemini账户-调用ForwardAIStudioGET", |
| hasGeminiAccount: true, |
| hasAntigravity: false, |
| expectedBehavior: "forward_to_upstream", |
| }, |
| { |
| name: "无Gemini有Antigravity-返回静态模型信息", |
| hasGeminiAccount: false, |
| hasAntigravity: true, |
| expectedBehavior: "static_model_info", |
| }, |
| { |
| name: "无任何账户-返回503", |
| hasGeminiAccount: false, |
| hasAntigravity: false, |
| expectedBehavior: "service_unavailable", |
| }, |
| } |
|
|
| for _, tt := range tests { |
| t.Run(tt.name, func(t *testing.T) { |
| |
| var behavior string |
|
|
| if tt.hasGeminiAccount { |
| behavior = "forward_to_upstream" |
| } else if tt.hasAntigravity { |
| behavior = "static_model_info" |
| } else { |
| behavior = "service_unavailable" |
| } |
|
|
| require.Equal(t, tt.expectedBehavior, behavior) |
| }) |
| } |
| } |
|
|