| 1 | //go:build e2e |
| 2 | |
| 3 | package e2e |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "regexp" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | "the-valley/flow/internal/valley" |
| 16 | ) |
| 17 | |
| 18 | // useRealValley has the harness use the valley CLI on PATH, and skips the |
| 19 | // test unless there is one whose help lists the verbs a change's life |
| 20 | // takes: ready, unready and changes. It sets valley.principal in the |
| 21 | // sandbox repo, which ready needs to name the change's owner; every |
| 22 | // worktree reads that repo's config. |
| 23 | func (h *Harness) useRealValley() { |
| 24 | h.t.Helper() |
| 25 | path, err := exec.LookPath("valley") |
| 26 | if err != nil { |
| 27 | skipWithout(h.t, "no valley on PATH") |
| 28 | } |
| 29 | help, _ := exec.Command(path, "help").CombinedOutput() |
| 30 | for _, verb := range []string{"ready", "unready", "changes"} { |
| 31 | if !regexp.MustCompile(`(?m)^\s*` + verb + `\b`).Match(help) { |
| 32 | skipWithout(h.t, "the valley on PATH (%s) has no %s", path, verb) |
| 33 | } |
| 34 | } |
| 35 | h.git(h.RepoDir, "config", "valley.principal", "operator") |
| 36 | } |
| 37 | |
| 38 | // valleyChanges is the real valley's view of the sandbox repo, as `valley |
| 39 | // changes --json` prints it. |
| 40 | func (h *Harness) valleyChanges() valley.Repo { |
| 41 | h.t.Helper() |
| 42 | cmd := exec.Command("valley", "changes", "--json") |
| 43 | cmd.Dir, cmd.Env = h.RepoDir, h.env() |
| 44 | var stderr bytes.Buffer |
| 45 | cmd.Stderr = &stderr |
| 46 | out, err := cmd.Output() |
| 47 | if err != nil { |
| 48 | h.t.Fatalf("valley changes: %v: %s", err, stderr.String()) |
| 49 | } |
| 50 | l, err := valley.Decode(out) |
| 51 | if err != nil || len(l.Repos) != 1 || l.Repos[0].Err() != "" { |
| 52 | h.t.Fatalf("valley changes printed %s (%v)", out, err) |
| 53 | } |
| 54 | return l.Repos[0] |
| 55 | } |
| 56 | |
| 57 | // gitTry runs real git in dir as GitOutput does, returning its combined |
| 58 | // output and error instead of failing the test. |
| 59 | func (h *Harness) gitTry(dir string, args ...string) (string, error) { |
| 60 | cmd := exec.Command("git", args...) |
| 61 | cmd.Dir = dir |
| 62 | cmd.Env = append(os.Environ(), "HOME="+h.Home, "GIT_CONFIG_GLOBAL="+filepath.Join(h.Home, ".gitconfig"), "GIT_CONFIG_NOSYSTEM=1") |
| 63 | out, err := cmd.CombinedOutput() |
| 64 | return string(out), err |
| 65 | } |
| 66 | |
| 67 | // leasedPush returns the push command the worker prompt gives for a |
| 68 | // restack, split into git's arguments. |
| 69 | func leasedPush(t *testing.T, sys string) []string { |
| 70 | t.Helper() |
| 71 | m := regexp.MustCompile("(?m)^`git (push --force-with-lease=[^`]+)`$").FindStringSubmatch(sys) |
| 72 | if m == nil { |
| 73 | t.Fatalf("worker prompt gives no leased push:\n%s", sys) |
| 74 | } |
| 75 | return strings.Fields(m[1]) |
| 76 | } |
| 77 | |
| 78 | // TestChangeRestacksOntoMain drives a valley change through a conflict with |
| 79 | // main, with real launches against the real valley CLI and bare repos |
| 80 | // (issue #346). A first run pushes v1 and finalize readies it; main then |
| 81 | // takes a commit that conflicts with it. A --change run is told it may |
| 82 | // restack the change and how to push it: it rebases onto origin/main, |
| 83 | // resolves the conflict and pushes with the prompt's own command, leased on |
| 84 | // the head it started from. Finalize records the rewritten head as v2 of |
| 85 | // the same change on the same branch, and origin holds no other branch. A |
| 86 | // later --change by the full change ID, as a conflict dispatch names the |
| 87 | // change, resolves to v2, and its lease, on v2, refuses to overwrite a push |
| 88 | // someone made meanwhile; finalize doesn't overwrite it either. |
| 89 | func TestChangeRestacksOntoMain(t *testing.T) { |
| 90 | t.Parallel() |
| 91 | h := NewHarness(t) |
| 92 | h.useRealValley() |
| 93 | h.joinValley() |
| 94 | h.WriteDefaultClaudeOutput(`{"type":"result","subtype":"success","total_cost_usd":0.5,"duration_ms":1000,"session_id":"` + changeSession + `"}` + "\n") |
| 95 | |
| 96 | first, _ := h.launchRun("add a greeting") |
| 97 | h.WaitForClaudeStart(30 * time.Second) |
| 98 | v1 := h.pushWork(first, "greeting.txt", "Add a greeting") |
| 99 | first = h.finishRun(first.ID) |
| 100 | if first.Valley == nil || first.Valley.Version != 1 || first.Valley.Error != "" || first.Valley.Handle == "" { |
| 101 | t.Fatalf("finalize should mark v1 ready: %+v", first.Valley) |
| 102 | } |
| 103 | branch, handle := first.Branch, first.Valley.Handle |
| 104 | |
| 105 | // Main takes a commit that conflicts with the change. |
| 106 | if err := os.WriteFile(filepath.Join(h.RepoDir, "greeting.txt"), []byte("Greet from main\n"), 0o644); err != nil { |
| 107 | t.Fatal(err) |
| 108 | } |
| 109 | h.git(h.RepoDir, "add", "greeting.txt") |
| 110 | h.git(h.RepoDir, "commit", "-qm", "Greet from main") |
| 111 | h.git(h.RepoDir, "push", "-q", "origin", "main") |
| 112 | mainHead := h.GitOutput(h.RepoDir, "rev-parse", "HEAD") |
| 113 | |
| 114 | // --change <handle>: the worker is told how to restack the change. |
| 115 | h.resetWorker() |
| 116 | restack, res := h.launchRun("--change", handle, "rebase the change onto main and resolve the conflict") |
| 117 | h.WaitForClaudeStart(30 * time.Second) |
| 118 | for _, want := range []string{ |
| 119 | "(continuing change " + handle + ")", |
| 120 | "branch: " + branch + "\n", |
| 121 | "change: " + first.ChangeID + " (inherited from change " + handle + ")", |
| 122 | } { |
| 123 | if !strings.Contains(res.Stdout, want) { |
| 124 | t.Errorf("launch output missing %q:\n%s", want, res.Stdout) |
| 125 | } |
| 126 | } |
| 127 | sys := h.WorkerSystemPrompt() |
| 128 | if strings.Contains(sys, "do not rebase") || !strings.Contains(sys, "never merge another branch into it") { |
| 129 | t.Errorf("a valley change's worker should be told to rebase, never merge:\n%s", sys) |
| 130 | } |
| 131 | push := leasedPush(t, sys) |
| 132 | if want := "push --force-with-lease=refs/heads/" + branch + ":" + v1 + " origin HEAD:refs/heads/" + branch; strings.Join(push, " ") != want { |
| 133 | t.Errorf("leased push = %q, want %q", strings.Join(push, " "), want) |
| 134 | } |
| 135 | |
| 136 | // The worker rebases, resolves the conflict and pushes as told. |
| 137 | wt := restack.Worktree |
| 138 | h.git(wt, "fetch", "-q", "origin") |
| 139 | if out, err := h.gitTry(wt, "rebase", "origin/main"); err == nil || !strings.Contains(out, "CONFLICT") { |
| 140 | t.Fatalf("rebasing onto main should conflict: %v\n%s", err, out) |
| 141 | } |
| 142 | if err := os.WriteFile(filepath.Join(wt, "greeting.txt"), []byte("Greet from main\nAdd a greeting\n"), 0o644); err != nil { |
| 143 | t.Fatal(err) |
| 144 | } |
| 145 | h.git(wt, "add", "greeting.txt") |
| 146 | h.git(wt, "-c", "core.editor=true", "rebase", "--continue") |
| 147 | h.git(wt, push...) |
| 148 | v2 := h.GitOutput(wt, "rev-parse", "HEAD") |
| 149 | if tip := h.GitOutput(h.OriginDir, "rev-parse", "refs/heads/"+branch); tip != v2 { |
| 150 | t.Fatalf("origin's %s at %s, want the rebased %s", branch, tip, v2) |
| 151 | } |
| 152 | h.git(h.OriginDir, "merge-base", "--is-ancestor", mainHead, v2) |
| 153 | |
| 154 | // Finalize records the rewritten head as v2 of the same change. |
| 155 | restack = h.finishRun(restack.ID) |
| 156 | if restack.ChangeID != first.ChangeID || restack.PushedBranch == nil || *restack.PushedBranch != branch || |
| 157 | restack.PushedSHA == nil || *restack.PushedSHA != v2 || restack.NeedsAttention != nil { |
| 158 | t.Errorf("the restack run recorded change %s, push %v at %v, attention %v; want %s, %s at %s, none", |
| 159 | restack.ChangeID, restack.PushedBranch, restack.PushedSHA, restack.NeedsAttention, first.ChangeID, branch, v2) |
| 160 | } |
| 161 | if v := restack.Valley; v == nil || v.Handle != handle || v.Version != 2 || v.Error != "" { |
| 162 | t.Errorf("finalize should mark the rebased head ready as %s v2: %+v", handle, v) |
| 163 | } |
| 164 | repo := h.valleyChanges() |
| 165 | if len(repo.Changes) != 1 { |
| 166 | t.Fatalf("the valley should record one change, got %+v", repo.Changes) |
| 167 | } |
| 168 | ch := repo.Changes[0] |
| 169 | if ch.ChangeID != first.ChangeID || ch.State != valley.StateReady || ch.Active != 2 || len(ch.Versions) != 2 || |
| 170 | ch.Versions[0].Head != v1 || ch.Versions[1].Head != v2 || ch.Versions[1].Branch != branch { |
| 171 | t.Errorf("the valley should record v1 at %s and v2 at %s, on %s, of %s: %+v", v1, v2, branch, first.ChangeID, ch) |
| 172 | } |
| 173 | if len(repo.Branches) != 0 { |
| 174 | t.Errorf("the valley lists stray branches: %+v", repo.Branches) |
| 175 | } |
| 176 | if got := h.originBranches(); strings.Join(got, " ") != branch+" main" { |
| 177 | t.Errorf("origin's branches = %v, want only %s and main", got, branch) |
| 178 | } |
| 179 | |
| 180 | // A later --change by the full change ID starts from v2 and leases on it. |
| 181 | h.resetWorker() |
| 182 | later, res := h.launchRun("--change", first.ChangeID, "tidy the greeting") |
| 183 | h.WaitForClaudeStart(30 * time.Second) |
| 184 | if later.Branch != branch || later.ChangeID != first.ChangeID || strings.Contains(res.Stderr, "warning") { |
| 185 | t.Errorf("later run on %s, change %s, stderr %q; want %s, %s and no warning", later.Branch, later.ChangeID, res.Stderr, branch, first.ChangeID) |
| 186 | } |
| 187 | if head := h.GitOutput(later.Worktree, "rev-parse", "HEAD"); head != v2 { |
| 188 | t.Errorf("later run's worktree at %s, want v2 %s", head, v2) |
| 189 | } |
| 190 | push = leasedPush(t, h.WorkerSystemPrompt()) |
| 191 | if lease := "--force-with-lease=refs/heads/" + branch + ":" + v2; push[1] != lease { |
| 192 | t.Errorf("later run's lease %q, want %q", push[1], lease) |
| 193 | } |
| 194 | |
| 195 | // Someone else pushes to the branch meanwhile: the lease refuses to |
| 196 | // overwrite it, and so does finalize. |
| 197 | other := filepath.Join(t.TempDir(), "other") |
| 198 | h.git("", "clone", "-q", "-b", branch, h.OriginDir, other) |
| 199 | if err := os.WriteFile(filepath.Join(other, "farewell.txt"), []byte("Bye\n"), 0o644); err != nil { |
| 200 | t.Fatal(err) |
| 201 | } |
| 202 | h.git(other, "add", "farewell.txt") |
| 203 | h.git(other, "commit", "-qm", "Add a farewell") |
| 204 | h.git(other, "push", "-q", "origin", branch) |
| 205 | theirs := h.GitOutput(other, "rev-parse", "HEAD") |
| 206 | h.git(later.Worktree, "commit", "-q", "--amend", "-m", "Add a tidier greeting") |
| 207 | if out, err := h.gitTry(later.Worktree, push...); err == nil || !strings.Contains(out, "stale info") { |
| 208 | t.Errorf("the leased push should be refused as stale: %v\n%s", err, out) |
| 209 | } |
| 210 | h.finishRun(later.ID) |
| 211 | if tip := h.GitOutput(h.OriginDir, "rev-parse", "refs/heads/"+branch); tip != theirs { |
| 212 | t.Errorf("origin's %s at %s, want the concurrent push %s kept", branch, tip, theirs) |
| 213 | } |
| 214 | if got := h.originBranches(); strings.Join(got, " ") != branch+" main" { |
| 215 | t.Errorf("origin's branches = %v, want only %s and main", got, branch) |
| 216 | } |
| 217 | } |