| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | tea "github.com/charmbracelet/bubbletea" |
| 11 | "github.com/charmbracelet/x/ansi" |
| 12 | "github.com/fsnotify/fsnotify" |
| 13 | "the-valley/flow/internal/jobs" |
| 14 | "the-valley/flow/internal/run" |
| 15 | ) |
| 16 | |
| 17 | // sampleJobs returns a running threaded consult, a failed one, a succeeded |
| 18 | // review whose answer is in its record, a succeeded consult with an answer |
| 19 | // file, and one that finished too long ago to list. |
| 20 | func sampleJobs(t *testing.T, now time.Time) []*jobs.Job { |
| 21 | t.Helper() |
| 22 | at := func(d time.Duration) *time.Time { x := now.Add(-d); return &x } |
| 23 | answer := filepath.Join(t.TempDir(), "answer") |
| 24 | if err := os.WriteFile(answer, []byte("first line of the answer\nsecond line of the answer\n"), 0o600); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | return []*jobs.Job{ |
| 28 | {ID: "consult-20261017-1200-aaaa0001", Kind: "consult", Thread: "design", Backend: "codex", Model: "gpt-9", |
| 29 | Prompt: "Design the jobs table", Status: jobs.Running, StartedAt: now.Add(-12 * time.Minute)}, |
| 30 | {ID: "consult-20261017-1150-bbbb0002", Kind: "consult", Backend: "claude", Prompt: "Why did it break", |
| 31 | Status: jobs.Failed, Error: "consult claude: exit status 7", RawOutput: "/tmp/consult-0002.raw", |
| 32 | StartedAt: now.Add(-6 * time.Minute), FinishedAt: at(5 * time.Minute)}, |
| 33 | {ID: "review-20261017-1140-cccc0003", Kind: "review", PR: "acme/widget#7", Backend: "agy", |
| 34 | Status: jobs.Succeeded, StartedAt: now.Add(-23 * time.Minute), FinishedAt: at(20 * time.Minute), |
| 35 | Review: &jobs.Review{Verdict: "Does what it says.", Findings: nil}}, |
| 36 | {ID: "consult-20261017-1130-dddd0004", Kind: "consult", Backend: "codex", Prompt: "Answer me", |
| 37 | Status: jobs.Succeeded, AnswerFile: answer, StartedAt: now.Add(-41 * time.Minute), FinishedAt: at(40 * time.Minute)}, |
| 38 | {ID: "consult-20261017-0100-eeee0005", Kind: "consult", Backend: "codex", Prompt: "Long ago", |
| 39 | Status: jobs.Succeeded, StartedAt: now.Add(-11 * time.Hour), FinishedAt: at(10 * time.Hour)}, |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func newJobsModel(t *testing.T, width, height int) dashboardModel { |
| 44 | t.Helper() |
| 45 | now := time.Now() |
| 46 | m := dashboardModel{ |
| 47 | states: []*run.State{{ID: "20261017-1200-0001", Type: "launch", Prompt: "active 01", TmuxPane: strPtr("%1"), |
| 48 | CreatedAt: now.Format(time.RFC3339), Worktree: "/wt", TargetRepo: strPtr("testowner/testrepo")}}, |
| 49 | tmuxDeps: testDashboardTmuxDeps(), |
| 50 | ghStatus: map[string]*prStatus{}, |
| 51 | hideAfter: defaultHideFinishedAfter, |
| 52 | } |
| 53 | m = send(m, tea.WindowSizeMsg{Width: width, Height: height}) |
| 54 | return send(m, jobsLoadedMsg{jobs: sampleJobs(t, now)}) |
| 55 | } |
| 56 | |
| 57 | func TestDashboardJobsSection(t *testing.T) { |
| 58 | m := newJobsModel(t, 140, 30) |
| 59 | view := ansi.Strip(m.View()) |
| 60 | lines := strings.Split(view, "\n") |
| 61 | find := func(s string) int { |
| 62 | for i, l := range lines { |
| 63 | if strings.Contains(l, s) { |
| 64 | return i |
| 65 | } |
| 66 | } |
| 67 | return -1 |
| 68 | } |
| 69 | row, header := find("active 01"), find("jobs · 1 running · 3 finished") |
| 70 | if row < 0 || header <= row { |
| 71 | t.Fatalf("jobs section should follow the run rows:\n%s", view) |
| 72 | } |
| 73 | for i, want := range [][]string{ |
| 74 | {"consult:0001", "design", "codex gpt-9", "RUNNING", "12m", "Design the jobs table"}, |
| 75 | {"consult:0002", "-", "claude", "FAILED", "1m", "Why did it break"}, |
| 76 | {"review:0003", "acme/widget#7", "agy", "SUCCEEDED", "3m"}, |
| 77 | {"consult:0004", "-", "codex", "SUCCEEDED", "1m", "Answer me"}, |
| 78 | } { |
| 79 | line := lines[header+1+i] |
| 80 | for _, w := range want { |
| 81 | if !strings.Contains(line, w) { |
| 82 | t.Errorf("job line %d %q missing %q", i, line, w) |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | if strings.Contains(view, "Long ago") { |
| 87 | t.Errorf("a job that finished 10h ago is listed:\n%s", view) |
| 88 | } |
| 89 | if !strings.Contains(view, "o open · tab jobs") { |
| 90 | t.Errorf("footer should offer tab:\n%s", view) |
| 91 | } |
| 92 | // The jobs don't join the run rows. |
| 93 | if !strings.Contains(view, "1 agents") || strings.Contains(view, "1 done") { |
| 94 | t.Errorf("summary counts jobs as rows:\n%s", view) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // On a short pane the jobs lines go after the spacing and source lines and |
| 99 | // before the errors, the footer and any run row; their header goes last. |
| 100 | func TestDashboardFitLayoutDropsJobsBeforeRows(t *testing.T) { |
| 101 | m := dashboardModel{recentErrors: []dashboardError{{Time: time.Now(), Message: "boom"}}} |
| 102 | const rows, nJobs = 10, 3 |
| 103 | prevJobs := nJobs |
| 104 | for h := 30; h >= 1; h-- { |
| 105 | m.height = h |
| 106 | items, bodyH := m.fitLayout(rows, nJobs, false) |
| 107 | count := map[chromeKind]int{} |
| 108 | for _, it := range items { |
| 109 | count[it.kind]++ |
| 110 | } |
| 111 | shown := count[chromeJob] |
| 112 | if shown > prevJobs { |
| 113 | t.Errorf("height %d: %d job lines, more than at a taller pane", h, shown) |
| 114 | } |
| 115 | prevJobs = shown |
| 116 | if bodyH < min(3, h) && len(items) > 0 { |
| 117 | t.Errorf("height %d: %d rows while %d chrome lines are kept", h, bodyH, len(items)) |
| 118 | } |
| 119 | if shown < nJobs && (count[chromeBlank] > 0 || count[chromeSource] > 0) { |
| 120 | t.Errorf("height %d: a job line went before the spacing and source lines: %+v", h, count) |
| 121 | } |
| 122 | if shown > 0 && (count[chromeError] == 0 || count[chromeFooter] == 0 || count[chromeJobs] == 0) { |
| 123 | t.Errorf("height %d: the errors, footer or jobs header went before a job line: %+v", h, count) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // The rendered view fits, and names the jobs it had no room for. |
| 128 | view := newJobsModel(t, 80, 7).View() |
| 129 | assertFits(t, view, 80, 7) |
| 130 | if plain := ansi.Strip(view); !strings.Contains(plain, "active 01") || !strings.Contains(plain, "not shown") { |
| 131 | t.Errorf("short pane should keep the row and count the hidden jobs:\n%s", plain) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | func TestDashboardSelectJobAndReadIt(t *testing.T) { |
| 136 | m := newJobsModel(t, 140, 30) |
| 137 | selected := func(view string) string { |
| 138 | for _, l := range strings.Split(ansi.Strip(view), "\n") { |
| 139 | if strings.HasPrefix(l, "> ") { |
| 140 | return l |
| 141 | } |
| 142 | } |
| 143 | return "" |
| 144 | } |
| 145 | if sel := selected(m.View()); !strings.Contains(sel, "active 01") { |
| 146 | t.Fatalf("the run row starts selected, got %q", sel) |
| 147 | } |
| 148 | |
| 149 | m = press(m, "tab") |
| 150 | view := ansi.Strip(m.View()) |
| 151 | if sel := selected(view); !strings.Contains(sel, "consult:0001") || strings.Count(view, "\n> ") != 1 { |
| 152 | t.Errorf("tab should select the top job, and only it: %q\n%s", sel, view) |
| 153 | } |
| 154 | if !strings.Contains(view, "consult:0001 running for 12m | enter read") { |
| 155 | t.Errorf("footer should say how the running job stands:\n%s", view) |
| 156 | } |
| 157 | |
| 158 | // The footer of a failed job says why, and where its raw output is. |
| 159 | m = press(m, "j") |
| 160 | view = ansi.Strip(m.View()) |
| 161 | if sel := selected(view); !strings.Contains(sel, "consult:0002") || |
| 162 | !strings.Contains(view, "failed: consult claude: exit status 7 · raw output: /tmp/consult-0002.raw") { |
| 163 | t.Errorf("failed job selection %q:\n%s", sel, view) |
| 164 | } |
| 165 | |
| 166 | // a and d act on PRs, not jobs; q still quits from the jobs. |
| 167 | m = press(m, "a") |
| 168 | if view := ansi.Strip(m.View()); !strings.Contains(view, "a job has no PR") { |
| 169 | t.Errorf("approve on a job should say why it can't:\n%s", view) |
| 170 | } |
| 171 | |
| 172 | // enter reads a review's answer, its findings, in place of the list; q |
| 173 | // closes it rather than quitting. |
| 174 | m = press(m, "j", "enter") |
| 175 | view = ansi.Strip(m.View()) |
| 176 | if !strings.Contains(view, "Verdict: Does what it says.") || !strings.Contains(view, "pr: acme/widget#7") || strings.Contains(view, "active 01") { |
| 177 | t.Errorf("review detail:\n%s", view) |
| 178 | } |
| 179 | m = press(m, "q") |
| 180 | if sel := selected(m.View()); !strings.Contains(sel, "review:0003") { |
| 181 | t.Fatalf("q should close the detail back onto the job, got %q", sel) |
| 182 | } |
| 183 | |
| 184 | // A consult's answer, scrolled with j/k; esc goes back, and again to |
| 185 | // the runs. |
| 186 | m = press(m, "G", "enter") |
| 187 | view = ansi.Strip(m.View()) |
| 188 | for _, want := range []string{"job consult-20261017-1130-dddd0004", "status: succeeded", "second line of the answer", "esc back"} { |
| 189 | if !strings.Contains(view, want) { |
| 190 | t.Errorf("detail missing %q:\n%s", want, view) |
| 191 | } |
| 192 | } |
| 193 | m = press(m, "esc", "esc") |
| 194 | if sel := selected(m.View()); !strings.Contains(sel, "active 01") { |
| 195 | t.Errorf("esc should go back to the runs, got %q", sel) |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | // The dashboard follows job records through its fsnotify watcher: a job |
| 200 | // appears when its record is written, and changes when it finishes. |
| 201 | func TestDashboardReloadsJobsOnRecordChange(t *testing.T) { |
| 202 | dir := t.TempDir() |
| 203 | store := run.NewHomeDirStoreFromPath(dir) |
| 204 | jobStore := jobs.NewStore(dir) |
| 205 | |
| 206 | // A run's record is told apart from a job's. |
| 207 | w, ok := startWatcherCmd(store, jobStore.Dir)().(*fsnotify.Watcher) |
| 208 | if !ok { |
| 209 | t.Fatal("watcher didn't start") |
| 210 | } |
| 211 | if err := store.Save(&run.State{ID: "20261017-1200-0001", Type: "launch", CreatedAt: time.Now().Format(time.RFC3339)}); err != nil { |
| 212 | t.Fatal(err) |
| 213 | } |
| 214 | if msg := awaitCmd(t, watchFSCmd(w, jobStore.Dir)); msg != (fsEventMsg{}) { |
| 215 | t.Errorf("a run's record gave %T, want fsEventMsg", msg) |
| 216 | } |
| 217 | w.Close() |
| 218 | |
| 219 | w, ok = startWatcherCmd(store, jobStore.Dir)().(*fsnotify.Watcher) |
| 220 | if !ok { |
| 221 | t.Fatal("watcher didn't start") |
| 222 | } |
| 223 | defer w.Close() |
| 224 | l := &teaLoop{t: t, msgs: make(chan tea.Msg, 64), m: dashboardModel{ |
| 225 | store: store, jobsDir: jobStore.Dir, states: []*run.State{}, |
| 226 | tmuxDeps: testDashboardTmuxDeps(), ghStatus: map[string]*prStatus{}, width: 120, height: 30, |
| 227 | }} |
| 228 | l.deliver(w) |
| 229 | |
| 230 | job := &jobs.Job{ID: "consult-20261017-1200-abcd0001", Kind: "consult", Backend: "codex", Prompt: "watch me"} |
| 231 | if err := jobStore.Start(job); err != nil { // by this test process, so it reads as running |
| 232 | t.Fatal(err) |
| 233 | } |
| 234 | l.until("watch me", "RUNNING") |
| 235 | if err := jobStore.Finish(job, jobs.TimedOut, "timeout after 1s"); err != nil { |
| 236 | t.Fatal(err) |
| 237 | } |
| 238 | l.until("watch me", "TIMED OUT") |
| 239 | } |
| 240 | |
| 241 | // awaitCmd runs cmd, failing the test if it doesn't return promptly. |
| 242 | func awaitCmd(t *testing.T, cmd tea.Cmd) tea.Msg { |
| 243 | t.Helper() |
| 244 | ch := make(chan tea.Msg, 1) |
| 245 | go func() { ch <- cmd() }() |
| 246 | select { |
| 247 | case msg := <-ch: |
| 248 | return msg |
| 249 | case <-time.After(10 * time.Second): |
| 250 | t.Fatal("command didn't return") |
| 251 | } |
| 252 | return nil |
| 253 | } |
| 254 | |
| 255 | // teaLoop runs a dashboard model's commands and feeds their messages back |
| 256 | // through Update, as bubbletea does. |
| 257 | type teaLoop struct { |
| 258 | t *testing.T |
| 259 | m dashboardModel |
| 260 | msgs chan tea.Msg |
| 261 | } |
| 262 | |
| 263 | func (l *teaLoop) start(c tea.Cmd) { |
| 264 | if c != nil { |
| 265 | go func() { l.msgs <- c() }() |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | func (l *teaLoop) deliver(msg tea.Msg) { |
| 270 | switch msg := msg.(type) { |
| 271 | case nil: |
| 272 | case tea.BatchMsg: |
| 273 | for _, c := range msg { |
| 274 | l.start(c) |
| 275 | } |
| 276 | default: |
| 277 | next, c := l.m.Update(msg) |
| 278 | l.m = next.(dashboardModel) |
| 279 | l.start(c) |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // until delivers messages until the view shows every one of want. |
| 284 | func (l *teaLoop) until(want ...string) { |
| 285 | l.t.Helper() |
| 286 | deadline := time.After(10 * time.Second) |
| 287 | for { |
| 288 | view := ansi.Strip(l.m.View()) |
| 289 | ok := true |
| 290 | for _, w := range want { |
| 291 | ok = ok && strings.Contains(view, w) |
| 292 | } |
| 293 | if ok { |
| 294 | return |
| 295 | } |
| 296 | select { |
| 297 | case msg := <-l.msgs: |
| 298 | l.deliver(msg) |
| 299 | case <-deadline: |
| 300 | l.t.Fatalf("view never showed %q:\n%s", want, view) |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | func TestDashboardProseJobsCountedNotListed(t *testing.T) { |
| 306 | now := time.Now() |
| 307 | at := func(d time.Duration) *time.Time { x := now.Add(-d); return &x } |
| 308 | runA := "20261017-1200-aaaa0001" |
| 309 | runB := "20261017-1201-bbbb0002" |
| 310 | |
| 311 | // 34-job case: 1 finished consult + 33 finished prose passes (8 failed, |
| 312 | // spanning failed/timed out/interrupted/died), with 13 belonging to runA |
| 313 | // (3 failed) and 20 to runB (5 failed), plus 1 running prose pass on runA |
| 314 | // and 1 finished prose pass older than 3h on runA. |
| 315 | list := []*jobs.Job{ |
| 316 | {ID: "prose-20261017-1205-run00001", Kind: jobs.KindProse, Run: runA, Backend: "agy", Model: "gemini-4", |
| 317 | Prompt: "rewrite docs/guide.md", Status: jobs.Running, StartedAt: now.Add(-30 * time.Second)}, |
| 318 | {ID: "consult-20261017-1150-cons0001", Kind: jobs.KindConsult, Thread: "design", Backend: "codex", Model: "gpt-9", |
| 319 | Prompt: "Design the jobs table", Status: jobs.Succeeded, StartedAt: now.Add(-12 * time.Minute), FinishedAt: at(10 * time.Minute)}, |
| 320 | } |
| 321 | failStatuses := []jobs.Status{jobs.Failed, jobs.TimedOut, jobs.Interrupted, jobs.Died} |
| 322 | for i := 0; i < 33; i++ { |
| 323 | owner := runB |
| 324 | status := jobs.Succeeded |
| 325 | if i < 13 { |
| 326 | owner = runA |
| 327 | if i < 3 { |
| 328 | status = failStatuses[i%len(failStatuses)] |
| 329 | } |
| 330 | } else if i-13 < 5 { |
| 331 | status = failStatuses[(i-13)%len(failStatuses)] |
| 332 | } |
| 333 | j := &jobs.Job{ |
| 334 | ID: "prose-20261017-1100-" + string(rune('a'+i/26)) + string(rune('a'+i%26)) + "0001", |
| 335 | Kind: jobs.KindProse, |
| 336 | Run: owner, |
| 337 | Backend: "agy", |
| 338 | Prompt: "rewrite README.md", |
| 339 | Status: status, |
| 340 | StartedAt: now.Add(-time.Duration(15+i) * time.Minute), |
| 341 | } |
| 342 | if status != jobs.Died { |
| 343 | j.FinishedAt = at(time.Duration(14+i) * time.Minute) |
| 344 | } |
| 345 | list = append(list, j) |
| 346 | } |
| 347 | // Outside the 3h window: excluded from the header count, still counted on runA's footer. |
| 348 | list = append(list, &jobs.Job{ |
| 349 | ID: "prose-20261017-0100-old00001", Kind: jobs.KindProse, Run: runA, Backend: "agy", |
| 350 | Prompt: "rewrite old.md", Status: jobs.Succeeded, |
| 351 | StartedAt: now.Add(-6 * time.Hour), FinishedAt: at(5 * time.Hour), |
| 352 | }) |
| 353 | |
| 354 | m := dashboardModel{ |
| 355 | states: []*run.State{ |
| 356 | {ID: runA, Type: "launch", Prompt: "long run", TmuxPane: strPtr("%1"), |
| 357 | CreatedAt: now.Add(-20 * time.Minute).Format(time.RFC3339), Worktree: "/wt-a", TargetRepo: strPtr("owner/app")}, |
| 358 | {ID: runB, Type: "launch", Prompt: "second run", TmuxPane: strPtr("%2"), |
| 359 | CreatedAt: now.Add(-10 * time.Minute).Format(time.RFC3339), Worktree: "/wt-b", TargetRepo: strPtr("owner/app")}, |
| 360 | }, |
| 361 | tmuxDeps: testDashboardTmuxDeps(), |
| 362 | ghStatus: map[string]*prStatus{}, |
| 363 | hideAfter: defaultHideFinishedAfter, |
| 364 | } |
| 365 | m = send(m, tea.WindowSizeMsg{Width: 140, Height: 30}) |
| 366 | m = send(m, jobsLoadedMsg{jobs: list}) |
| 367 | |
| 368 | view := ansi.Strip(m.View()) |
| 369 | if !strings.Contains(view, "jobs · 1 running · 1 finished · 33 prose passes, 8 failed") { |
| 370 | t.Fatalf("jobs header should count listed rows and finished prose passes:\n%s", view) |
| 371 | } |
| 372 | if !strings.Contains(view, "prose:0001") || !strings.Contains(view, "rewrite docs/guide.md") { |
| 373 | t.Errorf("running prose pass should be listed:\n%s", view) |
| 374 | } |
| 375 | if !strings.Contains(view, "consult:0001") || !strings.Contains(view, "Design the jobs table") { |
| 376 | t.Errorf("finished consult should be listed:\n%s", view) |
| 377 | } |
| 378 | if strings.Contains(view, "rewrite README.md") || strings.Contains(view, "rewrite old.md") { |
| 379 | t.Errorf("finished prose passes must not appear as rows:\n%s", view) |
| 380 | } |
| 381 | |
| 382 | // Row B is newer so it is selected first; footer shows its 20 prose passes, 5 failed. |
| 383 | if !strings.Contains(view, "20 prose passes, 5 failed |") { |
| 384 | t.Errorf("selected runB footer missing its prose count:\n%s", view) |
| 385 | } |
| 386 | // Move selection to runA: 13 recent + 1 older = 14 finished prose passes, 3 failed (running pass not counted). |
| 387 | m = press(m, "j") |
| 388 | view = ansi.Strip(m.View()) |
| 389 | if !strings.Contains(view, "14 prose passes, 3 failed |") { |
| 390 | t.Errorf("selected runA footer should count all its finished prose passes:\n%s", view) |
| 391 | } |
| 392 | |
| 393 | // Tab selects only the 2 listed jobs (running prose pass, then consult). |
| 394 | m = press(m, "tab") |
| 395 | if v := ansi.Strip(m.View()); !strings.Contains(v, "> prose:0001") { |
| 396 | t.Errorf("tab should land on running prose pass:\n%s", v) |
| 397 | } |
| 398 | m = press(m, "j") |
| 399 | if v := ansi.Strip(m.View()); !strings.Contains(v, "> consult:0001") { |
| 400 | t.Errorf("j should move to the consult:\n%s", v) |
| 401 | } |
| 402 | // Another j stays clamped on consult:0001, never landing on a hidden prose pass. |
| 403 | m = press(m, "j") |
| 404 | if v := ansi.Strip(m.View()); !strings.Contains(v, "> consult:0001") { |
| 405 | t.Errorf("j past last listed job must stay on consult:0001:\n%s", v) |
| 406 | } |
| 407 | |
| 408 | // When the running prose pass finishes, only the consult remains listed |
| 409 | // and the header prose count increases to 34 (8 failed). |
| 410 | list[0].Status = jobs.Succeeded |
| 411 | list[0].FinishedAt = at(time.Second) |
| 412 | m = send(m, jobsLoadedMsg{jobs: list}) |
| 413 | view = ansi.Strip(m.View()) |
| 414 | if !strings.Contains(view, "jobs · 1 finished · 34 prose passes, 8 failed") { |
| 415 | t.Errorf("after running prose finishes, header should update:\n%s", view) |
| 416 | } |
| 417 | if strings.Contains(view, "rewrite docs/guide.md") { |
| 418 | t.Errorf("finished prose pass should leave the table:\n%s", view) |
| 419 | } |
| 420 | if !strings.Contains(view, "> consult:0001") { |
| 421 | t.Errorf("selection should stay on consult:0001:\n%s", view) |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | func TestDashboardOnlyFinishedProseJobs(t *testing.T) { |
| 426 | now := time.Now() |
| 427 | finished := now.Add(-5 * time.Minute) |
| 428 | m := dashboardModel{ |
| 429 | states: []*run.State{ |
| 430 | {ID: "20261017-1200-aaaa0001", Type: "launch", Prompt: "only run", TmuxPane: strPtr("%1"), |
| 431 | CreatedAt: now.Format(time.RFC3339), Worktree: "/wt", TargetRepo: strPtr("owner/app")}, |
| 432 | }, |
| 433 | tmuxDeps: testDashboardTmuxDeps(), |
| 434 | ghStatus: map[string]*prStatus{}, |
| 435 | hideAfter: defaultHideFinishedAfter, |
| 436 | } |
| 437 | m = send(m, tea.WindowSizeMsg{Width: 140, Height: 20}) |
| 438 | m = send(m, jobsLoadedMsg{jobs: []*jobs.Job{ |
| 439 | {ID: "prose-20261017-1155-aaaa0001", Kind: jobs.KindProse, Run: "20261017-1200-aaaa0001", |
| 440 | Backend: "agy", Prompt: "rewrite README.md", Status: jobs.Succeeded, |
| 441 | StartedAt: now.Add(-6 * time.Minute), FinishedAt: &finished}, |
| 442 | }}) |
| 443 | |
| 444 | view := ansi.Strip(m.View()) |
| 445 | if !strings.Contains(view, "jobs · 1 prose pass") || strings.Contains(view, "failed") { |
| 446 | t.Errorf("header should show singular prose pass with no failed count:\n%s", view) |
| 447 | } |
| 448 | if strings.Contains(view, "tab jobs") { |
| 449 | t.Errorf("footer must not advertise tab jobs when no job rows are listed:\n%s", view) |
| 450 | } |
| 451 | if !strings.Contains(view, "1 prose pass |") { |
| 452 | t.Errorf("selected run footer should show its 1 prose pass:\n%s", view) |
| 453 | } |
| 454 | |
| 455 | // Pressing tab does not focus the jobs section. |
| 456 | m = press(m, "tab") |
| 457 | if m.jobFocus { |
| 458 | t.Error("tab must not enter jobFocus when only finished prose passes exist") |
| 459 | } |
| 460 | |
| 461 | // Pressing x notes the prose pass in the prompt; on a narrow pane it |
| 462 | // falls back to the compact variant so y/n stays visible. Confirming |
| 463 | // with y hides it from the header. |
| 464 | m = press(m, "x") |
| 465 | if v := ansi.Strip(m.View()); !strings.Contains(v, "hide 0 finished jobs, 1 prose pass? y/n") { |
| 466 | t.Errorf("clear prompt should report prose count honestly:\n%s", v) |
| 467 | } |
| 468 | narrow := send(m, tea.WindowSizeMsg{Width: 60, Height: 20}) |
| 469 | if v := ansi.Strip(narrow.View()); !strings.Contains(v, "clear: 0 runs, 0 jobs, 1 prose? y/n") { |
| 470 | t.Errorf("narrow clear prompt should keep y/n visible:\n%s", v) |
| 471 | } |
| 472 | m = press(m, "y") |
| 473 | if v := ansi.Strip(m.View()); strings.Contains(v, "jobs ·") { |
| 474 | t.Errorf("jobs header should disappear after clearing finished prose passes:\n%s", v) |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | func TestDashboardFooterCombinesApproveCommandAndProse(t *testing.T) { |
| 479 | now := time.Now() |
| 480 | finished := now.Add(-2 * time.Minute) |
| 481 | branch, sha := "fix-bug-12345678", strings.Repeat("a", 40) |
| 482 | m := dashboardModel{ |
| 483 | states: []*run.State{ |
| 484 | {ID: "20261017-1200-aaaa0001", Type: "launch", Prompt: "fix bug", |
| 485 | CreatedAt: now.Add(-10 * time.Minute).Format(time.RFC3339), |
| 486 | TargetRepo: strPtr("owner/app"), ChangeID: "chg-12345678", |
| 487 | PushedBranch: &branch, PushedSHA: &sha, |
| 488 | Valley: &run.Valley{Repo: "app", Handle: "abcd", Version: 1}}, |
| 489 | {ID: "20261017-1150-aaaa0000", Type: "launch", Prompt: "initial attempt", |
| 490 | CreatedAt: now.Add(-20 * time.Minute).Format(time.RFC3339), |
| 491 | TargetRepo: strPtr("owner/app"), ChangeID: "chg-12345678"}, |
| 492 | }, |
| 493 | tmuxDeps: testDashboardTmuxDeps(), |
| 494 | ghStatus: map[string]*prStatus{}, |
| 495 | approveTemplate: "valley approve {handle} --head {head}", |
| 496 | hideAfter: defaultHideFinishedAfter, |
| 497 | } |
| 498 | m = send(m, tea.WindowSizeMsg{Width: 160, Height: 20}) |
| 499 | m = send(m, jobsLoadedMsg{jobs: []*jobs.Job{ |
| 500 | {ID: "prose-20261017-1158-aaaa0001", Kind: jobs.KindProse, Run: "20261017-1200-aaaa0001", |
| 501 | Backend: "agy", Status: jobs.Succeeded, StartedAt: now.Add(-3 * time.Minute), FinishedAt: &finished}, |
| 502 | {ID: "prose-20261017-1159-aaaa0002", Kind: jobs.KindProse, Run: "20261017-1150-aaaa0000", |
| 503 | Backend: "agy", Status: jobs.Failed, StartedAt: now.Add(-3 * time.Minute), FinishedAt: &finished}, |
| 504 | }}) |
| 505 | |
| 506 | view := ansi.Strip(m.View()) |
| 507 | want := "approve: valley approve abcd --head " + sha + " · 2 prose passes, 1 failed" |
| 508 | if !strings.Contains(view, want) { |
| 509 | t.Errorf("footer missing %q:\n%s", want, view) |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | func TestDashboardJobsNotShownExcludesProse(t *testing.T) { |
| 514 | now := time.Now() |
| 515 | at := func(d time.Duration) *time.Time { x := now.Add(-d); return &x } |
| 516 | list := []*jobs.Job{ |
| 517 | {ID: "consult-20261017-1200-aaaa0001", Kind: jobs.KindConsult, Backend: "codex", Prompt: "c1", Status: jobs.Running, StartedAt: now.Add(-2 * time.Minute)}, |
| 518 | {ID: "consult-20261017-1155-bbbb0002", Kind: jobs.KindConsult, Backend: "codex", Prompt: "c2", Status: jobs.Succeeded, StartedAt: now.Add(-6 * time.Minute), FinishedAt: at(5 * time.Minute)}, |
| 519 | {ID: "review-20261017-1150-cccc0003", Kind: jobs.KindReview, PR: "owner/app#1", Backend: "agy", Status: jobs.Succeeded, StartedAt: now.Add(-11 * time.Minute), FinishedAt: at(10 * time.Minute)}, |
| 520 | {ID: "prose-20261017-1145-dddd0004", Kind: jobs.KindProse, Backend: "agy", Prompt: "p1", Status: jobs.Succeeded, StartedAt: now.Add(-16 * time.Minute), FinishedAt: at(15 * time.Minute)}, |
| 521 | {ID: "prose-20261017-1140-eeee0005", Kind: jobs.KindProse, Backend: "agy", Prompt: "p2", Status: jobs.Failed, StartedAt: now.Add(-21 * time.Minute), FinishedAt: at(20 * time.Minute)}, |
| 522 | } |
| 523 | m := dashboardModel{ |
| 524 | states: []*run.State{{ID: "20261017-1200-0001", Type: "launch", Prompt: "active 01", TmuxPane: strPtr("%1"), |
| 525 | CreatedAt: now.Format(time.RFC3339), Worktree: "/wt", TargetRepo: strPtr("owner/app")}}, |
| 526 | tmuxDeps: testDashboardTmuxDeps(), |
| 527 | ghStatus: map[string]*prStatus{}, |
| 528 | hideAfter: defaultHideFinishedAfter, |
| 529 | } |
| 530 | // Height 7 leaves room for header, summary, 1 row, jobs header, 2 of 3 listed jobs, footer -> 1 not shown (not 3). |
| 531 | m = send(m, tea.WindowSizeMsg{Width: 120, Height: 7}) |
| 532 | m = send(m, jobsLoadedMsg{jobs: list}) |
| 533 | view := ansi.Strip(m.View()) |
| 534 | assertFits(t, m.View(), 120, 7) |
| 535 | if !strings.Contains(view, "jobs · 1 running · 2 finished · 1 not shown · 2 prose passes, 1 failed") { |
| 536 | t.Errorf("short pane header counts wrong:\n%s", view) |
| 537 | } |
| 538 | } |