fleet/tools/github-manage/pkg/ghapi/workflows_test.go
George Karr df5e42a54c
Github Manager (#31540)
New tool to help with github management. Read all about it in the
[README](https://github.com/fleetdm/fleet/blob/gkarr-gm/tools/github-manage/README.md)
on this branch.
2025-08-07 15:00:36 -05:00

254 lines
6.3 KiB
Go

package ghapi
import (
"testing"
)
func TestBulkAddLabel(t *testing.T) {
tests := []struct {
name string
issues []Issue
label string
expectError bool
}{
{
name: "empty issues slice",
issues: []Issue{},
label: "test-label",
expectError: false,
},
{
name: "single issue",
issues: []Issue{
{Number: 123, Title: "Test Issue"},
},
label: "bug",
expectError: false, // Note: Will actually error due to GitHub CLI, but testing structure
},
{
name: "multiple issues",
issues: []Issue{
{Number: 123, Title: "Test Issue 1"},
{Number: 456, Title: "Test Issue 2"},
{Number: 789, Title: "Test Issue 3"},
},
label: "enhancement",
expectError: false, // Note: Will actually error due to GitHub CLI, but testing structure
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Since BulkAddLabel depends on AddLabelToIssue which calls GitHub CLI,
// we can only test the basic structure without mocking
if len(tt.issues) == 0 {
err := BulkAddLabel(tt.issues, tt.label)
if err != nil {
t.Errorf("BulkAddLabel with empty slice should not error, got: %v", err)
}
} else {
// For non-empty slices, we expect GitHub CLI errors in test environment
t.Skip("BulkAddLabel requires GitHub CLI setup and mocking for proper testing")
}
})
}
}
func TestBulkRemoveLabel(t *testing.T) {
tests := []struct {
name string
issues []Issue
label string
expectError bool
}{
{
name: "empty issues slice",
issues: []Issue{},
label: "test-label",
expectError: false,
},
{
name: "single issue",
issues: []Issue{
{Number: 123, Title: "Test Issue"},
},
label: "bug",
expectError: false, // Note: Will actually error due to GitHub CLI, but testing structure
},
{
name: "multiple issues",
issues: []Issue{
{Number: 123, Title: "Test Issue 1"},
{Number: 456, Title: "Test Issue 2"},
},
label: "outdated",
expectError: false, // Note: Will actually error due to GitHub CLI, but testing structure
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Since BulkRemoveLabel depends on RemoveLabelFromIssue which calls GitHub CLI,
// we can only test the basic structure without mocking
if len(tt.issues) == 0 {
err := BulkRemoveLabel(tt.issues, tt.label)
if err != nil {
t.Errorf("BulkRemoveLabel with empty slice should not error, got: %v", err)
}
} else {
// For non-empty slices, we expect GitHub CLI errors in test environment
t.Skip("BulkRemoveLabel requires GitHub CLI setup and mocking for proper testing")
}
})
}
}
func TestBulkSprintKickoff(t *testing.T) {
tests := []struct {
name string
issues []Issue
projectID int
}{
{
name: "empty issues slice",
issues: []Issue{},
projectID: 58,
},
{
name: "single issue",
issues: []Issue{
{Number: 123, Title: "Test Issue"},
},
projectID: 67,
},
{
name: "multiple issues",
issues: []Issue{
{Number: 123, Title: "Test Issue 1"},
{Number: 456, Title: "Test Issue 2"},
},
projectID: 70,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Skip("BulkSprintKickoff requires GitHub CLI setup and mocking for proper testing")
// Since BulkSprintKickoff depends on multiple GitHub CLI commands,
err := BulkSprintKickoff(tt.issues, tt.projectID, tt.projectID)
if err != nil {
t.Errorf("BulkSprintKickoff should return nil (stub implementation), got: %v", err)
}
})
}
}
func TestBulkMilestoneClose(t *testing.T) {
tests := []struct {
name string
issues []Issue
}{
{
name: "empty issues slice",
issues: []Issue{},
},
{
name: "single issue",
issues: []Issue{
{Number: 123, Title: "Test Issue"},
},
},
{
name: "multiple issues",
issues: []Issue{
{Number: 123, Title: "Test Issue 1"},
{Number: 456, Title: "Test Issue 2"},
{Number: 789, Title: "Test Issue 3"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Skip("BulkMilestoneClose requires GitHub CLI setup and mocking for proper testing")
// BulkMilestoneClose is currently a stub function that returns nil
err := BulkMilestoneClose(tt.issues)
if err != nil {
t.Errorf("BulkMilestoneClose should return nil (stub implementation), got: %v", err)
}
})
}
}
func TestWorkflowFunctionsSignatures(t *testing.T) {
// Test that all workflow functions have the expected signatures
t.Skip("Skipping signature tests as they are not executable without GitHub CLI setup")
// Test BulkAddLabel signature
var issues []Issue
err := BulkAddLabel(issues, "test")
if err != nil {
// Expected for empty slice
}
// Test BulkRemoveLabel signature
err = BulkRemoveLabel(issues, "test")
if err != nil {
// Expected for empty slice
}
// Test BulkSprintKickoff signature
err = BulkSprintKickoff(issues, 123, 123)
if err != nil {
t.Errorf("BulkSprintKickoff unexpected error: %v", err)
}
// Test BulkMilestoneClose signature
err = BulkMilestoneClose(issues)
if err != nil {
t.Errorf("BulkMilestoneClose unexpected error: %v", err)
}
}
func TestWorkflowsWithValidIssues(t *testing.T) {
// Create sample issues for testing
issues := []Issue{
{
Number: 123,
Title: "Test Issue 1",
Author: Author{Login: "testuser1"},
State: "open",
Labels: []Label{
{Name: "bug"},
},
},
{
Number: 456,
Title: "Test Issue 2",
Author: Author{Login: "testuser2"},
State: "open",
Labels: []Label{
{Name: "enhancement"},
},
},
}
t.Run("BulkSprintKickoff with valid issues", func(t *testing.T) {
t.Skip("BulkSprintKickoff requires GitHub CLI setup and mocking for proper testing")
err := BulkSprintKickoff(issues, 58, 58)
if err != nil {
t.Errorf("BulkSprintKickoff should return nil, got: %v", err)
}
})
t.Run("BulkMilestoneClose with valid issues", func(t *testing.T) {
t.Skip("BulkMilestoneClose requires GitHub CLI setup and mocking for proper testing")
err := BulkMilestoneClose(issues)
if err != nil {
t.Errorf("BulkMilestoneClose should return nil, got: %v", err)
}
})
// Note: BulkAddLabel and BulkRemoveLabel are skipped because they would
// require GitHub CLI mocking to test properly without external dependencies
}