beads/cmd/bd/gitlab_test.go
obsidian a7f9e86319 fix: resolve remaining errcheck violations in gitlab files
Add explicit error suppression (`_ =`) for unchecked resp.Body.Close()
in internal/gitlab/client.go and unchecked json.NewEncoder, w.Write,
json.NewDecoder, and os.Setenv/Unsetenv calls in gitlab test files.
Replace os.Setenv/Unsetenv with t.Setenv in gitlab_test.go for
automatic cleanup.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

Executed-By: beads/polecats/obsidian
Rig: beads
Role: polecats
2026-02-02 15:09:06 -07:00

188 lines
4.7 KiB
Go

// Package main provides the bd CLI commands.
package main
import (
"strings"
"testing"
)
// TestGitLabConfigFromEnv verifies config is read from environment variables.
func TestGitLabConfigFromEnv(t *testing.T) {
// Set environment variables
t.Setenv("GITLAB_URL", "https://gitlab.example.com")
t.Setenv("GITLAB_TOKEN", "test-token-123")
t.Setenv("GITLAB_PROJECT_ID", "42")
config := getGitLabConfig()
if config.URL != "https://gitlab.example.com" {
t.Errorf("URL = %q, want %q", config.URL, "https://gitlab.example.com")
}
if config.Token != "test-token-123" {
t.Errorf("Token = %q, want %q", config.Token, "test-token-123")
}
if config.ProjectID != "42" {
t.Errorf("ProjectID = %q, want %q", config.ProjectID, "42")
}
}
// TestGitLabConfigValidation verifies validation catches missing required fields.
func TestGitLabConfigValidation(t *testing.T) {
tests := []struct {
name string
config GitLabConfig
wantError string
}{
{
name: "missing URL",
config: GitLabConfig{Token: "tok", ProjectID: "1"},
wantError: "gitlab.url",
},
{
name: "missing token",
config: GitLabConfig{URL: "https://gitlab.com", ProjectID: "1"},
wantError: "gitlab.token",
},
{
name: "missing project_id",
config: GitLabConfig{URL: "https://gitlab.com", Token: "tok"},
wantError: "gitlab.project_id",
},
{
name: "all present",
config: GitLabConfig{URL: "https://gitlab.com", Token: "tok", ProjectID: "1"},
wantError: "",
},
{
name: "plain HTTP rejected",
config: GitLabConfig{URL: "http://gitlab.example.com", Token: "tok", ProjectID: "1"},
wantError: "HTTPS",
},
{
name: "localhost HTTP allowed",
config: GitLabConfig{URL: "http://localhost:8080", Token: "tok", ProjectID: "1"},
wantError: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateGitLabConfig(tt.config)
if tt.wantError == "" {
if err != nil {
t.Errorf("validateGitLabConfig() = %v, want nil", err)
}
} else {
if err == nil {
t.Error("validateGitLabConfig() = nil, want error")
} else if !strings.Contains(err.Error(), tt.wantError) {
t.Errorf("validateGitLabConfig() = %v, want error containing %q", err, tt.wantError)
}
}
})
}
}
// TestMaskGitLabToken verifies token masking for display.
func TestMaskGitLabToken(t *testing.T) {
tests := []struct {
name string
token string
want string
}{
{
name: "normal token",
token: "glpat-xxxxxxxxxxxxxxxxxxxx",
want: "glpa****",
},
{
name: "short token",
token: "abc",
want: "****",
},
{
name: "empty token",
token: "",
want: "(not set)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := maskGitLabToken(tt.token)
if got != tt.want {
t.Errorf("maskGitLabToken(%q) = %q, want %q", tt.token, got, tt.want)
}
})
}
}
// TestGitLabConfigEnvVar verifies environment variable mapping.
func TestGitLabConfigEnvVar(t *testing.T) {
tests := []struct {
key string
want string
}{
{"gitlab.url", "GITLAB_URL"},
{"gitlab.token", "GITLAB_TOKEN"},
{"gitlab.project_id", "GITLAB_PROJECT_ID"},
{"gitlab.unknown", ""},
}
for _, tt := range tests {
t.Run(tt.key, func(t *testing.T) {
got := gitlabConfigToEnvVar(tt.key)
if got != tt.want {
t.Errorf("gitlabConfigToEnvVar(%q) = %q, want %q", tt.key, got, tt.want)
}
})
}
}
// TestGitLabClientCreation verifies client is created with correct config.
func TestGitLabClientCreation(t *testing.T) {
t.Setenv("GITLAB_URL", "https://gitlab.test.com")
t.Setenv("GITLAB_TOKEN", "test-token-abc")
t.Setenv("GITLAB_PROJECT_ID", "99")
config := getGitLabConfig()
client := getGitLabClient(config)
if client.BaseURL != "https://gitlab.test.com" {
t.Errorf("client.BaseURL = %q, want %q", client.BaseURL, "https://gitlab.test.com")
}
if client.Token != "test-token-abc" {
t.Errorf("client.Token = %q, want %q", client.Token, "test-token-abc")
}
if client.ProjectID != "99" {
t.Errorf("client.ProjectID = %q, want %q", client.ProjectID, "99")
}
}
// TestGitLabCmdRegistration verifies the gitlab command and subcommands are registered.
func TestGitLabCmdRegistration(t *testing.T) {
// Check that gitlabCmd has expected subcommands
subcommands := gitlabCmd.Commands()
var hasSync, hasStatus, hasProjects bool
for _, cmd := range subcommands {
switch cmd.Name() {
case "sync":
hasSync = true
case "status":
hasStatus = true
case "projects":
hasProjects = true
}
}
if !hasSync {
t.Error("gitlabCmd missing 'sync' subcommand")
}
if !hasStatus {
t.Error("gitlabCmd missing 'status' subcommand")
}
if !hasProjects {
t.Error("gitlabCmd missing 'projects' subcommand")
}
}