From c63192fcb55db34b9ac5e670f34eb3cd23ddcb71 Mon Sep 17 00:00:00 2001 From: IanShaw027 <131567472+IanShaw027@users.noreply.github.com> Date: Thu, 1 Jan 2026 15:16:12 +0800 Subject: [PATCH] =?UTF-8?q?fix(test):=20=E4=BF=AE=E5=A4=8D=20CI=20?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=92=8C=20lint=20=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 为所有 mock 实现添加 GetByIDs 方法以满足 AccountRepository 接口 - 重构 account_handler.go 中的类型断言,使用类型安全的变量 - 修复 gofmt 格式问题 --- .../internal/handler/admin/account_handler.go | 25 +++++++++++-------- .../internal/pkg/geminicli/drive_client.go | 9 ++++--- .../pkg/geminicli/drive_client_test.go | 1 - .../service/account_service_delete_test.go | 4 +++ .../service/gateway_multiplatform_test.go | 10 ++++++++ .../service/gemini_multiplatform_test.go | 10 ++++++++ .../service/gemini_oauth_service_test.go | 1 - 7 files changed, 43 insertions(+), 17 deletions(-) diff --git a/backend/internal/handler/admin/account_handler.go b/backend/internal/handler/admin/account_handler.go index af1e7d91..f2d8a287 100644 --- a/backend/internal/handler/admin/account_handler.go +++ b/backend/internal/handler/admin/account_handler.go @@ -1099,12 +1099,8 @@ func (h *AccountHandler) BatchRefreshTier(c *gin.Context) { g.SetLimit(maxConcurrency) var mu sync.Mutex - results := gin.H{ - "total": len(accounts), - "success": 0, - "failed": 0, - "errors": []gin.H{}, - } + var successCount, failedCount int + var errors []gin.H for _, account := range accounts { acc := account // 闭包捕获 @@ -1112,8 +1108,8 @@ func (h *AccountHandler) BatchRefreshTier(c *gin.Context) { _, extra, creds, err := h.geminiOAuthService.RefreshAccountGoogleOneTier(gctx, acc) if err != nil { mu.Lock() - results["failed"] = results["failed"].(int) + 1 - results["errors"] = append(results["errors"].([]gin.H), gin.H{ + failedCount++ + errors = append(errors, gin.H{ "account_id": acc.ID, "error": err.Error(), }) @@ -1128,13 +1124,13 @@ func (h *AccountHandler) BatchRefreshTier(c *gin.Context) { mu.Lock() if updateErr != nil { - results["failed"] = results["failed"].(int) + 1 - results["errors"] = append(results["errors"].([]gin.H), gin.H{ + failedCount++ + errors = append(errors, gin.H{ "account_id": acc.ID, "error": updateErr.Error(), }) } else { - results["success"] = results["success"].(int) + 1 + successCount++ } mu.Unlock() @@ -1147,5 +1143,12 @@ func (h *AccountHandler) BatchRefreshTier(c *gin.Context) { return } + results := gin.H{ + "total": len(accounts), + "success": successCount, + "failed": failedCount, + "errors": errors, + } + response.Success(c, results) } diff --git a/backend/internal/pkg/geminicli/drive_client.go b/backend/internal/pkg/geminicli/drive_client.go index 77e2c476..8f9c745f 100644 --- a/backend/internal/pkg/geminicli/drive_client.go +++ b/backend/internal/pkg/geminicli/drive_client.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "io" "math/rand" "net/http" "strconv" @@ -112,10 +111,12 @@ func (c *driveClient) GetStorageQuota(ctx context.Context, accessToken, proxyURL } if resp.StatusCode != http.StatusOK { - body, _ := io.ReadAll(resp.Body) _ = resp.Body.Close() - // 记录完整错误 - fmt.Printf("[DriveClient] API error (status %d): %s\n", resp.StatusCode, string(body)) + statusText := http.StatusText(resp.StatusCode) + if statusText == "" { + statusText = resp.Status + } + fmt.Printf("[DriveClient] Drive API error: status=%d, msg=%s\n", resp.StatusCode, statusText) // 只返回通用错误 return nil, fmt.Errorf("drive API error: status %d", resp.StatusCode) } diff --git a/backend/internal/pkg/geminicli/drive_client_test.go b/backend/internal/pkg/geminicli/drive_client_test.go index d2c7f25b..b6dd1a69 100644 --- a/backend/internal/pkg/geminicli/drive_client_test.go +++ b/backend/internal/pkg/geminicli/drive_client_test.go @@ -16,4 +16,3 @@ func TestDriveStorageInfo(t *testing.T) { t.Errorf("Expected usage 50GB, got %d", info.Usage) } } - diff --git a/backend/internal/service/account_service_delete_test.go b/backend/internal/service/account_service_delete_test.go index 2648b828..43703763 100644 --- a/backend/internal/service/account_service_delete_test.go +++ b/backend/internal/service/account_service_delete_test.go @@ -40,6 +40,10 @@ func (s *accountRepoStub) GetByID(ctx context.Context, id int64) (*Account, erro panic("unexpected GetByID call") } +func (s *accountRepoStub) GetByIDs(ctx context.Context, ids []int64) ([]*Account, error) { + panic("unexpected GetByIDs call") +} + // ExistsByID 返回预设的存在性检查结果。 // 这是 Delete 方法调用的第一个仓储方法,用于验证账号是否存在。 func (s *accountRepoStub) ExistsByID(ctx context.Context, id int64) (bool, error) { diff --git a/backend/internal/service/gateway_multiplatform_test.go b/backend/internal/service/gateway_multiplatform_test.go index 560c7767..808a48b2 100644 --- a/backend/internal/service/gateway_multiplatform_test.go +++ b/backend/internal/service/gateway_multiplatform_test.go @@ -32,6 +32,16 @@ func (m *mockAccountRepoForPlatform) GetByID(ctx context.Context, id int64) (*Ac return nil, errors.New("account not found") } +func (m *mockAccountRepoForPlatform) GetByIDs(ctx context.Context, ids []int64) ([]*Account, error) { + var result []*Account + for _, id := range ids { + if acc, ok := m.accountsByID[id]; ok { + result = append(result, acc) + } + } + return result, nil +} + func (m *mockAccountRepoForPlatform) ExistsByID(ctx context.Context, id int64) (bool, error) { if m.accountsByID == nil { return false, nil diff --git a/backend/internal/service/gemini_multiplatform_test.go b/backend/internal/service/gemini_multiplatform_test.go index dcc945eb..6ca5052e 100644 --- a/backend/internal/service/gemini_multiplatform_test.go +++ b/backend/internal/service/gemini_multiplatform_test.go @@ -25,6 +25,16 @@ func (m *mockAccountRepoForGemini) GetByID(ctx context.Context, id int64) (*Acco return nil, errors.New("account not found") } +func (m *mockAccountRepoForGemini) GetByIDs(ctx context.Context, ids []int64) ([]*Account, error) { + var result []*Account + for _, id := range ids { + if acc, ok := m.accountsByID[id]; ok { + result = append(result, acc) + } + } + return result, nil +} + func (m *mockAccountRepoForGemini) ExistsByID(ctx context.Context, id int64) (bool, error) { if m.accountsByID == nil { return false, nil diff --git a/backend/internal/service/gemini_oauth_service_test.go b/backend/internal/service/gemini_oauth_service_test.go index 393812c2..026e6dc2 100644 --- a/backend/internal/service/gemini_oauth_service_test.go +++ b/backend/internal/service/gemini_oauth_service_test.go @@ -49,4 +49,3 @@ func TestInferGoogleOneTier(t *testing.T) { }) } } -