|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "strconv" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/qf-studio/studio-sdk/sdk/testutil" |
| 13 | +) |
| 14 | + |
| 15 | +func newErrClient(t *testing.T, status int, body string, headers map[string]string) *Client { |
| 16 | + t.Helper() |
| 17 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 18 | + for k, v := range headers { |
| 19 | + w.Header().Set(k, v) |
| 20 | + } |
| 21 | + w.WriteHeader(status) |
| 22 | + _, _ = w.Write([]byte(body)) |
| 23 | + })) |
| 24 | + t.Cleanup(srv.Close) |
| 25 | + return NewClientWithBaseURL(testutil.FakeGitHubToken, srv.URL) |
| 26 | +} |
| 27 | + |
| 28 | +func TestDoRequest_429_ReturnsTypedRateLimitError(t *testing.T) { |
| 29 | + c := newErrClient(t, http.StatusTooManyRequests, `{"message":"API rate limit exceeded"}`, |
| 30 | + map[string]string{"Retry-After": "42"}) |
| 31 | + |
| 32 | + _, err := c.GetIssue(context.Background(), "o", "r", 1) |
| 33 | + if err == nil { |
| 34 | + t.Fatal("expected error") |
| 35 | + } |
| 36 | + var rlErr *RateLimitError |
| 37 | + if !errors.As(err, &rlErr) { |
| 38 | + t.Fatalf("error is %T, want *RateLimitError (errors.As must match for autopilot handling)", err) |
| 39 | + } |
| 40 | + if rlErr.StatusCode != http.StatusTooManyRequests { |
| 41 | + t.Errorf("StatusCode = %d, want 429", rlErr.StatusCode) |
| 42 | + } |
| 43 | + if rlErr.RetryAfter != 42*time.Second { |
| 44 | + t.Errorf("RetryAfter = %v, want 42s", rlErr.RetryAfter) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestDoRequest_403RateLimit_ReturnsTypedRateLimitError(t *testing.T) { |
| 49 | + tests := []struct { |
| 50 | + name string |
| 51 | + body string |
| 52 | + headers map[string]string |
| 53 | + }{ |
| 54 | + { |
| 55 | + name: "remaining zero header", |
| 56 | + body: `{"message":"forbidden"}`, |
| 57 | + headers: map[string]string{"X-RateLimit-Remaining": "0", "X-RateLimit-Reset": strconv.FormatInt(time.Now().Add(90*time.Second).Unix(), 10)}, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "secondary rate limit message", |
| 61 | + body: `{"message":"You have exceeded a secondary rate limit"}`, |
| 62 | + }, |
| 63 | + } |
| 64 | + for _, tt := range tests { |
| 65 | + t.Run(tt.name, func(t *testing.T) { |
| 66 | + c := newErrClient(t, http.StatusForbidden, tt.body, tt.headers) |
| 67 | + _, err := c.GetIssue(context.Background(), "o", "r", 1) |
| 68 | + var rlErr *RateLimitError |
| 69 | + if !errors.As(err, &rlErr) { |
| 70 | + t.Fatalf("error is %T, want *RateLimitError", err) |
| 71 | + } |
| 72 | + if rlErr.StatusCode != http.StatusForbidden { |
| 73 | + t.Errorf("StatusCode = %d, want 403", rlErr.StatusCode) |
| 74 | + } |
| 75 | + }) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func TestDoRequest_403Plain_IsNotRateLimitError(t *testing.T) { |
| 80 | + c := newErrClient(t, http.StatusForbidden, `{"message":"Resource not accessible by integration"}`, nil) |
| 81 | + _, err := c.GetIssue(context.Background(), "o", "r", 1) |
| 82 | + if err == nil { |
| 83 | + t.Fatal("expected error") |
| 84 | + } |
| 85 | + var rlErr *RateLimitError |
| 86 | + if errors.As(err, &rlErr) { |
| 87 | + t.Error("plain 403 must NOT be classified as a rate limit") |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestDoRequest_401_ReturnsTypedAuthError(t *testing.T) { |
| 92 | + c := newErrClient(t, http.StatusUnauthorized, `{"message":"Bad credentials"}`, nil) |
| 93 | + _, err := c.GetIssue(context.Background(), "o", "r", 1) |
| 94 | + var authErr *AuthError |
| 95 | + if !errors.As(err, &authErr) { |
| 96 | + t.Fatalf("error is %T, want *AuthError", err) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestIsRetryable_TypedErrors(t *testing.T) { |
| 101 | + if !isRetryableError(&RateLimitError{StatusCode: 403, Message: "secondary rate limit"}) { |
| 102 | + t.Error("403-classified RateLimitError must be retryable (string path would miss it)") |
| 103 | + } |
| 104 | + if isRetryableError(&AuthError{Message: "Bad credentials"}) { |
| 105 | + t.Error("AuthError must not be retryable") |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestExtractRetryAfter_TypedFastPath(t *testing.T) { |
| 110 | + got := extractRetryAfter(&RateLimitError{StatusCode: 429, RetryAfter: 17 * time.Second}) |
| 111 | + if got != 17*time.Second { |
| 112 | + t.Errorf("extractRetryAfter = %v, want 17s", got) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestParseParentIssueNumber_Exported(t *testing.T) { |
| 117 | + if n := ParseParentIssueNumber("Parent: #37\n\nBody"); n != 37 { |
| 118 | + t.Errorf("ParseParentIssueNumber = %d, want 37", n) |
| 119 | + } |
| 120 | + if n := ParseParentIssueNumber("Parent: GH-42"); n != 42 { |
| 121 | + t.Errorf("ParseParentIssueNumber = %d, want 42", n) |
| 122 | + } |
| 123 | + if n := ParseParentIssueNumber("no parent here"); n != 0 { |
| 124 | + t.Errorf("ParseParentIssueNumber = %d, want 0", n) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func TestFailedRetryStateLabels(t *testing.T) { |
| 129 | + want := []string{"pilot-failed-retry-1", "pilot-failed-retry-2", "pilot-failed-retry-exhausted"} |
| 130 | + if len(FailedRetryStateLabels) != len(want) { |
| 131 | + t.Fatalf("FailedRetryStateLabels = %v", FailedRetryStateLabels) |
| 132 | + } |
| 133 | + for i, w := range want { |
| 134 | + if FailedRetryStateLabels[i] != w { |
| 135 | + t.Errorf("FailedRetryStateLabels[%d] = %q, want %q", i, FailedRetryStateLabels[i], w) |
| 136 | + } |
| 137 | + } |
| 138 | + if LabelPilot != "pilot" { |
| 139 | + t.Errorf("LabelPilot = %q, want pilot", LabelPilot) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func TestGetOpenSubIssueNumbers(t *testing.T) { |
| 144 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 145 | + w.Header().Set("Content-Type", "application/json") |
| 146 | + if r.URL.Path == "/graphql" { |
| 147 | + _, _ = w.Write([]byte(`{"data":{"node":{"subIssues":{"totalCount":3,"nodes":[ |
| 148 | + {"number":11,"state":"OPEN"},{"number":12,"state":"CLOSED"},{"number":13,"state":"OPEN"}]}}}}`)) |
| 149 | + return |
| 150 | + } |
| 151 | + _, _ = w.Write([]byte(`{"node_id":"I_parent","number":10}`)) |
| 152 | + })) |
| 153 | + defer srv.Close() |
| 154 | + |
| 155 | + c := NewClientWithBaseURL(testutil.FakeGitHubToken, srv.URL) |
| 156 | + numbers, hasLinks, err := c.GetOpenSubIssueNumbers(context.Background(), "o", "r", 10) |
| 157 | + if err != nil { |
| 158 | + t.Fatalf("GetOpenSubIssueNumbers: %v", err) |
| 159 | + } |
| 160 | + if !hasLinks { |
| 161 | + t.Error("hasNativeLinks = false, want true (totalCount 3)") |
| 162 | + } |
| 163 | + if len(numbers) != 2 || numbers[0] != 11 || numbers[1] != 13 { |
| 164 | + t.Errorf("numbers = %v, want [11 13]", numbers) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +func TestGetOpenSubIssueNumbers_NoNativeLinks(t *testing.T) { |
| 169 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 170 | + w.Header().Set("Content-Type", "application/json") |
| 171 | + if r.URL.Path == "/graphql" { |
| 172 | + _, _ = w.Write([]byte(`{"data":{"node":{"subIssues":{"totalCount":0,"nodes":[]}}}}`)) |
| 173 | + return |
| 174 | + } |
| 175 | + _, _ = w.Write([]byte(`{"node_id":"I_parent","number":10}`)) |
| 176 | + })) |
| 177 | + defer srv.Close() |
| 178 | + |
| 179 | + c := NewClientWithBaseURL(testutil.FakeGitHubToken, srv.URL) |
| 180 | + numbers, hasLinks, err := c.GetOpenSubIssueNumbers(context.Background(), "o", "r", 10) |
| 181 | + if err != nil { |
| 182 | + t.Fatalf("GetOpenSubIssueNumbers: %v", err) |
| 183 | + } |
| 184 | + if hasLinks || numbers != nil { |
| 185 | + t.Errorf("got (%v, %v), want (nil, false)", numbers, hasLinks) |
| 186 | + } |
| 187 | +} |
0 commit comments