mirror of
https://github.com/github/gh-stack.git
synced 2026-09-14 20:26:28 +08:00
f880f0d469
* Support addressing a stack by its stack number checkout now interprets a bare integer as a stack number first (the identifier shown in the github.com stack UI), falling back to a locally tracked PR number, then a PR number discovered from GitHub, then a branch name. A new checkoutStackByNumber resolves the stack via GetStack and checks out its top-most unmerged branch; the reconcile/import logic is shared with the PR-number path. unstack gains an optional <stack-number> positional argument to unstack a specific locally tracked stack instead of the current one. Copilot-Session: 03673c26-a245-42da-93ed-dfcebc92a740 * Surface the stack number in output and TUIs Show the human-facing stack number wherever it is known: - Append a "(stack #N)" label to submit, link, checkout, and unstack success messages. - Add a "Stack #N" header line to the view command (short and static) and the stackview TUI header. - Add a "Stack #N" info line to the submit TUI header when submitting an already-created stack. Copilot-Session: 03673c26-a245-42da-93ed-dfcebc92a740 * Update docs and agent instructions for the new API - cli.md: document checkout/unstack by stack number and drop the "PATs are not supported" note (any gh-authenticated user can now run stack operations). - quick-start.md: drop the PAT-not-supported note. - AGENTS.md / copilot-instructions.md: ClientOps is now 13 methods over the public Stacks REST API; remove the TokenForHostFn test hook; note the stack file's id/number identity. - SKILL.md: add checkout/unstack-by-stack-number quick references. Copilot-Session: 03673c26-a245-42da-93ed-dfcebc92a740 * address review comments
475 lines
14 KiB
Go
475 lines
14 KiB
Go
package stackview
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/github/gh-stack/internal/git"
|
|
ghapi "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 makeNodes(branches ...string) []BranchNode {
|
|
nodes := make([]BranchNode, len(branches))
|
|
for i, b := range branches {
|
|
nodes[i] = BranchNode{
|
|
Ref: stack.BranchRef{Branch: b},
|
|
}
|
|
}
|
|
return nodes
|
|
}
|
|
|
|
func keyMsg(k string) tea.KeyMsg {
|
|
switch k {
|
|
case "up":
|
|
return tea.KeyMsg(tea.Key{Type: tea.KeyUp})
|
|
case "down":
|
|
return tea.KeyMsg(tea.Key{Type: tea.KeyDown})
|
|
case "enter":
|
|
return tea.KeyMsg(tea.Key{Type: tea.KeyEnter})
|
|
case "esc":
|
|
return tea.KeyMsg(tea.Key{Type: tea.KeyEscape})
|
|
case "ctrl+c":
|
|
return tea.KeyMsg(tea.Key{Type: tea.KeyCtrlC})
|
|
default:
|
|
// Single rune key like 'c', 'f', 'q', 'o'
|
|
return tea.KeyMsg(tea.Key{Type: tea.KeyRunes, Runes: []rune(k)})
|
|
}
|
|
}
|
|
|
|
var testTrunk = stack.BranchRef{Branch: "main"}
|
|
|
|
func TestNew_CursorAtCurrentBranch(t *testing.T) {
|
|
nodes := makeNodes("b1", "b2", "b3")
|
|
nodes[1].IsCurrent = true
|
|
|
|
m := New(nodes, testTrunk, "0.0.1", 0)
|
|
|
|
assert.Equal(t, 1, m.cursor)
|
|
}
|
|
|
|
func TestNew_CursorAtZeroWhenNoCurrent(t *testing.T) {
|
|
nodes := makeNodes("b1", "b2", "b3")
|
|
|
|
m := New(nodes, testTrunk, "0.0.1", 0)
|
|
|
|
assert.Equal(t, 0, m.cursor)
|
|
}
|
|
|
|
func TestUpdate_KeyboardNavigation(t *testing.T) {
|
|
nodes := makeNodes("b1", "b2", "b3")
|
|
m := New(nodes, testTrunk, "0.0.1", 0)
|
|
assert.Equal(t, 0, m.cursor)
|
|
|
|
// Down
|
|
updated, _ := m.Update(keyMsg("down"))
|
|
m = updated.(Model)
|
|
assert.Equal(t, 1, m.cursor)
|
|
|
|
// Down again
|
|
updated, _ = m.Update(keyMsg("down"))
|
|
m = updated.(Model)
|
|
assert.Equal(t, 2, m.cursor)
|
|
|
|
// Down at bottom — should clamp
|
|
updated, _ = m.Update(keyMsg("down"))
|
|
m = updated.(Model)
|
|
assert.Equal(t, 2, m.cursor, "cursor should clamp at bottom")
|
|
|
|
// Up
|
|
updated, _ = m.Update(keyMsg("up"))
|
|
m = updated.(Model)
|
|
assert.Equal(t, 1, m.cursor)
|
|
|
|
// Up
|
|
updated, _ = m.Update(keyMsg("up"))
|
|
m = updated.(Model)
|
|
assert.Equal(t, 0, m.cursor)
|
|
|
|
// Up at top — should clamp
|
|
updated, _ = m.Update(keyMsg("up"))
|
|
m = updated.(Model)
|
|
assert.Equal(t, 0, m.cursor, "cursor should clamp at top")
|
|
}
|
|
|
|
func TestUpdate_ToggleCommits(t *testing.T) {
|
|
nodes := makeNodes("b1", "b2")
|
|
nodes[0].Commits = []git.CommitInfo{{SHA: "abc", Subject: "test"}}
|
|
m := New(nodes, testTrunk, "0.0.1", 0)
|
|
|
|
assert.False(t, m.nodes[0].CommitsExpanded)
|
|
|
|
updated, _ := m.Update(keyMsg("c"))
|
|
m = updated.(Model)
|
|
assert.True(t, m.nodes[0].CommitsExpanded)
|
|
|
|
// Toggle back
|
|
updated, _ = m.Update(keyMsg("c"))
|
|
m = updated.(Model)
|
|
assert.False(t, m.nodes[0].CommitsExpanded)
|
|
}
|
|
|
|
func TestUpdate_ToggleFiles(t *testing.T) {
|
|
nodes := makeNodes("b1", "b2")
|
|
m := New(nodes, testTrunk, "0.0.1", 0)
|
|
|
|
assert.False(t, m.nodes[0].FilesExpanded)
|
|
|
|
updated, _ := m.Update(keyMsg("f"))
|
|
m = updated.(Model)
|
|
assert.True(t, m.nodes[0].FilesExpanded)
|
|
|
|
// Toggle back
|
|
updated, _ = m.Update(keyMsg("f"))
|
|
m = updated.(Model)
|
|
assert.False(t, m.nodes[0].FilesExpanded)
|
|
}
|
|
|
|
func TestUpdate_Quit(t *testing.T) {
|
|
nodes := makeNodes("b1")
|
|
m := New(nodes, testTrunk, "0.0.1", 0)
|
|
|
|
quitKeys := []string{"q", "esc", "ctrl+c"}
|
|
for _, k := range quitKeys {
|
|
t.Run(k, func(t *testing.T) {
|
|
_, cmd := m.Update(keyMsg(k))
|
|
assert.NotNil(t, cmd, "key %q should produce a quit command", k)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUpdate_CheckoutOnEnter(t *testing.T) {
|
|
nodes := makeNodes("b1", "b2")
|
|
nodes[0].IsCurrent = true
|
|
nodes[1].PR = &ghapi.PRDetails{Number: 42, URL: "https://github.com/pr/42"}
|
|
m := New(nodes, testTrunk, "0.0.1", 0)
|
|
|
|
// Move to b2 (non-current)
|
|
updated, _ := m.Update(keyMsg("down"))
|
|
m = updated.(Model)
|
|
assert.Equal(t, 1, m.cursor)
|
|
|
|
// Press enter on non-current node
|
|
updated, cmd := m.Update(keyMsg("enter"))
|
|
m = updated.(Model)
|
|
|
|
assert.Equal(t, "b2", m.CheckoutBranch())
|
|
assert.NotNil(t, cmd, "enter on non-current should produce quit command")
|
|
}
|
|
|
|
func TestUpdate_EnterOnCurrentDoesNothing(t *testing.T) {
|
|
nodes := makeNodes("b1", "b2")
|
|
nodes[0].IsCurrent = true
|
|
m := New(nodes, testTrunk, "0.0.1", 0)
|
|
assert.Equal(t, 0, m.cursor)
|
|
|
|
// Press enter on current node
|
|
updated, cmd := m.Update(keyMsg("enter"))
|
|
m = updated.(Model)
|
|
|
|
assert.Equal(t, "", m.CheckoutBranch(), "enter on current branch should not set checkout")
|
|
assert.Nil(t, cmd, "enter on current branch should not quit")
|
|
}
|
|
|
|
func TestView_HeaderShownWhenTallEnough(t *testing.T) {
|
|
nodes := makeNodes("b1", "b2")
|
|
m := New(nodes, testTrunk, "0.0.1", 0)
|
|
|
|
// Simulate a tall and wide terminal
|
|
updated, _ := m.Update(tea.WindowSizeMsg{Width: 100, Height: 40})
|
|
m = updated.(Model)
|
|
|
|
view := m.View()
|
|
assert.Contains(t, view, "┌")
|
|
assert.Contains(t, view, "┘")
|
|
assert.Contains(t, view, "View Stack")
|
|
assert.Contains(t, view, "v0.0.1")
|
|
assert.Contains(t, view, "Base: main")
|
|
assert.Contains(t, view, "2 branches")
|
|
assert.Contains(t, view, "↑")
|
|
assert.Contains(t, view, "quit")
|
|
}
|
|
|
|
func TestView_HeaderHiddenWhenShort(t *testing.T) {
|
|
nodes := makeNodes("b1")
|
|
m := New(nodes, testTrunk, "0.0.1", 0)
|
|
|
|
// Simulate a short terminal (below minHeightForHeader)
|
|
updated, _ := m.Update(tea.WindowSizeMsg{Width: 80, Height: 20})
|
|
m = updated.(Model)
|
|
|
|
view := m.View()
|
|
// Should NOT contain header box
|
|
assert.NotContains(t, view, "┌")
|
|
assert.NotContains(t, view, "View Stack")
|
|
// Should NOT contain help bar either (hints are only in header)
|
|
assert.NotContains(t, view, "commits")
|
|
}
|
|
|
|
func TestView_HeaderHiddenWhenNarrow(t *testing.T) {
|
|
nodes := makeNodes("b1")
|
|
m := New(nodes, testTrunk, "0.0.1", 0)
|
|
|
|
// Tall but too narrow for header (below minWidthForHeader)
|
|
updated, _ := m.Update(tea.WindowSizeMsg{Width: 35, Height: 40})
|
|
m = updated.(Model)
|
|
|
|
view := m.View()
|
|
assert.NotContains(t, view, "┌")
|
|
assert.NotContains(t, view, "View Stack")
|
|
}
|
|
|
|
func TestView_HeaderShortcutsAlwaysVisible(t *testing.T) {
|
|
nodes := makeNodes("b1", "b2")
|
|
m := New(nodes, testTrunk, "0.0.1", 0)
|
|
|
|
// Even at medium width, shortcuts should still be visible
|
|
updated, _ := m.Update(tea.WindowSizeMsg{Width: 60, Height: 40})
|
|
m = updated.(Model)
|
|
|
|
view := m.View()
|
|
assert.Contains(t, view, "┌", "header should be shown")
|
|
assert.Contains(t, view, "checkout", "shortcuts should always be visible")
|
|
}
|
|
|
|
func TestView_HeaderShowsMergedCount(t *testing.T) {
|
|
nodes := makeNodes("b1", "b2", "b3")
|
|
nodes[0].Ref.PullRequest = &stack.PullRequestRef{Merged: true}
|
|
m := New(nodes, testTrunk, "0.0.1", 0)
|
|
|
|
updated, _ := m.Update(tea.WindowSizeMsg{Width: 100, Height: 40})
|
|
m = updated.(Model)
|
|
|
|
view := m.View()
|
|
assert.Contains(t, view, "3 branches (1 merged)")
|
|
}
|
|
|
|
func TestView_HeaderShowsQueuedCount(t *testing.T) {
|
|
nodes := makeNodes("b1", "b2", "b3")
|
|
nodes[1].Ref.Queued = true
|
|
nodes[1].Ref.PullRequest = &stack.PullRequestRef{Number: 10}
|
|
m := New(nodes, testTrunk, "0.0.1", 0)
|
|
|
|
updated, _ := m.Update(tea.WindowSizeMsg{Width: 100, Height: 40})
|
|
m = updated.(Model)
|
|
|
|
view := m.View()
|
|
assert.Contains(t, view, "3 branches (1 queued)")
|
|
}
|
|
|
|
func TestView_QueuedPRShowsQueuedLabel(t *testing.T) {
|
|
nodes := makeNodes("b1")
|
|
nodes[0].PR = &ghapi.PRDetails{Number: 99, IsQueued: true}
|
|
m := New(nodes, testTrunk, "0.0.1", 0)
|
|
|
|
updated, _ := m.Update(tea.WindowSizeMsg{Width: 80, Height: 30})
|
|
m = updated.(Model)
|
|
|
|
view := m.View()
|
|
assert.Contains(t, view, "QUEUED")
|
|
assert.Contains(t, view, "#99")
|
|
}
|
|
|
|
func TestView_BranchProgressIcon(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
merged []int // indices of merged branches
|
|
total int
|
|
wantIcon string
|
|
}{
|
|
{"none merged", nil, 3, "○"},
|
|
{"some merged", []int{0}, 3, "◐"},
|
|
{"all merged", []int{0, 1, 2}, 3, "●"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
names := make([]string, tt.total)
|
|
for i := range names {
|
|
names[i] = fmt.Sprintf("b%d", i)
|
|
}
|
|
nodes := makeNodes(names...)
|
|
for _, idx := range tt.merged {
|
|
nodes[idx].Ref.PullRequest = &stack.PullRequestRef{Merged: true}
|
|
}
|
|
m := New(nodes, testTrunk, "0.0.1", 0)
|
|
updated, _ := m.Update(tea.WindowSizeMsg{Width: 100, Height: 40})
|
|
m = updated.(Model)
|
|
|
|
view := m.View()
|
|
assert.Contains(t, view, tt.wantIcon)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMouseClick_HeaderAreaIgnored(t *testing.T) {
|
|
nodes := makeNodes("b1", "b2")
|
|
m := New(nodes, testTrunk, "0.0.1", 0)
|
|
updated, _ := m.Update(tea.WindowSizeMsg{Width: 100, Height: 40})
|
|
m = updated.(Model)
|
|
|
|
// Click inside the header area (row 5 is inside the 12-line header)
|
|
updated, _ = m.Update(tea.MouseMsg{
|
|
Action: tea.MouseActionPress,
|
|
Button: tea.MouseButtonLeft,
|
|
X: 10,
|
|
Y: 5,
|
|
})
|
|
result := updated.(Model)
|
|
assert.Equal(t, 0, result.cursor, "clicking in header should not change cursor")
|
|
}
|
|
|
|
func TestScrollClamp_CannotScrollPastContent(t *testing.T) {
|
|
nodes := makeNodes("b1", "b2")
|
|
m := New(nodes, testTrunk, "0.0.1", 0)
|
|
|
|
// Tall terminal with plenty of room for content
|
|
updated, _ := m.Update(tea.WindowSizeMsg{Width: 80, Height: 40})
|
|
m = updated.(Model)
|
|
|
|
// Scroll down many times — should not scroll past content
|
|
for i := 0; i < 50; i++ {
|
|
updated, _ = m.Update(tea.MouseMsg{
|
|
Action: tea.MouseActionPress,
|
|
Button: tea.MouseButtonWheelDown,
|
|
})
|
|
m = updated.(Model)
|
|
}
|
|
|
|
// scrollOffset should be clamped (content fits in view, so offset stays 0)
|
|
view := m.View()
|
|
assert.Contains(t, view, "b1", "content should still be visible after excessive scrolling")
|
|
}
|
|
|
|
func TestUpdate_CursorSkipsMergedBranches(t *testing.T) {
|
|
nodes := makeNodes("b1", "b2", "b3")
|
|
nodes[1].Ref.PullRequest = &stack.PullRequestRef{Number: 2, Merged: true}
|
|
m := New(nodes, testTrunk, "0.0.1", 0)
|
|
assert.Equal(t, 0, m.cursor, "cursor should start on first non-merged branch")
|
|
|
|
// Down should skip b2 (merged) and land on b3
|
|
updated, _ := m.Update(keyMsg("down"))
|
|
m = updated.(Model)
|
|
assert.Equal(t, 2, m.cursor, "down should skip merged b2 and land on b3")
|
|
|
|
// Up should skip b2 (merged) and land back on b1
|
|
updated, _ = m.Update(keyMsg("up"))
|
|
m = updated.(Model)
|
|
assert.Equal(t, 0, m.cursor, "up should skip merged b2 and land on b1")
|
|
}
|
|
|
|
func TestNew_CursorSkipsMergedBranch(t *testing.T) {
|
|
nodes := makeNodes("b1", "b2", "b3")
|
|
nodes[0].Ref.PullRequest = &stack.PullRequestRef{Number: 1, Merged: true}
|
|
m := New(nodes, testTrunk, "0.0.1", 0)
|
|
assert.Equal(t, 1, m.cursor, "cursor should skip merged b1 and start on b2")
|
|
}
|
|
|
|
func TestNew_CursorSkipsMergedCurrentBranch(t *testing.T) {
|
|
nodes := makeNodes("b1", "b2", "b3")
|
|
nodes[0].IsCurrent = true
|
|
nodes[0].Ref.PullRequest = &stack.PullRequestRef{Number: 1, Merged: true}
|
|
m := New(nodes, testTrunk, "0.0.1", 0)
|
|
assert.Equal(t, 1, m.cursor, "cursor should not start on merged current branch")
|
|
}
|
|
|
|
func TestUpdate_EnterOnMergedDoesNothing(t *testing.T) {
|
|
// All non-merged so we can navigate, but force cursor onto a merged node
|
|
// by having b1 active and b2 merged and b3 active.
|
|
nodes := makeNodes("b1", "b2")
|
|
nodes[0].Ref.PullRequest = &stack.PullRequestRef{Number: 1, Merged: true}
|
|
m := New(nodes, testTrunk, "0.0.1", 0)
|
|
// Cursor is on b2 (first non-merged). Manually set to b1 to test guard.
|
|
m.cursor = 0
|
|
|
|
updated, cmd := m.Update(keyMsg("enter"))
|
|
m = updated.(Model)
|
|
assert.Equal(t, "", m.CheckoutBranch(), "enter on merged branch should not set checkout")
|
|
assert.Nil(t, cmd, "enter on merged branch should not quit")
|
|
}
|
|
|
|
// makeAllMergedNodes returns nodes whose branches are all merged.
|
|
func makeAllMergedNodes(branches ...string) []BranchNode {
|
|
nodes := makeNodes(branches...)
|
|
for i := range nodes {
|
|
nodes[i].Ref.PullRequest = &stack.PullRequestRef{Number: i + 1, Merged: true}
|
|
}
|
|
return nodes
|
|
}
|
|
|
|
func TestNew_CursorHiddenWhenAllMerged(t *testing.T) {
|
|
m := New(makeAllMergedNodes("b1", "b2"), testTrunk, "0.0.1", 0)
|
|
assert.Equal(t, -1, m.cursor, "cursor should be hidden when every branch is merged")
|
|
}
|
|
|
|
func TestUpdate_AllMergedCursorStaysHidden(t *testing.T) {
|
|
m := New(makeAllMergedNodes("b1", "b2", "b3"), testTrunk, "0.0.1", 0)
|
|
|
|
updated, _ := m.Update(keyMsg("down"))
|
|
m = updated.(Model)
|
|
assert.Equal(t, -1, m.cursor, "down should not move the hidden cursor")
|
|
|
|
updated, _ = m.Update(keyMsg("up"))
|
|
m = updated.(Model)
|
|
assert.Equal(t, -1, m.cursor, "up should not move the hidden cursor")
|
|
|
|
updated, cmd := m.Update(keyMsg("enter"))
|
|
m = updated.(Model)
|
|
assert.Equal(t, "", m.CheckoutBranch(), "enter should not check out when all merged")
|
|
assert.Nil(t, cmd, "enter should not quit when all merged")
|
|
}
|
|
|
|
func TestView_AllMergedRendersWithoutPanic(t *testing.T) {
|
|
m := New(makeAllMergedNodes("b1", "b2"), testTrunk, "0.0.1", 0)
|
|
updated, _ := m.Update(tea.WindowSizeMsg{Width: 100, Height: 40})
|
|
m = updated.(Model)
|
|
// Should not panic with a hidden (-1) cursor.
|
|
view := m.View()
|
|
assert.Contains(t, view, "b1")
|
|
}
|
|
|
|
func TestBuildHeaderConfig_DisablesShortcutsWhenAllMerged(t *testing.T) {
|
|
m := New(makeAllMergedNodes("b1", "b2"), testTrunk, "0.0.1", 0)
|
|
cfg := m.buildHeaderConfig()
|
|
|
|
require.NotEmpty(t, cfg.Shortcuts)
|
|
for _, sc := range cfg.Shortcuts {
|
|
if sc.Desc == "quit" {
|
|
assert.False(t, sc.Disabled, "quit should stay enabled")
|
|
} else {
|
|
assert.True(t, sc.Disabled, "%q should be disabled when all branches merged", sc.Desc)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBuildHeaderConfig_ShortcutsEnabledWithActiveBranches(t *testing.T) {
|
|
m := New(makeNodes("b1", "b2"), testTrunk, "0.0.1", 0)
|
|
cfg := m.buildHeaderConfig()
|
|
|
|
require.NotEmpty(t, cfg.Shortcuts)
|
|
for _, sc := range cfg.Shortcuts {
|
|
assert.False(t, sc.Disabled, "%q should be enabled when there are active branches", sc.Desc)
|
|
}
|
|
}
|
|
|
|
func TestBuildHeaderConfig_ShowsStackNumber(t *testing.T) {
|
|
m := New(makeNodes("b1", "b2"), testTrunk, "0.0.1", 7)
|
|
cfg := m.buildHeaderConfig()
|
|
|
|
found := false
|
|
for _, line := range cfg.InfoLines {
|
|
if line.Label == "Stack #7" {
|
|
found = true
|
|
}
|
|
}
|
|
assert.True(t, found, "header should show the stack number when known")
|
|
|
|
// When the number is unknown (0), no stack-number line is shown.
|
|
m0 := New(makeNodes("b1", "b2"), testTrunk, "0.0.1", 0)
|
|
for _, line := range m0.buildHeaderConfig().InfoLines {
|
|
assert.NotContains(t, line.Label, "Stack #")
|
|
}
|
|
}
|