gunk-dev/the-valley

draft_test.go

source6a6791929c36main's published tip history
10.5 KiB
1package draft
2
3import (
4 "context"
5 "fmt"
6 "strings"
7 "testing"
8
9 "the-valley/flow/internal/event"
10)
11
12// recorderRunner is a Runner that captures every git/gh call and lets each
13// test stub specific responses by command-prefix match.
14type recorderRunner struct {
15 gitCalls []recorded
16 ghCalls []recorded
17 gitStubs []stub
18 ghStubs []stub
19}
20
21type recorded struct {
22 workdir string
23 args []string
24}
25
26type stub struct {
27 // match: substrings that must all appear in the joined args (space-separated)
28 match []string
29 out string
30 err error
31}
32
33func (r *recorderRunner) addGitStub(match []string, out string, err error) {
34 r.gitStubs = append(r.gitStubs, stub{match: match, out: out, err: err})
35}
36func (r *recorderRunner) addGHStub(match []string, out string, err error) {
37 r.ghStubs = append(r.ghStubs, stub{match: match, out: out, err: err})
38}
39
40func (r *recorderRunner) Git(_ context.Context, workdir string, args ...string) (string, error) {
41 r.gitCalls = append(r.gitCalls, recorded{workdir, args})
42 joined := strings.Join(args, " ")
43 for _, s := range r.gitStubs {
44 if allContain(joined, s.match) {
45 return s.out, s.err
46 }
47 }
48 return "", nil
49}
50
51func (r *recorderRunner) GH(_ context.Context, workdir string, args ...string) (string, error) {
52 r.ghCalls = append(r.ghCalls, recorded{workdir, args})
53 joined := strings.Join(args, " ")
54 for _, s := range r.ghStubs {
55 if allContain(joined, s.match) {
56 return s.out, s.err
57 }
58 }
59 return "", nil
60}
61
62func allContain(s string, parts []string) bool {
63 for _, p := range parts {
64 if !strings.Contains(s, p) {
65 return false
66 }
67 }
68 return true
69}
70
71// argsContainAll reports whether every part appears as a standalone arg.
72// Matches by exact arg equality so "pr" doesn't fuzzy-match "progress".
73func argsContainAll(args, parts []string) bool {
74 for _, p := range parts {
75 found := false
76 for _, a := range args {
77 if a == p {
78 found = true
79 break
80 }
81 }
82 if !found {
83 return false
84 }
85 }
86 return true
87}
88
89func (r *recorderRunner) findGitCall(parts ...string) *recorded {
90 for i := range r.gitCalls {
91 if argsContainAll(r.gitCalls[i].args, parts) {
92 return &r.gitCalls[i]
93 }
94 }
95 return nil
96}
97
98func (r *recorderRunner) findGHCall(parts ...string) *recorded {
99 for i := range r.ghCalls {
100 if argsContainAll(r.ghCalls[i].args, parts) {
101 return &r.ghCalls[i]
102 }
103 }
104 return nil
105}
106
107func TestHandleBudgetPause_CreatesNewPRWithDirtyWorktree(t *testing.T) {
108 r := &recorderRunner{}
109 // Dirty worktree.
110 r.addGitStub([]string{"status", "--porcelain"}, " M foo.go\n", nil)
111 // findPRForBranch returns nothing (no existing PR).
112 r.addGHStub([]string{"pr", "list", "--head"}, "", nil)
113 // gh pr create returns the new PR URL.
114 r.addGHStub([]string{"pr", "create"}, "https://github.com/owner/repo/pull/42\n", nil)
115
116 in := PauseInput{
117 RunID: "20260101-1200-aaaa",
118 Worktree: "/tmp/wt",
119 Branch: "agent/20260101-1200-aaaa",
120 Repo: "owner/repo",
121 Prompt: "fix the auth bug",
122 CostUSD: 4.95,
123 BudgetUSD: 5.00,
124 }
125 out, err := HandleBudgetPause(context.Background(), r, in)
126 if err != nil {
127 t.Fatalf("HandleBudgetPause error: %v", err)
128 }
129
130 if !out.CommittedWIP {
131 t.Error("expected CommittedWIP=true for dirty worktree")
132 }
133 if !out.CreatedNewPR {
134 t.Error("expected CreatedNewPR=true when no PR existed")
135 }
136 if out.PRNumber != "42" {
137 t.Errorf("expected PRNumber=42, got %q", out.PRNumber)
138 }
139 if out.PRURL != "https://github.com/owner/repo/pull/42" {
140 t.Errorf("expected PRURL set, got %q", out.PRURL)
141 }
142
143 // Verify the WIP commit happened.
144 if r.findGitCall("add", "-A") == nil {
145 t.Error("expected git add -A call")
146 }
147 if c := r.findGitCall("commit", "-m"); c == nil {
148 t.Error("expected git commit call")
149 } else {
150 // Commit message should reference the run ID.
151 if !strings.Contains(strings.Join(c.args, " "), "20260101-1200-aaaa") {
152 t.Errorf("commit message should include run ID, got %v", c.args)
153 }
154 }
155 // Verify the push.
156 if c := r.findGitCall("push", "--force-with-lease"); c == nil {
157 t.Error("expected git push --force-with-lease call")
158 }
159 // Verify label create.
160 if c := r.findGHCall("label", "create", event.BudgetPausedLabel); c == nil {
161 t.Error("expected gh label create call")
162 }
163 // Verify PR create with --draft.
164 if c := r.findGHCall("pr", "create", "--draft"); c == nil {
165 t.Error("expected gh pr create --draft call")
166 }
167 // Verify label apply.
168 if c := r.findGHCall("pr", "edit", "42", "--add-label"); c == nil {
169 t.Error("expected gh pr edit --add-label call")
170 }
171 // Verify PR comment.
172 if c := r.findGHCall("pr", "comment", "42"); c == nil {
173 t.Error("expected gh pr comment call")
174 }
175}
176
177func TestHandleBudgetPause_UsesExistingPR(t *testing.T) {
178 r := &recorderRunner{}
179 // Clean worktree.
180 r.addGitStub([]string{"status", "--porcelain"}, "", nil)
181 // lookupPR returns the existing PR.
182 r.addGHStub([]string{"pr", "view", "99", "--json"}, "99\nhttps://github.com/owner/repo/pull/99\n", nil)
183
184 in := PauseInput{
185 RunID: "20260101-1200-bbbb",
186 Worktree: "/tmp/wt",
187 Branch: "agent/20260101-1200-bbbb",
188 Repo: "owner/repo",
189 Prompt: "do the thing",
190 CostUSD: 4.99,
191 BudgetUSD: 5.00,
192 ExistingPR: "99",
193 }
194 out, err := HandleBudgetPause(context.Background(), r, in)
195 if err != nil {
196 t.Fatalf("HandleBudgetPause error: %v", err)
197 }
198
199 if out.CommittedWIP {
200 t.Error("expected CommittedWIP=false for clean worktree")
201 }
202 if out.CreatedNewPR {
203 t.Error("expected CreatedNewPR=false when PR existed")
204 }
205 if out.PRNumber != "99" {
206 t.Errorf("expected PRNumber=99, got %q", out.PRNumber)
207 }
208
209 // Should NOT have called pr create.
210 if r.findGHCall("pr", "create") != nil {
211 t.Error("should not call gh pr create when PR already exists")
212 }
213 // Should still apply label and comment.
214 if r.findGHCall("pr", "edit", "99", "--add-label") == nil {
215 t.Error("expected label apply on existing PR")
216 }
217 if r.findGHCall("pr", "comment", "99") == nil {
218 t.Error("expected PR comment on existing PR")
219 }
220}
221
222func TestClearBudgetPausedLabel_NoOpWhenAbsent(t *testing.T) {
223 r := &recorderRunner{}
224 // gh succeeds with no output — label not present is not an error for gh
225 // pr edit --remove-label, so we treat it as success.
226 if err := ClearBudgetPausedLabel(context.Background(), r, "/tmp/wt", "owner/repo", "42"); err != nil {
227 t.Fatalf("unexpected error: %v", err)
228 }
229 if c := r.findGHCall("pr", "edit", "42", "--remove-label", "klaus:budget-paused"); c == nil {
230 t.Error("expected gh pr edit --remove-label klaus:budget-paused call")
231 }
232 if c := r.findGHCall("pr", "edit", "42", "--remove-label", "flow:budget-paused"); c == nil {
233 t.Error("expected gh pr edit --remove-label flow:budget-paused call")
234 }
235}
236
237func TestClearBudgetPausedLabel_ToleratesNotFound(t *testing.T) {
238 r := &recorderRunner{}
239 r.addGHStub([]string{"--remove-label"}, "", fmt.Errorf("label not found"))
240 if err := ClearBudgetPausedLabel(context.Background(), r, "/tmp/wt", "owner/repo", "42"); err != nil {
241 t.Errorf("expected 'not found' to be tolerated, got %v", err)
242 }
243}
244
245func TestClearBudgetPausedLabels(t *testing.T) {
246 r := &recorderRunner{}
247 if err := ClearBudgetPausedLabels(context.Background(), r, "/tmp/wt", "owner/repo", "42", []string{"flow:budget-paused"}); err != nil {
248 t.Fatalf("unexpected error: %v", err)
249 }
250 if c := r.findGHCall("pr", "edit", "42", "--remove-label", "flow:budget-paused"); c == nil {
251 t.Error("expected gh pr edit --remove-label flow:budget-paused call")
252 }
253 if c := r.findGHCall("pr", "edit", "42", "--remove-label", "klaus:budget-paused"); c != nil {
254 t.Errorf("did not expect removal of absent label, got %v", c)
255 }
256}
257
258func TestHasBudgetPausedLabel(t *testing.T) {
259 tests := []struct {
260 name string
261 output string
262 want bool
263 wantLabels []string
264 wantErr bool
265 }{
266 {"legacy label present", "klaus:budget-paused\nother\n", true, []string{"klaus:budget-paused"}, false},
267 {"new label present", "flow:budget-paused\nother\n", true, []string{"flow:budget-paused"}, false},
268 {"both labels present", "klaus:budget-paused\nflow:budget-paused\n", true, []string{"klaus:budget-paused", "flow:budget-paused"}, false},
269 {"duplicate label deduplicated", "flow:budget-paused\nflow:budget-paused\n", true, []string{"flow:budget-paused"}, false},
270 {"label absent", "other\nfoo\n", false, nil, false},
271 {"no labels", "", false, nil, false},
272 {"only the label", "klaus:budget-paused", true, []string{"klaus:budget-paused"}, false},
273 }
274 for _, tt := range tests {
275 t.Run(tt.name, func(t *testing.T) {
276 r := &recorderRunner{}
277 r.addGHStub([]string{"pr", "view"}, tt.output, nil)
278 got, err := HasBudgetPausedLabel(context.Background(), r, "/tmp/wt", "owner/repo", "42")
279 if (err != nil) != tt.wantErr {
280 t.Errorf("err = %v, wantErr %v", err, tt.wantErr)
281 }
282 if got != tt.want {
283 t.Errorf("got %v, want %v", got, tt.want)
284 }
285 labels, err := BudgetPausedLabels(context.Background(), r, "/tmp/wt", "owner/repo", "42")
286 if (err != nil) != tt.wantErr {
287 t.Errorf("BudgetPausedLabels err = %v, wantErr %v", err, tt.wantErr)
288 }
289 if strings.Join(labels, ",") != strings.Join(tt.wantLabels, ",") {
290 t.Errorf("BudgetPausedLabels = %v, want %v", labels, tt.wantLabels)
291 }
292 })
293 }
294}
295
296func TestBudgetExhausted(t *testing.T) {
297 tests := []struct {
298 cost float64
299 cap float64
300 want bool
301 }{
302 {4.75, 5.00, true}, // at 95%
303 {4.95, 5.00, true}, // at 99%
304 {5.10, 5.00, true}, // overshoot
305 {4.00, 5.00, false}, // at 80%
306 {0.01, 5.00, false}, // tiny
307 {4.95, 0, false}, // unknown cap
308 {4.95, -1, false}, // bogus cap
309 }
310 for _, tt := range tests {
311 got := BudgetExhausted(tt.cost, tt.cap)
312 if got != tt.want {
313 t.Errorf("BudgetExhausted(%v, %v) = %v, want %v", tt.cost, tt.cap, got, tt.want)
314 }
315 }
316}
317
318func TestExtractPRNumberFromURL(t *testing.T) {
319 tests := []struct {
320 url string
321 want string
322 }{
323 {"https://github.com/owner/repo/pull/42", "42"},
324 {"https://github.com/owner/repo/pull/42\n", "42"},
325 {"https://github.com/owner/repo/pull/123/files", "123"},
326 {"not a url", ""},
327 {"", ""},
328 }
329 for _, tt := range tests {
330 got := extractPRNumberFromURL(tt.url)
331 if got != tt.want {
332 t.Errorf("extractPRNumberFromURL(%q) = %q, want %q", tt.url, got, tt.want)
333 }
334 }
335}
336
337func TestTitleFromPrompt(t *testing.T) {
338 tests := []struct {
339 prompt string
340 runID string
341 want string
342 }{
343 {"fix the auth bug", "id", "[budget-paused] fix the auth bug"},
344 {"line one\nline two", "id", "[budget-paused] line one"},
345 {"", "myid", "klaus run myid (budget paused)"},
346 {strings.Repeat("x", 100), "id", "[budget-paused] " + strings.Repeat("x", 72)},
347 }
348 for _, tt := range tests {
349 got := titleFromPrompt(tt.prompt, tt.runID)
350 if got != tt.want {
351 t.Errorf("titleFromPrompt(%q, %q) = %q, want %q", tt.prompt, tt.runID, got, tt.want)
352 }
353 }
354}