gunk-dev/the-valley

restacked_landing_test.go

sourcecaaa5bee5e11main's published tip history
11.9 KiB
1//go:build e2e
2
3package e2e
4
5import (
6 "bytes"
7 "os/exec"
8 "strings"
9 "testing"
10 "time"
11
12 "the-valley/flow/internal/valley"
13)
14
15// restackAsHost rebases change id onto origin/main and records the rebased
16// head as its next version, as the host's restacker does after a landing:
17// `valley rebase` under the restacker's principal rather than klaus's.
18func (h *Harness) restackAsHost(id string) {
19 h.t.Helper()
20 cmd := exec.Command("valley", "rebase", id)
21 cmd.Dir = h.RepoDir
22 cmd.Env = append(h.env(), "GIT_CONFIG_COUNT=1", "GIT_CONFIG_KEY_0=valley.principal", "GIT_CONFIG_VALUE_0=restacker")
23 var out bytes.Buffer
24 cmd.Stdout, cmd.Stderr = &out, &out
25 if err := cmd.Run(); err != nil {
26 h.t.Fatalf("valley rebase %s: %v\n%s", id, err, out.String())
27 }
28}
29
30// TestRestackedChangeLandsIntegrated drives a change the host restacks
31// twice and then lands at the restacked version, with a real launch against
32// the real valley CLI and bare repos. The run that readied v1 shows
33// integrated in klaus status, its change's dashboard row reads INTEGRATED in
34// the done group, and cleanup --done removes the run as finished.
35func TestRestackedChangeLandsIntegrated(t *testing.T) {
36 t.Parallel()
37 h := NewHarness(t)
38 h.useRealValley()
39 h.joinValley()
40 h.WriteDefaultClaudeOutput(`{"type":"result","subtype":"success","total_cost_usd":0.5,"duration_ms":1000,"session_id":"` + changeSession + `"}` + "\n")
41
42 first, _ := h.launchRun("add a greeting")
43 h.WaitForClaudeStart(30 * time.Second)
44 h.pushWork(first, "greeting.txt", "Add a greeting")
45 first = h.finishRun(first.ID)
46 if first.Valley == nil || first.Valley.Version != 1 || first.Valley.Handle == "" {
47 t.Fatalf("finalize should mark v1 ready: %+v", first.Valley)
48 }
49 id, handle := first.ChangeID, first.Valley.Handle
50
51 // Two other changes land, and the host restacks this one after each.
52 h.commitToMain("one.txt", "one\n")
53 h.restackAsHost(id)
54 h.commitToMain("two.txt", "two\n")
55 h.restackAsHost(id)
56 ch := h.changeOf(id)
57 if ch.Active != 3 || len(ch.Versions) != 3 || ch.Versions[2].Uploader != "restacker" {
58 t.Fatalf("the restacker should have recorded v2 and v3: %+v", ch)
59 }
60
61 // v3 lands.
62 h.git(h.OriginDir, "update-ref", "refs/heads/main", ch.Versions[2].Head)
63 if ch := h.changeOf(id); ch.Versions[0].Landing != valley.NotLanded || ch.Versions[2].Landing != valley.Landed {
64 t.Fatalf("the valley should say v3 landed and v1 didn't: %+v", ch.Versions)
65 }
66
67 res := h.RunKlaus("status")
68 if got := statusOf(t, res.Stdout, first.ID); got != "integrated" {
69 t.Errorf("the run that readied v1 shows %q, want integrated:\n%s%s", got, res.Stdout, res.Stderr)
70 }
71
72 _, screen := h.startDashboard(170)
73 s := screen("1 done", "INTEGRATED")
74 if row := rowOf(t, s, handle); !strings.Contains(row, "INTEGRATED") || !strings.Contains(row, "v3@") {
75 t.Errorf("the change's row should read INTEGRATED at v3: %q", row)
76 }
77
78 res = h.RunKlaus("cleanup", "--done")
79 if res.ExitCode != 0 || !strings.Contains(res.Stdout, "removed "+first.ID+" ") {
80 t.Errorf("cleanup --done should remove the run of the landed change (exit %d):\n%s%s", res.ExitCode, res.Stdout, res.Stderr)
81 }
82}
83
84// landOnMain lands head on origin's main and keeps main's content: main
85// becomes a commit on head alone holding head's tree merged with main's, so
86// head is on main's first-parent history with no merge above it, and the
87// valley reads head's version landed. The integrator's own landing, a commit
88// with a signed transfer statement, needs the integrator's key.
89func (h *Harness) landOnMain(head string) {
90 h.t.Helper()
91 tree := h.GitOutput(h.OriginDir, "merge-tree", "--write-tree", head, "main")
92 tip := h.GitOutput(h.OriginDir, "commit-tree", tree, "-p", head, "-m", "land "+head)
93 h.git(h.OriginDir, "update-ref", "refs/heads/main", tip)
94}
95
96// TestChangeLandedBehindItsActiveVersionIntegrated drives a change whose
97// approved restack lands after the host recorded another, with real
98// launches against the real valley CLI and bare repos. Runs ready v1 and
99// v2, the host restacks the change as v3, v3 is approved, another change
100// lands and the host restacks v3 as v4, and then v3's request lands. The
101// valley counts the change landed and `valley pending` leaves it out; both
102// runs show integrated in klaus status, the change's row reads INTEGRATED
103// in the done group at v4, and cleanup --done removes both runs.
104func TestChangeLandedBehindItsActiveVersionIntegrated(t *testing.T) {
105 t.Parallel()
106 h := NewHarness(t)
107 h.useRealValley()
108 h.joinValley()
109 h.WriteDefaultClaudeOutput(`{"type":"result","subtype":"success","total_cost_usd":0.5,"duration_ms":1000,"session_id":"` + changeSession + `"}` + "\n")
110
111 first, _ := h.launchRun("add a greeting")
112 h.WaitForClaudeStart(30 * time.Second)
113 h.pushWork(first, "greeting.txt", "Add a greeting")
114 first = h.finishRun(first.ID)
115 if first.Valley == nil || first.Valley.Version != 1 || first.Valley.Handle == "" {
116 t.Fatalf("finalize should mark v1 ready: %+v", first.Valley)
117 }
118 id, handle := first.ChangeID, first.Valley.Handle
119
120 time.Sleep(time.Second) // the later run is created in a later second
121 h.resetWorker()
122 later, _ := h.launchRun("--change", handle, "tidy the greeting")
123 h.WaitForClaudeStart(30 * time.Second)
124 h.pushWork(later, "greeting.txt", "Tidy the greeting")
125 later = h.finishRun(later.ID)
126 if later.Valley == nil || later.Valley.Version != 2 || later.ChangeID != id {
127 t.Fatalf("finalize should mark the later head ready as v2 of %s: %+v", id, later.Valley)
128 }
129
130 // The host restacks the change as v3 after another lands, and v3 is
131 // approved: its integration request names v3's head.
132 h.commitToMain("one.txt", "one\n")
133 h.restackAsHost(id)
134 v3 := h.changeOf(id).Versions[2]
135 h.git(h.OriginDir, "update-ref", "refs/the-valley/integration-requests/main/c-"+id, v3.Head)
136
137 // Another change lands before v3 does, and the host restacks v3 as v4.
138 h.commitToMain("two.txt", "two\n")
139 h.restackAsHost(id)
140 if ch := h.changeOf(id); ch.Active != 4 || ch.Versions[2].Request == nil || ch.Versions[3].Uploader != "restacker" {
141 t.Fatalf("the restacker should have recorded v4 after v3 was approved: %+v", ch)
142 }
143
144 h.landOnMain(v3.Head)
145 ch := h.changeOf(id)
146 if ch.State != valley.StateReady || ch.Active != 4 || ch.Versions[2].Landing != valley.Landed || ch.Versions[3].Landing != valley.NotLanded {
147 t.Fatalf("the valley should list the change ready at v4, with v3 landed and v4 not: %+v", ch)
148 }
149 if out := h.valleyIn(h.RepoDir, "pending", "--json"); strings.Contains(out, id) {
150 t.Fatalf("valley pending should count the change landed and leave it out:\n%s", out)
151 }
152
153 res := h.RunKlaus("status")
154 for _, r := range []string{first.ID, later.ID} {
155 if got := statusOf(t, res.Stdout, r); got != "integrated" {
156 t.Errorf("%s shows %q, want integrated:\n%s%s", r, got, res.Stdout, res.Stderr)
157 }
158 }
159
160 _, screen := h.startDashboard(170)
161 s := screen("1 done", "INTEGRATED")
162 if row := rowOf(t, s, handle); !strings.Contains(row, "INTEGRATED") || !strings.Contains(row, "v4@") {
163 t.Errorf("the change's row should read INTEGRATED at v4: %q", row)
164 }
165
166 res = h.RunKlaus("cleanup", "--done")
167 for _, r := range []string{first.ID, later.ID} {
168 if res.ExitCode != 0 || !strings.Contains(res.Stdout, "removed "+r+" ") {
169 t.Errorf("cleanup --done should remove %s, a run of the landed change (exit %d):\n%s%s", r, res.ExitCode, res.Stdout, res.Stderr)
170 }
171 }
172}
173
174// TestNextRoundAfterALandingStaysOpen drives a change that lands and is
175// then continued, with real launches against the real valley CLI and bare
176// repos. A run readies v1 and v1 lands; a later run continues the change on
177// top of the landing and readies v2, the change's next round, which the
178// valley reads unlanded (landed_version null). The later run shows
179// awaiting-approval, the change's row reads READY at v2, and cleanup --done
180// keeps the later run.
181func TestNextRoundAfterALandingStaysOpen(t *testing.T) {
182 t.Parallel()
183 h := NewHarness(t)
184 h.useRealValley()
185 h.joinValley()
186 h.WriteDefaultClaudeOutput(`{"type":"result","subtype":"success","total_cost_usd":0.5,"duration_ms":1000,"session_id":"` + changeSession + `"}` + "\n")
187
188 first, _ := h.launchRun("add a greeting")
189 h.WaitForClaudeStart(30 * time.Second)
190 h.pushWork(first, "greeting.txt", "Add a greeting")
191 first = h.finishRun(first.ID)
192 if first.Valley == nil || first.Valley.Version != 1 || first.Valley.Handle == "" {
193 t.Fatalf("finalize should mark v1 ready: %+v", first.Valley)
194 }
195 id, handle := first.ChangeID, first.Valley.Handle
196
197 h.git(h.OriginDir, "update-ref", "refs/heads/main", *first.PushedSHA)
198 ch := h.changeOf(id)
199 if !ch.LandedVersion.Listed {
200 skipWithout(t, "the valley on PATH lists no landed_version")
201 }
202 if ch.LandedVersion.N == nil || *ch.LandedVersion.N != 1 {
203 t.Fatalf("the valley should say the change landed as v1: %+v", ch)
204 }
205
206 time.Sleep(time.Second) // the later run is created in a later second
207 h.resetWorker()
208 later, _ := h.launchRun("--change", handle, "tidy the greeting")
209 h.WaitForClaudeStart(30 * time.Second)
210 h.pushWork(later, "greeting.txt", "Tidy the greeting")
211 later = h.finishRun(later.ID)
212 if later.Valley == nil || later.Valley.Version != 2 || later.ChangeID != id {
213 t.Fatalf("finalize should mark the later head ready as v2 of %s: %+v", id, later.Valley)
214 }
215 if ch := h.changeOf(id); ch.Active != 2 || !ch.LandedVersion.Listed || ch.LandedVersion.N != nil {
216 t.Fatalf("the valley should read the next round unlanded: %+v", ch)
217 }
218
219 res := h.RunKlaus("status")
220 if got := statusOf(t, res.Stdout, later.ID); got != "awaiting-approval" {
221 t.Errorf("the next round's run shows %q, want awaiting-approval:\n%s%s", got, res.Stdout, res.Stderr)
222 }
223
224 _, screen := h.startDashboard(170)
225 s := screen(handle, "READY")
226 if row := rowOf(t, s, handle); !strings.Contains(row, "READY") || !strings.Contains(row, "v2@") {
227 t.Errorf("the change's row should read READY at v2: %q", row)
228 }
229
230 res = h.RunKlaus("cleanup", "--done")
231 if res.ExitCode != 0 || strings.Contains(res.Stdout, "removed "+later.ID+" ") {
232 t.Errorf("cleanup --done should keep the next round's run (exit %d):\n%s%s", res.ExitCode, res.Stdout, res.Stderr)
233 }
234}
235
236// TestOlderVersionUntilTheChangeLands drives a change a later run gives a
237// newer version, with real launches against the real valley CLI and bare
238// repos. Until the change lands, the run that readied v1 shows
239// older-version and the later run awaiting-approval; once the change is
240// withdrawn, both show withdrawn.
241func TestOlderVersionUntilTheChangeLands(t *testing.T) {
242 t.Parallel()
243 h := NewHarness(t)
244 h.useRealValley()
245 h.joinValley()
246 h.WriteDefaultClaudeOutput(`{"type":"result","subtype":"success","total_cost_usd":0.5,"duration_ms":1000,"session_id":"` + changeSession + `"}` + "\n")
247
248 first, _ := h.launchRun("add a greeting")
249 h.WaitForClaudeStart(30 * time.Second)
250 h.pushWork(first, "greeting.txt", "Add a greeting")
251 first = h.finishRun(first.ID)
252 if first.Valley == nil || first.Valley.Version != 1 {
253 t.Fatalf("finalize should mark v1 ready: %+v", first.Valley)
254 }
255
256 time.Sleep(time.Second) // the later run is created in a later second
257 h.resetWorker()
258 later, _ := h.launchRun("--change", first.Valley.Handle, "tidy the greeting")
259 h.WaitForClaudeStart(30 * time.Second)
260 h.pushWork(later, "greeting.txt", "Tidy the greeting")
261 later = h.finishRun(later.ID)
262 if later.Valley == nil || later.Valley.Version != 2 || later.ChangeID != first.ChangeID {
263 t.Fatalf("finalize should mark the later head ready as v2 of %s: %+v", first.ChangeID, later.Valley)
264 }
265
266 res := h.RunKlaus("status")
267 for id, want := range map[string]string{first.ID: "older-version", later.ID: "awaiting-approval"} {
268 if got := statusOf(t, res.Stdout, id); got != want {
269 t.Errorf("%s shows %q, want %q:\n%s%s", id, got, want, res.Stdout, res.Stderr)
270 }
271 }
272
273 cmd := exec.Command("valley", "withdraw", first.ChangeID)
274 cmd.Dir, cmd.Env = h.RepoDir, h.env()
275 if out, err := cmd.CombinedOutput(); err != nil {
276 t.Fatalf("valley withdraw: %v\n%s", err, out)
277 }
278 res = h.RunKlaus("status")
279 for _, id := range []string{first.ID, later.ID} {
280 if got := statusOf(t, res.Stdout, id); got != "withdrawn" {
281 t.Errorf("%s shows %q, want withdrawn:\n%s%s", id, got, res.Stdout, res.Stderr)
282 }
283 }
284}