|
| 1 | +package model |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestQueryCommandStream_ErrorResponseBody(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + statusCode int |
| 16 | + responseBody interface{} |
| 17 | + expectedErrMsg string |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "quota exceeded returns error message from body", |
| 21 | + statusCode: http.StatusTooManyRequests, |
| 22 | + responseBody: errorResponse{ |
| 23 | + ErrorCode: http.StatusTooManyRequests, |
| 24 | + ErrorMessage: "monthly AI credit quota exceeded", |
| 25 | + }, |
| 26 | + expectedErrMsg: "monthly AI credit quota exceeded", |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "unauthorized returns error message from body", |
| 30 | + statusCode: http.StatusUnauthorized, |
| 31 | + responseBody: errorResponse{ |
| 32 | + ErrorCode: http.StatusUnauthorized, |
| 33 | + ErrorMessage: "unauthorized", |
| 34 | + }, |
| 35 | + expectedErrMsg: "unauthorized", |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "service unavailable returns error message from body", |
| 39 | + statusCode: http.StatusServiceUnavailable, |
| 40 | + responseBody: errorResponse{ |
| 41 | + ErrorCode: http.StatusServiceUnavailable, |
| 42 | + ErrorMessage: "AI service is not available", |
| 43 | + }, |
| 44 | + expectedErrMsg: "AI service is not available", |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "non-JSON response falls back to status code", |
| 48 | + statusCode: http.StatusInternalServerError, |
| 49 | + responseBody: "not json", |
| 50 | + expectedErrMsg: fmt.Sprintf("server returned status %d", http.StatusInternalServerError), |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "empty error message falls back to status code", |
| 54 | + statusCode: http.StatusBadRequest, |
| 55 | + responseBody: errorResponse{ |
| 56 | + ErrorCode: http.StatusBadRequest, |
| 57 | + ErrorMessage: "", |
| 58 | + }, |
| 59 | + expectedErrMsg: fmt.Sprintf("server returned status %d", http.StatusBadRequest), |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + for _, tt := range tests { |
| 64 | + t.Run(tt.name, func(t *testing.T) { |
| 65 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 66 | + w.Header().Set("Content-Type", "application/json") |
| 67 | + w.WriteHeader(tt.statusCode) |
| 68 | + if s, ok := tt.responseBody.(string); ok { |
| 69 | + w.Write([]byte(s)) |
| 70 | + } else { |
| 71 | + json.NewEncoder(w).Encode(tt.responseBody) |
| 72 | + } |
| 73 | + })) |
| 74 | + defer server.Close() |
| 75 | + |
| 76 | + svc := NewAIService() |
| 77 | + err := svc.QueryCommandStream( |
| 78 | + context.Background(), |
| 79 | + CommandSuggestVariables{Shell: "bash", Os: "linux", Query: "test"}, |
| 80 | + Endpoint{APIEndpoint: server.URL, Token: "test-token"}, |
| 81 | + func(token string) {}, |
| 82 | + ) |
| 83 | + |
| 84 | + if err == nil { |
| 85 | + t.Fatal("expected error, got nil") |
| 86 | + } |
| 87 | + if err.Error() != tt.expectedErrMsg { |
| 88 | + t.Errorf("expected error %q, got %q", tt.expectedErrMsg, err.Error()) |
| 89 | + } |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
0 commit comments