Files
github__gh-stack/internal/github/github_test.go
Copilot ad21053fe9 Guard GraphQL PR number conversion against int32 overflow (#56)
* Initial plan

* fix: validate int range before GraphQL Int conversion

Agent-Logs-Url: https://github.com/github/gh-stack/sessions/dbb2b50f-34fb-4957-ac08-e19c1f96ba41

Co-authored-by: skarim <1701557+skarim@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: skarim <1701557+skarim@users.noreply.github.com>
2026-04-23 12:21:43 -04:00

63 lines
1.5 KiB
Go

package github
import (
"testing"
graphql "github.com/cli/shurcooL-graphql"
"github.com/stretchr/testify/assert"
)
func TestPRURL(t *testing.T) {
tests := []struct {
name string
host string
owner string
repo string
number int
want string
}{
{"github.com", "github.com", "owner", "repo", 42, "https://github.com/owner/repo/pull/42"},
{"GHES host", "ghes.example.com", "myorg", "myrepo", 99, "https://ghes.example.com/myorg/myrepo/pull/99"},
{"empty host defaults to github.com", "", "owner", "repo", 1, "https://github.com/owner/repo/pull/1"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := PRURL(tt.host, tt.owner, tt.repo, tt.number)
assert.Equal(t, tt.want, got)
})
}
}
func TestPullRequest_IsQueued(t *testing.T) {
t.Run("not queued when MergeQueueEntry is nil", func(t *testing.T) {
pr := &PullRequest{Number: 1}
assert.False(t, pr.IsQueued())
})
t.Run("queued when MergeQueueEntry has ID", func(t *testing.T) {
pr := &PullRequest{
Number: 1,
MergeQueueEntry: &MergeQueueEntry{ID: "MQE_123"},
}
assert.True(t, pr.IsQueued())
})
t.Run("nil receiver is safe", func(t *testing.T) {
var pr *PullRequest
assert.False(t, pr.IsQueued())
})
}
func TestToGraphQLInt(t *testing.T) {
t.Run("in range", func(t *testing.T) {
got, err := toGraphQLInt(123)
assert.NoError(t, err)
assert.Equal(t, graphql.Int(123), got)
})
t.Run("out of range", func(t *testing.T) {
_, err := toGraphQLInt(1 << 40)
assert.Error(t, err)
})
}