gunk-dev/the-valley

track_test.go

sourcec14b0e68e9c3main's published tip history
5.9 KiB
1package cmd
2
3import (
4 "context"
5 "testing"
6
7 "the-valley/flow/internal/run"
8)
9
10func TestParsePRRef(t *testing.T) {
11 tests := []struct {
12 name string
13 ref string
14 wantRepo string
15 wantPR string
16 wantErr bool
17 }{
18 {"bare number", "405", "", "405", false},
19 {"full URL", "https://github.com/org/repo/pull/405", "org/repo", "405", false},
20 {"owner/repo#number", "owner/repo#123", "owner/repo", "123", false},
21 {"invalid ref", "not-a-pr", "", "", true},
22 {"empty ref", "", "", "", true},
23 {"bad URL", "https://github.com/bad", "", "", true},
24 }
25
26 for _, tt := range tests {
27 t.Run(tt.name, func(t *testing.T) {
28 repo, pr, err := parsePRRef(tt.ref)
29 if (err != nil) != tt.wantErr {
30 t.Errorf("parsePRRef(%q) error = %v, wantErr %v", tt.ref, err, tt.wantErr)
31 return
32 }
33 if repo != tt.wantRepo {
34 t.Errorf("parsePRRef(%q) repo = %q, want %q", tt.ref, repo, tt.wantRepo)
35 }
36 if pr != tt.wantPR {
37 t.Errorf("parsePRRef(%q) pr = %q, want %q", tt.ref, pr, tt.wantPR)
38 }
39 })
40 }
41}
42
43func TestTrackCreateState(t *testing.T) {
44 tmpDir := t.TempDir()
45 store := run.NewHomeDirStoreFromPath(tmpDir)
46 if err := store.EnsureDirs(); err != nil {
47 t.Fatalf("EnsureDirs: %v", err)
48 }
49
50 // Simulate what trackPR does (without calling gh CLI)
51 prURL := "https://github.com/owner/repo/pull/42"
52 title := "Fix the widget"
53 repo := "owner/repo"
54
55 id, err := run.GenID()
56 if err != nil {
57 t.Fatalf("GenID: %v", err)
58 }
59
60 st := &run.State{
61 ID: id,
62 Prompt: title,
63 Branch: "fix-widget",
64 PRURL: &prURL,
65 Type: "track",
66 TargetRepo: &repo,
67 CreatedAt: "2026-04-01T00:00:00Z",
68 }
69 if err := store.Save(st); err != nil {
70 t.Fatalf("Save: %v", err)
71 }
72
73 // Verify the state was saved correctly
74 loaded, err := store.Load(id)
75 if err != nil {
76 t.Fatalf("Load: %v", err)
77 }
78 if loaded.Type != "track" {
79 t.Errorf("Type = %q, want %q", loaded.Type, "track")
80 }
81 if loaded.PRURL == nil || *loaded.PRURL != prURL {
82 t.Errorf("PRURL = %v, want %q", loaded.PRURL, prURL)
83 }
84 if loaded.Prompt != title {
85 t.Errorf("Prompt = %q, want %q", loaded.Prompt, title)
86 }
87 if loaded.Branch != "fix-widget" {
88 t.Errorf("Branch = %q, want %q", loaded.Branch, "fix-widget")
89 }
90 if loaded.TargetRepo == nil || *loaded.TargetRepo != repo {
91 t.Errorf("TargetRepo = %v, want %q", loaded.TargetRepo, repo)
92 }
93 if loaded.Worktree != "" {
94 t.Errorf("Worktree = %q, want empty", loaded.Worktree)
95 }
96}
97
98func TestTrackDuplicateDetection(t *testing.T) {
99 tmpDir := t.TempDir()
100 store := run.NewHomeDirStoreFromPath(tmpDir)
101 if err := store.EnsureDirs(); err != nil {
102 t.Fatalf("EnsureDirs: %v", err)
103 }
104
105 prURL := "https://github.com/owner/repo/pull/42"
106 existing := &run.State{
107 ID: "20260401-0000-aaaa",
108 Prompt: "existing PR",
109 Branch: "fix-it",
110 PRURL: &prURL,
111 Type: "track",
112 CreatedAt: "2026-04-01T00:00:00Z",
113 }
114 if err := store.Save(existing); err != nil {
115 t.Fatalf("Save: %v", err)
116 }
117
118 states, err := store.List()
119 if err != nil {
120 t.Fatalf("List: %v", err)
121 }
122
123 // Check that the duplicate detection logic works
124 isDuplicate := false
125 for _, s := range states {
126 if s.PRURL != nil && *s.PRURL == prURL {
127 isDuplicate = true
128 break
129 }
130 }
131 if !isDuplicate {
132 t.Error("should detect duplicate PRURL")
133 }
134}
135
136func TestUntrackOnlyRemovesTrackedType(t *testing.T) {
137 tmpDir := t.TempDir()
138 store := run.NewHomeDirStoreFromPath(tmpDir)
139 if err := store.EnsureDirs(); err != nil {
140 t.Fatalf("EnsureDirs: %v", err)
141 }
142
143 prURL42 := "https://github.com/owner/repo/pull/42"
144 prURL99 := "https://github.com/owner/repo/pull/99"
145
146 // Agent-created state (Type is empty, not "track")
147 agentState := &run.State{
148 ID: "20260401-0000-aaaa",
149 Prompt: "agent work",
150 Branch: "agent/branch",
151 PRURL: &prURL42,
152 Type: "agent",
153 CreatedAt: "2026-04-01T00:00:00Z",
154 }
155 // Tracked state
156 trackedState := &run.State{
157 ID: "20260401-0000-bbbb",
158 Prompt: "tracked PR",
159 Branch: "fix-it",
160 PRURL: &prURL99,
161 Type: "track",
162 CreatedAt: "2026-04-01T00:01:00Z",
163 }
164 for _, s := range []*run.State{agentState, trackedState} {
165 if err := store.Save(s); err != nil {
166 t.Fatalf("Save: %v", err)
167 }
168 }
169
170 states, _ := store.List()
171
172 // Try to untrack PR #42 (agent-created) — should not find it
173 found := false
174 for _, s := range states {
175 if s.Type != "track" {
176 continue
177 }
178 if extractPRNumber(s) == "42" {
179 found = true
180 }
181 }
182 if found {
183 t.Error("should not find agent-created PR #42 as a tracked PR")
184 }
185
186 // Untrack PR #99 (tracked) — should find it
187 found = false
188 for _, s := range states {
189 if s.Type != "track" {
190 continue
191 }
192 if extractPRNumber(s) == "99" {
193 found = true
194 if err := store.Delete(s.ID); err != nil {
195 t.Fatalf("Delete: %v", err)
196 }
197 }
198 }
199 if !found {
200 t.Error("should find tracked PR #99")
201 }
202
203 // Verify agent state still exists
204 remaining, _ := store.List()
205 if len(remaining) != 1 {
206 t.Errorf("expected 1 remaining state, got %d", len(remaining))
207 }
208 if remaining[0].ID != "20260401-0000-aaaa" {
209 t.Errorf("remaining state should be agent state, got %s", remaining[0].ID)
210 }
211}
212
213func TestDetermineStatusTrack(t *testing.T) {
214 tests := []struct {
215 name string
216 s *run.State
217 want string
218 }{
219 {
220 name: "tracked PR returns tracking",
221 s: &run.State{Type: "track", PRURL: strPtr("https://github.com/owner/repo/pull/1")},
222 want: "tracking",
223 },
224 {
225 name: "tracked PR with merged returns merged",
226 s: &run.State{
227 Type: "track",
228 PRURL: strPtr("https://github.com/owner/repo/pull/1"),
229 MergedAt: strPtr("2026-04-01T00:00:00Z"),
230 },
231 want: "merged",
232 },
233 {
234 name: "tracked PR no PRURL returns tracking",
235 s: &run.State{Type: "track"},
236 want: "tracking",
237 },
238 }
239
240 ctx := context.Background()
241 tc := &fakeTmux{}
242 for _, tt := range tests {
243 t.Run(tt.name, func(t *testing.T) {
244 got := determineStatus(ctx, tt.s, tc)
245 if got != tt.want {
246 t.Errorf("determineStatus() = %q, want %q", got, tt.want)
247 }
248 })
249 }
250}