Files
CLIProxyAPI/internal/util/github_test.go
Luis Pater d36b776c79 fix(test): improve cross-platform compatibility across unit tests
- Support CRLF line endings and skip hidden dot directories in nocopy invariant tests.
- Handle environment variable overrides and skip case-sensitive token priority tests on Windows.
- Use static OS and architecture values in Claude header fingerprint assertions for deterministic test results.

Closes: #5295
2026-08-28 05:01:42 +08:00

77 lines
2.0 KiB
Go

package util
import (
"runtime"
"testing"
)
func TestResolveGitHubToken(t *testing.T) {
tests := []struct {
name string
githubToken string
lowerToken string
gitStoreToken string
gitStoreURL string
want string
}{
{
name: "GITHUB_TOKEN has highest priority",
githubToken: " primary-token ",
lowerToken: "lower-token",
gitStoreToken: "gitstore-token",
gitStoreURL: "https://github.com/example/repo.git",
want: "primary-token",
},
{
name: "lowercase token is second priority",
githubToken: " ",
lowerToken: " lower-token ",
gitStoreToken: "gitstore-token",
gitStoreURL: "https://github.com/example/repo.git",
want: "lower-token",
},
{
name: "Git store token is used for GitHub",
gitStoreToken: " gitstore-token ",
gitStoreURL: "HTTPS://GITHUB.COM/example/repo.git",
want: "gitstore-token",
},
{
name: "Git store token is ignored for other hosts",
gitStoreToken: "gitstore-token",
gitStoreURL: "https://gitlab.com/example/repo.git",
},
{
name: "Git store token is ignored without URL",
gitStoreToken: "gitstore-token",
},
{
name: "no token configured",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if runtime.GOOS == "windows" && tt.name == "GITHUB_TOKEN has highest priority" {
t.Skip("environment variables are case-insensitive on Windows")
}
if tt.githubToken != "" {
t.Setenv("GITHUB_TOKEN", tt.githubToken)
}
if tt.lowerToken != "" {
t.Setenv("github_token", tt.lowerToken)
}
if tt.githubToken == "" && tt.lowerToken == "" {
t.Setenv("GITHUB_TOKEN", "")
t.Setenv("github_token", "")
}
t.Setenv("GITSTORE_GIT_TOKEN", tt.gitStoreToken)
t.Setenv("GITSTORE_GIT_URL", tt.gitStoreURL)
if got := ResolveGitHubToken(); got != tt.want {
t.Fatalf("ResolveGitHubToken() = %q, want %q", got, tt.want)
}
})
}
}