-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathunstack_test.go
More file actions
729 lines (632 loc) · 24.1 KB
/
Copy pathunstack_test.go
File metadata and controls
729 lines (632 loc) · 24.1 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
package cmd
import (
"encoding/json"
"errors"
"os"
"path/filepath"
"testing"
"github.com/cli/go-gh/v2/pkg/api"
"github.com/github/gh-stack/internal/config"
"github.com/github/gh-stack/internal/git"
"github.com/github/gh-stack/internal/github"
"github.com/github/gh-stack/internal/stack"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func writeTwoStacks(t *testing.T, dir string, s1, s2 stack.Stack) {
t.Helper()
sf := &stack.StackFile{
SchemaVersion: 1,
Stacks: []stack.Stack{s1, s2},
}
data, err := json.MarshalIndent(sf, "", " ")
require.NoError(t, err)
require.NoError(t, os.WriteFile(filepath.Join(dir, "gh-stack"), data, 0644))
}
func TestUnstack_RemovesStack(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()
s1 := stack.Stack{
ID: "42",
Number: 42,
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}},
}
s2 := stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{{Branch: "b3"}, {Branch: "b4"}},
}
writeTwoStacks(t, gitDir, s1, s2)
var unstackedNumber int
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
UnstackFn: func(n int) (*github.RemoteStack, bool, error) {
unstackedNumber = n
return nil, true, nil // dissolved
},
}
err := runUnstack(cfg, &unstackOptions{})
output := collectOutput(cfg, outR, errR)
require.NoError(t, err)
assert.Contains(t, output, "Stack removed from local tracking")
assert.Contains(t, output, "Stack removed on GitHub")
assert.Equal(t, 42, unstackedNumber)
sf, err := stack.Load(gitDir)
require.NoError(t, err)
require.Len(t, sf.Stacks, 1)
assert.Equal(t, []string{"b3", "b4"}, sf.Stacks[0].BranchNames())
}
func TestUnstack_Local(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()
err := runUnstack(cfg, &unstackOptions{local: true})
output := collectOutput(cfg, outR, errR)
require.NoError(t, err)
assert.Contains(t, output, "Stack removed")
// With --local, the GitHub API should NOT be called.
assert.NotContains(t, output, "Stack removed on GitHub")
sf, err := stack.Load(gitDir)
require.NoError(t, err)
assert.Empty(t, sf.Stacks)
}
func TestUnstack_NoStackID_WarnsAndSkipsAPI(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()
// Stack with no ID/Number (never synced to GitHub)
writeStackFile(t, gitDir, stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}},
})
apiCalled := false
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
UnstackFn: func(int) (*github.RemoteStack, bool, error) {
apiCalled = true
return nil, true, nil
},
}
err := runUnstack(cfg, &unstackOptions{})
output := collectOutput(cfg, outR, errR)
require.NoError(t, err)
assert.False(t, apiCalled, "API should not be called when stack has no ID")
assert.Contains(t, output, "no remote ID")
assert.Contains(t, output, "Stack removed from local tracking")
assert.NotContains(t, output, "Stack removed on GitHub")
}
func TestUnstack_ResolvesNumberFromID(t *testing.T) {
// A local stack that predates the Number field (only ID stored) resolves
// its stack number from the remote list before unstacking.
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{
ID: "99",
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "b1", PullRequest: &stack.PullRequestRef{Number: 101}},
{Branch: "b2", PullRequest: &stack.PullRequestRef{Number: 102}},
},
})
var unstackedNumber int
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
ListStacksFn: func() ([]github.RemoteStack, error) {
return []github.RemoteStack{{ID: 99, Number: 7, PullRequests: []int{101, 102}}}, nil
},
UnstackFn: func(n int) (*github.RemoteStack, bool, error) {
unstackedNumber = n
return nil, true, nil
},
}
err := runUnstack(cfg, &unstackOptions{})
output := collectOutput(cfg, outR, errR)
require.NoError(t, err)
assert.Equal(t, 7, unstackedNumber, "should resolve the stack number from the internal ID")
assert.Contains(t, output, "Stack removed from local tracking")
sf, err := stack.Load(gitDir)
require.NoError(t, err)
assert.Empty(t, sf.Stacks)
}
func TestUnstack_API404_TreatedAsIdempotentSuccess(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{
ID: "99",
Number: 99,
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "b1", PullRequest: &stack.PullRequestRef{Number: 101, Merged: true}},
{Branch: "b2", PullRequest: &stack.PullRequestRef{Number: 102}},
},
})
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
UnstackFn: func(int) (*github.RemoteStack, bool, error) {
return nil, false, &api.HTTPError{StatusCode: 404, Message: "Not Found"}
},
}
err := runUnstack(cfg, &unstackOptions{})
output := collectOutput(cfg, outR, errR)
// 404 means already gone — should succeed and remove locally
require.NoError(t, err)
assert.Contains(t, output, "continuing with local unstack")
assert.Contains(t, output, "Stack removed from local tracking")
sf, err := stack.Load(gitDir)
require.NoError(t, err)
assert.Empty(t, sf.Stacks)
}
func TestUnstack_ServerError_StopsLocalDeletion(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{
ID: "99",
Number: 99,
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "b1", PullRequest: &stack.PullRequestRef{Number: 101, Merged: true}},
{Branch: "b2", PullRequest: &stack.PullRequestRef{Number: 102}},
},
})
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
UnstackFn: func(int) (*github.RemoteStack, bool, error) {
return nil, false, &api.HTTPError{StatusCode: 409, Message: "Stack is currently being modified"}
},
}
err := runUnstack(cfg, &unstackOptions{})
output := collectOutput(cfg, outR, errR)
assert.ErrorIs(t, err, ErrAPIFailure)
assert.Contains(t, output, "Failed to unstack on GitHub (HTTP 409)")
// Should NOT remove locally when remote fails
assert.NotContains(t, output, "Stack removed from local tracking")
// Stack should still exist locally
sf, err := stack.Load(gitDir)
require.NoError(t, err)
require.Len(t, sf.Stacks, 1)
}
func TestUnstack_RemovesCorrectStackByPointer(t *testing.T) {
// Two stacks share the same trunk "main". Current branch "b3" should remove
// only the second stack (b3,b4), leaving the first (b1,b2) intact.
// This verifies pointer-based removal instead of branch-name-based.
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
CurrentBranchFn: func() (string, error) { return "b3", nil },
})
defer restore()
s1 := stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}},
}
s2 := stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{{Branch: "b3"}, {Branch: "b4"}},
}
writeTwoStacks(t, gitDir, s1, s2)
cfg, outR, errR := config.NewTestConfig()
err := runUnstack(cfg, &unstackOptions{local: true})
output := collectOutput(cfg, outR, errR)
require.NoError(t, err)
assert.Contains(t, output, "Stack removed from local tracking")
sf, err := stack.Load(gitDir)
require.NoError(t, err)
require.Len(t, sf.Stacks, 1, "should remove exactly one stack")
assert.Equal(t, []string{"b1", "b2"}, sf.Stacks[0].BranchNames(), "should keep the OTHER stack intact")
}
func TestUnstack_AllLocked_ServerRejects(t *testing.T) {
// Every PR is queued for merge or has auto-merge enabled. The server
// (not the client) rejects the unstack with a 422; the command surfaces the
// error and leaves local tracking in place.
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{
ID: "99",
Number: 99,
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "b1", PullRequest: &stack.PullRequestRef{Number: 101}},
{Branch: "b2", PullRequest: &stack.PullRequestRef{Number: 102}},
},
})
unstackCalled := false
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
UnstackFn: func(int) (*github.RemoteStack, bool, error) {
unstackCalled = true
return nil, false, &api.HTTPError{StatusCode: 422, Message: "all pull requests are queued for merge or have auto-merge enabled"}
},
}
err := runUnstack(cfg, &unstackOptions{})
output := collectOutput(cfg, outR, errR)
assert.ErrorIs(t, err, ErrInvalidArgs)
assert.True(t, unstackCalled, "the server decides eligibility, so Unstack is called")
assert.Contains(t, output, "Unstacking not allowed")
assert.NotContains(t, output, "Stack removed from local tracking")
sf, loadErr := stack.Load(gitDir)
require.NoError(t, loadErr)
require.Len(t, sf.Stacks, 1)
}
func TestUnstack_PartialUnstack_KeepsLocalTracking(t *testing.T) {
// Some PRs (queued for merge / auto-merge) remain stacked, so the server
// returns the surviving stack (dissolved=false). Local tracking is kept.
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{
ID: "99",
Number: 99,
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "b1", PullRequest: &stack.PullRequestRef{Number: 101}},
{Branch: "b2", PullRequest: &stack.PullRequestRef{Number: 102}},
},
})
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
UnstackFn: func(int) (*github.RemoteStack, bool, error) {
return &github.RemoteStack{ID: 99, Number: 99, PullRequests: []int{102}}, false, nil
},
}
err := runUnstack(cfg, &unstackOptions{})
output := collectOutput(cfg, outR, errR)
require.NoError(t, err)
assert.Contains(t, output, "remain stacked on GitHub")
assert.Contains(t, output, "local tracking is unchanged")
assert.NotContains(t, output, "Stack removed from local tracking")
// The stack still exists remotely, so local tracking is preserved.
sf, loadErr := stack.Load(gitDir)
require.NoError(t, loadErr)
require.Len(t, sf.Stacks, 1)
}
func TestUnstack_NumberLookupFailure_StopsDeletion(t *testing.T) {
// Resolving the stack number from its ID fails (list API error), so the
// command aborts without touching local tracking.
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{
ID: "99",
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{{Branch: "b1", PullRequest: &stack.PullRequestRef{Number: 101}}},
})
unstackCalled := false
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
ListStacksFn: func() ([]github.RemoteStack, error) {
return nil, errors.New("network error")
},
UnstackFn: func(int) (*github.RemoteStack, bool, error) {
unstackCalled = true
return nil, true, nil
},
}
err := runUnstack(cfg, &unstackOptions{})
output := collectOutput(cfg, outR, errR)
assert.ErrorIs(t, err, ErrAPIFailure)
assert.False(t, unstackCalled, "Unstack should not be called if number lookup fails")
assert.Contains(t, output, "failed to look up stack on GitHub")
assert.NotContains(t, output, "Stack removed from local tracking")
sf, loadErr := stack.Load(gitDir)
require.NoError(t, loadErr)
require.Len(t, sf.Stacks, 1)
}
func TestUnstack_ByStackNumber(t *testing.T) {
// Target a specific stack by its number, regardless of the current branch.
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
CurrentBranchFn: func() (string, error) { return "b1", nil },
})
defer restore()
s1 := stack.Stack{
ID: "42",
Number: 42,
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}},
}
s2 := stack.Stack{
ID: "99",
Number: 7,
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{{Branch: "b3"}, {Branch: "b4"}},
}
writeTwoStacks(t, gitDir, s1, s2)
var unstackedNumber int
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
UnstackFn: func(n int) (*github.RemoteStack, bool, error) {
unstackedNumber = n
return nil, true, nil
},
}
err := runUnstack(cfg, &unstackOptions{stackNumber: 7})
output := collectOutput(cfg, outR, errR)
require.NoError(t, err)
assert.Equal(t, 7, unstackedNumber)
assert.Contains(t, output, "Stack removed from local tracking")
// The targeted stack (number 7 / b3,b4) is removed; the other is kept.
sf, err := stack.Load(gitDir)
require.NoError(t, err)
require.Len(t, sf.Stacks, 1)
assert.Equal(t, []string{"b1", "b2"}, sf.Stacks[0].BranchNames())
}
func TestUnstack_ByStackNumber_RemoteOnly_Dissolved(t *testing.T) {
// A stack number that isn't tracked locally is unstacked directly on GitHub
// (remote-first), leaving unrelated local tracking untouched.
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{
ID: "42",
Number: 42,
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}},
})
var unstackedNumber int
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
UnstackFn: func(n int) (*github.RemoteStack, bool, error) {
unstackedNumber = n
return nil, true, nil // dissolved
},
}
err := runUnstack(cfg, &unstackOptions{stackNumber: 999})
output := collectOutput(cfg, outR, errR)
require.NoError(t, err)
assert.Equal(t, 999, unstackedNumber, "should unstack the requested number on GitHub")
assert.Contains(t, output, "Stack removed on GitHub")
// No local tracking was touched.
assert.NotContains(t, output, "Stack removed from local tracking")
sf, err := stack.Load(gitDir)
require.NoError(t, err)
require.Len(t, sf.Stacks, 1)
assert.Equal(t, 42, sf.Stacks[0].Number, "the unrelated local stack is left intact")
}
func TestUnstack_ByStackNumber_RemoteOnly_NotFound(t *testing.T) {
// With no local tracking to reconcile, a 404 means the targeted stack does
// not exist on GitHub — a hard error, not an idempotent success.
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{
ID: "42",
Number: 42,
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}},
})
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
UnstackFn: func(int) (*github.RemoteStack, bool, error) {
return nil, false, &api.HTTPError{StatusCode: 404, Message: "Not Found"}
},
}
err := runUnstack(cfg, &unstackOptions{stackNumber: 999})
output := collectOutput(cfg, outR, errR)
assert.ErrorIs(t, err, ErrNotInStack)
assert.Contains(t, output, "stack #999 not found on GitHub")
assert.NotContains(t, output, "Stack removed")
sf, err := stack.Load(gitDir)
require.NoError(t, err)
require.Len(t, sf.Stacks, 1)
}
func TestUnstack_ByStackNumber_RemoteOnly_Partial(t *testing.T) {
// Some PRs (queued for merge / auto-merge) remain stacked. There is no local
// tracking to keep, so the command reports the outcome and succeeds.
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{
ID: "42",
Number: 42,
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}},
})
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
UnstackFn: func(int) (*github.RemoteStack, bool, error) {
return &github.RemoteStack{ID: 555, Number: 999, PullRequests: []int{102}}, false, nil
},
}
err := runUnstack(cfg, &unstackOptions{stackNumber: 999})
output := collectOutput(cfg, outR, errR)
require.NoError(t, err)
assert.Contains(t, output, "remain stacked on GitHub")
// No local tracking is involved, so no local-tracking messaging is shown.
assert.NotContains(t, output, "local tracking is unchanged")
assert.NotContains(t, output, "Stack removed from local tracking")
sf, err := stack.Load(gitDir)
require.NoError(t, err)
require.Len(t, sf.Stacks, 1)
}
func TestUnstack_ByStackNumber_RemoteOnly_AllLocked(t *testing.T) {
// Every PR is queued for merge or has auto-merge enabled; the server rejects
// the unstack with a 422 and the command surfaces the error.
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{
ID: "42",
Number: 42,
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}},
})
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
UnstackFn: func(int) (*github.RemoteStack, bool, error) {
return nil, false, &api.HTTPError{StatusCode: 422, Message: "all pull requests are queued for merge or have auto-merge enabled"}
},
}
err := runUnstack(cfg, &unstackOptions{stackNumber: 999})
output := collectOutput(cfg, outR, errR)
assert.ErrorIs(t, err, ErrInvalidArgs)
assert.Contains(t, output, "Unstacking not allowed")
sf, err := stack.Load(gitDir)
require.NoError(t, err)
require.Len(t, sf.Stacks, 1)
}
func TestUnstack_ByStackNumber_NotTracked_LocalFlag(t *testing.T) {
// --local never contacts GitHub. Targeting a number that isn't tracked
// locally with --local is an error: there is nothing to remove locally.
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{
ID: "42",
Number: 42,
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}},
})
unstackCalled := false
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
UnstackFn: func(int) (*github.RemoteStack, bool, error) {
unstackCalled = true
return nil, true, nil
},
}
err := runUnstack(cfg, &unstackOptions{stackNumber: 999, local: true})
output := collectOutput(cfg, outR, errR)
assert.ErrorIs(t, err, ErrNotInStack)
assert.False(t, unstackCalled, "--local must never contact GitHub")
assert.Contains(t, output, "stack #999 is not tracked locally")
sf, err := stack.Load(gitDir)
require.NoError(t, err)
require.Len(t, sf.Stacks, 1)
}
func TestUnstack_ByStackNumber_LocalFlag_LegacyStack_NoRemoteCall(t *testing.T) {
// --local must never contact GitHub. A legacy stack (Number == 0) can only
// be matched by number via a remote backfill (ListStacks); under --local
// that lookup must be skipped and the number reported as not tracked.
gitDir := t.TempDir()
restore := git.SetOps(&git.MockOps{
GitDirFn: func() (string, error) { return gitDir, nil },
CurrentBranchFn: func() (string, error) { return "b1", nil },
})
defer restore()
// Legacy: internal ID present, Number unset (0).
writeStackFile(t, gitDir, stack.Stack{
ID: "99",
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "b1", PullRequest: &stack.PullRequestRef{Number: 101}},
{Branch: "b2", PullRequest: &stack.PullRequestRef{Number: 102}},
},
})
listCalled := false
unstackCalled := false
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
ListStacksFn: func() ([]github.RemoteStack, error) {
listCalled = true
return []github.RemoteStack{{ID: 99, Number: 7, PullRequests: []int{101, 102}}}, nil
},
UnstackFn: func(int) (*github.RemoteStack, bool, error) {
unstackCalled = true
return nil, true, nil
},
}
err := runUnstack(cfg, &unstackOptions{stackNumber: 7, local: true})
output := collectOutput(cfg, outR, errR)
assert.ErrorIs(t, err, ErrNotInStack)
assert.False(t, listCalled, "--local must not contact GitHub (no ListStacks backfill)")
assert.False(t, unstackCalled, "--local must not contact GitHub")
assert.Contains(t, output, "stack #7 is not tracked locally")
// Local tracking is untouched.
sf, loadErr := stack.Load(gitDir)
require.NoError(t, loadErr)
require.Len(t, sf.Stacks, 1)
}
func TestUnstack_ByStackNumber_LegacyStackResolvedByID(t *testing.T) {
// A stack tracked before the number was recorded (Number == 0) is resolved
// by mapping its internal ID to the remote stack number, and the backfilled
// number is persisted.
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{
ID: "99", // legacy: internal ID present, Number unset (0)
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{
{Branch: "b1", PullRequest: &stack.PullRequestRef{Number: 101}},
{Branch: "b2", PullRequest: &stack.PullRequestRef{Number: 102}},
},
})
var unstackedNumber int
cfg, outR, errR := config.NewTestConfig()
cfg.GitHubClientOverride = &github.MockClient{
ListStacksFn: func() ([]github.RemoteStack, error) {
return []github.RemoteStack{{ID: 99, Number: 7, PullRequests: []int{101, 102}}}, nil
},
UnstackFn: func(n int) (*github.RemoteStack, bool, error) {
unstackedNumber = n
// Some PRs remain stacked, so local tracking is kept.
return &github.RemoteStack{ID: 99, Number: 7, PullRequests: []int{102}}, false, nil
},
}
err := runUnstack(cfg, &unstackOptions{stackNumber: 7})
output := collectOutput(cfg, outR, errR)
require.NoError(t, err)
assert.Equal(t, 7, unstackedNumber, "should resolve the legacy stack and unstack by its remote number")
assert.Contains(t, output, "remain stacked on GitHub")
// The backfilled number is persisted to the stack file.
sf, loadErr := stack.Load(gitDir)
require.NoError(t, loadErr)
require.Len(t, sf.Stacks, 1)
assert.Equal(t, 7, sf.Stacks[0].Number, "the resolved stack number should be persisted")
}