| 1 | //go:build e2e |
| 2 | |
| 3 | package e2e |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "the-valley/flow/internal/run" |
| 12 | ) |
| 13 | |
| 14 | // TestStatusRendersSeededRun is the warm-up scenario: it proves the harness |
| 15 | // can build + run the real binary, that HOME isolation routes state reads to |
| 16 | // the temp store, and that the rendered table reflects seeded state — all |
| 17 | // without touching tmux (the seeded run has no pane). |
| 18 | func TestStatusRendersSeededRun(t *testing.T) { |
| 19 | t.Parallel() |
| 20 | h := NewHarness(t) |
| 21 | |
| 22 | budget := "5.00" |
| 23 | h.SeedState(&run.State{ |
| 24 | ID: "20260608-0101-deadbeef", |
| 25 | Prompt: "implement the frobnicator", |
| 26 | Branch: "agent/20260608-0101-deadbeef", |
| 27 | Budget: &budget, |
| 28 | CreatedAt: "2026-06-08T01:01:00Z", |
| 29 | // No TmuxPane and no PRURL: determineStatus -> "exited" with no tmux/gh calls. |
| 30 | }) |
| 31 | |
| 32 | res := h.RunKlaus("status") |
| 33 | if res.ExitCode != 0 { |
| 34 | t.Fatalf("status exited %d; stderr: %s", res.ExitCode, res.Stderr) |
| 35 | } |
| 36 | |
| 37 | for _, want := range []string{"20260608-0101-deadbeef", "implement the frobnicator", "exited"} { |
| 38 | if !strings.Contains(res.Stdout, want) { |
| 39 | t.Errorf("status output missing %q\n--- stdout ---\n%s", want, res.Stdout) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // TestStatusKeepsAttentionAFailedSuccessorTookOn: a run that took on a |
| 45 | // salvaged run's work (took_over) and then failed carries its attention, |
| 46 | // so klaus status shows it needs-attention, and the salvaged run |
| 47 | // superseded, whether GitHub says its PR is merged, closed or out of |
| 48 | // reach. A run's PR that merged with no attention taken on shows merged. |
| 49 | func TestStatusKeepsAttentionAFailedSuccessorTookOn(t *testing.T) { |
| 50 | t.Parallel() |
| 51 | h := NewHarness(t) |
| 52 | h.WriteStub("gh", `#!/usr/bin/env bash |
| 53 | case "$*" in |
| 54 | *"--json state"*) ;; |
| 55 | *) exit 0 ;; |
| 56 | esac |
| 57 | case "$*" in |
| 58 | *pull/77|*pull/80) echo MERGED ;; |
| 59 | *pull/78) echo CLOSED ;; |
| 60 | *pull/79) echo 'GraphQL: Could not resolve to a PullRequest with the number of 79. (repository.pullRequest)' >&2; exit 1 ;; |
| 61 | esac |
| 62 | exit 0 |
| 63 | `) |
| 64 | cost, took := 0.5, int64(1000) |
| 65 | pr := func(n int) *string { return strPtrE2E(fmt.Sprintf("https://github.com/acme/widget/pull/%d", n)) } |
| 66 | want := map[string]string{} |
| 67 | for i, num := range []int{77, 78, 79} { |
| 68 | change := strings.Repeat(string(rune('k'+i)), 32) |
| 69 | salvaged := h.seedSalvaged(fmt.Sprintf("20261021-2300-0000000%d", i), "agent/"+change[:4], strings.Repeat("a", 40), change) |
| 70 | successor := fmt.Sprintf("20261021-2300-0000001%d", i) |
| 71 | h.SeedState(&run.State{ID: successor, Type: "pr-fix", Prompt: "carry on", TargetRepo: strPtrE2E("acme/widget"), |
| 72 | Branch: salvaged.Branch, ChangeID: change, PRURL: pr(num), TookOver: []string{salvaged.ID}, |
| 73 | FailureReason: strPtrE2E("error_during_execution"), CostUSD: &cost, DurationMS: &took, |
| 74 | CreatedAt: time.Now().Format(time.RFC3339)}) |
| 75 | want[salvaged.ID], want[successor] = "superseded", "needs-attention" |
| 76 | } |
| 77 | h.SeedState(&run.State{ID: "20261021-2300-00000020", Type: "launch", Prompt: "an ordinary PR", TargetRepo: strPtrE2E("acme/widget"), |
| 78 | Branch: "agent/plain", ChangeID: strings.Repeat("n", 32), PRURL: pr(80), CostUSD: &cost, DurationMS: &took, |
| 79 | CreatedAt: time.Now().Format(time.RFC3339)}) |
| 80 | want["20261021-2300-00000020"] = "merged" |
| 81 | |
| 82 | res := h.RunKlaus("status") |
| 83 | if res.ExitCode != 0 { |
| 84 | t.Fatalf("status exited %d; stderr: %s", res.ExitCode, res.Stderr) |
| 85 | } |
| 86 | for id, status := range want { |
| 87 | var line string |
| 88 | for _, l := range strings.Split(res.Stdout, "\n") { |
| 89 | if strings.HasPrefix(l, id+" ") { |
| 90 | line = l |
| 91 | } |
| 92 | } |
| 93 | if f := strings.Fields(line); len(f) < 2 || f[1] != status { |
| 94 | t.Errorf("run %s should be %s: %q\n%s", id, status, line, res.Stdout) |
| 95 | } |
| 96 | } |
| 97 | } |