gunk-dev/the-valley

status_test.go

source28abc5737165main's published tip history
8.2 KiB
1package cmd
2
3import (
4 "context"
5 "reflect"
6 "testing"
7
8 gh "the-valley/flow/internal/github"
9 "the-valley/flow/internal/run"
10)
11
12func TestComputeMergeStatus(t *testing.T) {
13 tests := []struct {
14 name string
15 ci string
16 conflicts string
17 reviewDecision string
18 behind int
19 want string
20 }{
21 {"all green approved", "passing", "none", "APPROVED", 0, "ready"},
22 {"all green no review", "passing", "none", "", 0, "ready"},
23 {"ci failing", "failing", "none", "APPROVED", 0, "blocked"},
24 {"conflicts", "passing", "yes", "APPROVED", 0, "blocked"},
25 {"changes requested", "passing", "none", "CHANGES_REQUESTED", 0, "blocked"},
26 {"ci pending", "pending", "none", "APPROVED", 0, "pending"},
27 {"review unknown", "passing", "none", "unknown", 0, "pending"},
28 {"ci failing and conflicts", "failing", "yes", "", 0, "blocked"},
29 {"changes requested case insensitive", "passing", "none", "changes_requested", 0, "blocked"},
30 {"unknown CI unknown review", "unknown", "none", "unknown", 0, "pending"},
31 {"unknown CI no conflicts", "unknown", "none", "", 0, "pending"},
32 {"ready but behind", "passing", "none", "APPROVED", 2, "behind 2"},
33 {"behind ignored when not ready", "failing", "none", "APPROVED", 2, "blocked"},
34 }
35
36 for _, tt := range tests {
37 t.Run(tt.name, func(t *testing.T) {
38 got := computeMergeStatus(tt.ci, tt.conflicts, tt.reviewDecision, tt.behind)
39 if got != tt.want {
40 t.Errorf("computeMergeStatus(%q, %q, %q, %d) = %q, want %q",
41 tt.ci, tt.conflicts, tt.reviewDecision, tt.behind, got, tt.want)
42 }
43 })
44 }
45}
46
47func TestExtractPRNumber(t *testing.T) {
48 tests := []struct {
49 name string
50 prURL *string
51 want string
52 }{
53 {"nil URL", nil, ""},
54 {"valid URL", strPtr("https://github.com/owner/repo/pull/42"), "42"},
55 {"URL with trailing number", strPtr("https://github.com/owner/repo/pull/123"), "123"},
56 {"no slash in URL", strPtr("nourl"), ""},
57 {"single segment URL", strPtr("42"), ""},
58 {"trailing slash stripped", strPtr("https://github.com/owner/repo/pull/99"), "99"},
59 }
60
61 for _, tt := range tests {
62 t.Run(tt.name, func(t *testing.T) {
63 s := &run.State{PRURL: tt.prURL}
64 got := extractPRNumber(s)
65 if got != tt.want {
66 t.Errorf("extractPRNumber(%v) = %q, want %q", tt.prURL, got, tt.want)
67 }
68 })
69 }
70}
71
72func TestFormatPR(t *testing.T) {
73 tests := []struct {
74 name string
75 prURL *string
76 want string
77 }{
78 {"nil URL", nil, "-"},
79 {"typical URL", strPtr("https://github.com/owner/repo/pull/22"), "#22"},
80 }
81
82 for _, tt := range tests {
83 t.Run(tt.name, func(t *testing.T) {
84 s := &run.State{PRURL: tt.prURL}
85 got := formatPR(s)
86 if got != tt.want {
87 t.Errorf("formatPR() = %q, want %q", got, tt.want)
88 }
89 })
90 }
91}
92
93func TestDetermineStatus(t *testing.T) {
94 tests := []struct {
95 name string
96 s *run.State
97 want string
98 }{
99 {
100 name: "no PR URL returns exited",
101 s: &run.State{},
102 want: "exited",
103 },
104 {
105 name: "with PR URL returns pr-created",
106 s: &run.State{PRURL: strPtr("https://github.com/owner/repo/pull/1")},
107 want: "pr-created",
108 },
109 {
110 name: "pushed branch without PR returns awaiting-integration",
111 s: &run.State{PushedBranch: strPtr("agent/x"), PushedSHA: strPtr("0123456789abcdef")},
112 want: "awaiting-integration",
113 },
114 {
115 name: "salvaged pushed branch still needs attention",
116 s: &run.State{NeedsAttention: strPtr("no_pr"), PushedBranch: strPtr("agent/x"), PushedSHA: strPtr("0123456789abcdef")},
117 want: "needs-attention",
118 },
119 {
120 name: "merged pushed branch returns merged",
121 s: &run.State{MergedAt: strPtr("2026-10-05T03:00:00Z"), PushedBranch: strPtr("agent/x"), PushedSHA: strPtr("0123456789abcdef")},
122 want: "merged",
123 },
124 {
125 name: "merged run that took on others' attention and failed still needs attention",
126 s: &run.State{MergedAt: strPtr("2026-10-05T03:00:00Z"), PRURL: strPtr("https://github.com/owner/repo/pull/1"),
127 TookOver: []string{"20261005-0200-aaaa"}, FailureReason: strPtr("error_during_execution")},
128 want: "needs-attention",
129 },
130 {
131 name: "session type with missing worktree returns ended",
132 s: &run.State{Type: "session", Worktree: "/nonexistent/path/that/does/not/exist"},
133 want: "ended",
134 },
135 }
136
137 ctx := context.Background()
138 tc := &fakeTmux{}
139 for _, tt := range tests {
140 t.Run(tt.name, func(t *testing.T) {
141 got := determineStatus(ctx, tt.s, tc)
142 if got != tt.want {
143 t.Errorf("determineStatus() = %q, want %q", got, tt.want)
144 }
145 })
146 }
147}
148
149func TestTruncate(t *testing.T) {
150 tests := []struct {
151 input string
152 max int
153 want string
154 }{
155 {"short", 10, "short"},
156 {"exactly10!", 10, "exactly10!"},
157 {"this is a longer string", 10, "this is a ..."},
158 {"this is a long string", 10, "this is a ..."},
159 }
160
161 for _, tt := range tests {
162 t.Run(tt.input, func(t *testing.T) {
163 got := truncate(tt.input, tt.max)
164 if got != tt.want {
165 t.Errorf("truncate(%q, %d) = %q, want %q", tt.input, tt.max, got, tt.want)
166 }
167 })
168 }
169}
170
171func TestFormatCost(t *testing.T) {
172 cost := 1.5
173 budget := "5"
174 tests := []struct {
175 name string
176 s *run.State
177 want string
178 }{
179 {"no cost or budget", &run.State{}, "-"},
180 {"with cost", &run.State{CostUSD: &cost}, "$1.50"},
181 {"with budget only", &run.State{Budget: &budget}, "<$5"},
182 }
183
184 for _, tt := range tests {
185 t.Run(tt.name, func(t *testing.T) {
186 got := formatCost(tt.s)
187 if got != tt.want {
188 t.Errorf("formatCost() = %q, want %q", got, tt.want)
189 }
190 })
191 }
192}
193
194func TestGHCommandArgOrder(t *testing.T) {
195 // Verify that all gh command builders place flags BEFORE the "--" separator
196 // and the PR ref AFTER it. This prevents the bug where flags after "--"
197 // are treated as positional arguments by gh.
198 client := gh.NewPRClient("")
199 tests := []struct {
200 name string
201 fn func(string) []string
202 want []string
203 }{
204 {
205 name: "getPRState args with number",
206 fn: client.ViewStateArgs,
207 want: []string{"pr", "view", "--json", "state", "-q", ".state", "--", "42"},
208 },
209 {
210 name: "getPRCI args with number",
211 fn: client.ChecksArgs,
212 want: []string{"pr", "checks", "--", "42"},
213 },
214 {
215 name: "getPRConflicts args with number",
216 fn: client.ViewConflictsArgs,
217 want: []string{"pr", "view", "--json", "mergeable", "-q", ".mergeable", "--", "42"},
218 },
219 {
220 name: "getPRReviewDecision args with number",
221 fn: client.ViewReviewDecisionArgs,
222 want: []string{"pr", "view", "--json", "reviewDecision", "-q", ".reviewDecision", "--", "42"},
223 },
224 }
225
226 for _, tt := range tests {
227 t.Run(tt.name, func(t *testing.T) {
228 got := tt.fn("42")
229 if !reflect.DeepEqual(got, tt.want) {
230 t.Errorf("args = %v, want %v", got, tt.want)
231 }
232
233 // Verify structural invariant: all flags appear before "--"
234 separatorIdx := -1
235 for i, arg := range got {
236 if arg == "--" {
237 separatorIdx = i
238 break
239 }
240 }
241 if separatorIdx == -1 {
242 t.Fatal("missing '--' separator")
243 }
244 for i := separatorIdx + 1; i < len(got); i++ {
245 if got[i][0] == '-' {
246 t.Errorf("flag %q found after '--' separator at index %d", got[i], i)
247 }
248 }
249 })
250 }
251}
252
253func TestGHCommandArgsAcceptURL(t *testing.T) {
254 // Verify that gh command builders work with full PR URLs, which allows
255 // gh to resolve PRs regardless of the current working directory.
256 url := "https://github.com/owner/repo/pull/42"
257 client := gh.NewPRClient("")
258 fns := []struct {
259 name string
260 fn func(string) []string
261 }{
262 {"ViewStateArgs", client.ViewStateArgs},
263 {"ChecksArgs", client.ChecksArgs},
264 {"ViewConflictsArgs", client.ViewConflictsArgs},
265 {"ViewReviewDecisionArgs", client.ViewReviewDecisionArgs},
266 }
267 for _, tt := range fns {
268 t.Run(tt.name, func(t *testing.T) {
269 args := tt.fn(url)
270 // The URL should be the last argument, after "--"
271 last := args[len(args)-1]
272 if last != url {
273 t.Errorf("last arg = %q, want %q", last, url)
274 }
275 })
276 }
277}
278
279func TestExtractPRRef(t *testing.T) {
280 tests := []struct {
281 name string
282 prURL *string
283 want string
284 }{
285 {"nil URL", nil, ""},
286 {"valid URL", strPtr("https://github.com/owner/repo/pull/42"), "https://github.com/owner/repo/pull/42"},
287 }
288 for _, tt := range tests {
289 t.Run(tt.name, func(t *testing.T) {
290 s := &run.State{PRURL: tt.prURL}
291 got := extractPRRef(s)
292 if got != tt.want {
293 t.Errorf("extractPRRef() = %q, want %q", got, tt.want)
294 }
295 })
296 }
297}
298
299func strPtr(s string) *string {
300 return &s
301}