-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathswitch_test.go
More file actions
291 lines (241 loc) · 7.51 KB
/
Copy pathswitch_test.go
File metadata and controls
291 lines (241 loc) · 7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package cmd
import (
"io"
"testing"
"github.com/github/gh-stack/internal/config"
"github.com/github/gh-stack/internal/git"
"github.com/github/gh-stack/internal/stack"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSwitch_SwitchesToSelectedBranch(t *testing.T) {
gitDir := t.TempDir()
var checkedOut string
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
CurrentBranchFn: func() (string, error) { return "b1", nil },
CheckoutBranchFn: func(name string) error {
checkedOut = name
return nil
},
})
defer restore()
writeStackFile(t, gitDir, stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "b1"},
{Branch: "b2"},
{Branch: "b3"},
},
})
cfg, outR, errR := config.NewTestConfig()
cfg.ForceInteractive = true
// Simulate selecting the first option (index 0) which is "3. b3" (top of stack)
cfg.SelectFn = func(prompt, def string, options []string) (int, error) {
// Verify prompt text
assert.Equal(t, "Select a branch in the stack to switch to:", prompt)
// Verify options are in reverse order with numbering
assert.Equal(t, []string{"3. b3", "2. b2", "1. b1"}, options)
return 0, nil // select "3. b3"
}
err := runSwitch(cfg)
output := collectOutput(cfg, outR, errR)
require.NoError(t, err)
assert.Equal(t, "b3", checkedOut)
assert.Contains(t, output, "Switched to b3")
}
func TestSwitch_SelectMiddleBranch(t *testing.T) {
gitDir := t.TempDir()
var checkedOut string
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
CurrentBranchFn: func() (string, error) { return "b1", nil },
CheckoutBranchFn: func(name string) error {
checkedOut = name
return nil
},
})
defer restore()
writeStackFile(t, gitDir, stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "b1"},
{Branch: "b2"},
{Branch: "b3"},
},
})
cfg, outR, errR := config.NewTestConfig()
cfg.ForceInteractive = true
// Select index 1 which is "2. b2"
cfg.SelectFn = func(prompt, def string, options []string) (int, error) {
return 1, nil // select "2. b2"
}
err := runSwitch(cfg)
output := collectOutput(cfg, outR, errR)
require.NoError(t, err)
assert.Equal(t, "b2", checkedOut)
assert.Contains(t, output, "Switched to b2")
}
func TestSwitch_AlreadyOnSelectedBranch(t *testing.T) {
gitDir := t.TempDir()
checkoutCalled := false
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
CurrentBranchFn: func() (string, error) { return "b2", nil },
CheckoutBranchFn: func(name string) error {
checkoutCalled = true
return nil
},
})
defer restore()
writeStackFile(t, gitDir, stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "b1"},
{Branch: "b2"},
{Branch: "b3"},
},
})
cfg, outR, errR := config.NewTestConfig()
cfg.ForceInteractive = true
// Select "2. b2" which is option index 1 — the branch we're already on
cfg.SelectFn = func(prompt, def string, options []string) (int, error) {
return 1, nil // select "2. b2"
}
err := runSwitch(cfg)
output := collectOutput(cfg, outR, errR)
require.NoError(t, err)
assert.False(t, checkoutCalled, "CheckoutBranch should not be called when already on target")
assert.Contains(t, output, "Already on b2")
}
func TestSwitch_NotInStack(t *testing.T) {
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
CurrentBranchFn: func() (string, error) { return "orphan", nil },
})
defer restore()
// Write a stack that doesn't contain "orphan"
writeStackFile(t, gitDir, stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{{Branch: "b1"}},
})
cfg, _, _ := config.NewTestConfig()
cfg.ForceInteractive = true
err := runSwitch(cfg)
assert.ErrorIs(t, err, ErrNotInStack)
}
func TestSwitch_NonInteractive(t *testing.T) {
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
CurrentBranchFn: func() (string, error) { return "b1", nil },
})
defer restore()
writeStackFile(t, gitDir, stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}},
})
cfg, outR, errR := config.NewTestConfig()
// ForceInteractive not set — non-interactive mode
err := runSwitch(cfg)
output := collectOutput(cfg, outR, errR)
assert.ErrorIs(t, err, ErrSilent)
assert.Contains(t, output, "switch requires an interactive terminal")
}
func TestSwitch_DisplayOrder(t *testing.T) {
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
CurrentBranchFn: func() (string, error) { return "first", nil },
CheckoutBranchFn: func(name string) error {
return nil
},
})
defer restore()
writeStackFile(t, gitDir, stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "first"},
{Branch: "second"},
{Branch: "third"},
{Branch: "fourth"},
{Branch: "fifth"},
},
})
cfg, _, _ := config.NewTestConfig()
cfg.ForceInteractive = true
var capturedOptions []string
cfg.SelectFn = func(prompt, def string, options []string) (int, error) {
capturedOptions = options
return 0, nil // select top
}
err := runSwitch(cfg)
require.NoError(t, err)
expected := []string{
"5. fifth",
"4. fourth",
"3. third",
"2. second",
"1. first",
}
assert.Equal(t, expected, capturedOptions)
}
func TestSwitch_NoBranches(t *testing.T) {
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
CurrentBranchFn: func() (string, error) { return "main", nil },
})
defer restore()
writeStackFile(t, gitDir, stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{},
})
cfg, _, _ := config.NewTestConfig()
cfg.ForceInteractive = true
err := runSwitch(cfg)
assert.ErrorIs(t, err, ErrNotInStack)
}
func TestSwitch_CmdIntegration(t *testing.T) {
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
CurrentBranchFn: func() (string, error) { return "b1", nil },
CheckoutBranchFn: func(name string) error {
return nil
},
})
defer restore()
writeStackFile(t, gitDir, stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "b1"},
{Branch: "b2"},
},
})
cfg, _, _ := config.NewTestConfig()
cfg.ForceInteractive = true
cfg.SelectFn = func(prompt, def string, options []string) (int, error) {
return 0, nil // select top
}
cmd := SwitchCmd(cfg)
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
err := cmd.Execute()
assert.NoError(t, err)
}
func TestNewSwitchSelect_EnablesVimNavigation(t *testing.T) {
options := []string{"3. résumé", "2. b2", "1. b1"}
prompt := newSwitchSelect("Select a branch:", "2. b2", options)
assert.True(t, prompt.VimMode)
assert.Equal(t, selectPromptPageSize, prompt.PageSize)
assert.Equal(t, "2. b2", prompt.Default)
assert.Equal(t, options, prompt.Options)
require.NotNil(t, prompt.Filter)
assert.True(t, prompt.Filter("resume", options[0], 0), "filter should retain diacritic-insensitive matching")
}
func TestSwitchCmd_DescribesVimNavigation(t *testing.T) {
cfg, _, _ := config.NewTestConfig()
assert.Contains(t, SwitchCmd(cfg).Long, "down/up arrow keys or j/k")
}