| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "slices" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "the-valley/flow/internal/run" |
| 14 | "the-valley/flow/internal/valley" |
| 15 | ) |
| 16 | |
| 17 | // TestResolveChange covers the order --change resolves a ref in: a run ID, |
| 18 | // then the runs in the target repo by change ID, ID prefix or recorded |
| 19 | // handle, then the valley's listing; and where the branch comes from. |
| 20 | func TestResolveChange(t *testing.T) { |
| 21 | str := func(s string) *string { return &s } |
| 22 | id := func(prefix string) string { return prefix + strings.Repeat("k", 32-len(prefix)) } |
| 23 | pushed := func(s *run.State, branch, sha string) *run.State { |
| 24 | s.PushedBranch, s.PushedSHA = &branch, &sha |
| 25 | return s |
| 26 | } |
| 27 | |
| 28 | // Change nxvp in widget: v1 by an older run, a follow-up v2 recorded in |
| 29 | // another session, and a run that never pushed. |
| 30 | v1 := pushed(&run.State{ID: "20261013-0900-0001", TargetRepo: str("site/widget"), ChangeID: id("nxvp"), |
| 31 | CreatedAt: "2026-10-13T09:00:00Z", Valley: &run.Valley{Repo: "widget", Handle: "nxvp", Title: "Add a health check"}}, |
| 32 | "add-a-health-check-0001", "1111111") |
| 33 | v2 := pushed(&run.State{ID: "20261013-1000-0002", TargetRepo: str("widget"), ChangeID: id("nxvp"), |
| 34 | CreatedAt: "2026-10-13T10:00:00Z", Valley: &run.Valley{Repo: "widget", Handle: "nxvp"}}, |
| 35 | "add-a-health-check-0001", "2222222") |
| 36 | unpushed := &run.State{ID: "20261013-1100-0003", TargetRepo: str("widget"), ChangeID: id("nxvp"), CreatedAt: "2026-10-13T11:00:00Z"} |
| 37 | // The same handle's change in another repo, and a change whose recorded |
| 38 | // handle has since lengthened: the listing would now call it rstuv. |
| 39 | gadget := pushed(&run.State{ID: "20261013-0900-0004", TargetRepo: str("gadget"), ChangeID: id("nxvpz"), Prompt: "port the health check\nto gadget", |
| 40 | CreatedAt: "2026-10-13T09:00:00Z", Valley: &run.Valley{Repo: "gadget", Handle: "nxvp"}}, "port-0004", "4444444") |
| 41 | rstu := pushed(&run.State{ID: "20261013-0900-0005", TargetRepo: str("widget"), ChangeID: id("rstuv"), |
| 42 | CreatedAt: "2026-10-13T09:00:00Z", Valley: &run.Valley{Repo: "widget", Handle: "rstu"}}, "tune-0005", "5555555") |
| 43 | // A run from before change IDs, one whose valley calls its repo widget |
| 44 | // while it targeted acme/source, and a second widget change starting nx. |
| 45 | legacy := &run.State{ID: "20261001-0900-0006", TargetRepo: str("widget"), CreatedAt: "2026-10-01T09:00:00Z"} |
| 46 | renamed := pushed(&run.State{ID: "20261013-0900-0007", TargetRepo: str("acme/source"), ChangeID: id("wxyz"), |
| 47 | CreatedAt: "2026-10-13T09:00:00Z", Valley: &run.Valley{Repo: "widget", Handle: "wxyz"}}, "renamed-0007", "7777777") |
| 48 | nxwr := pushed(&run.State{ID: "20261013-0900-0008", TargetRepo: str("widget"), ChangeID: id("nxwr"), Prompt: "cache the health check\nfor a minute", |
| 49 | CreatedAt: "2026-10-13T09:00:00Z"}, "cache-0008", "8888888") |
| 50 | runs := []*run.State{v1, v2, unpushed, gadget, rstu, legacy, renamed, nxwr, {ID: "session-x", Type: "session"}} |
| 51 | |
| 52 | listing := func(changes ...valley.Change) func() (*valley.Changes, error) { |
| 53 | return func() (*valley.Changes, error) { |
| 54 | return &valley.Changes{Schema: valley.SchemaV1, Repos: []valley.Repo{{Repo: "widget", Changes: changes}}}, nil |
| 55 | } |
| 56 | } |
| 57 | elsewhere := valley.Change{ChangeID: id("lmno"), Handle: "lmno", State: valley.StateReady, Active: 2, Versions: []valley.Version{ |
| 58 | {N: 1, Head: "aaaaaaa", Branch: "first-try"}, |
| 59 | {N: 2, Head: "bbbbbbb", Branch: "second-try"}, |
| 60 | }} |
| 61 | unreachable := func() (*valley.Changes, error) { return nil, errors.New("valley changes: cannot reach valley-host") } |
| 62 | |
| 63 | for _, tc := range []struct { |
| 64 | name, ref, repo string |
| 65 | valley func() (*valley.Changes, error) |
| 66 | want changeTarget // Runs compared by ID |
| 67 | runIDs []string |
| 68 | err []string |
| 69 | }{ |
| 70 | { |
| 71 | name: "a run ID names its run's change, and the newest pushed run gives the branch", |
| 72 | ref: v1.ID, repo: "widget", |
| 73 | want: changeTarget{ID: id("nxvp"), Name: "nxvp", Repo: "widget", Branch: "add-a-health-check-0001", Head: "2222222"}, |
| 74 | runIDs: []string{unpushed.ID, v2.ID, v1.ID}, |
| 75 | }, |
| 76 | { |
| 77 | name: "a handle matches the runs in the target repo only", |
| 78 | ref: "nxvp", repo: "widget", |
| 79 | want: changeTarget{ID: id("nxvp"), Name: "nxvp", Repo: "widget", Branch: "add-a-health-check-0001", Head: "2222222"}, |
| 80 | runIDs: []string{unpushed.ID, v2.ID, v1.ID}, |
| 81 | }, |
| 82 | { |
| 83 | name: "a full change ID matches its runs", |
| 84 | ref: id("nxvpz"), repo: "gadget", |
| 85 | want: changeTarget{ID: id("nxvpz"), Name: "nxvp", Repo: "gadget", Branch: "port-0004", Head: "4444444"}, |
| 86 | runIDs: []string{gadget.ID}, |
| 87 | }, |
| 88 | { |
| 89 | name: "a lengthened handle matches the change ID's prefix", |
| 90 | ref: "rstuv", repo: "widget", |
| 91 | want: changeTarget{ID: id("rstuv"), Name: "rstu", Repo: "widget", Branch: "tune-0005", Head: "5555555"}, |
| 92 | runIDs: []string{rstu.ID}, |
| 93 | }, |
| 94 | { |
| 95 | name: "a run is in the repo its valley names, not its target's", |
| 96 | ref: "wxyz", repo: "widget", |
| 97 | want: changeTarget{ID: id("wxyz"), Name: "wxyz", Repo: "widget", Branch: "renamed-0007", Head: "7777777"}, |
| 98 | runIDs: []string{renamed.ID}, |
| 99 | }, |
| 100 | { |
| 101 | name: "a handle doesn't name a change in its target's repo when its valley names another", |
| 102 | ref: "wxyz", repo: "source", |
| 103 | err: []string{`no run on a change "wxyz" is recorded in source`}, |
| 104 | }, |
| 105 | { |
| 106 | name: "a run ID doesn't name a change in its target's repo when its valley names another", |
| 107 | ref: renamed.ID, repo: "source", |
| 108 | err: []string{"run " + renamed.ID + " is in widget, not source"}, |
| 109 | }, |
| 110 | { |
| 111 | name: "an unknown target repo matches no repo", |
| 112 | ref: v1.ID, repo: "", |
| 113 | err: []string{"can't tell which repo the launch targets"}, |
| 114 | }, |
| 115 | { |
| 116 | name: "the valley's active version gives the branch and handle when it lists the change", |
| 117 | ref: v1.ID, repo: "widget", |
| 118 | valley: listing(valley.Change{ChangeID: id("nxvp"), Handle: "nxvpk", State: valley.StateReady, Active: 3, |
| 119 | Versions: []valley.Version{{N: 3, Head: "3333333", Branch: "readied-elsewhere"}}}), |
| 120 | want: changeTarget{ID: id("nxvp"), Name: "nxvpk", Repo: "widget", Branch: "readied-elsewhere", Head: "3333333"}, |
| 121 | runIDs: []string{unpushed.ID, v2.ID, v1.ID}, |
| 122 | }, |
| 123 | { |
| 124 | name: "runs decide when the valley can't be asked", |
| 125 | ref: "nxvp", repo: "widget", valley: unreachable, |
| 126 | want: changeTarget{ID: id("nxvp"), Name: "nxvp", Repo: "widget", Branch: "add-a-health-check-0001", Head: "2222222"}, |
| 127 | runIDs: []string{unpushed.ID, v2.ID, v1.ID}, |
| 128 | }, |
| 129 | { |
| 130 | name: "with no run on it, the valley's listing names the change", |
| 131 | ref: "lmno", repo: "widget", valley: listing(elsewhere), |
| 132 | want: changeTarget{ID: id("lmno"), Name: "lmno", Repo: "widget", Branch: "second-try", Head: "bbbbbbb"}, |
| 133 | }, |
| 134 | { |
| 135 | name: "a handle naming two changes lists them", |
| 136 | ref: "nx", repo: "widget", |
| 137 | err: []string{`"nx" names 2 changes in widget`, id("nxvp") + " (nxvp), latest run " + unpushed.ID + "\n", |
| 138 | id("nxwr") + ", latest run " + nxwr.ID + ": cache the health check"}, |
| 139 | }, |
| 140 | { |
| 141 | name: "a handle naming two listed changes lists them", |
| 142 | ref: "lmn", repo: "widget", |
| 143 | valley: listing(elsewhere, valley.Change{ChangeID: id("lmnz"), Handle: "lmnz"}), |
| 144 | err: []string{`"lmn" names 2 changes in widget`, id("lmno") + " (lmno)", id("lmnz") + " (lmnz)"}, |
| 145 | }, |
| 146 | { |
| 147 | name: "a run in another repo is refused", |
| 148 | ref: gadget.ID, repo: "widget", |
| 149 | err: []string{"run " + gadget.ID + " is in gadget, not widget"}, |
| 150 | }, |
| 151 | { |
| 152 | name: "a run from before change IDs is refused", |
| 153 | ref: legacy.ID, repo: "widget", |
| 154 | err: []string{"run " + legacy.ID + " has no change ID", "--resume-from " + legacy.ID}, |
| 155 | }, |
| 156 | { |
| 157 | name: "an unknown handle without a valley", |
| 158 | ref: "qqqq", repo: "widget", |
| 159 | err: []string{`no run on a change "qqqq" is recorded in widget`}, |
| 160 | }, |
| 161 | { |
| 162 | name: "an unknown handle the valley doesn't list either", |
| 163 | ref: "qqqq", repo: "widget", valley: listing(elsewhere), |
| 164 | err: []string{`no change "qqqq": no run in widget records it, and the valley doesn't list it`}, |
| 165 | }, |
| 166 | { |
| 167 | name: "an unknown handle when the valley can't be asked", |
| 168 | ref: "qqqq", repo: "widget", valley: unreachable, |
| 169 | err: []string{"the valley couldn't be asked", "cannot reach valley-host"}, |
| 170 | }, |
| 171 | } { |
| 172 | t.Run(tc.name, func(t *testing.T) { |
| 173 | got, err := resolveChange(tc.ref, changeSources{Runs: runs, Repo: tc.repo, Valley: tc.valley}) |
| 174 | if tc.err != nil { |
| 175 | if err == nil { |
| 176 | t.Fatalf("got %+v, want an error", got) |
| 177 | } |
| 178 | for _, want := range tc.err { |
| 179 | if !strings.Contains(err.Error(), want) { |
| 180 | t.Errorf("error %q should contain %q", err, want) |
| 181 | } |
| 182 | } |
| 183 | return |
| 184 | } |
| 185 | if err != nil { |
| 186 | t.Fatal(err) |
| 187 | } |
| 188 | var ids []string |
| 189 | for _, s := range got.Runs { |
| 190 | ids = append(ids, s.ID) |
| 191 | } |
| 192 | if strings.Join(ids, ",") != strings.Join(tc.runIDs, ",") { |
| 193 | t.Errorf("runs %v, want %v newest first", ids, tc.runIDs) |
| 194 | } |
| 195 | got.Runs = nil |
| 196 | if fmt.Sprint(*got) != fmt.Sprint(tc.want) { |
| 197 | t.Errorf("got %+v, want %+v", *got, tc.want) |
| 198 | } |
| 199 | }) |
| 200 | } |
| 201 | |
| 202 | t.Run("the valley is asked once", func(t *testing.T) { |
| 203 | calls := 0 |
| 204 | _, err := resolveChange("lmno", changeSources{Runs: runs, Repo: "widget", Valley: func() (*valley.Changes, error) { |
| 205 | calls++ |
| 206 | return listing(elsewhere)() |
| 207 | }}) |
| 208 | if err != nil || calls != 1 { |
| 209 | t.Errorf("err %v, valley asked %d times, want once", err, calls) |
| 210 | } |
| 211 | }) |
| 212 | } |
| 213 | |
| 214 | // TestLiveHolder: a continuing launch yields to a live run on its branch or |
| 215 | // its change in the target repo, whatever else that run is on. |
| 216 | func TestLiveHolder(t *testing.T) { |
| 217 | str := func(s string) *string { return &s } |
| 218 | change := "nxvp" + strings.Repeat("k", 28) |
| 219 | onBranch := &run.State{ID: "on-branch", TargetRepo: str("acme/widget"), Branch: "fix-0001"} |
| 220 | onChange := &run.State{ID: "on-change", TargetRepo: str("widget"), Branch: "other-0002", ChangeID: change} |
| 221 | valleyNamed := &run.State{ID: "valley-named", TargetRepo: str("acme/source"), Branch: "fix-0001", Valley: &run.Valley{Repo: "widget"}} |
| 222 | elsewhere := &run.State{ID: "elsewhere", TargetRepo: str("gadget"), Branch: "fix-0001", ChangeID: change} |
| 223 | for _, tc := range []struct { |
| 224 | name string |
| 225 | states []*run.State |
| 226 | live []string |
| 227 | branch string |
| 228 | changeID string |
| 229 | repos []string |
| 230 | want string |
| 231 | }{ |
| 232 | {"a live run on the branch", []*run.State{onBranch}, []string{"on-branch"}, "fix-0001", "", []string{"widget"}, "on-branch"}, |
| 233 | {"a run on the branch that isn't live", []*run.State{onBranch}, nil, "fix-0001", "", []string{"widget"}, ""}, |
| 234 | {"a live run on the change on another branch", []*run.State{onChange}, []string{"on-change"}, "fix-0001", change, []string{"widget"}, "on-change"}, |
| 235 | {"no change ID matches no change", []*run.State{onChange}, []string{"on-change"}, "fix-0001", "", []string{"widget"}, ""}, |
| 236 | {"a run in the repo the change's valley names", []*run.State{valleyNamed}, []string{"valley-named"}, "fix-0001", "", []string{"source", "widget"}, "valley-named"}, |
| 237 | {"a run in another repo", []*run.State{elsewhere}, []string{"elsewhere"}, "fix-0001", change, []string{"widget", ""}, ""}, |
| 238 | {"a session", []*run.State{{ID: "s", Type: "session", TargetRepo: str("widget"), Branch: "fix-0001"}}, []string{"s"}, "fix-0001", "", []string{"widget"}, ""}, |
| 239 | } { |
| 240 | t.Run(tc.name, func(t *testing.T) { |
| 241 | running := func(s *run.State) bool { return slices.Contains(tc.live, s.ID) } |
| 242 | var got string |
| 243 | if s := liveHolder(tc.states, running, tc.branch, tc.changeID, tc.repos...); s != nil { |
| 244 | got = s.ID |
| 245 | } |
| 246 | if got != tc.want { |
| 247 | t.Errorf("liveHolder = %q, want %q", got, tc.want) |
| 248 | } |
| 249 | }) |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | func TestCheckContinuation(t *testing.T) { |
| 254 | for _, tc := range []struct { |
| 255 | pr, change string |
| 256 | branch bool |
| 257 | err string |
| 258 | }{ |
| 259 | {pr: "42"}, |
| 260 | {change: "nxvp"}, |
| 261 | {branch: true}, |
| 262 | {pr: "42", change: "nxvp", err: "--pr and --change are mutually exclusive"}, |
| 263 | {pr: "42", branch: true, err: "--branch and --pr are mutually exclusive"}, |
| 264 | {change: "nxvp", branch: true, err: "--branch and --change are mutually exclusive"}, |
| 265 | } { |
| 266 | err := checkContinuation(tc.pr, tc.change, tc.branch) |
| 267 | if (err == nil) != (tc.err == "") || err != nil && !strings.Contains(err.Error(), tc.err) { |
| 268 | t.Errorf("checkContinuation(%q, %q, %v) = %v, want %q", tc.pr, tc.change, tc.branch, err, tc.err) |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | // TestChangeBranchMustBeOnOrigin checks the branch against origin as fetched |
| 274 | // into a real clone. |
| 275 | func TestChangeBranchMustBeOnOrigin(t *testing.T) { |
| 276 | _, repo, _, _ := setupBareRemote(t) |
| 277 | ctx := context.Background() |
| 278 | if err := (&changeTarget{Name: "nxvp", Branch: "main", Head: "1111111"}).checkBranch(ctx, repo); err != nil { |
| 279 | t.Errorf("a branch on origin: %v", err) |
| 280 | } |
| 281 | for _, tc := range []struct { |
| 282 | t changeTarget |
| 283 | want string |
| 284 | }{ |
| 285 | {changeTarget{Name: "nxvp", Branch: "gone", Head: "1111111"}, "change nxvp's branch gone is not on origin (last known head 1111111)"}, |
| 286 | {changeTarget{Name: "nxvp"}, "change nxvp has no known branch or head"}, |
| 287 | } { |
| 288 | if err := tc.t.checkBranch(ctx, repo); err == nil || !strings.Contains(err.Error(), tc.want) { |
| 289 | t.Errorf("checkBranch(%+v) = %v, want %q", tc.t, err, tc.want) |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // TestResumeFromFollowsARestack: a --resume-from run continues its run's |
| 295 | // branch after the change was restacked onto main, which rewrote that run's |
| 296 | // push, when a later run of the change pushed the rewrite, even one the |
| 297 | // valley didn't record, or the valley records it as the change's active |
| 298 | // version there. A rewrite neither records has diverged. |
| 299 | func TestResumeFromFollowsARestack(t *testing.T) { |
| 300 | _, repo, _, _ := setupBareRemote(t) |
| 301 | ctx := context.Background() |
| 302 | commit := func(file string) string { |
| 303 | if err := os.WriteFile(filepath.Join(repo, file), []byte(file+"\n"), 0o644); err != nil { |
| 304 | t.Fatal(err) |
| 305 | } |
| 306 | runGitCmd(t, repo, "add", file) |
| 307 | runGitCmd(t, repo, "commit", "-qm", "add "+file) |
| 308 | sha, err := gitOut(t, repo, "rev-parse", "HEAD") |
| 309 | if err != nil { |
| 310 | t.Fatal(sha) |
| 311 | } |
| 312 | return sha |
| 313 | } |
| 314 | runGitCmd(t, repo, "checkout", "-qb", "greet") |
| 315 | v1 := commit("greeting.txt") |
| 316 | runGitCmd(t, repo, "push", "-q", "origin", "greet") |
| 317 | runGitCmd(t, repo, "checkout", "-q", "main") |
| 318 | commit("main.txt") |
| 319 | runGitCmd(t, repo, "push", "-q", "origin", "main") |
| 320 | runGitCmd(t, repo, "checkout", "-q", "greet") |
| 321 | runGitCmd(t, repo, "rebase", "-q", "main") |
| 322 | runGitCmd(t, repo, "push", "-q", "--force", "origin", "greet") |
| 323 | v2, _ := gitOut(t, repo, "rev-parse", "HEAD") |
| 324 | |
| 325 | change, target, branch := strings.Repeat("k", 32), "acme/widget", "greet" |
| 326 | prior := &run.State{ID: "20261026-0900-00000001", TargetRepo: &target, ChangeID: change, Branch: branch, |
| 327 | PushedBranch: &branch, PushedSHA: &v1, CreatedAt: "2026-10-26T09:00:00Z"} |
| 328 | restack := &run.State{ID: "20261026-1000-00000002", TargetRepo: &target, ChangeID: change, Branch: branch, |
| 329 | PushedBranch: &branch, PushedSHA: &v2, CreatedAt: "2026-10-26T10:00:00Z"} |
| 330 | listing := func(versions ...string) func() (*valley.Changes, error) { |
| 331 | ch := valley.Change{ChangeID: change, Handle: "kkkk", State: valley.StateDraft, Active: len(versions)} |
| 332 | for i, head := range versions { |
| 333 | ch.Versions = append(ch.Versions, valley.Version{N: i + 1, Head: head, Branch: branch}) |
| 334 | } |
| 335 | return func() (*valley.Changes, error) { |
| 336 | return &valley.Changes{Schema: valley.SchemaV1, Repos: []valley.Repo{{Repo: "widget", Changes: []valley.Change{ch}}}}, nil |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | for _, tc := range []struct { |
| 341 | name string |
| 342 | runs []*run.State |
| 343 | valley func() (*valley.Changes, error) |
| 344 | note string |
| 345 | }{ |
| 346 | {"a later run pushed the rewrite", []*run.State{prior, restack}, listing(v1), ""}, |
| 347 | {"the valley records the rewrite", []*run.State{prior}, listing(v1, v2), ""}, |
| 348 | {"nothing records the rewrite", []*run.State{prior}, listing(v1), "origin's greet has diverged from run " + prior.ID + "'s push " + v1[:7]}, |
| 349 | } { |
| 350 | t.Run(tc.name, func(t *testing.T) { |
| 351 | got, note := resumeTarget(ctx, repo, prior.ID, changeSources{Runs: tc.runs, Repo: "widget", Valley: tc.valley}) |
| 352 | if note != tc.note { |
| 353 | t.Fatalf("note = %q, want %q", note, tc.note) |
| 354 | } |
| 355 | if tc.note == "" && (got == nil || got.ID != change || got.Branch != branch) { |
| 356 | t.Errorf("resumeTarget = %+v, want change %s on %s", got, change, branch) |
| 357 | } |
| 358 | }) |
| 359 | } |
| 360 | } |