-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
249 lines (233 loc) · 4.66 KB
/
main_test.go
File metadata and controls
249 lines (233 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
import (
"strings"
"testing"
)
func TestTruncate(t *testing.T) {
tests := []struct {
name string
input string
length int
expected string
}{
{
name: "short string",
input: "hello",
length: 10,
expected: "hello",
},
{
name: "exact length",
input: "hello world",
length: 11,
expected: "hello world",
},
{
name: "needs truncation",
input: "this is a very long string that needs truncation",
length: 20,
expected: "this is a very lo...",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := truncate(tt.input, tt.length)
if got != tt.expected {
t.Errorf("truncate(%q, %d) = %q, want %q", tt.input, tt.length, got, tt.expected)
}
})
}
}
func TestIsBlockingOnUser(t *testing.T) {
tests := []struct {
name string
pr PR
username string
expected bool
}{
{
name: "user is requested reviewer",
pr: PR{
RequestedReviewers: []User{
{Login: "alice"},
{Login: "bob"},
},
},
username: "alice",
expected: true,
},
{
name: "user is not requested reviewer",
pr: PR{
RequestedReviewers: []User{
{Login: "alice"},
{Login: "bob"},
},
},
username: "charlie",
expected: false,
},
{
name: "no requested reviewers",
pr: PR{},
username: "alice",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := isBlockingOnUser(&tt.pr, tt.username)
if got != tt.expected {
t.Errorf("isBlockingOnUser() = %v, want %v", got, tt.expected)
}
})
}
}
func TestWasBlockingBefore(t *testing.T) {
pr1 := PR{
Number: 123,
Repository: struct {
FullName string `json:"full_name"`
}{FullName: "org/repo"},
RequestedReviewers: []User{{Login: "alice"}},
}
pr2 := PR{
Number: 456,
Repository: struct {
FullName string `json:"full_name"`
}{FullName: "org/repo"},
RequestedReviewers: []User{{Login: "bob"}},
}
lastPRs := []PR{pr1, pr2}
tests := []struct {
name string
pr PR
lastPRs []PR
username string
expected bool
}{
{
name: "was blocking before",
pr: PR{
Number: 123,
Repository: struct {
FullName string `json:"full_name"`
}{FullName: "org/repo"},
},
lastPRs: lastPRs,
username: "alice",
expected: true,
},
{
name: "was not blocking before",
pr: PR{
Number: 456,
Repository: struct {
FullName string `json:"full_name"`
}{FullName: "org/repo"},
},
lastPRs: lastPRs,
username: "alice",
expected: false,
},
{
name: "new PR",
pr: PR{
Number: 789,
Repository: struct {
FullName string `json:"full_name"`
}{FullName: "org/repo"},
},
lastPRs: lastPRs,
username: "alice",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := wasBlockingBefore(&tt.pr, tt.lastPRs, tt.username)
if got != tt.expected {
t.Errorf("wasBlockingBefore() = %v, want %v", got, tt.expected)
}
})
}
}
func TestOrgFromURL(t *testing.T) {
tests := []struct {
name string
url string
expected string
}{
{
name: "valid github PR URL",
url: "https://github.com/owner/repo/pull/123",
expected: "owner",
},
{
name: "valid github issue URL",
url: "https://github.com/org/project/issues/456",
expected: "org",
},
{
name: "non-github URL",
url: "https://example.com/something/else",
expected: "",
},
{
name: "malformed URL",
url: "not-a-url",
expected: "",
},
{
name: "github URL with too few parts",
url: "https://github.com/",
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := orgFromURL(tt.url)
if got != tt.expected {
t.Errorf("orgFromURL(%q) = %q, want %q", tt.url, got, tt.expected)
}
})
}
}
func TestCategorizePRs(t *testing.T) {
prs := []PR{
{
Number: 1,
User: User{Login: "alice"},
},
{
Number: 2,
User: User{Login: "bob"},
},
{
Number: 3,
User: User{Login: "alice"},
},
{
Number: 4,
User: User{Login: "charlie"},
},
}
incoming, outgoing := categorizePRs(prs, "alice")
if len(outgoing) != 2 {
t.Errorf("Expected 2 outgoing PRs, got %d", len(outgoing))
}
if len(incoming) != 2 {
t.Errorf("Expected 2 incoming PRs, got %d", len(incoming))
}
// Verify correct categorization
for _, pr := range outgoing {
if pr.User.Login != "alice" {
t.Errorf("Outgoing PR has wrong author: %s", pr.User.Login)
}
}
for _, pr := range incoming {
if pr.User.Login == "alice" {
t.Errorf("Incoming PR has wrong author: %s", pr.User.Login)
}
}
}