gunk-dev/the-valley

approve_test.go

sourced9f7bacd171bmain's published tip history
7.4 KiB
1package cmd
2
3import (
4 "testing"
5
6 "the-valley/flow/internal/event"
7 "the-valley/flow/internal/run"
8)
9
10func TestApproveByPRNumbers(t *testing.T) {
11 tmpDir := t.TempDir()
12 store := run.NewHomeDirStoreFromPath(tmpDir)
13 if err := store.EnsureDirs(); err != nil {
14 t.Fatalf("EnsureDirs: %v", err)
15 }
16
17 prURL42 := "https://github.com/owner/repo/pull/42"
18 prURL99 := "https://github.com/owner/repo/pull/99"
19
20 states := []*run.State{
21 {ID: "20260101-0000-aaaa", Prompt: "fix bug", Branch: "b1", PRURL: &prURL42, CreatedAt: "2026-01-01T00:00:00Z"},
22 {ID: "20260101-0000-bbbb", Prompt: "add feature", Branch: "b2", PRURL: &prURL99, CreatedAt: "2026-01-01T00:01:00Z"},
23 }
24 for _, s := range states {
25 if err := store.Save(s); err != nil {
26 t.Fatalf("Save: %v", err)
27 }
28 }
29
30 allStates, _ := store.List()
31 err := approveByPRNumbers([]string{"42"}, allStates, store)
32 if err != nil {
33 t.Fatalf("approveByPRNumbers() error = %v", err)
34 }
35
36 // Reload and verify
37 s42, err := store.Load("20260101-0000-aaaa")
38 if err != nil {
39 t.Fatalf("Load: %v", err)
40 }
41 if s42.Approved == nil || !*s42.Approved {
42 t.Error("PR #42 should be approved")
43 }
44 if s42.ApprovedAt == nil {
45 t.Error("PR #42 should have ApprovedAt set")
46 }
47
48 // PR #99 should not be approved
49 s99, err := store.Load("20260101-0000-bbbb")
50 if err != nil {
51 t.Fatalf("Load: %v", err)
52 }
53 if s99.Approved != nil {
54 t.Error("PR #99 should not be approved")
55 }
56}
57
58func TestApproveAll(t *testing.T) {
59 tmpDir := t.TempDir()
60 store := run.NewHomeDirStoreFromPath(tmpDir)
61 if err := store.EnsureDirs(); err != nil {
62 t.Fatalf("EnsureDirs: %v", err)
63 }
64
65 prURL42 := "https://github.com/owner/repo/pull/42"
66 prURL99 := "https://github.com/owner/repo/pull/99"
67 mergedAt := "2026-01-01T00:00:00Z"
68
69 states := []*run.State{
70 {ID: "20260101-0000-aaaa", Prompt: "fix bug", Branch: "b1", PRURL: &prURL42, CreatedAt: "2026-01-01T00:00:00Z"},
71 {ID: "20260101-0000-bbbb", Prompt: "add feature", Branch: "b2", PRURL: &prURL99, CreatedAt: "2026-01-01T00:01:00Z", MergedAt: &mergedAt},
72 {ID: "20260101-0000-cccc", Prompt: "no pr", Branch: "b3", CreatedAt: "2026-01-01T00:02:00Z"},
73 }
74 for _, s := range states {
75 if err := store.Save(s); err != nil {
76 t.Fatalf("Save: %v", err)
77 }
78 }
79
80 allStates, _ := store.List()
81 err := approveAll(allStates, store)
82 if err != nil {
83 t.Fatalf("approveAll() error = %v", err)
84 }
85
86 // PR #42 should be approved (has PR URL, not merged)
87 s42, _ := store.Load("20260101-0000-aaaa")
88 if s42.Approved == nil || !*s42.Approved {
89 t.Error("PR #42 should be approved")
90 }
91
92 // PR #99 should NOT be approved (already merged)
93 s99, _ := store.Load("20260101-0000-bbbb")
94 if s99.Approved != nil {
95 t.Error("already-merged PR #99 should not be approved")
96 }
97
98 // No-PR run should not be approved
99 scc, _ := store.Load("20260101-0000-cccc")
100 if scc.Approved != nil {
101 t.Error("run without PR should not be approved")
102 }
103}
104
105func TestFindRunByPR(t *testing.T) {
106 prURL42 := "https://github.com/owner/repo/pull/42"
107 states := []*run.State{
108 {ID: "20260101-0000-aaaa", Prompt: "fix bug", Branch: "b1", PRURL: &prURL42, CreatedAt: "2026-01-01T00:00:00Z"},
109 }
110
111 tmpDir := t.TempDir()
112 store := run.NewHomeDirStoreFromPath(tmpDir)
113 store.EnsureDirs()
114 store.Save(states[0])
115
116 // Should find by PR number
117 found, _, err := findRunByPR("42", states, store)
118 if err != nil {
119 t.Fatalf("findRunByPR() error = %v", err)
120 }
121 if found.ID != "20260101-0000-aaaa" {
122 t.Errorf("found wrong run: %s", found.ID)
123 }
124
125 // Should handle # prefix
126 found, _, err = findRunByPR("#42", states, store)
127 if err != nil {
128 t.Fatalf("findRunByPR() error = %v", err)
129 }
130 if found.ID != "20260101-0000-aaaa" {
131 t.Errorf("found wrong run: %s", found.ID)
132 }
133
134 // Should return error for missing PR
135 _, _, err = findRunByPR("999", states, store)
136 if err == nil {
137 t.Error("expected error for missing PR")
138 }
139}
140
141func TestShortID(t *testing.T) {
142 tests := []struct {
143 id string
144 want string
145 }{
146 {"20260101-0000-aaaa", "aaaa"},
147 {"abc", "abc"},
148 {"a-b-c-d", "d"},
149 }
150 for _, tt := range tests {
151 got := shortID(tt.id)
152 if got != tt.want {
153 t.Errorf("shortID(%q) = %q, want %q", tt.id, got, tt.want)
154 }
155 }
156}
157
158func TestMarkApproved(t *testing.T) {
159 tmpDir := t.TempDir()
160 store := run.NewHomeDirStoreFromPath(tmpDir)
161 store.EnsureDirs()
162
163 s := &run.State{ID: "20260101-0000-aaaa", Prompt: "fix", Branch: "b1", CreatedAt: "2026-01-01T00:00:00Z"}
164 store.Save(s)
165
166 err := markApproved(s, store)
167 if err != nil {
168 t.Fatalf("markApproved() error = %v", err)
169 }
170 if s.Approved == nil || !*s.Approved {
171 t.Error("should be approved")
172 }
173 if s.ApprovedAt == nil {
174 t.Error("should have ApprovedAt")
175 }
176
177 // Verify persisted
178 reloaded, _ := store.Load("20260101-0000-aaaa")
179 if reloaded.Approved == nil || !*reloaded.Approved {
180 t.Error("approval should be persisted")
181 }
182}
183
184// TestMarkApprovedEmitsEvent verifies that approval emits a PRApprovalChanged
185// event to the session event log. This is the invalidation signal the
186// dashboard consumes to wake the pipeline FSM without waiting for a GitHub
187// webhook (see internal/cmd/dashboard.go internalEventMsg handler).
188func TestMarkApprovedEmitsEvent(t *testing.T) {
189 tmpDir := t.TempDir()
190 store := run.NewHomeDirStoreFromPath(tmpDir)
191 if err := store.EnsureDirs(); err != nil {
192 t.Fatalf("EnsureDirs: %v", err)
193 }
194
195 prURL := "https://github.com/owner/repo/pull/42"
196 s := &run.State{
197 ID: "20260101-0000-aaaa",
198 Prompt: "fix",
199 Branch: "b1",
200 PRURL: &prURL,
201 CreatedAt: "2026-01-01T00:00:00Z",
202 }
203 if err := store.Save(s); err != nil {
204 t.Fatalf("Save: %v", err)
205 }
206
207 if err := markApproved(s, store); err != nil {
208 t.Fatalf("markApproved: %v", err)
209 }
210
211 log := event.NewLog(tmpDir)
212 events, err := log.Read()
213 if err != nil {
214 t.Fatalf("reading event log: %v", err)
215 }
216
217 var approval *event.Event
218 for i := range events {
219 if events[i].Type == event.PRApprovalChanged {
220 approval = &events[i]
221 break
222 }
223 }
224 if approval == nil {
225 t.Fatalf("expected a %s event, found %d events of other types", event.PRApprovalChanged, len(events))
226 }
227 if approval.RunID != s.ID {
228 t.Errorf("event run_id = %q, want %q", approval.RunID, s.ID)
229 }
230 if approval.Data["pr_number"] != "42" {
231 t.Errorf("event pr_number = %v, want 42", approval.Data["pr_number"])
232 }
233 if approval.Data["pr_url"] != prURL {
234 t.Errorf("event pr_url = %v, want %q", approval.Data["pr_url"], prURL)
235 }
236}
237
238// TestApproveByPRNumbersEmitsEvent verifies the event is emitted from the
239// real CLI entry point, not just the lower-level helper. This is the path
240// `klaus approve <num>` actually takes.
241func TestApproveByPRNumbersEmitsEvent(t *testing.T) {
242 tmpDir := t.TempDir()
243 store := run.NewHomeDirStoreFromPath(tmpDir)
244 if err := store.EnsureDirs(); err != nil {
245 t.Fatalf("EnsureDirs: %v", err)
246 }
247
248 prURL := "https://github.com/owner/repo/pull/42"
249 s := &run.State{
250 ID: "20260101-0000-aaaa",
251 Prompt: "fix",
252 Branch: "b1",
253 PRURL: &prURL,
254 CreatedAt: "2026-01-01T00:00:00Z",
255 }
256 if err := store.Save(s); err != nil {
257 t.Fatalf("Save: %v", err)
258 }
259
260 allStates, _ := store.List()
261 if err := approveByPRNumbers([]string{"42"}, allStates, store); err != nil {
262 t.Fatalf("approveByPRNumbers: %v", err)
263 }
264
265 log := event.NewLog(tmpDir)
266 events, err := log.Read()
267 if err != nil {
268 t.Fatalf("reading event log: %v", err)
269 }
270 found := false
271 for _, e := range events {
272 if e.Type == event.PRApprovalChanged && e.Data["pr_number"] == "42" {
273 found = true
274 }
275 }
276 if !found {
277 t.Errorf("expected %s event for PR #42, got events: %v", event.PRApprovalChanged, events)
278 }
279}