From 99e6dc9b8e6296532aa2a97d3e13e670c97affd6 Mon Sep 17 00:00:00 2001 From: erio Date: Sun, 5 Apr 2026 13:52:48 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20resolve=20CI=20failures=20=E2=80=94=20go?= =?UTF-8?q?fmt,=20unused=20functions,=20missing=20test=20helpers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Run gofmt on user schema, config test, group handler - Remove unused mergeGroupIDs function - Restore shared test helpers (newJSONResponse, queuedHTTPUpstream) that were in deleted Sora test file --- backend/ent/schema/user.go | 1 - backend/internal/config/config_test.go | 1 - .../internal/handler/admin/account_handler.go | 19 -------- .../internal/handler/admin/group_handler.go | 12 ++--- backend/internal/server/api_contract_test.go | 6 +-- .../service/account_test_helpers_test.go | 48 +++++++++++++++++++ .../service/antigravity_smart_retry_test.go | 2 - .../internal/service/openai_oauth_service.go | 14 ------ 8 files changed, 57 insertions(+), 46 deletions(-) create mode 100644 backend/internal/service/account_test_helpers_test.go diff --git a/backend/ent/schema/user.go b/backend/ent/schema/user.go index 6076bdc432..d443ef455c 100644 --- a/backend/ent/schema/user.go +++ b/backend/ent/schema/user.go @@ -72,7 +72,6 @@ func (User) Fields() []ent.Field { field.Time("totp_enabled_at"). Optional(). Nillable(), - } } diff --git a/backend/internal/config/config_test.go b/backend/internal/config/config_test.go index 2ee8e2bc07..2de5451ee0 100644 --- a/backend/internal/config/config_test.go +++ b/backend/internal/config/config_test.go @@ -1554,7 +1554,6 @@ func TestValidateConfig_LogRequiredAndRotationBounds(t *testing.T) { } } - func TestLoad_DefaultGatewayUsageRecordConfig(t *testing.T) { resetViperWithJWTSecret(t) cfg, err := Load() diff --git a/backend/internal/handler/admin/account_handler.go b/backend/internal/handler/admin/account_handler.go index 7e3dd3e67d..714f39f487 100644 --- a/backend/internal/handler/admin/account_handler.go +++ b/backend/internal/handler/admin/account_handler.go @@ -1641,25 +1641,6 @@ func (h *AccountHandler) ResetQuota(c *gin.Context) { response.Success(c, h.buildAccountResponseWithRuntime(c.Request.Context(), account)) } -// mergeGroupIDs 合并两个 groupID 切片并去重。 -func mergeGroupIDs(a, b []int64) []int64 { - seen := make(map[int64]struct{}, len(a)+len(b)) - result := make([]int64, 0, len(a)+len(b)) - for _, id := range a { - if _, ok := seen[id]; !ok { - seen[id] = struct{}{} - result = append(result, id) - } - } - for _, id := range b { - if _, ok := seen[id]; !ok { - seen[id] = struct{}{} - result = append(result, id) - } - } - return result -} - // GetTempUnschedulable handles getting temporary unschedulable status // GET /api/v1/admin/accounts/:id/temp-unschedulable func (h *AccountHandler) GetTempUnschedulable(c *gin.Context) { diff --git a/backend/internal/handler/admin/group_handler.go b/backend/internal/handler/admin/group_handler.go index a6bf2dba15..6aba4ef606 100644 --- a/backend/internal/handler/admin/group_handler.go +++ b/backend/internal/handler/admin/group_handler.go @@ -99,9 +99,9 @@ type CreateGroupRequest struct { FallbackGroupID *int64 `json:"fallback_group_id"` FallbackGroupIDOnInvalidRequest *int64 `json:"fallback_group_id_on_invalid_request"` // 模型路由配置(仅 anthropic 平台使用) - ModelRouting map[string][]int64 `json:"model_routing"` - ModelRoutingEnabled bool `json:"model_routing_enabled"` - MCPXMLInject *bool `json:"mcp_xml_inject"` + ModelRouting map[string][]int64 `json:"model_routing"` + ModelRoutingEnabled bool `json:"model_routing_enabled"` + MCPXMLInject *bool `json:"mcp_xml_inject"` // 支持的模型系列(仅 antigravity 平台使用) SupportedModelScopes []string `json:"supported_model_scopes"` // OpenAI Messages 调度配置(仅 openai 平台使用) @@ -131,9 +131,9 @@ type UpdateGroupRequest struct { FallbackGroupID *int64 `json:"fallback_group_id"` FallbackGroupIDOnInvalidRequest *int64 `json:"fallback_group_id_on_invalid_request"` // 模型路由配置(仅 anthropic 平台使用) - ModelRouting map[string][]int64 `json:"model_routing"` - ModelRoutingEnabled *bool `json:"model_routing_enabled"` - MCPXMLInject *bool `json:"mcp_xml_inject"` + ModelRouting map[string][]int64 `json:"model_routing"` + ModelRoutingEnabled *bool `json:"model_routing_enabled"` + MCPXMLInject *bool `json:"mcp_xml_inject"` // 支持的模型系列(仅 antigravity 平台使用) SupportedModelScopes *[]string `json:"supported_model_scopes"` // OpenAI Messages 调度配置(仅 openai 平台使用) diff --git a/backend/internal/server/api_contract_test.go b/backend/internal/server/api_contract_test.go index 0e74b5bd62..dc29da84f3 100644 --- a/backend/internal/server/api_contract_test.go +++ b/backend/internal/server/api_contract_test.go @@ -650,12 +650,12 @@ func newContractDeps(t *testing.T) *contractDeps { settingRepo := newStubSettingRepo() settingService := service.NewSettingService(settingRepo, cfg) - adminService := service.NewAdminService(userRepo, groupRepo, &accountRepo, nil, proxyRepo, apiKeyRepo, redeemRepo, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) + adminService := service.NewAdminService(userRepo, groupRepo, &accountRepo, proxyRepo, apiKeyRepo, redeemRepo, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) authHandler := handler.NewAuthHandler(cfg, nil, userService, settingService, nil, redeemService, nil) apiKeyHandler := handler.NewAPIKeyHandler(apiKeyService) usageHandler := handler.NewUsageHandler(usageService, apiKeyService) - adminSettingHandler := adminhandler.NewSettingHandler(settingService, nil, nil, nil, nil, nil, nil) - adminAccountHandler := adminhandler.NewAccountHandler(adminService, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) + adminSettingHandler := adminhandler.NewSettingHandler(settingService, nil, nil, nil) + adminAccountHandler := adminhandler.NewAccountHandler(adminService, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) jwtAuth := func(c *gin.Context) { c.Set(string(middleware.ContextKeyUser), middleware.AuthSubject{ diff --git a/backend/internal/service/account_test_helpers_test.go b/backend/internal/service/account_test_helpers_test.go new file mode 100644 index 0000000000..3154b199e5 --- /dev/null +++ b/backend/internal/service/account_test_helpers_test.go @@ -0,0 +1,48 @@ +package service + +import ( + "fmt" + "io" + "net/http" + "strings" + + "github.com/Wei-Shaw/sub2api/internal/pkg/tlsfingerprint" +) + +// queuedHTTPUpstream is a test helper that serves pre-loaded responses in order. +type queuedHTTPUpstream struct { + responses []*http.Response + requests []*http.Request + tlsFlags []bool +} + +func (u *queuedHTTPUpstream) Do(_ *http.Request, _ string, _ int64, _ int) (*http.Response, error) { + return nil, fmt.Errorf("unexpected Do call") +} + +func (u *queuedHTTPUpstream) DoWithTLS(req *http.Request, _ string, _ int64, _ int, profile *tlsfingerprint.Profile) (*http.Response, error) { + u.requests = append(u.requests, req) + u.tlsFlags = append(u.tlsFlags, profile != nil) + if len(u.responses) == 0 { + return nil, fmt.Errorf("no mocked response") + } + resp := u.responses[0] + u.responses = u.responses[1:] + return resp, nil +} + +// newJSONResponse creates a simple HTTP response for testing. +func newJSONResponse(status int, body string) *http.Response { + return &http.Response{ + StatusCode: status, + Header: make(http.Header), + Body: io.NopCloser(strings.NewReader(body)), + } +} + +// newJSONResponseWithHeader creates a JSON response with a custom header. +func newJSONResponseWithHeader(status int, body, key, value string) *http.Response { + resp := newJSONResponse(status, body) + resp.Header.Set(key, value) + return resp +} diff --git a/backend/internal/service/antigravity_smart_retry_test.go b/backend/internal/service/antigravity_smart_retry_test.go index 8b45c551db..e910325e78 100644 --- a/backend/internal/service/antigravity_smart_retry_test.go +++ b/backend/internal/service/antigravity_smart_retry_test.go @@ -9,8 +9,6 @@ import ( "net/http" "strings" "testing" - "time" - "github.com/Wei-Shaw/sub2api/internal/pkg/tlsfingerprint" "github.com/stretchr/testify/require" ) diff --git a/backend/internal/service/openai_oauth_service.go b/backend/internal/service/openai_oauth_service.go index e90e0d8ecc..e7b1f2339c 100644 --- a/backend/internal/service/openai_oauth_service.go +++ b/backend/internal/service/openai_oauth_service.go @@ -359,20 +359,6 @@ func (s *OpenAIOAuthService) Stop() { s.sessionStore.Stop() } -func (s *OpenAIOAuthService) resolveProxyURL(ctx context.Context, proxyID *int64) (string, error) { - if proxyID == nil { - return "", nil - } - proxy, err := s.proxyRepo.GetByID(ctx, *proxyID) - if err != nil { - return "", infraerrors.Newf(http.StatusBadRequest, "OPENAI_OAUTH_PROXY_NOT_FOUND", "proxy not found: %v", err) - } - if proxy == nil { - return "", nil - } - return proxy.URL(), nil -} - func normalizeOpenAIOAuthPlatform(platform string) string { return openai.OAuthPlatformOpenAI }