gunk-dev/the-valley

dashboard_test.go

source30e24c82839bmain's published tip history
10.0 KiB
1//go:build e2e
2
3package e2e
4
5import (
6 "fmt"
7 "os"
8 "os/exec"
9 "path/filepath"
10 "strings"
11 "testing"
12 "time"
13
14 "the-valley/flow/internal/run"
15)
16
17// TestDashboardScrollsLongRunList runs the real dashboard in a small tmux pane
18// over a long session: 4 running agents and 40 merged PRs, 30 of them merged
19// over an hour ago. It drives the keys through tmux and reads the screen back.
20func TestDashboardScrollsLongRunList(t *testing.T) {
21 t.Parallel()
22 h := NewHarness(t)
23
24 strPtr := func(s string) *string { return &s }
25 now := time.Now()
26 ago := func(d time.Duration) string { return now.Add(-d).Format(time.RFC3339) }
27 cost := 1.0
28 for i := 1; i <= 4; i++ {
29 // Unfinalized, on a live pane: the dashboard sees these as running.
30 pane := h.InitialPane
31 h.SeedState(&run.State{
32 ID: fmt.Sprintf("20261004-1200-a%03d", i), Type: "launch", Prompt: fmt.Sprintf("active %02d", i),
33 TargetRepo: strPtr("acme/widget"), TmuxPane: &pane, Worktree: "/wt",
34 CreatedAt: ago(time.Duration(i) * time.Minute),
35 })
36 }
37 for i := 1; i <= 40; i++ {
38 merged := time.Duration(i) * 5 * time.Minute
39 if i > 10 {
40 merged += 2 * time.Hour
41 }
42 h.SeedState(&run.State{
43 ID: fmt.Sprintf("20261004-1200-m%03d", i), Type: "launch", Prompt: fmt.Sprintf("merged %02d", i),
44 TargetRepo: strPtr("acme/widget"), PRURL: strPtr(fmt.Sprintf("https://github.com/acme/widget/pull/%d", 100+i)),
45 CostUSD: &cost, CreatedAt: ago(merged + 30*time.Minute), MergedAt: strPtr(ago(merged)),
46 })
47 }
48
49 pane := strings.TrimSpace(h.tmux("new-session", "-d", "-s", "dash", "-x", "70", "-y", "14", "-c", h.RepoDir,
50 "-P", "-F", "#{pane_id}", "env KLAUS_SESSION_ID="+h.SessionID+" klaus dashboard"))
51
52 screen := func(want ...string) string {
53 t.Helper()
54 var last string
55 for deadline := time.Now().Add(20 * time.Second); time.Now().Before(deadline); time.Sleep(200 * time.Millisecond) {
56 last = h.tmux("capture-pane", "-p", "-t", pane)
57 ok := true
58 for _, w := range want {
59 ok = ok && strings.Contains(last, w)
60 }
61 if ok {
62 return last
63 }
64 }
65 t.Fatalf("dashboard screen never showed %q; last screen:\n%s", want, last)
66 return ""
67 }
68 selected := func(s string) string {
69 for _, line := range strings.Split(s, "\n") {
70 if strings.HasPrefix(line, "> ") {
71 return line
72 }
73 }
74 return ""
75 }
76
77 // 14 lines: header, source, summary, blank, 8 rows, blank, footer. The
78 // running agents come first; the 30 old merged PRs are hidden.
79 s := screen("1–8 of 14", "30 hidden")
80 if sel := selected(s); !strings.Contains(sel, "active 01") {
81 t.Errorf("first row should be selected, got %q:\n%s", sel, s)
82 }
83
84 h.tmux("send-keys", "-t", pane, "G")
85 s = screen("7–14 of 14")
86 if sel := selected(s); !strings.Contains(sel, "#110") {
87 t.Errorf("G should select the last row (#110), got %q:\n%s", sel, s)
88 }
89
90 h.tmux("send-keys", "-t", pane, "h")
91 s = screen("of 44", "all shown")
92 if sel := selected(s); !strings.Contains(sel, "#110") {
93 t.Errorf("show all should keep #110 selected and in view, got %q:\n%s", sel, s)
94 }
95
96 // Shrink the pane: the selection stays in view and nothing wraps.
97 h.tmux("resize-window", "-t", "dash", "-x", "40", "-y", "8")
98 s = screen("of 44")
99 if lines := strings.Split(strings.TrimRight(s, "\n"), "\n"); len(lines) > 8 {
100 t.Errorf("screen has %d lines in an 8-line pane:\n%s", len(lines), s)
101 }
102 if sel := selected(s); !strings.Contains(sel, "#110") {
103 t.Errorf("after resize #110 should still be selected and visible, got %q:\n%s", sel, s)
104 }
105}
106
107// TestDashboardShowsRunningConsult runs a real consult beside the real
108// dashboard: its job shows as RUNNING while the backend works and as
109// SUCCEEDED once it answers, and tab then enter reads the answer.
110func TestDashboardShowsRunningConsult(t *testing.T) {
111 t.Parallel()
112 h := NewHarness(t)
113 // The dashboard shows nothing until it has a run.
114 h.SeedState(&run.State{ID: "20261017-1200-a001", Type: "launch", Prompt: "seeded run",
115 CreatedAt: time.Now().Add(-time.Minute).Format(time.RFC3339)})
116 release := filepath.Join(h.E2EDir, "release-codex")
117 h.WriteStub("codex", "#!/bin/sh\ncat >/dev/null\nwhile [ ! -e "+release+" ]; do sleep 0.1; done\necho the e2e answer\n")
118
119 pane := strings.TrimSpace(h.tmux("new-session", "-d", "-s", "dash", "-x", "120", "-y", "24", "-c", h.RepoDir,
120 "-P", "-F", "#{pane_id}", "env KLAUS_SESSION_ID="+h.SessionID+" klaus dashboard"))
121 screen := func(want ...string) string {
122 t.Helper()
123 var last string
124 for deadline := time.Now().Add(20 * time.Second); time.Now().Before(deadline); time.Sleep(200 * time.Millisecond) {
125 last = h.tmux("capture-pane", "-p", "-t", pane)
126 ok := true
127 for _, w := range want {
128 ok = ok && strings.Contains(last, w)
129 }
130 if ok {
131 return last
132 }
133 }
134 t.Fatalf("dashboard screen never showed %q; last screen:\n%s", want, last)
135 return ""
136 }
137 screen("seeded run")
138
139 consult := exec.Command(klausBinary, "consult", "--backend", "codex", "--timeout", "1m", "What is the e2e answer?")
140 consult.Dir = h.RepoDir
141 consult.Env = h.env()
142 if err := consult.Start(); err != nil {
143 t.Fatal(err)
144 }
145 t.Cleanup(func() { _ = consult.Process.Kill(); _ = consult.Wait() })
146 screen("jobs · 1 running", "consult:", "RUNNING", "What is the e2e answer?")
147
148 if err := os.WriteFile(release, nil, 0o644); err != nil {
149 t.Fatal(err)
150 }
151 if err := consult.Wait(); err != nil {
152 t.Fatalf("consult: %v", err)
153 }
154 screen("jobs · 1 finished", "SUCCEEDED")
155
156 h.tmux("send-keys", "-t", pane, "Tab")
157 screen("succeeded | enter read")
158 h.tmux("send-keys", "-t", pane, "Enter")
159 screen("status: succeeded", "the e2e answer", "esc back")
160}
161
162// TestDashboardScreenStaysWhole runs the real dashboard over a valley change
163// in a pane its view fills, while the valley on PATH refuses `changes
164// --checks`, as one from before the flag does. klaus notes the refusal in
165// dashboard.log, not on the screen: a line written under the dashboard
166// scrolls the pane, and the renderer, which redraws only the lines that
167// changed, would leave the scrolled lines on screen for good. Output from
168// outside klaus does the same; r redraws the whole screen.
169func TestDashboardScreenStaysWhole(t *testing.T) {
170 t.Parallel()
171 h := NewHarness(t)
172 h.installValleyStub()
173 sha := strings.Repeat("1", 40)
174 ready := h.valleyRun("20261103-1644-0001", "prompt one", "nxvp", "Add a health check", 1, sha, time.Now().Add(-5*time.Minute))
175 h.writeValleyChanges(map[string]any{
176 "schema": "valley-changes/v1", "host": "valley-host",
177 "repos": []any{map[string]any{
178 "repo": "widget", "url": "git@valley-host:widget.git", "error": nil, "branches": []any{},
179 "changes": []any{change(ready, "ready", 1, version(1, sha, "Add a health check", "not-landed"))},
180 }},
181 })
182 hold := filepath.Join(h.E2EDir, "valley.checks-hold")
183 for _, f := range []string{"valley.checks-refuse", "valley.checks-hold"} {
184 if err := os.WriteFile(filepath.Join(h.E2EDir, f), nil, 0o644); err != nil {
185 t.Fatal(err)
186 }
187 }
188
189 // 7 lines: header, source, summary, blank, the row, blank, footer.
190 pane := strings.TrimSpace(h.tmux("new-session", "-d", "-s", "dash", "-x", "120", "-y", "7", "-c", h.RepoDir,
191 "-P", "-F", "#{pane_id}", "env KLAUS_SESSION_ID="+h.SessionID+" klaus dashboard"))
192 capture := func() []string {
193 return strings.Split(strings.TrimRight(h.tmux("capture-pane", "-p", "-t", pane), "\n"), "\n")
194 }
195 // whole says what is wrong with the screen, if anything: each line in
196 // its place, once.
197 whole := func(lines []string) string {
198 count := func(s string) int {
199 n := 0
200 for _, l := range lines {
201 if strings.Contains(l, s) {
202 n++
203 }
204 }
205 return n
206 }
207 switch {
208 case len(lines) != 7:
209 return fmt.Sprintf("%d lines, want 7", len(lines))
210 case !strings.Contains(lines[0], "flow dashboard"):
211 return "the header isn't the first line"
212 case !strings.Contains(lines[1], "polling: 30s"):
213 return "the source line isn't the second line"
214 case !strings.Contains(lines[2], "1–1 of 1") || count("1–1 of 1") != 1:
215 return "the summary isn't the third line, once"
216 case !strings.HasPrefix(lines[4], "> widget nxvp") || count("nxvp") != 1:
217 return "the row isn't the fifth line, once"
218 case !strings.Contains(lines[6], "q quit"):
219 return "the footer isn't the last line"
220 case count("does not take") > 0 || count("stray") > 0:
221 return "output from under the dashboard is on screen"
222 }
223 return ""
224 }
225 waitWhole := func(after string) {
226 t.Helper()
227 var lines []string
228 for deadline := time.Now().Add(20 * time.Second); time.Now().Before(deadline); time.Sleep(200 * time.Millisecond) {
229 if lines = capture(); whole(lines) == "" {
230 return
231 }
232 }
233 t.Fatalf("%s: %s:\n%s", after, whole(lines), strings.Join(lines, "\n"))
234 }
235
236 // The valley refuses --checks once the dashboard is on screen, as one
237 // that takes seconds to list does; the refusal comes before the listing
238 // without --checks.
239 waitWhole("at start")
240 if err := os.Remove(hold); err != nil {
241 t.Fatal(err)
242 }
243 for deadline := time.Now().Add(20 * time.Second); ; time.Sleep(100 * time.Millisecond) {
244 plain := 0
245 for _, c := range h.valleyCalls("changes") {
246 if !strings.Contains(c, "--checks") {
247 plain++
248 }
249 }
250 if plain > 0 {
251 break
252 }
253 if time.Now().After(deadline) {
254 t.Fatalf("the dashboard never listed changes without --checks: %q", h.valleyCalls("changes"))
255 }
256 }
257 waitWhole("after the valley refused --checks")
258 if log, _ := os.ReadFile(filepath.Join(h.Home, ".klaus", "sessions", h.SessionID, "dashboard.log")); !strings.Contains(string(log), "does not take --checks") {
259 t.Errorf("dashboard.log should note the refusal:\n%s", log)
260 }
261
262 // Something else writes to the pane: the view scrolls up under it.
263 tty, err := os.OpenFile(strings.TrimSpace(h.tmux("display-message", "-p", "-t", pane, "#{pane_tty}")), os.O_WRONLY, 0)
264 if err != nil {
265 t.Fatal(err)
266 }
267 _, err = tty.WriteString("stray output\r\n")
268 tty.Close()
269 if err != nil {
270 t.Fatal(err)
271 }
272 for deadline := time.Now().Add(5 * time.Second); whole(capture()) == ""; time.Sleep(100 * time.Millisecond) {
273 if time.Now().After(deadline) {
274 t.Fatal("the stray output never showed; the test can't tell r redraws the screen")
275 }
276 }
277 h.tmux("send-keys", "-t", pane, "r")
278 waitWhole("after r")
279}