gunk-dev/the-valley

ready_test.go

sourcecfedff357a41main's published tip history
10.4 KiB
1//go:build e2e
2
3package e2e
4
5import (
6 "fmt"
7 "os"
8 "path/filepath"
9 "strings"
10 "testing"
11 "time"
12
13 "the-valley/flow/internal/event"
14 "the-valley/flow/internal/run"
15)
16
17// TestReadyAtFinalizeOffLeavesDraftForKlausReady turns ready_at_finalize off,
18// in the project's config or in ~/.klaus/config.json, and lets the real
19// finalize run against a fake valley: it marks the pushed head ready and
20// returns the change to draft at once, records the draft and emits
21// agent:change-draft, not agent:change-ready. klaus status and the dashboard
22// show the change awaiting review, with no approve command. klaus ready, by
23// the change's handle, marks the change's head ready, clears the draft on
24// the run and emits agent:change-ready; status then shows it awaiting
25// approval. While a follow-up runs on the change, klaus ready refuses; once
26// it has pushed a newer head, klaus ready --head the reviewed head refuses,
27// and --head the newer one readies exactly that. The
28// project config lives only in the host checkout, uncommitted, so finalize
29// reads it there, not from the agent's worktree.
30func TestReadyAtFinalizeOffLeavesDraftForKlausReady(t *testing.T) {
31 for _, tc := range []struct {
32 name string
33 global bool
34 }{
35 {name: "project config"},
36 {name: "global config", global: true},
37 } {
38 t.Run(tc.name, func(t *testing.T) {
39 t.Parallel()
40 h := NewHarness(t)
41 h.installValleyStub()
42 h.joinValley()
43 if err := os.WriteFile(filepath.Join(h.E2EDir, "valley.has-unready"), nil, 0o644); err != nil {
44 t.Fatal(err)
45 }
46 h.AmendRepoConfig(map[string]any{
47 "dashboard": map[string]any{"approve_command": "valley approve {handle} --head {head}"},
48 })
49 if tc.global {
50 if err := os.MkdirAll(filepath.Join(h.Home, ".klaus"), 0o755); err != nil {
51 t.Fatal(err)
52 }
53 if err := os.WriteFile(filepath.Join(h.Home, ".klaus", "config.json"), []byte(`{"ready_at_finalize": false}`), 0o644); err != nil {
54 t.Fatal(err)
55 }
56 } else {
57 h.AmendRepoConfig(map[string]any{"ready_at_finalize": false})
58 }
59
60 res := h.RunKlaus("launch", "add a health check endpoint")
61 if res.ExitCode != 0 {
62 t.Fatalf("launch exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr)
63 }
64 id := h.RunIDs()[0]
65 h.WaitForClaudeStart(30 * time.Second)
66 st, err := h.ReadState(id)
67 if err != nil {
68 t.Fatal(err)
69 }
70 worktree, branch := st.Worktree, st.Branch
71 if err := os.WriteFile(filepath.Join(worktree, "health.go"), []byte("package health\n"), 0o644); err != nil {
72 t.Fatal(err)
73 }
74 h.git(worktree, "add", "-A")
75 h.git(worktree, "commit", "-qm", "Add a health check")
76 h.git(worktree, "push", "-q", "origin", branch)
77 head := h.GitOutput(worktree, "rev-parse", "HEAD")
78
79 h.ReleaseClaude()
80 st = h.WaitForState(id, func(s *run.State) bool {
81 return s.DurationMS != nil && s.TmuxPane == nil && s.Worktree == ""
82 }, 60*time.Second)
83
84 // Finalize readies the head, then returns the change to draft.
85 calls := append(h.valleyCalls("ready"), h.valleyCalls("unready")...)
86 want := []string{
87 fmt.Sprintf("%s: ready --change %s --head %s %s", worktree, st.ChangeID, head, branch),
88 fmt.Sprintf("%s: unready %s", worktree, st.ChangeID),
89 }
90 if strings.Join(calls, "\n") != strings.Join(want, "\n") {
91 t.Fatalf("valley calls = %q, want %q", calls, want)
92 }
93 if st.Valley == nil || *st.Valley != (run.Valley{Repo: "widget", Handle: "nxvp", Version: 1, Title: "Add a health check", Draft: true}) {
94 t.Fatalf("recorded valley = %+v, want a draft of widget nxvp v1", st.Valley)
95 }
96 var types []string
97 var draft map[string]any
98 for _, e := range h.runEvents(id) {
99 types = append(types, e.Type)
100 if e.Type == event.AgentChangeDraft {
101 draft = e.Data
102 }
103 }
104 wantTypes := []string{event.AgentCompleted, event.AgentBranchPushed, event.AgentChangeDraft, event.PhaseCompleted}
105 if len(types) < len(wantTypes) || strings.Join(types[len(types)-len(wantTypes):], ",") != strings.Join(wantTypes, ",") {
106 t.Errorf("events = %v, want them to end with %v", types, wantTypes)
107 }
108 for _, typ := range types {
109 if typ == event.AgentChangeReady {
110 t.Errorf("a draft must not emit %s: events %v", typ, types)
111 }
112 }
113 if draft["handle"] != "nxvp" || draft["head"] != head || draft["version"] != float64(1) || draft["change_id"] != st.ChangeID {
114 t.Errorf("agent:change-draft data = %v", draft)
115 }
116
117 // The valley lists the change in state, with a version at each
118 // head on the run's branch, the last active.
119 listing := func(state string, heads ...string) {
120 var vs []map[string]any
121 for i, hd := range heads {
122 v := version(i+1, hd, "Add a health check", "not-landed")
123 v["branch"] = branch
124 vs = append(vs, v)
125 }
126 h.writeValleyChanges(map[string]any{
127 "schema": "valley-changes/v1",
128 "host": "valley-host",
129 "repos": []any{map[string]any{
130 "repo": "widget", "url": "git@valley-host:widget.git", "error": nil,
131 "changes": []any{change(st, state, len(vs), vs...)},
132 "branches": []any{},
133 }},
134 })
135 }
136 listing("draft", head)
137 res = h.RunKlaus("status")
138 if res.ExitCode != 0 {
139 t.Fatalf("status exited %d; stderr: %s", res.ExitCode, res.Stderr)
140 }
141 if got := statusOf(t, res.Stdout, id); got != "awaiting-review" {
142 t.Errorf("status of the draft = %q, want awaiting-review:\n%s", got, res.Stdout)
143 }
144
145 if !tc.global {
146 // The dashboard: REVIEW with the pipeline, no approve
147 // command, and the klaus ready that readies it.
148 _, screen := h.startDashboard(170)
149 s := screen("1 pipeline", "REVIEW v1@"+head[:7], "valley flow ready nxvp --head "+head[:7])
150 if strings.Contains(s, "approve: ") || strings.Contains(s, "READY") {
151 t.Errorf("a draft awaiting review must offer no approve command:\n%s", s)
152 }
153 }
154
155 res = h.RunKlaus("ready", "nxvp")
156 if res.ExitCode != 0 {
157 t.Fatalf("klaus ready exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr)
158 }
159 if got, want := strings.TrimSpace(res.Stdout), "widget nxvp v1 ready at "+head+" (run "+id+")"; got != want {
160 t.Errorf("klaus ready printed %q, want %q", got, want)
161 }
162 readies := h.valleyCalls("ready")
163 if want := fmt.Sprintf("%s: ready --change %s --head %s %s", h.RepoDir, st.ChangeID, head, branch); len(readies) != 2 || readies[1] != want {
164 t.Errorf("valley ready calls = %q, want the second to be %q", readies, want)
165 }
166 st, err = h.ReadState(id)
167 if err != nil {
168 t.Fatal(err)
169 }
170 if st.Valley == nil || *st.Valley != (run.Valley{Repo: "widget", Handle: "nxvp", Version: 1, Title: "Add a health check"}) {
171 t.Errorf("after klaus ready, recorded valley = %+v, want widget nxvp v1 ready", st.Valley)
172 }
173 var ready map[string]any
174 for _, e := range h.runEvents(id) {
175 if e.Type == event.AgentChangeReady {
176 ready = e.Data
177 }
178 }
179 if ready == nil || ready["via"] != "klaus ready" || ready["handle"] != "nxvp" || ready["head"] != head ||
180 ready["version"] != float64(1) || ready["change_id"] != st.ChangeID || ready["title"] != "Add a health check" {
181 t.Errorf("agent:change-ready data = %v", ready)
182 }
183
184 listing("ready", head)
185 res = h.RunKlaus("status")
186 if got := statusOf(t, res.Stdout, id); got != "awaiting-approval" {
187 t.Errorf("status once ready = %q, want awaiting-approval:\n%s", got, res.Stdout)
188 }
189 if tc.global {
190 return
191 }
192
193 // While a follow-up runs on the change, klaus ready refuses,
194 // naming it, and asks the valley nothing.
195 for _, f := range []string{"claude.started", "claude.release"} {
196 if err := os.Remove(filepath.Join(h.E2EDir, f)); err != nil {
197 t.Fatal(err)
198 }
199 }
200 h.RegisterProject("acme/widget", h.RepoDir)
201 waitNextSecond()
202 follow, _ := h.launchRun("--repo", "widget", "--change", id, "test the health check")
203 h.WaitForClaudeStart(30 * time.Second)
204 res = h.RunKlaus("ready", "nxvp")
205 if res.ExitCode == 0 || !strings.Contains(res.Stderr, "run "+follow.ID+" is still running on change nxvp") {
206 t.Errorf("klaus ready during a follow-up exited %d, want a refusal naming run %s\nstdout:\n%s\nstderr:\n%s",
207 res.ExitCode, follow.ID, res.Stdout, res.Stderr)
208 }
209 if n := len(h.valleyCalls("ready")); n != 2 {
210 t.Errorf("a refused klaus ready must not run valley ready: %d calls in all", n)
211 }
212
213 // The follow-up pushes after the review: v2, a draft at a newer
214 // head. klaus ready --head the reviewed head refuses, naming the
215 // newer one, and asks the valley nothing; --head the newer one,
216 // once reviewed, readies exactly it.
217 newer := h.pushWork(follow, "health_test.go", "Test the health check")
218 h.finishRun(follow.ID)
219 if n := len(h.valleyCalls("ready")); n != 3 {
220 t.Fatalf("the follow-up's finalize should have run valley ready: %d calls in all", n)
221 }
222 // While the valley lists only v1, as when the follow-up's ready
223 // failed, origin's branch says the change moved on.
224 listing("draft", head)
225 res = h.RunKlaus("ready", "nxvp", "--head", head[:12])
226 if want := "change nxvp's current head is " + newer + " (origin's " + branch + ", a push the valley doesn't hold as a version yet), not " + head[:12]; res.ExitCode == 0 || !strings.Contains(res.Stderr, want) {
227 t.Errorf("klaus ready --head <reviewed> past an unlisted push exited %d, want a refusal: %q\nstdout:\n%s\nstderr:\n%s",
228 res.ExitCode, want, res.Stdout, res.Stderr)
229 }
230 listing("draft", head, newer)
231 res = h.RunKlaus("ready", "nxvp", "--head", head[:12])
232 if res.ExitCode == 0 || !strings.Contains(res.Stderr, "change nxvp's current head is "+newer+", not "+head[:12]) {
233 t.Errorf("klaus ready --head <reviewed> after a newer push exited %d, want a refusal naming %s\nstdout:\n%s\nstderr:\n%s",
234 res.ExitCode, newer, res.Stdout, res.Stderr)
235 }
236 if n := len(h.valleyCalls("ready")); n != 3 {
237 t.Errorf("a refused klaus ready --head must not run valley ready: %d calls in all", n)
238 }
239 res = h.RunKlaus("ready", "nxvp", "--head", newer)
240 if res.ExitCode != 0 {
241 t.Fatalf("klaus ready --head <newer> exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr)
242 }
243 if got, want := strings.TrimSpace(res.Stdout), "widget nxvp v1 ready at "+newer+" (run "+follow.ID+")"; got != want {
244 t.Errorf("klaus ready --head printed %q, want %q", got, want)
245 }
246 readies = h.valleyCalls("ready")
247 if want := fmt.Sprintf("%s: ready --change %s --head %s %s", h.RepoDir, st.ChangeID, newer, branch); len(readies) != 4 || readies[3] != want {
248 t.Errorf("valley ready calls = %q, want the fourth to be %q", readies, want)
249 }
250 })
251 }
252}