gunk-dev/the-valley

draft_test.go

source60ad80d5a5a5main's published tip history
8.9 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"); c == nil {
230 t.Error("expected gh pr edit --remove-label call")
231 }
232}
233
234func TestClearBudgetPausedLabel_ToleratesNotFound(t *testing.T) {
235 r := &recorderRunner{}
236 r.addGHStub([]string{"--remove-label"}, "", fmt.Errorf("label not found"))
237 if err := ClearBudgetPausedLabel(context.Background(), r, "/tmp/wt", "owner/repo", "42"); err != nil {
238 t.Errorf("expected 'not found' to be tolerated, got %v", err)
239 }
240}
241
242func TestHasBudgetPausedLabel(t *testing.T) {
243 tests := []struct {
244 name string
245 output string
246 want bool
247 wantErr bool
248 }{
249 {"label present", "klaus:budget-paused\nother\n", true, false},
250 {"label absent", "other\nfoo\n", false, false},
251 {"no labels", "", false, false},
252 {"only the label", "klaus:budget-paused", true, false},
253 }
254 for _, tt := range tests {
255 t.Run(tt.name, func(t *testing.T) {
256 r := &recorderRunner{}
257 r.addGHStub([]string{"pr", "view"}, tt.output, nil)
258 got, err := HasBudgetPausedLabel(context.Background(), r, "/tmp/wt", "owner/repo", "42")
259 if (err != nil) != tt.wantErr {
260 t.Errorf("err = %v, wantErr %v", err, tt.wantErr)
261 }
262 if got != tt.want {
263 t.Errorf("got %v, want %v", got, tt.want)
264 }
265 })
266 }
267}
268
269func TestBudgetExhausted(t *testing.T) {
270 tests := []struct {
271 cost float64
272 cap float64
273 want bool
274 }{
275 {4.75, 5.00, true}, // at 95%
276 {4.95, 5.00, true}, // at 99%
277 {5.10, 5.00, true}, // overshoot
278 {4.00, 5.00, false}, // at 80%
279 {0.01, 5.00, false}, // tiny
280 {4.95, 0, false}, // unknown cap
281 {4.95, -1, false}, // bogus cap
282 }
283 for _, tt := range tests {
284 got := BudgetExhausted(tt.cost, tt.cap)
285 if got != tt.want {
286 t.Errorf("BudgetExhausted(%v, %v) = %v, want %v", tt.cost, tt.cap, got, tt.want)
287 }
288 }
289}
290
291func TestExtractPRNumberFromURL(t *testing.T) {
292 tests := []struct {
293 url string
294 want string
295 }{
296 {"https://github.com/owner/repo/pull/42", "42"},
297 {"https://github.com/owner/repo/pull/42\n", "42"},
298 {"https://github.com/owner/repo/pull/123/files", "123"},
299 {"not a url", ""},
300 {"", ""},
301 }
302 for _, tt := range tests {
303 got := extractPRNumberFromURL(tt.url)
304 if got != tt.want {
305 t.Errorf("extractPRNumberFromURL(%q) = %q, want %q", tt.url, got, tt.want)
306 }
307 }
308}
309
310func TestTitleFromPrompt(t *testing.T) {
311 tests := []struct {
312 prompt string
313 runID string
314 want string
315 }{
316 {"fix the auth bug", "id", "[budget-paused] fix the auth bug"},
317 {"line one\nline two", "id", "[budget-paused] line one"},
318 {"", "myid", "klaus run myid (budget paused)"},
319 {strings.Repeat("x", 100), "id", "[budget-paused] " + strings.Repeat("x", 72)},
320 }
321 for _, tt := range tests {
322 got := titleFromPrompt(tt.prompt, tt.runID)
323 if got != tt.want {
324 t.Errorf("titleFromPrompt(%q, %q) = %q, want %q", tt.prompt, tt.runID, got, tt.want)
325 }
326 }
327}