-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
411 lines (379 loc) · 10.2 KB
/
main_test.go
File metadata and controls
411 lines (379 loc) · 10.2 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
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)
}
}
}
// countPRLines counts PR lines in a section by looking for bullet prefixes.
func countPRLines(output, sectionHeader, nextSection string) int {
lines := strings.Split(output, "\n")
sectionStarted := false
count := 0
for _, line := range lines {
if strings.Contains(line, sectionHeader) {
sectionStarted = true
continue
}
if nextSection != "" && strings.Contains(line, nextSection) {
break
}
if sectionStarted && (strings.HasPrefix(line, "• ") || strings.HasPrefix(line, "► ") || strings.HasPrefix(line, " ")) {
count++
}
}
return count
}
func TestGeneratePRDisplayBlockedFlag(t *testing.T) {
username := "alice"
// Create test PRs with blocking/non-blocking scenarios
incomingBlocked := PR{
Number: 1,
Title: "Incoming blocked PR",
HTMLURL: "https://github.com/org/repo/pull/1",
User: User{Login: "bob"},
RequestedReviewers: []User{{Login: "alice"}}, // alice is requested reviewer
}
incomingNotBlocked := PR{
Number: 2,
Title: "Incoming non-blocked PR",
HTMLURL: "https://github.com/org/repo/pull/2",
User: User{Login: "charlie"},
RequestedReviewers: []User{{Login: "dave"}}, // alice is not requested reviewer
}
outgoingBlocked := PR{
Number: 3,
Title: "Outgoing blocked PR",
HTMLURL: "https://github.com/org/repo/pull/3",
User: User{Login: "alice"}, // alice is author
RequestedReviewers: []User{{Login: "alice"}}, // alice is requested reviewer (perhaps due to turn server logic)
}
outgoingNotBlocked := PR{
Number: 4,
Title: "Outgoing non-blocked PR",
HTMLURL: "https://github.com/org/repo/pull/4",
User: User{Login: "alice"}, // alice is author
RequestedReviewers: []User{{Login: "eve"}}, // alice is not requested reviewer
}
allPRs := []PR{incomingBlocked, incomingNotBlocked, outgoingBlocked, outgoingNotBlocked}
tests := []struct {
name string
prs []PR
blockingOnly bool
expectIncoming bool
expectOutgoing bool
expectIncomingCount int
expectOutgoingCount int
}{
{
name: "normal mode shows all PRs",
prs: allPRs,
blockingOnly: false,
expectIncoming: true,
expectOutgoing: true,
expectIncomingCount: 2, // both incoming PRs
expectOutgoingCount: 2, // both outgoing PRs
},
{
name: "blocked mode shows only blocked PRs",
prs: allPRs,
blockingOnly: true,
expectIncoming: true, // has blocked incoming
expectOutgoing: true, // has blocked outgoing
expectIncomingCount: 1, // only blocked incoming
expectOutgoingCount: 1, // only blocked outgoing
},
{
name: "blocked mode with only incoming blocked",
prs: []PR{incomingBlocked, incomingNotBlocked, outgoingNotBlocked},
blockingOnly: true,
expectIncoming: true, // has blocked incoming
expectOutgoing: false, // no blocked outgoing
expectIncomingCount: 1, // only blocked incoming
expectOutgoingCount: 0, // no outgoing shown
},
{
name: "blocked mode with only outgoing blocked",
prs: []PR{incomingNotBlocked, outgoingBlocked, outgoingNotBlocked},
blockingOnly: true,
expectIncoming: false, // no blocked incoming
expectOutgoing: true, // has blocked outgoing
expectIncomingCount: 0, // no incoming shown
expectOutgoingCount: 1, // only blocked outgoing
},
{
name: "blocked mode with no blocked PRs",
prs: []PR{incomingNotBlocked, outgoingNotBlocked},
blockingOnly: true,
expectIncoming: false, // no blocked incoming
expectOutgoing: false, // no blocked outgoing
expectIncomingCount: 0, // no incoming shown
expectOutgoingCount: 0, // no outgoing shown
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
output := generatePRDisplay(tt.prs, username, tt.blockingOnly, true, nil)
// Debug output for failing test
if tt.name == "blocked mode with only outgoing blocked" {
t.Logf("Debug output: %q", output)
}
// For the case with no blocked PRs, expect empty output
if !tt.expectIncoming && !tt.expectOutgoing {
if strings.TrimSpace(output) != "" {
t.Errorf("Expected empty output for no blocked PRs, got: %q", output)
}
return
}
// Check for incoming section presence
hasIncomingSection := strings.Contains(output, "incoming -")
if hasIncomingSection != tt.expectIncoming {
t.Errorf("Expected incoming section: %v, got: %v", tt.expectIncoming, hasIncomingSection)
}
// Check for outgoing section presence
hasOutgoingSection := strings.Contains(output, "outgoing -")
if hasOutgoingSection != tt.expectOutgoing {
t.Errorf("Expected outgoing section: %v, got: %v", tt.expectOutgoing, hasOutgoingSection)
}
// Count actual PRs shown in each section
if tt.expectIncoming {
incomingLines := countPRLines(output, "incoming -", "outgoing -")
if incomingLines != tt.expectIncomingCount {
t.Errorf("Expected %d incoming PRs shown, got %d", tt.expectIncomingCount, incomingLines)
}
}
if tt.expectOutgoing {
outgoingLines := countPRLines(output, "outgoing -", "")
if outgoingLines != tt.expectOutgoingCount {
t.Errorf("Expected %d outgoing PRs shown, got %d", tt.expectOutgoingCount, outgoingLines)
}
}
})
}
}