| 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/event" |
| 14 | ) |
| 15 | |
| 16 | // listing is a valley-changes/v1 listing of the sandbox repo, widget, with |
| 17 | // the given change records. |
| 18 | func listing(changes ...any) map[string]any { |
| 19 | return map[string]any{ |
| 20 | "schema": "valley-changes/v1", |
| 21 | "host": "valley-host", |
| 22 | "repos": []any{map[string]any{"repo": "widget", "url": "git@valley-host:widget.git", "error": nil, |
| 23 | "branches": []any{}, "changes": changes}}, |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | // changeRecord is a valley change record with the given ID and handle, in |
| 28 | // state, whose one version is head on branch. |
| 29 | func changeRecord(id, handle, state, head, branch, title string) map[string]any { |
| 30 | v := version(1, head, title, "not-landed") |
| 31 | v["branch"] = branch |
| 32 | return map[string]any{"change_id": id, "handle": handle, "target": "main", "owner": "operator", |
| 33 | "state": state, "record": "0000000000000000000000000000000000000000", "seq": 1, "active": 1, |
| 34 | "versions": []any{v}, "includes": []string{}} |
| 35 | } |
| 36 | |
| 37 | // TestFinalizeRecordsThePushedChange replays run 20261022-1036-d8deeb25: a |
| 38 | // --resume-from run continues another run's valley change on its branch, |
| 39 | // and its agent makes a new change instead, switching to a branch of its |
| 40 | // own from origin/main and committing with its own Change-Id. Finalize |
| 41 | // records the pushed change and branch in place of launch's, marks the |
| 42 | // pushed head ready as a version of that change, and says so in |
| 43 | // agent:change-recorded. klaus status and the dashboard then show the run |
| 44 | // under the change it pushed, apart from the one it was launched on. |
| 45 | func TestFinalizeRecordsThePushedChange(t *testing.T) { |
| 46 | t.Parallel() |
| 47 | h := NewHarness(t) |
| 48 | h.RegisterProject("acme/widget", h.RepoDir) |
| 49 | h.installValleyStub() |
| 50 | h.joinValley() |
| 51 | if err := os.WriteFile(filepath.Join(h.E2EDir, "valley.has-unready"), nil, 0o644); err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | h.WriteDefaultClaudeOutput(`{"type":"result","subtype":"success","total_cost_usd":0.5,"duration_ms":1000,"session_id":"` + changeSession + `"}` + "\n") |
| 55 | |
| 56 | first, _ := h.launchRun("build approval keys") |
| 57 | h.WaitForClaudeStart(30 * time.Second) |
| 58 | v1 := h.pushWork(first, "keys.go", "Build approval keys") |
| 59 | first = h.finishRun(first.ID) |
| 60 | h.writeValleyChanges(listing(changeRecord(first.ChangeID, "qvzz", "ready", v1, first.Branch, "Build approval keys"))) |
| 61 | |
| 62 | h.resetWorker() |
| 63 | follow, _ := h.launchRun("--repo", "widget", "--resume-from", first.ID, "land the registry fix on its own") |
| 64 | h.WaitForClaudeStart(30 * time.Second) |
| 65 | if follow.Branch != first.Branch || follow.ChangeID != first.ChangeID { |
| 66 | t.Fatalf("follow-up on branch %q, change %q; want it to continue %q, %q", follow.Branch, follow.ChangeID, first.Branch, first.ChangeID) |
| 67 | } |
| 68 | |
| 69 | // The agent makes a new change from origin/main instead. |
| 70 | worktree := follow.Worktree |
| 71 | const branch, subject = "registry-reads-its-commit", "Read the registry through its commit" |
| 72 | newID := "vpkz" + strings.Repeat("pk", 14) |
| 73 | h.git(worktree, "switch", "-q", "-c", branch, "origin/main") |
| 74 | if err := os.WriteFile(filepath.Join(worktree, "registry.go"), []byte("package registry\n"), 0o644); err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | h.git(worktree, "add", "-A") |
| 78 | h.git(worktree, "commit", "-qm", subject+"\n\nChange-Id: "+newID) |
| 79 | h.git(worktree, "push", "-q", "-u", "origin", "HEAD") |
| 80 | head := h.GitOutput(worktree, "rev-parse", "HEAD") |
| 81 | if got := h.GitOutput(worktree, "log", "-1", "--format=%(trailers:key=Change-Id,valueonly)"); strings.TrimSpace(got) != newID { |
| 82 | t.Fatalf("the commit's Change-Id = %q, want the agent's own %s", got, newID) |
| 83 | } |
| 84 | follow = h.finishRun(follow.ID) |
| 85 | |
| 86 | if follow.ChangeID != newID || follow.Branch != branch { |
| 87 | t.Errorf("run recorded change %q on branch %q; want the pushed %s on %s", follow.ChangeID, follow.Branch, newID, branch) |
| 88 | } |
| 89 | if follow.PushedBranch == nil || *follow.PushedBranch != branch || follow.PushedSHA == nil || *follow.PushedSHA != head { |
| 90 | t.Errorf("pushed %v at %v; want %s at %s", follow.PushedBranch, follow.PushedSHA, branch, head) |
| 91 | } |
| 92 | if follow.PushedSubject != subject { |
| 93 | t.Errorf("pushed subject = %q, want %q", follow.PushedSubject, subject) |
| 94 | } |
| 95 | // Launch's draft of the change it continued stays on record, as that |
| 96 | // change's. |
| 97 | if follow.Unready == nil || follow.Unready.ChangeID != first.ChangeID || follow.Unready.Handle != "nxvp" { |
| 98 | t.Errorf("unready = %+v, want launch's draft of %s", follow.Unready, first.ChangeID) |
| 99 | } |
| 100 | ready := fmt.Sprintf("%s: ready --change %s --head %s %s", worktree, newID, head, branch) |
| 101 | if calls := h.valleyCalls("ready"); len(calls) != 2 || calls[1] != ready { |
| 102 | t.Errorf("valley ready calls = %q, want the second to be %q", calls, ready) |
| 103 | } |
| 104 | var types []string |
| 105 | var recorded map[string]any |
| 106 | for _, e := range h.runEvents(follow.ID) { |
| 107 | types = append(types, e.Type) |
| 108 | if e.Type == event.AgentChangeRecorded { |
| 109 | recorded = e.Data |
| 110 | } |
| 111 | } |
| 112 | want := map[string]any{"id": follow.ID, "change_id": newID, "branch": branch, "head": head, |
| 113 | "launch_change_id": first.ChangeID, "launch_branch": first.Branch, |
| 114 | "repo_key": "valley:widget", "key": "valley:widget@" + newID, "dispatch_reason": "resume", "head_sha": head} |
| 115 | if fmt.Sprint(recorded) != fmt.Sprint(want) { |
| 116 | t.Errorf("agent:change-recorded data = %v, want %v", recorded, want) |
| 117 | } |
| 118 | if i := strings.Index(strings.Join(types, ","), event.AgentChangeRecorded+","+event.AgentCompleted); i < 0 { |
| 119 | t.Errorf("events = %v, want agent:change-recorded just before agent:completed", types) |
| 120 | } |
| 121 | |
| 122 | // The valley lists both changes: the one the run was launched on, |
| 123 | // withdrawn, and the one it pushed, ready. |
| 124 | h.writeValleyChanges(listing( |
| 125 | changeRecord(first.ChangeID, "qvzz", "withdrawn", v1, first.Branch, "Build approval keys"), |
| 126 | changeRecord(newID, "vpkz", "ready", head, branch, subject))) |
| 127 | res := h.RunKlaus("status") |
| 128 | if res.ExitCode != 0 { |
| 129 | t.Fatalf("status exited %d\n%s%s", res.ExitCode, res.Stdout, res.Stderr) |
| 130 | } |
| 131 | for id, want := range map[string][]string{ |
| 132 | follow.ID: {"awaiting-approval", " vpkz ", subject}, |
| 133 | first.ID: {"withdrawn", " qvzz "}, |
| 134 | } { |
| 135 | line := lineContaining(res.Stdout, id) |
| 136 | for _, w := range want { |
| 137 | if !strings.Contains(line, w) { |
| 138 | t.Errorf("status line for %s lacks %q:\n%s", id, w, res.Stdout) |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | _, screen := h.startDashboard(160) |
| 144 | row := rowOf(t, screen("vpkz", subject), "vpkz") |
| 145 | for _, w := range []string{"READY", subject} { |
| 146 | if !strings.Contains(row, w) { |
| 147 | t.Errorf("dashboard row for vpkz lacks %q: %q", w, row) |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // TestTrackValleyChange tracks a valley change no run pushed, by handle, |
| 153 | // and finds it shown like a tracked PR: klaus status lists it where the |
| 154 | // valley's listing says it stands, under its handle and title, and so does |
| 155 | // the dashboard. The other forms of ref name the same change, so it isn't |
| 156 | // added twice; a change the valley doesn't list adds nothing. Untrack takes |
| 157 | // the same refs and removes it. |
| 158 | func TestTrackValleyChange(t *testing.T) { |
| 159 | t.Parallel() |
| 160 | h := NewHarness(t) |
| 161 | h.RegisterProject("acme/widget", h.RepoDir) |
| 162 | h.installValleyStub() |
| 163 | const title = "Read the registry through its commit" |
| 164 | id := "vpkz" + strings.Repeat("pk", 14) |
| 165 | head := h.GitOutput(h.RepoDir, "rev-parse", "HEAD") |
| 166 | h.writeValleyChanges(listing(changeRecord(id, "vpkz", "ready", head, "registry-reads-its-commit", title))) |
| 167 | |
| 168 | res := h.RunKlaus("track", "--repo", "widget", "vpkz") |
| 169 | if res.ExitCode != 0 || !strings.Contains(res.Stdout, "Tracking change vpkz (widget) — "+title) { |
| 170 | t.Fatalf("track exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr) |
| 171 | } |
| 172 | if calls := h.valleyCalls("changes"); len(calls) != 1 || calls[0] != h.RepoDir+": changes --json" { |
| 173 | t.Errorf("valley changes calls = %q, want one in the project's checkout", calls) |
| 174 | } |
| 175 | res = h.RunKlaus("track", "widget:vpkz", "--repo", "widget", id, "zzzz") |
| 176 | if want := "Already tracking change vpkz (widget)\nAlready tracking change vpkz (widget)\n"; res.Stdout != want { |
| 177 | t.Errorf("tracking the change again by its other refs printed %q, want %q", res.Stdout, want) |
| 178 | } |
| 179 | if want := "warning: zzzz: the valley lists no change zzzz in widget"; !strings.Contains(res.Stderr, want) { |
| 180 | t.Errorf("track of an unlisted change should warn %q:\n%s", want, res.Stderr) |
| 181 | } |
| 182 | ids := h.RunIDs() |
| 183 | if len(ids) != 1 { |
| 184 | t.Fatalf("track recorded %d states, want 1", len(ids)) |
| 185 | } |
| 186 | st, err := h.ReadState(ids[0]) |
| 187 | if err != nil { |
| 188 | t.Fatal(err) |
| 189 | } |
| 190 | if st.Type != "track" || st.ChangeID != id || st.Valley == nil || st.Valley.Repo != "widget" || st.Valley.Handle != "vpkz" || st.HeadSHA != head { |
| 191 | t.Errorf("track record = %+v, valley %+v", st, st.Valley) |
| 192 | } |
| 193 | |
| 194 | status := func(want ...string) { |
| 195 | t.Helper() |
| 196 | res := h.RunKlaus("status") |
| 197 | line := lineContaining(res.Stdout, st.ID) |
| 198 | for _, w := range want { |
| 199 | if !strings.Contains(line, w) { |
| 200 | t.Errorf("status line for the tracked change lacks %q:\n%s%s", w, res.Stdout, res.Stderr) |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | status("awaiting-approval", " vpkz ", title) |
| 205 | |
| 206 | _, screen := h.startDashboard(160) |
| 207 | if row := rowOf(t, screen("vpkz", title), "vpkz"); !strings.Contains(row, "READY") { |
| 208 | t.Errorf("dashboard row for the tracked change = %q, want READY", row) |
| 209 | } |
| 210 | |
| 211 | // The valley says it landed; then it can't be asked. |
| 212 | landed := changeRecord(id, "vpkz", "ready", head, "registry-reads-its-commit", title) |
| 213 | landed["versions"].([]any)[0].(map[string]any)["landing"] = "landed" |
| 214 | h.writeValleyChanges(listing(landed)) |
| 215 | status("integrated", " vpkz ") |
| 216 | for _, f := range []string{"valley.changes.json", "valley.changes.checks.json"} { |
| 217 | if err := os.Remove(filepath.Join(h.E2EDir, f)); err != nil { |
| 218 | t.Fatal(err) |
| 219 | } |
| 220 | } |
| 221 | status("tracking", " vpkz ") |
| 222 | |
| 223 | res = h.RunKlaus("untrack", "widget:vpkz") |
| 224 | if !strings.Contains(res.Stdout, "Stopped tracking change vpkz (widget)") { |
| 225 | t.Errorf("untrack output:\n%s%s", res.Stdout, res.Stderr) |
| 226 | } |
| 227 | if ids := h.RunIDs(); len(ids) != 0 { |
| 228 | t.Errorf("untrack left %d states", len(ids)) |
| 229 | } |
| 230 | if res := h.RunKlaus("untrack", "vpkz"); !strings.Contains(res.Stderr, "warning: no tracked change vpkz found") { |
| 231 | t.Errorf("untracking it again should warn:\n%s%s", res.Stdout, res.Stderr) |
| 232 | } |
| 233 | } |