Borg/pkg/github/github_test.go
google-labs-jules[bot] 3d7c7c4634 feat: Add configurable timeouts for HTTP requests
This commit introduces configurable timeouts for HTTP requests made by the `collect` commands.

Key changes:
- Created a new `pkg/httpclient` package with a `NewClient` function that returns an `http.Client` with configurable timeouts for total, connect, TLS, and header stages.
- Added `--timeout`, `--connect-timeout`, `--tls-timeout`, and `--header-timeout` persistent flags to the `collect` command, making them available to all its subcommands.
- Refactored the `pkg/website`, `pkg/pwa`, and `pkg/github` packages to accept and use a custom `http.Client`, allowing the timeout configurations to be injected.
- Updated the `collect website`, `collect pwa`, and `collect github repos` commands to create a configured HTTP client based on the new flags and pass it to the respective packages.
- Added unit tests for the `pkg/httpclient` package to verify correct timeout configuration.
- Fixed all test and build failures that resulted from the refactoring.
- Addressed an unrelated build failure by creating a placeholder file (`pkg/player/frontend/demo-track.smsg`).

This work addresses the initial requirement for configurable timeouts via command-line flags. Further work is needed to implement per-domain overrides from a configuration file and idle timeouts for large file downloads.

Co-authored-by: Snider <631881+Snider@users.noreply.github.com>
2026-02-02 00:58:11 +00:00

184 lines
6.6 KiB
Go

package github
import (
"bytes"
"context"
"io"
"net/http"
"strings"
"testing"
"github.com/Snider/Borg/pkg/mocks"
)
func TestGetPublicRepos_Good(t *testing.T) {
t.Run("User Repos", func(t *testing.T) {
mockClient := mocks.NewMockClient(map[string]*http.Response{
"https://api.github.com/users/testuser/repos": {
StatusCode: http.StatusOK,
Header: http.Header{"Content-Type": []string{"application/json"}},
Body: io.NopCloser(bytes.NewBufferString(`[{"clone_url": "https://github.com/testuser/repo1.git"}]`)),
},
})
client := setupMockClient(t, mockClient)
repos, err := client.getPublicReposWithAPIURL(context.Background(), "https://api.github.com", "testuser")
if err != nil {
t.Fatalf("getPublicReposWithAPIURL for user failed: %v", err)
}
if len(repos) != 1 || repos[0] != "https://github.com/testuser/repo1.git" {
t.Errorf("unexpected user repos: %v", repos)
}
})
t.Run("Org Repos with Pagination", func(t *testing.T) {
mockClient := mocks.NewMockClient(map[string]*http.Response{
"https://api.github.com/users/testorg/repos": {
StatusCode: http.StatusNotFound, // Trigger fallback to org
Status: "404 Not Found",
Body: io.NopCloser(bytes.NewBufferString(`{}`)),
},
"https://api.github.com/orgs/testorg/repos": {
StatusCode: http.StatusOK,
Header: http.Header{"Content-Type": []string{"application/json"}, "Link": []string{`<https://api.github.com/organizations/123/repos?page=2>; rel="next"`}},
Body: io.NopCloser(bytes.NewBufferString(`[{"clone_url": "https://github.com/testorg/repo1.git"}]`)),
},
"https://api.github.com/organizations/123/repos?page=2": {
StatusCode: http.StatusOK,
Header: http.Header{"Content-Type": []string{"application/json"}},
Body: io.NopCloser(bytes.NewBufferString(`[{"clone_url": "https://github.com/testorg/repo2.git"}]`)),
},
})
client := setupMockClient(t, mockClient)
repos, err := client.getPublicReposWithAPIURL(context.Background(), "https://api.github.com", "testorg")
if err != nil {
t.Fatalf("getPublicReposWithAPIURL for org failed: %v", err)
}
if len(repos) != 2 || repos[0] != "https://github.com/testorg/repo1.git" || repos[1] != "https://github.com/testorg/repo2.git" {
t.Errorf("unexpected org repos: %v", repos)
}
})
}
func TestGetPublicRepos_Bad(t *testing.T) {
t.Run("Not Found", func(t *testing.T) {
mockClient := mocks.NewMockClient(map[string]*http.Response{
"https://api.github.com/users/testuser/repos": {
StatusCode: http.StatusNotFound,
Status: "404 Not Found",
Body: io.NopCloser(bytes.NewBufferString(`{"message": "Not Found"}`)),
},
"https://api.github.com/orgs/testuser/repos": {
StatusCode: http.StatusNotFound,
Status: "404 Not Found",
Body: io.NopCloser(bytes.NewBufferString(`{"message": "Not Found"}`)),
},
})
client := setupMockClient(t, mockClient)
_, err := client.getPublicReposWithAPIURL(context.Background(), "https://api.github.com", "testuser")
if err == nil {
t.Fatal("expected an error but got nil")
}
if !strings.Contains(err.Error(), "404 Not Found") {
t.Errorf("expected '404 Not Found' in error message, got %q", err)
}
})
t.Run("Invalid JSON", func(t *testing.T) {
mockClient := mocks.NewMockClient(map[string]*http.Response{
"https://api.github.com/users/badjson/repos": {
StatusCode: http.StatusOK,
Header: http.Header{"Content-Type": []string{"application/json"}},
Body: io.NopCloser(bytes.NewBufferString(`[{"clone_url": "invalid}`)),
},
})
client := setupMockClient(t, mockClient)
_, err := client.getPublicReposWithAPIURL(context.Background(), "https://api.github.com", "badjson")
if err == nil {
t.Fatal("expected an error for invalid JSON, but got nil")
}
})
}
func TestGetPublicRepos_Ugly(t *testing.T) {
t.Run("Empty Repo List", func(t *testing.T) {
mockClient := mocks.NewMockClient(map[string]*http.Response{
"https://api.github.com/users/empty/repos": {
StatusCode: http.StatusOK,
Header: http.Header{"Content-Type": []string{"application/json"}},
Body: io.NopCloser(bytes.NewBufferString(`[]`)),
},
})
client := setupMockClient(t, mockClient)
repos, err := client.getPublicReposWithAPIURL(context.Background(), "https://api.github.com", "empty")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(repos) != 0 {
t.Errorf("expected 0 repos, got %d", len(repos))
}
})
}
func TestFindNextURL_Good(t *testing.T) {
client := &githubClient{}
linkHeader := `<https://api.github.com/organizations/123/repos?page=2>; rel="next", <https://api.github.com/organizations/123/repos?page=1>; rel="prev"`
nextURL := client.findNextURL(linkHeader)
if nextURL != "https://api.github.com/organizations/123/repos?page=2" {
t.Errorf("unexpected next URL: %s", nextURL)
}
}
func TestFindNextURL_Bad(t *testing.T) {
client := &githubClient{}
linkHeader := `<https://api.github.com/organizations/123/repos?page=1>; rel="prev"`
nextURL := client.findNextURL(linkHeader)
if nextURL != "" {
t.Errorf("unexpected next URL for header with no 'next': %s", nextURL)
}
nextURL = client.findNextURL("")
if nextURL != "" {
t.Errorf("unexpected next URL for empty header: %s", nextURL)
}
}
func TestFindNextURL_Ugly(t *testing.T) {
client := &githubClient{}
// Malformed: missing angle brackets
linkHeader := `https://api.github.com/organizations/123/repos?page=2; rel="next"`
nextURL := client.findNextURL(linkHeader)
if nextURL != "" {
t.Errorf("unexpected next URL for malformed header: %s", nextURL)
}
}
func TestNewAuthenticatedClient_Good(t *testing.T) {
t.Setenv("GITHUB_TOKEN", "test-token")
client := NewAuthenticatedClient(context.Background(), http.DefaultClient)
if client == http.DefaultClient {
t.Error("expected an authenticated client, but got http.DefaultClient")
}
}
func TestNewAuthenticatedClient_Bad(t *testing.T) {
// Unset the variable to ensure it's not present
t.Setenv("GITHUB_TOKEN", "")
client := NewAuthenticatedClient(context.Background(), http.DefaultClient)
if client != http.DefaultClient {
t.Error("expected http.DefaultClient when no token is set, but got something else")
}
}
// setupMockClient is a helper function to inject a mock http.Client.
func setupMockClient(t *testing.T, mock *http.Client) *githubClient {
client := &githubClient{client: mock}
originalNewAuthenticatedClient := NewAuthenticatedClient
NewAuthenticatedClient = func(ctx context.Context, baseClient *http.Client) *http.Client {
return mock
}
// Restore the original function after the test
t.Cleanup(func() {
NewAuthenticatedClient = originalNewAuthenticatedClient
})
return client
}