gunk-dev/the-valley

integration_test.go

sourcecaaa5bee5e11main's published tip history
8.1 KiB
1//go:build e2e
2
3package e2e
4
5import (
6 "fmt"
7 "os"
8 "path/filepath"
9 "regexp"
10 "strings"
11 "testing"
12 "time"
13
14 "the-valley/flow/internal/run"
15)
16
17// pushBranch commits a file on a new branch off main in the sandbox repo and
18// pushes it, as a direct-push agent finishes, then returns to main. It
19// returns the pushed SHA.
20func (h *Harness) pushBranch(branch, file string) string {
21 h.t.Helper()
22 h.git(h.RepoDir, "checkout", "-q", "-b", branch, "origin/main")
23 if err := os.WriteFile(filepath.Join(h.RepoDir, file), []byte(branch+"\n"), 0o644); err != nil {
24 h.t.Fatal(err)
25 }
26 h.git(h.RepoDir, "add", file)
27 h.git(h.RepoDir, "commit", "-qm", "add "+file)
28 h.git(h.RepoDir, "push", "-q", "origin", branch)
29 sha := h.GitOutput(h.RepoDir, "rev-parse", "HEAD")
30 h.git(h.RepoDir, "checkout", "-q", "main")
31 h.git(h.RepoDir, "branch", "-q", "-D", branch)
32 return sha
33}
34
35// integrateBranch lands branch as the valley's integrator does when it can't
36// fast-forward: one new commit on main whose tree is the merge-tree.
37func (h *Harness) integrateBranch(branch string) {
38 h.t.Helper()
39 h.git(h.RepoDir, "fetch", "-q", "origin")
40 main := h.GitOutput(h.RepoDir, "rev-parse", "origin/main")
41 tree := h.GitOutput(h.RepoDir, "merge-tree", "--write-tree", main, "origin/"+branch)
42 commit := h.GitOutput(h.RepoDir, "commit-tree", tree, "-p", main, "-m", "land "+branch)
43 h.git(h.RepoDir, "push", "-q", "origin", commit+":refs/heads/main")
44}
45
46// seedPushed seeds a run that finished by pushing branch at sha from the
47// sandbox repo's clone.
48func (h *Harness) seedPushed(id, prompt, branch, sha string, created time.Time) {
49 h.t.Helper()
50 clone := h.RepoDir
51 cost := 1.0
52 h.SeedState(&run.State{
53 ID: id, Type: "launch", Prompt: prompt, TargetRepo: strPtrE2E("acme/widget"), CloneDir: &clone,
54 Branch: branch, PushedBranch: &branch, PushedSHA: &sha, CostUSD: &cost,
55 CreatedAt: created.Format(time.RFC3339),
56 })
57}
58
59func strPtrE2E(s string) *string { return &s }
60
61// statusLine returns runID's row in `klaus status` output.
62func statusLine(t *testing.T, out, runID string) string {
63 t.Helper()
64 for _, line := range strings.Split(out, "\n") {
65 if f := strings.Fields(line); len(f) > 7 && f[0] == runID {
66 return line
67 }
68 }
69 t.Fatalf("no row for %s in:\n%s", runID, out)
70 return ""
71}
72
73// statusCellSep is the gap between `klaus status` columns: at least two
74// spaces, as a cell may hold single spaces ("claude (default model)").
75var statusCellSep = regexp.MustCompile(` {2,}`)
76
77// statusCells splits runID's row in `klaus status` output into its columns.
78func statusCells(t *testing.T, out, runID string) []string {
79 t.Helper()
80 return statusCellSep.Split(statusLine(t, out, runID), -1)
81}
82
83// statusOf returns the STATUS column of runID's row in `klaus status` output.
84func statusOf(t *testing.T, out, runID string) string {
85 t.Helper()
86 return statusCells(t, out, runID)[1]
87}
88
89// changeColumn returns the CHANGE column of runID's row in `klaus status`
90// output: its PR's number, its valley handle, or its change ID's prefix.
91func changeColumn(t *testing.T, out, runID string) string {
92 t.Helper()
93 return statusCells(t, out, runID)[8]
94}
95
96// TestStatusShowsIntegration runs the real `klaus status` over pushed runs
97// in every state, checked against the run's real clone and origin, and a PR
98// that GitHub no longer shows the agent.
99func TestStatusShowsIntegration(t *testing.T) {
100 t.Parallel()
101 h := NewHarness(t)
102 created := time.Now().Add(-time.Hour)
103
104 waitingSHA := h.pushBranch("agent/waiting", "w.txt")
105 h.seedPushed("20261012-0900-0001", "waiting", "agent/waiting", waitingSHA, created)
106
107 ffSHA := h.pushBranch("agent/ff", "f.txt")
108 h.git(h.RepoDir, "push", "-q", "origin", ffSHA+":refs/heads/main")
109 h.seedPushed("20261012-0900-0002", "landed by fast-forward", "agent/ff", ffSHA, created)
110
111 contentSHA := h.pushBranch("agent/content", "c.txt")
112 h.integrateBranch("agent/content")
113 h.git(h.RepoDir, "push", "-q", "origin", "--delete", "agent/content")
114 h.seedPushed("20261012-0900-0003", "landed by the integrator", "agent/content", contentSHA, created)
115
116 goneSHA := h.pushBranch("agent/gone", "g.txt")
117 h.git(h.RepoDir, "push", "-q", "origin", "--delete", "agent/gone")
118 h.seedPushed("20261012-0900-0004", "rejected", "agent/gone", goneSHA, created)
119
120 // The clone's origin URL is broken: the check can't run, so it stays
121 // awaiting rather than guessing.
122 brokenClone := filepath.Join(t.TempDir(), "broken")
123 h.git("", "clone", "-q", h.OriginDir, brokenClone)
124 h.git(brokenClone, "remote", "set-url", "origin", filepath.Join(t.TempDir(), "missing.git"))
125 cost := 1.0
126 h.SeedState(&run.State{
127 ID: "20261012-0900-0005", Type: "launch", Prompt: "unreachable", CloneDir: &brokenClone,
128 PushedBranch: strPtrE2E("agent/gone"), PushedSHA: &goneSHA, CostUSD: &cost, CreatedAt: created.Format(time.RFC3339),
129 })
130
131 // GitHub answers every PR query for the old site PR with "can't see it".
132 h.WriteStub("gh", fmt.Sprintf(`#!/usr/bin/env bash
133{ printf 'gh'; for a in "$@"; do printf ' %%q' "$a"; done; printf '\n'; } >> %q
134echo "GraphQL: Could not resolve to a Repository with the name 'acme/site'. (repository)" >&2
135exit 1
136`, filepath.Join(h.E2EDir, "gh.argv")))
137 h.SeedState(&run.State{
138 ID: "20261005-0900-0006", Type: "launch", Prompt: "old site PR", CostUSD: &cost,
139 PRURL: strPtrE2E("https://github.com/acme/site/pull/7"), CreatedAt: created.Format(time.RFC3339),
140 })
141
142 res := h.RunKlaus("status")
143 if res.ExitCode != 0 {
144 t.Fatalf("status exited %d; stderr: %s", res.ExitCode, res.Stderr)
145 }
146 for id, want := range map[string]string{
147 "20261012-0900-0001": "awaiting-integration",
148 "20261012-0900-0002": "integrated",
149 "20261012-0900-0003": "integrated",
150 "20261012-0900-0004": "branch-gone",
151 "20261012-0900-0005": "awaiting-integration",
152 "20261005-0900-0006": "no-access",
153 } {
154 if got := statusOf(t, res.Stdout, id); got != want {
155 t.Errorf("%s: status %q, want %q", id, got, want)
156 }
157 }
158 // No CI, conflict or review queries for a PR we can't read.
159 if calls := strings.TrimSpace(h.GHArgv()); strings.Count(calls, "\n") != 0 || !strings.Contains(calls, "state") {
160 t.Errorf("gh should be asked only for the PR's state, got:\n%s", calls)
161 }
162}
163
164// TestDashboardShowsWhatsYours runs the real dashboard over a pushed branch
165// that is waiting and one that has landed: the waiting one is the operator's
166// SUBMIT row, with the configured submit command in the footer; the landed
167// one is done.
168func TestDashboardShowsWhatsYours(t *testing.T) {
169 t.Parallel()
170 h := NewHarness(t)
171 h.AmendRepoConfig(map[string]any{
172 "dashboard": map[string]any{"submit_command": "cd ~/hack/{repo} && valley review {branch}"},
173 })
174
175 landedSHA := h.pushBranch("agent/landed", "l.txt")
176 h.git(h.RepoDir, "push", "-q", "origin", landedSHA+":refs/heads/main")
177 h.seedPushed("20261012-0900-0001", "landed 01", "agent/landed", landedSHA, time.Now().Add(-20*time.Minute))
178 waitingSHA := h.pushBranch("agent/waiting", "w.txt")
179 h.seedPushed("20261012-0900-0002", "waiting 01", "agent/waiting", waitingSHA, time.Now().Add(-3*time.Hour))
180
181 pane := strings.TrimSpace(h.tmux("new-session", "-d", "-s", "dash", "-x", "160", "-y", "14", "-c", h.RepoDir,
182 "-P", "-F", "#{pane_id}", "env KLAUS_SESSION_ID="+h.SessionID+" klaus dashboard"))
183 screen := func(want ...string) string {
184 t.Helper()
185 var last string
186 for deadline := time.Now().Add(30 * time.Second); time.Now().Before(deadline); time.Sleep(200 * time.Millisecond) {
187 last = h.tmux("capture-pane", "-p", "-t", pane)
188 ok := true
189 for _, w := range want {
190 ok = ok && strings.Contains(last, w)
191 }
192 if ok {
193 return last
194 }
195 }
196 t.Fatalf("dashboard screen never showed %q; last screen:\n%s", want, last)
197 return ""
198 }
199
200 // The first check runs at startup.
201 s := screen("1 yours", "1 done", "INTEGRATED")
202 var rows []string
203 for _, line := range strings.Split(s, "\n") {
204 if strings.Contains(line, "landed 01") || strings.Contains(line, "waiting 01") {
205 rows = append(rows, line)
206 }
207 }
208 if len(rows) != 2 || !strings.Contains(rows[0], "waiting 01") || !strings.Contains(rows[0], "SUBMIT") ||
209 !strings.Contains(rows[1], "landed 01") {
210 t.Errorf("want the SUBMIT row first, then the integrated one; screen:\n%s", s)
211 }
212
213 h.tmux("send-keys", "-t", pane, "g")
214 screen("submit: cd ~/hack/widget && valley review agent/waiting")
215}