| 1 | //go:build e2e |
| 2 | |
| 3 | package e2e |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "the-valley/flow/internal/run" |
| 14 | ) |
| 15 | |
| 16 | // launchOne runs `klaus launch args...`, which must succeed and record exactly |
| 17 | // one new run, and returns that run's state. |
| 18 | func launchOne(t *testing.T, h *Harness, args ...string) *run.State { |
| 19 | t.Helper() |
| 20 | before := h.RunIDs() |
| 21 | res := h.RunKlaus(append([]string{"launch"}, args...)...) |
| 22 | if res.ExitCode != 0 { |
| 23 | t.Fatalf("launch %v exited %d\nstdout:\n%s\nstderr:\n%s", args, res.ExitCode, res.Stdout, res.Stderr) |
| 24 | } |
| 25 | ids := h.RunIDs() |
| 26 | for _, id := range ids { |
| 27 | if !contains(before, id) { |
| 28 | st, err := h.ReadState(id) |
| 29 | if err != nil { |
| 30 | t.Fatalf("reading state: %v", err) |
| 31 | } |
| 32 | return st |
| 33 | } |
| 34 | } |
| 35 | t.Fatalf("launch %v recorded no new run (runs: %v)", args, ids) |
| 36 | return nil |
| 37 | } |
| 38 | |
| 39 | // runIDTail is the random end of a run ID, the suffix of a derived branch name. |
| 40 | func runIDTail(id string) string { |
| 41 | return id[strings.LastIndex(id, "-")+1:] |
| 42 | } |
| 43 | |
| 44 | // TestLaunchDerivedBranchSalvaged launches from a brief that opens with a |
| 45 | // "# <repo>: title" heading. The worktree's branch is named from the title, |
| 46 | // and when the worker leaves unfinished work without a PR, finalize salvages |
| 47 | // it to origin under that same name. |
| 48 | func TestLaunchDerivedBranchSalvaged(t *testing.T) { |
| 49 | t.Parallel() |
| 50 | h := NewHarness(t) |
| 51 | h.WriteDefaultClaudeOutput(`{"type":"result","subtype":"success","total_cost_usd":0.01,"duration_ms":10,"session_id":"11111111-2222-4333-8444-555555555555"}` + "\n") |
| 52 | h.WriteStub("claude", h.claudeStubScript()+"echo wip > unfinished.txt\n") |
| 53 | |
| 54 | brief := filepath.Join(h.E2EDir, "brief.md") |
| 55 | if err := os.WriteFile(brief, []byte("# repo: Add a health check endpoint\n\nServe 200 on /healthz.\n"), 0o644); err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | st := launchOne(t, h, "--prompt-file", brief) |
| 59 | |
| 60 | want := "add-a-health-check-endpoint-" + runIDTail(st.ID) |
| 61 | if st.Branch != want { |
| 62 | t.Fatalf("state branch = %q, want %q", st.Branch, want) |
| 63 | } |
| 64 | if got := h.GitOutput(st.Worktree, "symbolic-ref", "--short", "HEAD"); got != want { |
| 65 | t.Fatalf("worktree is on %q, want %q", got, want) |
| 66 | } |
| 67 | |
| 68 | h.WaitForClaudeStart(30 * time.Second) |
| 69 | releaseBackendWorkers(t, h) |
| 70 | final, err := h.ReadState(st.ID) |
| 71 | if err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | if final.NeedsAttention == nil { |
| 75 | t.Fatalf("salvaged run not marked needs-attention: %+v", final) |
| 76 | } |
| 77 | if got := h.GitOutput(h.OriginDir, "show", want+":unfinished.txt"); got != "wip" { |
| 78 | t.Fatalf("origin's %s holds unfinished.txt = %q, want the salvaged work", want, got) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // TestLaunchBranchFlag covers the issue title as the name's source, --branch |
| 83 | // naming the branch exactly, and the --branch values launch refuses. |
| 84 | func TestLaunchBranchFlag(t *testing.T) { |
| 85 | t.Parallel() |
| 86 | h := NewHarness(t) |
| 87 | h.WriteStub("gh", fmt.Sprintf(`#!/usr/bin/env bash |
| 88 | dir=%q |
| 89 | { printf 'gh'; for a in "$@"; do printf ' %%q' "$a"; done; printf '\n'; } >> "$dir/gh.argv" |
| 90 | if [ "$1 $2" = "issue view" ]; then echo "Dashboard crashes on resize"; fi |
| 91 | exit 0 |
| 92 | `, h.E2EDir)) |
| 93 | |
| 94 | st := launchOne(t, h, "--issue", "7", "# Something else\n\nfix it") |
| 95 | if want := "dashboard-crashes-on-resize-" + runIDTail(st.ID); st.Branch != want { |
| 96 | t.Errorf("issue launch branch = %q, want %q", st.Branch, want) |
| 97 | } |
| 98 | if !strings.Contains(h.GHArgv(), "gh issue view") { |
| 99 | t.Errorf("launch did not look up the issue title; gh calls:\n%s", h.GHArgv()) |
| 100 | } |
| 101 | |
| 102 | st = launchOne(t, h, "--branch", "feat/explicit-name", "--issue", "7", "# Something else") |
| 103 | if st.Branch != "feat/explicit-name" { |
| 104 | t.Errorf("--branch launch state branch = %q, want feat/explicit-name", st.Branch) |
| 105 | } |
| 106 | if got := h.GitOutput(st.Worktree, "symbolic-ref", "--short", "HEAD"); got != "feat/explicit-name" { |
| 107 | t.Errorf("--branch worktree is on %q, want feat/explicit-name", got) |
| 108 | } |
| 109 | |
| 110 | h.git(h.RepoDir, "push", "-q", "origin", "main:refs/heads/taken-on-origin") |
| 111 | runs := h.RunIDs() |
| 112 | for _, tc := range []struct { |
| 113 | name, want string |
| 114 | args []string |
| 115 | }{ |
| 116 | {"with --pr", "mutually exclusive", []string{"--branch", "x", "--pr", "1"}}, |
| 117 | {"invalid name", "not a valid branch name", []string{"--branch", "bad..name"}}, |
| 118 | {"empty name", "not a valid branch name", []string{"--branch", ""}}, |
| 119 | {"existing local branch", "already exists", []string{"--branch", "main"}}, |
| 120 | {"branch only on origin", "already exists on origin", []string{"--branch", "taken-on-origin"}}, |
| 121 | {"branch of a live run", "already exists", []string{"--branch", "feat/explicit-name"}}, |
| 122 | } { |
| 123 | t.Run(tc.name, func(t *testing.T) { |
| 124 | res := h.RunKlaus(append([]string{"launch"}, append(tc.args, "do it")...)...) |
| 125 | if res.ExitCode == 0 { |
| 126 | t.Fatalf("launch %v succeeded, want a refusal\nstdout:\n%s", tc.args, res.Stdout) |
| 127 | } |
| 128 | if !strings.Contains(res.Stderr, tc.want) { |
| 129 | t.Errorf("stderr should say %q, got:\n%s", tc.want, res.Stderr) |
| 130 | } |
| 131 | }) |
| 132 | } |
| 133 | if got := h.RunIDs(); len(got) != len(runs) { |
| 134 | t.Errorf("refused launches recorded runs: %v -> %v", runs, got) |
| 135 | } |
| 136 | |
| 137 | h.WaitForClaudeStart(30 * time.Second) |
| 138 | releaseBackendWorkers(t, h) |
| 139 | } |