| 1 | //go:build e2e |
| 2 | |
| 3 | package e2e |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "slices" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "the-valley/flow/internal/event" |
| 14 | "the-valley/flow/internal/run" |
| 15 | ) |
| 16 | |
| 17 | // checksListing is a valley-changes/v1 listing of widget with one change, |
| 18 | // c's (handle nxvp), owned by operator and in state at v1. checks is what |
| 19 | // the valley lists for the version's checks; nil lists none, as a valley |
| 20 | // from before checks were listed does. |
| 21 | func checksListing(c strictChange, state string, checks map[string]any) map[string]any { |
| 22 | v := map[string]any{"n": 1, "head": c.v1, "title": "Add a greeting", "branch": c.branch, |
| 23 | "uploader": "operator", "landing": "not-landed", "landed_commit": nil, "request": nil, "outcome": nil} |
| 24 | if checks != nil { |
| 25 | v["checks"] = checks |
| 26 | } |
| 27 | ch := map[string]any{ |
| 28 | "change_id": c.id, "handle": "nxvp", "target": "main", "owner": "operator", "state": state, |
| 29 | "record": c.record, "seq": 1, "active": 1, "includes": []string{}, "versions": []any{v}, "restack": nil, |
| 30 | } |
| 31 | return map[string]any{"schema": "valley-changes/v1", "host": "valley-host", "repos": []any{map[string]any{ |
| 32 | "repo": "widget", "url": "git@valley-host:widget.git", "error": nil, "changes": []any{ch}, "branches": []any{}, |
| 33 | }}} |
| 34 | } |
| 35 | |
| 36 | // checks is a version's checks in overall state, with valley-request in |
| 37 | // request and integrator-unit passed. |
| 38 | func checks(state, request, why string) map[string]any { |
| 39 | vr := map[string]any{"name": "valley-request", "state": request, "signer": "host-worker"} |
| 40 | if why != "" { |
| 41 | vr["why"] = why |
| 42 | } |
| 43 | return map[string]any{"state": state, "tree": "sha256:" + strings.Repeat("ab", 32), "required": []any{ |
| 44 | vr, |
| 45 | map[string]any{"name": "integrator-unit", "state": "passed", "signer": "host-worker"}, |
| 46 | map[string]any{"name": "web-unit", "state": "other"}, |
| 47 | }} |
| 48 | } |
| 49 | |
| 50 | // serve has the stub valley list state as ready, and as a draft once |
| 51 | // something returns the change to draft, as launch does. |
| 52 | func (h *Harness) serve(c strictChange, chk map[string]any) { |
| 53 | h.t.Helper() |
| 54 | os.Remove(filepath.Join(h.E2EDir, "valley.after-unready")) |
| 55 | h.writeValleyChanges(checksListing(c, "ready", chk)) |
| 56 | h.writeE2EJSON("valley.changes.unready.json", checksListing(c, "draft", nil)) |
| 57 | } |
| 58 | |
| 59 | // failedChecks runs klaus failed-checks and returns its output and the run |
| 60 | // it launched, if any. |
| 61 | func (h *Harness) failedChecks() (string, *run.State) { |
| 62 | h.t.Helper() |
| 63 | known := h.RunIDs() |
| 64 | res := h.RunKlaus("failed-checks") |
| 65 | if res.ExitCode != 0 { |
| 66 | h.t.Fatalf("failed-checks exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr) |
| 67 | } |
| 68 | return res.Stdout, h.newRun(known) |
| 69 | } |
| 70 | |
| 71 | // newRun returns the run recorded since known, if any, failing the test on |
| 72 | // more than one. |
| 73 | func (h *Harness) newRun(known []string) *run.State { |
| 74 | h.t.Helper() |
| 75 | var found *run.State |
| 76 | for _, id := range h.RunIDs() { |
| 77 | if slices.Contains(known, id) { |
| 78 | continue |
| 79 | } |
| 80 | if found != nil { |
| 81 | h.t.Fatalf("more than one run launched: %s and %s", found.ID, id) |
| 82 | } |
| 83 | st, err := h.ReadState(id) |
| 84 | if err != nil { |
| 85 | h.t.Fatal(err) |
| 86 | } |
| 87 | found = st |
| 88 | } |
| 89 | return found |
| 90 | } |
| 91 | |
| 92 | // TestValleyChecksFailedDispatchesAFix runs the real klaus against a fake |
| 93 | // valley whose listing moves a ready change klaus owns from no checks, to |
| 94 | // pending, to failed, to passed. With no checks listed, status's CI reads |
| 95 | // "-" and nothing is dispatched. The dashboard shows pending checks in the |
| 96 | // CI column. While a run works on the change, a failed head gets no fix |
| 97 | // run. Once it is done, the dashboard reports change:checks-failed once, |
| 98 | // shows the failed check, and launches one fix run on the change, for |
| 99 | // checks-failed, whose brief names the check, the worker's reason and how |
| 100 | // to read its output. The same failed head gets no second run. Passed |
| 101 | // checks are reported once, and the status stays awaiting-approval. |
| 102 | func TestValleyChecksFailedDispatchesAFix(t *testing.T) { |
| 103 | t.Parallel() |
| 104 | h := NewHarness(t) |
| 105 | c := h.strictChange() |
| 106 | sessionDir := filepath.Join(h.Home, ".klaus", "sessions", h.SessionID) |
| 107 | if err := os.WriteFile(filepath.Join(sessionDir, "events.jsonl"), nil, 0o644); err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | checksEvents := func() []event.Event { |
| 111 | var out []event.Event |
| 112 | for _, e := range h.runEvents("nxvp") { |
| 113 | if strings.HasPrefix(e.Type, "change:checks-") { |
| 114 | out = append(out, e) |
| 115 | } |
| 116 | } |
| 117 | return out |
| 118 | } |
| 119 | statusRow := func() string { |
| 120 | res := h.RunKlaus("status") |
| 121 | if res.ExitCode != 0 { |
| 122 | t.Fatalf("status exited %d: %s", res.ExitCode, res.Stderr) |
| 123 | } |
| 124 | return res.Stdout |
| 125 | } |
| 126 | |
| 127 | // No checks listed: nothing to show, nothing to dispatch. |
| 128 | h.serve(c, nil) |
| 129 | if out, fix := h.failedChecks(); fix != nil || !strings.Contains(out, "No failed checks") { |
| 130 | t.Fatalf("with no checks listed, failed-checks launched %v:\n%s", fix, out) |
| 131 | } |
| 132 | if out := statusRow(); !strings.Contains(out, "awaiting-approval") || strings.Contains(out, "pending") { |
| 133 | t.Errorf("with no checks listed, status should read awaiting-approval and CI -:\n%s", out) |
| 134 | } |
| 135 | |
| 136 | // Pending: the dashboard's first check records the change, and shows |
| 137 | // the checks. |
| 138 | h.serve(c, checks("pending", "pending", "")) |
| 139 | _, screen := h.startDashboard(220) |
| 140 | screen("CI …") |
| 141 | h.tmux("kill-session", "-t", "dash") |
| 142 | if out := statusRow(); !strings.Contains(out, "awaiting-approval") || !strings.Contains(out, "pending") { |
| 143 | t.Errorf("status should read awaiting-approval with CI pending:\n%s", out) |
| 144 | } |
| 145 | |
| 146 | // A run works on the change: its failed head gets no fix run. |
| 147 | h.resetWorker() |
| 148 | busy, _ := h.launchRun("--change", c.id, "--", "more work") |
| 149 | h.WaitForClaudeStart(30 * time.Second) |
| 150 | failed := checks("failed", "failed", "go test ./web/... failed:\n TestServe") |
| 151 | h.serve(c, failed) |
| 152 | if out, fix := h.failedChecks(); fix != nil || !strings.Contains(out, "a run is working on the change") { |
| 153 | t.Fatalf("with run %s working on the change, failed-checks launched %v:\n%s", busy.ID, fix, out) |
| 154 | } |
| 155 | h.finishRun(busy.ID) |
| 156 | |
| 157 | // The dashboard reports the failure, and dispatches one fix run. |
| 158 | h.resetWorker() |
| 159 | h.serve(c, failed) |
| 160 | known := h.RunIDs() |
| 161 | _, screen = h.startDashboard(220) |
| 162 | screen("CI ✗", "valley-request") |
| 163 | var fix *run.State |
| 164 | for deadline := time.Now().Add(60 * time.Second); fix == nil && time.Now().Before(deadline); time.Sleep(200 * time.Millisecond) { |
| 165 | fix = h.newRun(known) |
| 166 | } |
| 167 | h.tmux("kill-session", "-t", "dash") |
| 168 | if fix == nil { |
| 169 | t.Fatal("the dashboard dispatched no fix run") |
| 170 | } |
| 171 | if fix.ChangeID != c.id || fix.Branch != c.branch || fix.DispatchReason != event.DispatchChecksFailed { |
| 172 | t.Errorf("the fix run is on change %s, branch %s, for %q", fix.ChangeID, fix.Branch, fix.DispatchReason) |
| 173 | } |
| 174 | h.WaitForClaudeStart(30 * time.Second) |
| 175 | brief := h.ClaudeStdin() |
| 176 | for _, want := range []string{"valley-request", "TestServe", "valley telemetry --json read --source worker --logs", |
| 177 | "--since", c.id, "valley checks --names origin/main HEAD", "--amend"} { |
| 178 | if !strings.Contains(brief, want) { |
| 179 | t.Errorf("the brief should name %q:\n%s", want, brief) |
| 180 | } |
| 181 | } |
| 182 | if strings.Contains(brief, "integrator-unit") { |
| 183 | t.Errorf("the brief names a check that passed:\n%s", brief) |
| 184 | } |
| 185 | evts := checksEvents() |
| 186 | if len(evts) != 1 || evts[0].Type != event.ChangeChecksFailed { |
| 187 | t.Fatalf("checks events %v, want one change:checks-failed", evts) |
| 188 | } |
| 189 | if d := evts[0].Data; d["head"] != c.v1 || d["repo"] != "widget" || d["version"] != float64(1) || |
| 190 | !slices.Equal(d["failed"].([]any), []any{"valley-request"}) { |
| 191 | t.Errorf("change:checks-failed carries %v", d) |
| 192 | } |
| 193 | h.finishRun(fix.ID) |
| 194 | |
| 195 | // The same failed head again: no second run, and no second event. |
| 196 | h.serve(c, failed) |
| 197 | if out, again := h.failedChecks(); again != nil || !strings.Contains(out, "fix run "+fix.ID+" dispatched (attempt 1 of 3)") { |
| 198 | t.Fatalf("a second pass on the same failed head launched %v:\n%s", again, out) |
| 199 | } |
| 200 | if out := statusRow(); !strings.Contains(out, "failing") || !strings.Contains(out, "nxvp v1: valley-request") { |
| 201 | t.Errorf("status should read CI failing and list the failed check:\n%s", out) |
| 202 | } |
| 203 | |
| 204 | // Passed: reported once, and nothing dispatched. |
| 205 | h.serve(c, checks("passed", "passed", "")) |
| 206 | known = h.RunIDs() |
| 207 | for i := 0; i < 2; i++ { |
| 208 | _, screen = h.startDashboard(220) |
| 209 | screen("CI ✓") |
| 210 | h.tmux("kill-session", "-t", "dash") |
| 211 | } |
| 212 | if fix := h.newRun(known); fix != nil { |
| 213 | t.Errorf("passed checks launched %s", fix.ID) |
| 214 | } |
| 215 | if evts := checksEvents(); len(evts) != 2 || evts[1].Type != event.ChangeChecksPassed { |
| 216 | t.Errorf("checks events %v, want change:checks-failed then change:checks-passed", evts) |
| 217 | } |
| 218 | res := h.RunKlaus("notifications", "--all") |
| 219 | if !strings.Contains(res.Stdout, "CI failed: widget nxvp v1: valley-request") || !strings.Contains(res.Stdout, "CI passed: widget nxvp v1") { |
| 220 | t.Errorf("notifications should list the checks:\n%s", res.Stdout) |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // A valley CLI from before `changes --checks` refuses the flag. klaus lists |
| 225 | // again without it, so the change's landing state still shows, its checks |
| 226 | // don't, and nothing is dispatched from them. The refusal is remembered, so |
| 227 | // a second check lists once, not twice. |
| 228 | func TestValleyChecksFlagRefusedFallsBackToPlainListing(t *testing.T) { |
| 229 | t.Parallel() |
| 230 | h := NewHarness(t) |
| 231 | c := h.strictChange() |
| 232 | h.serve(c, checks("failed", "failed", "go test ./web/... failed")) |
| 233 | h.writeE2EJSON("valley.checks-refuse", map[string]any{}) |
| 234 | |
| 235 | res := h.RunKlaus("status") |
| 236 | if res.ExitCode != 0 { |
| 237 | t.Fatalf("status exited %d: %s", res.ExitCode, res.Stderr) |
| 238 | } |
| 239 | if !strings.Contains(res.Stdout, "awaiting-approval") || strings.Contains(res.Stdout, "failing") || |
| 240 | strings.Contains(res.Stdout, "valley-request") { |
| 241 | t.Errorf("status should read awaiting-approval with no checks shown:\n%s", res.Stdout) |
| 242 | } |
| 243 | if out, fix := h.failedChecks(); fix != nil || !strings.Contains(out, "No failed checks") { |
| 244 | t.Fatalf("with --checks refused, failed-checks launched %v:\n%s", fix, out) |
| 245 | } |
| 246 | argv, err := os.ReadFile(filepath.Join(h.E2EDir, "valley.argv")) |
| 247 | if err != nil { |
| 248 | t.Fatal(err) |
| 249 | } |
| 250 | var withFlag, plain int |
| 251 | for _, l := range strings.Split(string(argv), "\n") { |
| 252 | if !strings.Contains(l, " changes ") { |
| 253 | continue |
| 254 | } |
| 255 | if strings.Contains(l, "--checks") { |
| 256 | withFlag++ |
| 257 | } else { |
| 258 | plain++ |
| 259 | } |
| 260 | } |
| 261 | if plain == 0 { |
| 262 | t.Errorf("klaus never listed without --checks:\n%s", argv) |
| 263 | } |
| 264 | if withFlag == 0 || withFlag > 2 { |
| 265 | t.Errorf("klaus asked with --checks %d times, want it tried per process (status, failed-checks):\n%s", withFlag, argv) |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | // serveStale has the stub valley list c as ready with an integration |
| 270 | // request the integrator judged stale, its checks as chk (nil lists none) |
| 271 | // and the change's restack report as restack (nil lists none). |
| 272 | func (h *Harness) serveStale(c strictChange, chk map[string]any, restack map[string]any) { |
| 273 | h.t.Helper() |
| 274 | l := checksListing(c, "ready", chk) |
| 275 | ch := l["repos"].([]any)[0].(map[string]any)["changes"].([]any)[0].(map[string]any) |
| 276 | v := ch["versions"].([]any)[0].(map[string]any) |
| 277 | v["request"] = map[string]any{"id": "req-1"} |
| 278 | v["outcome"] = map[string]any{"decision": "stale", "head": c.v1, "tip": strings.Repeat("c", 40)} |
| 279 | if restack != nil { |
| 280 | ch["restack"] = restack |
| 281 | } |
| 282 | h.writeValleyChanges(l) |
| 283 | } |
| 284 | |
| 285 | // TestValleyApprovedStaleOnPendingChecksStaysApproved runs the real klaus against |
| 286 | // a fake valley whose integrator judges an approved change stale. While its |
| 287 | // checks are pending, the dashboard shows APPROVED with CI pending and reports change:approved |
| 288 | // but no change:stale. Failed checks show as checks failed, a current restack |
| 289 | // conflict as conflicting, and passed checks (or none listed) as stale, which |
| 290 | // reports change:stale once. |
| 291 | func TestValleyApprovedStaleOnPendingChecksStaysApproved(t *testing.T) { |
| 292 | t.Parallel() |
| 293 | h := NewHarness(t) |
| 294 | c := h.strictChange() |
| 295 | sessionDir := filepath.Join(h.Home, ".klaus", "sessions", h.SessionID) |
| 296 | if err := os.WriteFile(filepath.Join(sessionDir, "events.jsonl"), nil, 0o644); err != nil { |
| 297 | t.Fatal(err) |
| 298 | } |
| 299 | count := func(typ string) int { |
| 300 | n := 0 |
| 301 | for _, e := range h.runEvents("nxvp") { |
| 302 | if e.Type == typ { |
| 303 | n++ |
| 304 | } |
| 305 | } |
| 306 | return n |
| 307 | } |
| 308 | status := func() string { |
| 309 | res := h.RunKlaus("status") |
| 310 | if res.ExitCode != 0 { |
| 311 | t.Fatalf("status exited %d: %s", res.ExitCode, res.Stderr) |
| 312 | } |
| 313 | return res.Stdout |
| 314 | } |
| 315 | look := func(want string) { |
| 316 | t.Helper() |
| 317 | _, screen := h.startDashboard(220) |
| 318 | screen(want) |
| 319 | h.tmux("kill-session", "-t", "dash") |
| 320 | } |
| 321 | conflict := map[string]any{"ref": "refs/valley/restack/x", "commit": strings.Repeat("d", 40), "current": true, |
| 322 | "report": map[string]any{"schema": "valley-restack-report/v1", "change_id": c.id, "result": "conflict"}} |
| 323 | |
| 324 | // The first look records the change as ready. |
| 325 | h.serve(c, nil) |
| 326 | look("READY") |
| 327 | |
| 328 | // Approved, judged stale, checks pending: still approved, CI pending. |
| 329 | h.serveStale(c, checks("pending", "pending", ""), nil) |
| 330 | look("APPROVED") |
| 331 | if out := status(); !strings.Contains(out, "approved") || !strings.Contains(out, "pending") || strings.Contains(out, "stale") { |
| 332 | t.Errorf("status should read approved with CI pending:\n%s", out) |
| 333 | } |
| 334 | if count(event.ChangeApproved) != 1 || count(event.ChangeStale) != 0 { |
| 335 | t.Errorf("approved: %d approved, %d stale events, want 1 and 0", count(event.ChangeApproved), count(event.ChangeStale)) |
| 336 | } |
| 337 | |
| 338 | // A current restack conflict is not approved. status only: the |
| 339 | // dashboard would dispatch a fix for failed checks. |
| 340 | h.serveStale(c, checks("pending", "pending", ""), conflict) |
| 341 | if out := status(); !strings.Contains(out, "conflicting") { |
| 342 | t.Errorf("status should read conflicting:\n%s", out) |
| 343 | } |
| 344 | h.serveStale(c, checks("failed", "failed", "go test failed"), nil) |
| 345 | if out := status(); !strings.Contains(out, "checks-failed") { |
| 346 | t.Errorf("status should read checks-failed:\n%s", out) |
| 347 | } |
| 348 | |
| 349 | // Passed checks and still stale: stale, reported once. |
| 350 | h.serveStale(c, checks("passed", "passed", ""), nil) |
| 351 | for i := 0; i < 2; i++ { |
| 352 | look("STALE") |
| 353 | } |
| 354 | if out := status(); !strings.Contains(out, "stale") { |
| 355 | t.Errorf("status should read stale:\n%s", out) |
| 356 | } |
| 357 | if count(event.ChangeStale) != 1 || count(event.ChangeApproved) != 1 { |
| 358 | t.Errorf("stale: %d stale, %d approved events, want 1 and 1", count(event.ChangeStale), count(event.ChangeApproved)) |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | // With no checks listed (an older valley), a stale decision is stale as |
| 363 | // before, and change:stale is reported. |
| 364 | func TestValleyApprovedStaleWithoutChecksStaysStale(t *testing.T) { |
| 365 | t.Parallel() |
| 366 | h := NewHarness(t) |
| 367 | c := h.strictChange() |
| 368 | sessionDir := filepath.Join(h.Home, ".klaus", "sessions", h.SessionID) |
| 369 | if err := os.WriteFile(filepath.Join(sessionDir, "events.jsonl"), nil, 0o644); err != nil { |
| 370 | t.Fatal(err) |
| 371 | } |
| 372 | h.serve(c, nil) |
| 373 | _, screen := h.startDashboard(220) |
| 374 | screen("READY") |
| 375 | h.tmux("kill-session", "-t", "dash") |
| 376 | h.serveStale(c, nil, nil) |
| 377 | _, screen = h.startDashboard(220) |
| 378 | screen("STALE") |
| 379 | h.tmux("kill-session", "-t", "dash") |
| 380 | n := 0 |
| 381 | for _, e := range h.runEvents("nxvp") { |
| 382 | if e.Type == event.ChangeStale { |
| 383 | n++ |
| 384 | } |
| 385 | } |
| 386 | if n != 1 { |
| 387 | t.Errorf("%d change:stale events, want 1", n) |
| 388 | } |
| 389 | } |