gunk-dev/the-valley

changeid_test.go

source60ad80d5a5a5main's published tip history
6.1 KiB
1package cmd
2
3import (
4 "context"
5 "strings"
6 "testing"
7
8 "the-valley/flow/internal/run"
9)
10
11const (
12 openerChangeID = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
13 laterChangeID = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
14 otherChangeID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
15 resumeChangeID = "wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww"
16 prURL7 = "https://github.com/acme/widget/pull/7"
17)
18
19// seedSession saves states into a fresh session store under the test HOME.
20func seedSession(t *testing.T, sessionID string, states ...*run.State) *run.HomeDirStore {
21 t.Helper()
22 store, err := run.NewHomeDirStore(sessionID)
23 if err != nil {
24 t.Fatal(err)
25 }
26 if err := store.EnsureDirs(); err != nil {
27 t.Fatal(err)
28 }
29 for _, s := range states {
30 if err := store.Save(s); err != nil {
31 t.Fatal(err)
32 }
33 }
34 return store
35}
36
37func TestChooseChangeID(t *testing.T) {
38 t.Setenv("HOME", t.TempDir())
39 ctx := context.Background()
40 str := func(s string) *string { return &s }
41
42 // The PR was opened in another coordinator session. A pre-change-ID run
43 // on the same PR and a same-named branch in another repo are older still.
44 seedSession(t, "20261001-0900-opener",
45 &run.State{ID: "20261001-0900-oldstate", Branch: "fix-thing-1234", PRURL: str(prURL7), CreatedAt: "2026-10-01T08:00:00Z"},
46 &run.State{ID: "20261001-0900-otherrepo", Branch: "fix-thing-1234", PRURL: str("https://github.com/acme/other/pull/7"), ChangeID: otherChangeID, CreatedAt: "2026-10-01T08:30:00Z"},
47 &run.State{ID: "20261001-0900-opener", Branch: "fix-thing-1234", PRURL: str(prURL7), ChangeID: openerChangeID, CreatedAt: "2026-10-01T09:00:00Z"},
48 &run.State{ID: "20261002-0900-fix", Type: "pr-fix", Branch: "fix-thing-1234", PRURL: str(prURL7), ChangeID: laterChangeID, CreatedAt: "2026-10-02T09:00:00Z"},
49 )
50 store := seedSession(t, "20261013-1500-current",
51 &run.State{ID: "20261013-1400-resumed", Branch: "resumed-1111", ChangeID: resumeChangeID, CreatedAt: "2026-10-13T14:00:00Z"},
52 &run.State{ID: "20261013-1410-legacy", Branch: "legacy-2222", CreatedAt: "2026-10-13T14:10:00Z"},
53 )
54
55 t.Run("--pr inherits from the run that opened the PR", func(t *testing.T) {
56 c, err := chooseChangeID(ctx, store, changeIDInput{IsPRFix: true, PRURL: prURL7, PRBranch: "fix-thing-1234"})
57 if err != nil {
58 t.Fatal(err)
59 }
60 if c.ID != openerChangeID || !strings.Contains(c.From, "20261001-0900-opener") {
61 t.Errorf("got %+v, want the opener's %s", c, openerChangeID)
62 }
63 })
64
65 t.Run("without PR URLs, --pr matches the branch in the same repo only", func(t *testing.T) {
66 seedSession(t, "20261003-0900-nourl",
67 &run.State{ID: "20261003-0800-elsewhere", Branch: "shared-name", TargetRepo: str("acme/other"), ChangeID: otherChangeID, CreatedAt: "2026-10-03T08:00:00Z"},
68 &run.State{ID: "20261003-0830-unknown", Branch: "shared-name", ChangeID: resumeChangeID, CreatedAt: "2026-10-03T08:30:00Z"},
69 &run.State{ID: "20261003-0900-here", Branch: "shared-name", TargetRepo: str("acme/widget"), ChangeID: laterChangeID, CreatedAt: "2026-10-03T09:00:00Z"},
70 )
71 c, err := chooseChangeID(ctx, store, changeIDInput{IsPRFix: true, PRBranch: "shared-name", TargetRepo: str("acme/widget")})
72 if err != nil {
73 t.Fatal(err)
74 }
75 if c.ID != laterChangeID {
76 t.Errorf("got %+v, want acme/widget's %s", c, laterChangeID)
77 }
78 })
79
80 t.Run("--resume-from inherits from the resumed run", func(t *testing.T) {
81 c, err := chooseChangeID(ctx, store, changeIDInput{ResumeFrom: "20261013-1400-resumed"})
82 if err != nil {
83 t.Fatal(err)
84 }
85 if c.ID != resumeChangeID {
86 t.Errorf("got %+v, want %s", c, resumeChangeID)
87 }
88 })
89
90 t.Run("--change inherits the change it names, whatever --resume-from says", func(t *testing.T) {
91 c, err := chooseChangeID(ctx, store, changeIDInput{ResumeFrom: "20261013-1400-resumed",
92 Change: &changeTarget{ID: laterChangeID, Name: "yyyy", Repo: "widget"}})
93 if err != nil {
94 t.Fatal(err)
95 }
96 if c.ID != laterChangeID || c.From != "inherited from change yyyy" || !c.Inherited || c.Repo != "widget" {
97 t.Errorf("got %+v, want change yyyy's %s in widget", c, laterChangeID)
98 }
99 })
100
101 t.Run("continuing a run that predates change IDs generates one and says so", func(t *testing.T) {
102 c, err := chooseChangeID(ctx, store, changeIDInput{ResumeFrom: "20261013-1410-legacy"})
103 if err != nil {
104 t.Fatal(err)
105 }
106 if !run.ValidChangeID(c.ID) || !strings.Contains(c.From, "no change ID is recorded for run 20261013-1410-legacy") {
107 t.Errorf("got %+v, want a new ID noting the legacy run", c)
108 }
109 })
110
111 t.Run("fresh launch generates one", func(t *testing.T) {
112 c, err := chooseChangeID(ctx, store, changeIDInput{})
113 if err != nil {
114 t.Fatal(err)
115 }
116 if !run.ValidChangeID(c.ID) || c.From != "new" {
117 t.Errorf("got %+v, want a new ID", c)
118 }
119 for _, known := range []string{openerChangeID, laterChangeID, otherChangeID, resumeChangeID} {
120 if c.ID == known {
121 t.Errorf("fresh launch reused %s", known)
122 }
123 }
124 })
125
126 t.Run("--pr with no recorded run reads the PR head commit", func(t *testing.T) {
127 _, _, worktree, branch := setupBareRemote(t)
128 runGitCmd(t, worktree, "add", "-A")
129 runGitCmd(t, worktree, "commit", "-m", "wip\n\nChange-Id: "+laterChangeID+"\nKlaus-Run: 20261012-0000-elsewhere")
130
131 c, err := chooseChangeID(ctx, store, changeIDInput{IsPRFix: true, PRURL: "https://github.com/acme/widget/pull/99", PRBranch: branch, Worktree: worktree, DefaultBranch: "main"})
132 if err != nil {
133 t.Fatal(err)
134 }
135 if c.ID != laterChangeID {
136 t.Errorf("got %+v, want the head commit's %s", c, laterChangeID)
137 }
138 })
139
140 t.Run("--pr whose head is on the default branch generates one", func(t *testing.T) {
141 _, repo, worktree, branch := setupBareRemote(t)
142 // main's tip belongs to a change that already landed.
143 runGitCmd(t, repo, "commit", "--allow-empty", "-m", "landed\n\nChange-Id: "+otherChangeID)
144 runGitCmd(t, repo, "push", "origin", "main")
145 runGitCmd(t, worktree, "reset", "--hard", "origin/main")
146
147 c, err := chooseChangeID(ctx, store, changeIDInput{IsPRFix: true, PRURL: "https://github.com/acme/widget/pull/98", PRBranch: branch, Worktree: worktree, DefaultBranch: "main"})
148 if err != nil {
149 t.Fatal(err)
150 }
151 if c.ID == otherChangeID || !strings.Contains(c.From, "no change ID is recorded for the PR") {
152 t.Errorf("got %+v, want a new ID, not the landed change's", c)
153 }
154 })
155}