| 1 | //go:build e2e |
| 2 | |
| 3 | package e2e |
| 4 | |
| 5 | import ( |
| 6 | "encoding/json" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "the-valley/flow/internal/run" |
| 14 | ) |
| 15 | |
| 16 | // TestDraftAwaitingReviewWaitsOnTheCoordinator lets the real finalize |
| 17 | // leave a draft for review (ready_at_finalize off) while a queue watch |
| 18 | // waits. An empty queue can't end the watch, so it returns only once |
| 19 | // finalize's draft is in the queue: it prints the queue with the draft and |
| 20 | // the valley flow ready that readies it, and a next: line naming the |
| 21 | // queue's digest. valley flow queue lists the draft too, as does --json, |
| 22 | // and status dates it. The watch on that next: line returns once valley |
| 23 | // flow ready marks the draft ready, which puts it in the operator's group, |
| 24 | // with no command. |
| 25 | func TestDraftAwaitingReviewWaitsOnTheCoordinator(t *testing.T) { |
| 26 | t.Parallel() |
| 27 | h := NewHarness(t) |
| 28 | h.installValleyStub() |
| 29 | h.joinValley() |
| 30 | if err := os.WriteFile(filepath.Join(h.E2EDir, "valley.has-unready"), nil, 0o644); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | h.AmendRepoConfig(map[string]any{"ready_at_finalize": false}) |
| 34 | |
| 35 | res := h.RunKlaus("launch", "add a health check endpoint") |
| 36 | if res.ExitCode != 0 { |
| 37 | t.Fatalf("launch exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr) |
| 38 | } |
| 39 | id := h.RunIDs()[0] |
| 40 | h.WaitForClaudeStart(30 * time.Second) |
| 41 | st, err := h.ReadState(id) |
| 42 | if err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | if err := os.WriteFile(filepath.Join(st.Worktree, "health.go"), []byte("package health\n"), 0o644); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | h.git(st.Worktree, "add", "-A") |
| 49 | h.git(st.Worktree, "commit", "-qm", "Add a health check") |
| 50 | h.git(st.Worktree, "push", "-q", "origin", st.Branch) |
| 51 | head := h.GitOutput(st.Worktree, "rev-parse", "HEAD") |
| 52 | // The fake valley's ready names the change nxvp. |
| 53 | rec := *st |
| 54 | rec.Valley = &run.Valley{Handle: "nxvp"} |
| 55 | listing := func(state string) { |
| 56 | v := version(1, head, "Add a health check", "not-landed") |
| 57 | v["branch"] = st.Branch |
| 58 | h.writeValleyChanges(map[string]any{ |
| 59 | "schema": "valley-changes/v1", |
| 60 | "host": "valley-host", |
| 61 | "repos": []any{map[string]any{ |
| 62 | "repo": "widget", "url": "git@valley-host:widget.git", "error": nil, |
| 63 | "changes": []any{change(&rec, state, 1, v)}, "branches": []any{}, |
| 64 | }}, |
| 65 | }) |
| 66 | } |
| 67 | listing("draft") |
| 68 | |
| 69 | // Nothing waits while the agent runs. |
| 70 | if res := h.RunKlaus("queue"); res.ExitCode != 0 || res.Stdout != "" { |
| 71 | t.Fatalf("queue while the agent runs exited %d with:\n%s%s", res.ExitCode, res.Stdout, res.Stderr) |
| 72 | } |
| 73 | w := h.startKlaus(nil, "watch", "--queue") |
| 74 | |
| 75 | h.ReleaseClaude() |
| 76 | h.WaitForState(id, func(s *run.State) bool { return s.DurationMS != nil && s.TmuxPane == nil }, 60*time.Second) |
| 77 | out := w.waitWithin(60 * time.Second) |
| 78 | ready := " run: valley flow ready " + id + " --head " + head + "\n" |
| 79 | for _, want := range []string{"Waiting on the coordinator\n", "◇ widget nxvp · awaiting-review · for ", ready} { |
| 80 | if !strings.Contains(out.Stdout, want) { |
| 81 | t.Errorf("watch --queue output lacks %q:\n%s", want, out.Stdout) |
| 82 | } |
| 83 | } |
| 84 | next := queueWatchNext(t, out.Stdout) |
| 85 | |
| 86 | res = h.RunKlaus("queue") |
| 87 | if res.ExitCode != 0 || !strings.Contains(res.Stdout, ready) || strings.Contains(res.Stdout, "Waiting on the operator") { |
| 88 | t.Errorf("queue exited %d, want the draft alone:\n%s%s", res.ExitCode, res.Stdout, res.Stderr) |
| 89 | } |
| 90 | res = h.RunKlaus("queue", "--json") |
| 91 | var q struct { |
| 92 | Coordinator []map[string]any `json:"coordinator"` |
| 93 | Operator []map[string]any `json:"operator"` |
| 94 | } |
| 95 | if err := json.Unmarshal([]byte(res.Stdout), &q); err != nil { |
| 96 | t.Fatalf("queue --json: %v\n%s", err, res.Stdout) |
| 97 | } |
| 98 | if len(q.Coordinator) != 1 || q.Coordinator[0]["run"] != id || q.Coordinator[0]["state"] != "awaiting-review" || |
| 99 | q.Coordinator[0]["head"] != head || q.Coordinator[0]["change"] != "nxvp" || len(q.Operator) != 0 { |
| 100 | t.Errorf("queue --json = %s", res.Stdout) |
| 101 | } |
| 102 | |
| 103 | res = h.RunKlaus("status") |
| 104 | if cells := statusCells(t, res.Stdout, id); cells[1] != "awaiting-review" || cells[2] == "-" { |
| 105 | t.Errorf("status shows %q for %q, want awaiting-review with its age:\n%s", cells[2], cells[1], res.Stdout) |
| 106 | } |
| 107 | |
| 108 | // The watch from the first one's next: line returns once the draft |
| 109 | // is ready, with the change in the operator's group. |
| 110 | w = h.startKlaus(nil, next...) |
| 111 | listing("ready") |
| 112 | if res := h.RunKlaus("ready", id, "--head", head); res.ExitCode != 0 { |
| 113 | t.Fatalf("ready exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr) |
| 114 | } |
| 115 | out = w.waitWithin(60 * time.Second) |
| 116 | if strings.Contains(out.Stdout, "Waiting on the coordinator") || strings.Contains(out.Stdout, "run:") || |
| 117 | !strings.Contains(out.Stdout, "Waiting on the operator\n◇ widget nxvp · awaiting-approval · for ") { |
| 118 | t.Errorf("the next watch, once the draft is ready, printed:\n%s", out.Stdout) |
| 119 | } |
| 120 | if again := queueWatchNext(t, out.Stdout); strings.Join(again, " ") == strings.Join(next, " ") { |
| 121 | t.Errorf("the next: line names the queue it already saw: %v", again) |
| 122 | } |
| 123 | res = h.RunKlaus("queue") |
| 124 | if res.ExitCode != 0 || strings.Contains(res.Stdout, "Waiting on the coordinator") || |
| 125 | !strings.Contains(res.Stdout, "Waiting on the operator\n◇ widget nxvp · awaiting-approval · for ") || strings.Contains(res.Stdout, "run:") { |
| 126 | t.Errorf("queue once ready exited %d, want the change waiting on the operator, with no command:\n%s%s", res.ExitCode, res.Stdout, res.Stderr) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // waitWithin waits up to d for p to exit 0, and returns its outcome. |
| 131 | func (p *klausProcess) waitWithin(d time.Duration) RunResult { |
| 132 | p.t.Helper() |
| 133 | select { |
| 134 | case <-p.done: |
| 135 | case <-time.After(d): |
| 136 | res := p.stop() |
| 137 | p.t.Fatalf("klaus didn't exit within %s\nstdout:\n%s\nstderr:\n%s", d, res.Stdout, res.Stderr) |
| 138 | } |
| 139 | res := p.wait() |
| 140 | if res.ExitCode != 0 { |
| 141 | p.t.Fatalf("klaus exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr) |
| 142 | } |
| 143 | return res |
| 144 | } |
| 145 | |
| 146 | // queueWatchNext is the klaus command line on the last line of a watch |
| 147 | // --queue's output, its next: line, which waits from the queue it printed. |
| 148 | func queueWatchNext(t *testing.T, out string) []string { |
| 149 | t.Helper() |
| 150 | lines := strings.Split(strings.TrimSuffix(out, "\n"), "\n") |
| 151 | last := lines[len(lines)-1] |
| 152 | cmdline, ok := strings.CutPrefix(last, "next: valley flow ") |
| 153 | if !ok || !strings.HasPrefix(cmdline, "watch --queue --seen ") { |
| 154 | t.Fatalf("last line %q is not a next: line naming a digest", last) |
| 155 | } |
| 156 | return strings.Fields(cmdline) |
| 157 | } |