From c64afdc6c26184af172f5cc26fceb17435aa89b5 Mon Sep 17 00:00:00 2001 From: fengsilin Date: Sat, 16 May 2026 10:13:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B8=85=E7=90=86=E6=B8=A0=E9=81=93?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E5=88=A0=E9=99=A4=E5=90=8E=E7=9A=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E8=B4=A8=E9=87=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - router/api-router.go: 修复删除路由后的缩进异常 - model/channel_pricing.go: 清理 Insert/Update/Delete 中遗留空行 - controller/relay_test.go: newTestContext 接收 *testing.T 参数并调用 t.Helper() --- controller/relay_test.go | 29 +++++++++++++++-------------- model/channel_pricing.go | 3 --- router/api-router.go | 4 ++-- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/controller/relay_test.go b/controller/relay_test.go index 8130f54..8174bb9 100644 --- a/controller/relay_test.go +++ b/controller/relay_test.go @@ -16,7 +16,8 @@ import ( // shouldRetry — 渠道选择删除后的验证测试 // --------------------------------------------------------------------------- -func newTestContext() *gin.Context { +func newTestContext(t *testing.T) *gin.Context { + t.Helper() gin.SetMode(gin.TestMode) w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) @@ -25,7 +26,7 @@ func newTestContext() *gin.Context { } func TestShouldRetry_NilError_ReturnsFalse(t *testing.T) { - c := newTestContext() + c := newTestContext(t) assert.False(t, shouldRetry(c, nil, 3)) } @@ -33,7 +34,7 @@ func TestShouldRetry_5xxErrorWithRetryLeft_ReturnsTrue(t *testing.T) { // 渠道选择删除后,所有请求都能正常重试 // 之前设置了 specific_channel_id 时 shouldRetry 会返回 false // 现在即使不设置任何 channel 相关 context,5xx 也应该重试 - c := newTestContext() + c := newTestContext(t) apiErr := types.NewErrorWithStatusCode( errors.New("upstream error"), types.ErrorCodeBadResponseStatusCode, @@ -43,7 +44,7 @@ func TestShouldRetry_5xxErrorWithRetryLeft_ReturnsTrue(t *testing.T) { } func TestShouldRetry_5xxErrorZeroRetryLeft_ReturnsFalse(t *testing.T) { - c := newTestContext() + c := newTestContext(t) apiErr := types.NewErrorWithStatusCode( errors.New("upstream error"), types.ErrorCodeBadResponseStatusCode, @@ -53,7 +54,7 @@ func TestShouldRetry_5xxErrorZeroRetryLeft_ReturnsFalse(t *testing.T) { } func TestShouldRetry_SkipRetryError_ReturnsFalse(t *testing.T) { - c := newTestContext() + c := newTestContext(t) apiErr := types.NewError( errors.New("skip retry"), types.ErrorCodeBadResponseStatusCode, @@ -64,7 +65,7 @@ func TestShouldRetry_SkipRetryError_ReturnsFalse(t *testing.T) { func TestShouldRetry_ChannelError_ReturnsTrue(t *testing.T) { // channel: 前缀的错误码会触发立即重试 - c := newTestContext() + c := newTestContext(t) apiErr := types.NewError( errors.New("channel error"), "channel:test", @@ -74,7 +75,7 @@ func TestShouldRetry_ChannelError_ReturnsTrue(t *testing.T) { } func TestShouldRetry_2xxStatus_ReturnsFalse(t *testing.T) { - c := newTestContext() + c := newTestContext(t) apiErr := types.NewErrorWithStatusCode( errors.New("ok"), types.ErrorCodeBadResponseStatusCode, @@ -84,7 +85,7 @@ func TestShouldRetry_2xxStatus_ReturnsFalse(t *testing.T) { } func TestShouldRetry_429Status_ReturnsTrue(t *testing.T) { - c := newTestContext() + c := newTestContext(t) apiErr := types.NewErrorWithStatusCode( errors.New("rate limited"), types.ErrorCodeBadResponseStatusCode, @@ -98,12 +99,12 @@ func TestShouldRetry_429Status_ReturnsTrue(t *testing.T) { // --------------------------------------------------------------------------- func TestShouldRetryTaskRelay_NilError_ReturnsFalse(t *testing.T) { - c := newTestContext() + c := newTestContext(t) assert.False(t, shouldRetryTaskRelay(c, 1, nil, 3)) } func TestShouldRetryTaskRelay_5xxWithRetryLeft_ReturnsTrue(t *testing.T) { - c := newTestContext() + c := newTestContext(t) taskErr := &dto.TaskError{ Error: errors.New("server error"), StatusCode: http.StatusInternalServerError, @@ -112,7 +113,7 @@ func TestShouldRetryTaskRelay_5xxWithRetryLeft_ReturnsTrue(t *testing.T) { } func TestShouldRetryTaskRelay_5xxZeroRetryLeft_ReturnsFalse(t *testing.T) { - c := newTestContext() + c := newTestContext(t) taskErr := &dto.TaskError{ Error: errors.New("server error"), StatusCode: http.StatusInternalServerError, @@ -121,7 +122,7 @@ func TestShouldRetryTaskRelay_5xxZeroRetryLeft_ReturnsFalse(t *testing.T) { } func TestShouldRetryTaskRelay_429_ReturnsTrue(t *testing.T) { - c := newTestContext() + c := newTestContext(t) taskErr := &dto.TaskError{ Error: errors.New("rate limited"), StatusCode: http.StatusTooManyRequests, @@ -130,7 +131,7 @@ func TestShouldRetryTaskRelay_429_ReturnsTrue(t *testing.T) { } func TestShouldRetryTaskRelay_400_ReturnsFalse(t *testing.T) { - c := newTestContext() + c := newTestContext(t) taskErr := &dto.TaskError{ Error: errors.New("bad request"), StatusCode: http.StatusBadRequest, @@ -141,7 +142,7 @@ func TestShouldRetryTaskRelay_400_ReturnsFalse(t *testing.T) { func TestShouldRetryTaskRelay_LocalError_ReturnsFalse(t *testing.T) { // LocalError + 非 5xx 状态码(如 403)时,LocalError 检查生效返回 false // 注意:5xx 分支在 LocalError 检查之前,所以 localError+5xx 仍会重试 - c := newTestContext() + c := newTestContext(t) taskErr := &dto.TaskError{ Error: errors.New("local error"), StatusCode: http.StatusForbidden, diff --git a/model/channel_pricing.go b/model/channel_pricing.go index 60d774c..131c9fb 100644 --- a/model/channel_pricing.go +++ b/model/channel_pricing.go @@ -81,7 +81,6 @@ func (cp *ChannelPricing) Insert() error { err := DB.Create(cp).Error if err == nil { setCache(getChannelPricingCacheKey(cp.ModelName, cp.ChannelId), cp) - } return err } @@ -95,7 +94,6 @@ func (cp *ChannelPricing) Update() error { Updates(cp).Error if err == nil { setCache(getChannelPricingCacheKey(cp.ModelName, cp.ChannelId), cp) - } return err } @@ -108,7 +106,6 @@ func (cp *ChannelPricing) Delete() error { err := DB.Delete(cp).Error if err == nil { removeCache(getChannelPricingCacheKey(existing.ModelName, existing.ChannelId)) - } return err } diff --git a/router/api-router.go b/router/api-router.go index 16c9b4d..7b08f5c 100644 --- a/router/api-router.go +++ b/router/api-router.go @@ -77,7 +77,7 @@ func SetApiRouter(router *gin.Engine) { selfRoute.GET("/self/groups", controller.GetUserGroups) selfRoute.GET("/self", controller.GetSelf) selfRoute.GET("/models", controller.GetUserModels) - selfRoute.PUT("/self", controller.UpdateSelf) + selfRoute.PUT("/self", controller.UpdateSelf) selfRoute.DELETE("/self", controller.DeleteSelf) selfRoute.GET("/token", controller.GenerateAccessToken) selfRoute.GET("/passkey", controller.PasskeyStatus) @@ -202,7 +202,7 @@ func SetApiRouter(router *gin.Engine) { channelPricingRoute.POST("/batch", controller.BatchCreateChannelPricing) channelPricingRoute.POST("/copy_global/:channel_id", controller.CopyGlobalPricing) channelPricingRoute.DELETE("/:id", controller.DeleteChannelPricing) - } + } // 用户倍率路由(管理员权限) userChannelRatioRoute := apiRouter.Group("/user_channel_ratio")