|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/Wei-Shaw/sub2api/internal/service" |
| 11 | + coderws "github.com/coder/websocket" |
| 12 | + "github.com/gin-gonic/gin" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func TestClassifyAccountShareModeHTTPErrorPreservesSpecificContract(t *testing.T) { |
| 17 | + wrapSelection := func(err error) error { |
| 18 | + return fmt.Errorf("%w: %w", service.ErrAccountShareModeSelection, err) |
| 19 | + } |
| 20 | + recovering := service.NewAccountShareModeRecoveringError(17) |
| 21 | + |
| 22 | + tests := []struct { |
| 23 | + name string |
| 24 | + err error |
| 25 | + status int |
| 26 | + openAIType string |
| 27 | + anthropicType string |
| 28 | + message string |
| 29 | + retryAfter int |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "plain true unbound", |
| 33 | + err: service.ErrAccountShareModeGroupUnbound, |
| 34 | + status: http.StatusBadRequest, |
| 35 | + openAIType: "account_share_mode_unbound", |
| 36 | + anthropicType: "invalid_request_error", |
| 37 | + message: "该分组未绑定账号", |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "wrapped true unbound wins over broad selection", |
| 41 | + err: wrapSelection(service.ErrAccountShareModeGroupUnbound), |
| 42 | + status: http.StatusBadRequest, |
| 43 | + openAIType: "account_share_mode_unbound", |
| 44 | + anthropicType: "invalid_request_error", |
| 45 | + message: "该分组未绑定账号", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "plain recovering exposes safe retry after", |
| 49 | + err: recovering, |
| 50 | + status: http.StatusServiceUnavailable, |
| 51 | + openAIType: "account_share_recovering", |
| 52 | + anthropicType: "api_error", |
| 53 | + message: "共享账号正在恢复,请稍后重试", |
| 54 | + retryAfter: 17, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "wrapped recovering wins over broad selection", |
| 58 | + err: wrapSelection(recovering), |
| 59 | + status: http.StatusServiceUnavailable, |
| 60 | + openAIType: "account_share_recovering", |
| 61 | + anthropicType: "api_error", |
| 62 | + message: "共享账号正在恢复,请稍后重试", |
| 63 | + retryAfter: 17, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "idle timeout requires rejoin", |
| 67 | + err: service.ErrAccountShareMembershipIdleTimeout, |
| 68 | + status: http.StatusConflict, |
| 69 | + openAIType: "account_share_idle_timeout", |
| 70 | + anthropicType: "invalid_request_error", |
| 71 | + message: "账号房间绑定已因空闲超时结束,请重新加入房间", |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + for _, test := range tests { |
| 76 | + t.Run(test.name, func(t *testing.T) { |
| 77 | + details, handled := classifyAccountShareModeHTTPError(test.err) |
| 78 | + |
| 79 | + require.True(t, handled) |
| 80 | + require.Equal(t, test.status, details.status) |
| 81 | + require.Equal(t, test.openAIType, details.openAIType) |
| 82 | + require.Equal(t, test.anthropicType, details.anthropicType) |
| 83 | + require.Equal(t, test.message, details.message) |
| 84 | + require.Equal(t, test.retryAfter, details.retryAfter) |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestAccountShareModeWSCloseDetailsSeparatesUnboundFromRecovering(t *testing.T) { |
| 90 | + wrapSelection := func(err error) error { |
| 91 | + return fmt.Errorf("%w: %w", service.ErrAccountShareModeSelection, err) |
| 92 | + } |
| 93 | + tests := []struct { |
| 94 | + name string |
| 95 | + err error |
| 96 | + status coderws.StatusCode |
| 97 | + reason string |
| 98 | + }{ |
| 99 | + { |
| 100 | + name: "true unbound is a policy violation", |
| 101 | + err: wrapSelection(service.ErrAccountShareModeGroupUnbound), |
| 102 | + status: coderws.StatusPolicyViolation, |
| 103 | + reason: "该分组未绑定账号", |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "recovering asks the client to retry later", |
| 107 | + err: wrapSelection(service.NewAccountShareModeRecoveringError(11)), |
| 108 | + status: coderws.StatusTryAgainLater, |
| 109 | + reason: "共享账号正在恢复,请稍后重试", |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "unsupported model is a policy violation", |
| 113 | + err: wrapSelection(service.ErrAccountShareModeUnsupportedModel), |
| 114 | + status: coderws.StatusPolicyViolation, |
| 115 | + reason: "模型不支持", |
| 116 | + }, |
| 117 | + } |
| 118 | + |
| 119 | + for _, test := range tests { |
| 120 | + t.Run(test.name, func(t *testing.T) { |
| 121 | + status, reason, handled := accountShareModeWSCloseDetails(test.err) |
| 122 | + |
| 123 | + require.True(t, handled) |
| 124 | + require.Equal(t, test.status, status) |
| 125 | + require.Equal(t, test.reason, reason) |
| 126 | + }) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func TestHandleAccountShareModeSelectionErrorWritesSafeHTTPContract(t *testing.T) { |
| 131 | + gin.SetMode(gin.TestMode) |
| 132 | + handler := &OpenAIGatewayHandler{} |
| 133 | + |
| 134 | + tests := []struct { |
| 135 | + name string |
| 136 | + err error |
| 137 | + status int |
| 138 | + retryAfter string |
| 139 | + errType string |
| 140 | + message string |
| 141 | + }{ |
| 142 | + { |
| 143 | + name: "recovering", |
| 144 | + err: fmt.Errorf("account_id=797016 membership_id=73814 upstream=secret: %w", service.NewAccountShareModeRecoveringError(17)), |
| 145 | + status: http.StatusServiceUnavailable, |
| 146 | + retryAfter: "17", |
| 147 | + errType: "account_share_recovering", |
| 148 | + message: "共享账号正在恢复,请稍后重试", |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "true unbound", |
| 152 | + err: fmt.Errorf("internal binding detail: %w", service.ErrAccountShareModeGroupUnbound), |
| 153 | + status: http.StatusBadRequest, |
| 154 | + errType: "account_share_mode_unbound", |
| 155 | + message: "该分组未绑定账号", |
| 156 | + }, |
| 157 | + { |
| 158 | + name: "idle timeout", |
| 159 | + err: fmt.Errorf("membership_id=73814: %w", service.ErrAccountShareMembershipIdleTimeout), |
| 160 | + status: http.StatusConflict, |
| 161 | + errType: "account_share_idle_timeout", |
| 162 | + message: "账号房间绑定已因空闲超时结束,请重新加入房间", |
| 163 | + }, |
| 164 | + } |
| 165 | + |
| 166 | + for _, test := range tests { |
| 167 | + t.Run(test.name, func(t *testing.T) { |
| 168 | + recorder := httptest.NewRecorder() |
| 169 | + ctx, _ := gin.CreateTestContext(recorder) |
| 170 | + |
| 171 | + require.True(t, handler.handleAccountShareModeSelectionError(ctx, test.err, false)) |
| 172 | + require.Equal(t, test.status, recorder.Code) |
| 173 | + require.Equal(t, test.retryAfter, recorder.Header().Get("Retry-After")) |
| 174 | + require.JSONEq(t, fmt.Sprintf( |
| 175 | + `{"error":{"type":%q,"message":%q}}`, |
| 176 | + test.errType, |
| 177 | + test.message, |
| 178 | + ), recorder.Body.String()) |
| 179 | + for _, secret := range []string{"797016", "73814", "upstream=secret", "internal binding detail"} { |
| 180 | + require.NotContains(t, recorder.Body.String(), secret) |
| 181 | + } |
| 182 | + }) |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +func TestHandleAccountShareModeAnthropicErrorWritesSafeHTTPContract(t *testing.T) { |
| 187 | + gin.SetMode(gin.TestMode) |
| 188 | + recorder := httptest.NewRecorder() |
| 189 | + ctx, _ := gin.CreateTestContext(recorder) |
| 190 | + handler := &OpenAIGatewayHandler{} |
| 191 | + err := fmt.Errorf("account_id=797016 upstream=secret: %w", service.NewAccountShareModeRecoveringError(23)) |
| 192 | + |
| 193 | + require.True(t, handler.handleAccountShareModeAnthropicError(ctx, err, false)) |
| 194 | + require.Equal(t, http.StatusServiceUnavailable, recorder.Code) |
| 195 | + require.Equal(t, "23", recorder.Header().Get("Retry-After")) |
| 196 | + require.JSONEq(t, |
| 197 | + `{"type":"error","error":{"type":"api_error","message":"共享账号正在恢复,请稍后重试"}}`, |
| 198 | + recorder.Body.String(), |
| 199 | + ) |
| 200 | + require.NotContains(t, recorder.Body.String(), "797016") |
| 201 | + require.NotContains(t, recorder.Body.String(), "upstream=secret") |
| 202 | +} |
| 203 | + |
| 204 | +func TestAccountShareModeWSCloseReasonDoesNotExposeCause(t *testing.T) { |
| 205 | + cause := fmt.Errorf("account_id=797016 membership_id=73814 upstream=secret: %w", service.NewAccountShareModeRecoveringError(11)) |
| 206 | + status, reason, handled := accountShareModeWSCloseDetails(cause) |
| 207 | + |
| 208 | + require.True(t, handled) |
| 209 | + require.Equal(t, coderws.StatusTryAgainLater, status) |
| 210 | + require.Equal(t, "共享账号正在恢复,请稍后重试", reason) |
| 211 | + for _, secret := range []string{"797016", "73814", "upstream=secret"} { |
| 212 | + require.False(t, strings.Contains(reason, secret)) |
| 213 | + } |
| 214 | +} |
0 commit comments