gunk-dev/the-valley

pr_client_test.go

source68d12192bbd3main's published tip history
7.2 KiB
1package github
2
3import (
4 "os"
5 "path/filepath"
6 "reflect"
7 "strings"
8 "testing"
9)
10
11func TestNewPRClient(t *testing.T) {
12 c := NewPRClient("owner/repo")
13 if c.Repo() != "owner/repo" {
14 t.Errorf("Repo() = %q, want %q", c.Repo(), "owner/repo")
15 }
16
17 empty := NewPRClient("")
18 if empty.Repo() != "" {
19 t.Errorf("Repo() = %q, want empty", empty.Repo())
20 }
21}
22
23func TestGHArgsWithRepo(t *testing.T) {
24 c := NewGHCLIClient("owner/repo")
25 got := c.ChecksArgs("42")
26 want := []string{"pr", "checks", "--repo", "owner/repo", "--", "42"}
27 if !reflect.DeepEqual(got, want) {
28 t.Errorf("ChecksArgs() = %v, want %v", got, want)
29 }
30}
31
32func TestGHArgsWithoutRepo(t *testing.T) {
33 c := NewGHCLIClient("")
34 got := c.ChecksArgs("42")
35 want := []string{"pr", "checks", "--", "42"}
36 if !reflect.DeepEqual(got, want) {
37 t.Errorf("ChecksArgs() = %v, want %v", got, want)
38 }
39}
40
41func TestAllArgBuildersPlaceFlagsBeforeSeparator(t *testing.T) {
42 client := NewGHCLIClient("owner/repo")
43 builders := []struct {
44 name string
45 fn func(string) []string
46 }{
47 {"ChecksArgs", client.ChecksArgs},
48 {"ViewStateArgs", client.ViewStateArgs},
49 {"ViewConflictsArgs", client.ViewConflictsArgs},
50 {"ViewReviewDecisionArgs", client.ViewReviewDecisionArgs},
51 {"ViewTitleArgs", client.ViewTitleArgs},
52 }
53
54 for _, b := range builders {
55 t.Run(b.name, func(t *testing.T) {
56 args := b.fn("42")
57
58 separatorIdx := -1
59 for i, arg := range args {
60 if arg == "--" {
61 separatorIdx = i
62 break
63 }
64 }
65 if separatorIdx == -1 {
66 t.Fatal("missing '--' separator")
67 }
68 for i := separatorIdx + 1; i < len(args); i++ {
69 if strings.HasPrefix(args[i], "-") {
70 t.Errorf("flag %q found after '--' separator at index %d", args[i], i)
71 }
72 }
73
74 // Last arg should be the PR ref
75 last := args[len(args)-1]
76 if last != "42" {
77 t.Errorf("last arg = %q, want %q", last, "42")
78 }
79 })
80 }
81}
82
83func TestMergeArgsVariants(t *testing.T) {
84 tests := []struct {
85 name string
86 prNumber string
87 mergeMethod string
88 deleteBranch bool
89 repo string
90 want []string
91 }{
92 {
93 name: "squash with delete",
94 prNumber: "42",
95 mergeMethod: "squash",
96 deleteBranch: true,
97 want: []string{"pr", "merge", "--squash", "--delete-branch", "--", "42"},
98 },
99 {
100 name: "merge without delete",
101 prNumber: "10",
102 mergeMethod: "merge",
103 deleteBranch: false,
104 want: []string{"pr", "merge", "--merge", "--", "10"},
105 },
106 {
107 name: "with repo",
108 prNumber: "7",
109 mergeMethod: "rebase",
110 deleteBranch: true,
111 repo: "owner/repo",
112 want: []string{"pr", "merge", "--rebase", "--delete-branch", "--repo", "owner/repo", "--", "7"},
113 },
114 }
115
116 for _, tt := range tests {
117 t.Run(tt.name, func(t *testing.T) {
118 got := MergeArgs(tt.prNumber, tt.mergeMethod, tt.deleteBranch, tt.repo)
119 if !reflect.DeepEqual(got, tt.want) {
120 t.Errorf("MergeArgs() = %v, want %v", got, tt.want)
121 }
122 })
123 }
124}
125
126func TestParseCIStatus(t *testing.T) {
127 tests := []struct {
128 name string
129 output string
130 want string
131 }{
132 {
133 name: "all passing",
134 output: "Build\tpass\t1m30s\thttps://example.com\nLint\tpass\t30s\thttps://example.com",
135 want: "passing",
136 },
137 {
138 name: "one failing",
139 output: "Build\tpass\t1m30s\thttps://example.com\nLint\tfail\t30s\thttps://example.com",
140 want: "failing",
141 },
142 {
143 name: "one pending",
144 output: "Build\tpass\t1m30s\thttps://example.com\nDeploy\t\t0\thttps://example.com",
145 want: "pending",
146 },
147 {
148 name: "skipped checks treated as passing",
149 output: "E2E Tests\tpass\t2m\thttps://example.com\nDispatch Preview Cleanup\tskipping\t0\thttps://example.com",
150 want: "passing",
151 },
152 {
153 name: "all skipped",
154 output: "Dispatch Preview Cleanup\tskipping\t0\thttps://example.com",
155 want: "passing",
156 },
157 {
158 name: "skipped with failure",
159 output: "Build\tfail\t1m\thttps://example.com\nCleanup\tskipping\t0\thttps://example.com",
160 want: "failing",
161 },
162 {
163 name: "empty output",
164 output: "",
165 want: "unknown",
166 },
167 }
168
169 for _, tt := range tests {
170 t.Run(tt.name, func(t *testing.T) {
171 got := ParseCIStatus(tt.output)
172 if got != tt.want {
173 t.Errorf("ParseCIStatus() = %q, want %q", got, tt.want)
174 }
175 })
176 }
177}
178
179func TestArgsAcceptFullURL(t *testing.T) {
180 url := "https://github.com/owner/repo/pull/42"
181 client := NewGHCLIClient("")
182
183 builders := []struct {
184 name string
185 fn func(string) []string
186 }{
187 {"ChecksArgs", client.ChecksArgs},
188 {"ViewStateArgs", client.ViewStateArgs},
189 {"ViewConflictsArgs", client.ViewConflictsArgs},
190 {"ViewReviewDecisionArgs", client.ViewReviewDecisionArgs},
191 }
192
193 for _, b := range builders {
194 t.Run(b.name, func(t *testing.T) {
195 args := b.fn(url)
196 last := args[len(args)-1]
197 if last != url {
198 t.Errorf("last arg = %q, want %q", last, url)
199 }
200 })
201 }
202}
203
204func TestGetCI_NoChecksConfigured(t *testing.T) {
205 // Create a fake "gh" that exits 1 with no stdout (simulates no CI checks).
206 dir := t.TempDir()
207 script := filepath.Join(dir, "gh")
208 if err := os.WriteFile(script, []byte("#!/bin/sh\necho \"no checks reported on the 'main' branch\" >&2\nexit 1\n"), 0o755); err != nil {
209 t.Fatal(err)
210 }
211
212 // Prepend our fake gh to PATH so exec.Command("gh", ...) finds it first.
213 origPath := os.Getenv("PATH")
214 t.Setenv("PATH", dir+string(filepath.ListSeparator)+origPath)
215
216 client := NewGHCLIClient("")
217 got := client.GetCI(nil, "42")
218 if got != "passing" {
219 t.Errorf("GetCI() with no checks configured = %q, want %q", got, "passing")
220 }
221}
222
223func TestGetCI_EmptyStdoutSuccess(t *testing.T) {
224 // Simulate gh pr checks returning exit 0 with empty stdout (e.g., GraphQL rate limiting).
225 dir := t.TempDir()
226 script := filepath.Join(dir, "gh")
227 if err := os.WriteFile(script, []byte("#!/bin/sh\nexit 0\n"), 0o755); err != nil {
228 t.Fatal(err)
229 }
230
231 origPath := os.Getenv("PATH")
232 t.Setenv("PATH", dir+string(filepath.ListSeparator)+origPath)
233
234 client := NewGHCLIClient("")
235 got := client.GetCI(nil, "42")
236 if got != "unknown" {
237 t.Errorf("GetCI() with empty stdout = %q, want %q", got, "unknown")
238 }
239}
240
241func TestGetCI_GhCommandFails(t *testing.T) {
242 // Create a fake "gh" that exits 1 with a non-"no checks" error on stderr.
243 dir := t.TempDir()
244 script := filepath.Join(dir, "gh")
245 if err := os.WriteFile(script, []byte("#!/bin/sh\necho \"authentication required\" >&2\nexit 1\n"), 0o755); err != nil {
246 t.Fatal(err)
247 }
248
249 origPath := os.Getenv("PATH")
250 t.Setenv("PATH", dir+string(filepath.ListSeparator)+origPath)
251
252 client := NewGHCLIClient("")
253 got := client.GetCI(nil, "42")
254 if got != "unknown" {
255 t.Errorf("GetCI() with gh failure = %q, want %q", got, "unknown")
256 }
257}
258
259func TestUpdateBranchArgs(t *testing.T) {
260 tests := []struct {
261 name string
262 prNumber string
263 repo string
264 want []string
265 }{
266 {
267 name: "no repo",
268 prNumber: "42",
269 want: []string{"pr", "update-branch", "--", "42"},
270 },
271 {
272 name: "with repo",
273 prNumber: "734",
274 repo: "owner/repo",
275 want: []string{"pr", "update-branch", "--repo", "owner/repo", "--", "734"},
276 },
277 }
278
279 for _, tt := range tests {
280 t.Run(tt.name, func(t *testing.T) {
281 got := UpdateBranchArgs(tt.prNumber, tt.repo)
282 if !reflect.DeepEqual(got, tt.want) {
283 t.Errorf("UpdateBranchArgs() = %v, want %v", got, tt.want)
284 }
285 })
286 }
287}