Files
skillshare/internal/install/github_download_test.go
T
Willie b014f8c6dc feat: optimize large-repo install and update flows
Implement Issue #46 across install/update paths to reduce perceived hangs and improve subdir installation performance on large repositories.

Main changes:
- Add git progress streaming primitives in internal/install/git_progress.go.
  - runGitCommandWithProgress supports stderr streaming callbacks in TTY mode.
  - scanGitProgress handles both CR and LF progress delimiters.
- Thread InstallOptions.OnProgress through clone/pull/discovery/install/update paths.
  - Keep non-TTY behavior quiet to avoid noisy logs.
  - Add TreeSpinner.Update for live message updates.
- Add progress-aware git update APIs in internal/git/info.go:
  - PullWithProgress and ForcePullWithProgress (keep PullWithEnv/ForcePullWithEnv for compatibility).
- Introduce shared GitHub client utilities in internal/github/client.go.
  - Centralize token lookup, request construction, and rate-limit handling.
  - Refactor internal/search/search.go to use shared client while preserving compatibility aliases.
- Add GitHub/GHE API subdir fast path in internal/install/github_download.go.
  - github.com => api.github.com
  - github-like enterprise hosts => https://<host>/api/v3
  - On API failure, emit warning and fallback to git-based flows.
- Add sparse checkout fallback for non-GitHub subdir sources in internal/install/sparse_checkout.go.
  - Use filter+no-checkout+depth=1 then sparse-checkout set + checkout.
- Extend discovery/install metadata plumbing.
  - DiscoveryResult now includes CommitHash and Warnings.
  - install/discover subdir paths preserve warnings and commit hashes from API/sparse/full-clone flows.
- Preserve compatibility and UX expectations.
  - Keep existing action strings (for tests/UI stability).
  - Keep legacy discover/pull APIs as wrappers around new progress-aware variants.
- Improve --track download efficiency.
  - Track clone now attempts --filter=blob:none --depth 1 --single-branch first.
  - Falls back to full clone only on remote capability mismatch, not on auth/not-found errors.

CLI wiring:
- cmd/skillshare/install.go
  - TTY handlers now pass progress callbacks and use with-progress discover APIs.
  - Subdir handler shows API-vs-clone stage messages and renders discovery fallback warnings.
- cmd/skillshare/update.go and cmd/skillshare/update_project.go
  - Use PullWithProgress/ForcePullWithProgress for tracked repos.
  - Ensure reinstall/update install flows forward progress callbacks.

Tests added/updated:
- internal/install/git_progress_test.go
- internal/install/github_download_test.go
- internal/install/sparse_checkout_test.go
- internal/install/tracked_clone_test.go
- internal/install/source_test.go (GitHubOwner/GitHubRepo coverage)
- internal/git/info_test.go (PullWithProgress/ForcePullWithProgress)
- tests/integration/install_subdir_optimization_test.go
- tests/integration/install_online_test.go (GitHub API subdir case)

Validation performed:
- go test ./internal/install -count=1
- go test ./internal/git -run 'Test.*Pull.*' -count=1
- go build -o bin/skillshare ./cmd/skillshare && SKILLSHARE_TEST_BINARY="/Users/williehung/Developer/Apps/github/skillshare/bin/skillshare" go test ./tests/integration -run 'TestInstall_Subdir|TestUpdate_' -count=1
- go build -o bin/skillshare ./cmd/skillshare && SKILLSHARE_TEST_BINARY="/Users/williehung/Developer/Apps/github/skillshare/bin/skillshare" go test ./tests/integration -run 'TestInstall_Track_|TestUpdate_TrackedRepo' -count=1
- go test -tags online ./tests/integration -run TestInstall_GitHubSubdirViaAPI -count=1
- make lint
- make test
2026-02-25 07:02:54 +08:00

140 lines
3.8 KiB
Go

package install
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
ghclient "skillshare/internal/github"
)
func TestDownloadGitHubDirWithAPIBase_Recursive(t *testing.T) {
var server *httptest.Server
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/v3/repos/acme/repo/contents/skills/bundle":
fmt.Fprint(w, `[
{"type":"file","name":"SKILL.md","path":"skills/bundle/SKILL.md","download_url":"`+server.URL+`/raw/skill"},
{"type":"dir","name":"child","path":"skills/bundle/child"}
]`)
case "/api/v3/repos/acme/repo/contents/skills/bundle/child":
fmt.Fprint(w, `[
{"type":"file","name":"README.md","path":"skills/bundle/child/README.md","download_url":"`+server.URL+`/raw/readme"}
]`)
case "/api/v3/repos/acme/repo/commits":
fmt.Fprint(w, `[{"sha":"1234567890abcdef"}]`)
case "/raw/skill":
fmt.Fprint(w, "# skill")
case "/raw/readme":
fmt.Fprint(w, "child")
default:
http.NotFound(w, r)
}
}))
defer server.Close()
dest := t.TempDir()
source := &Source{CloneURL: "https://github.acme.com/acme/repo.git"}
commit, err := downloadGitHubDirWithAPIBase("acme", "repo", "skills/bundle", dest, server.URL+"/api/v3", source, nil)
if err != nil {
t.Fatalf("downloadGitHubDirWithAPIBase() error = %v", err)
}
if commit != "1234567" {
t.Fatalf("commit = %q, want %q", commit, "1234567")
}
skillPath := filepath.Join(dest, "SKILL.md")
readmePath := filepath.Join(dest, "child", "README.md")
if _, err := os.Stat(skillPath); err != nil {
t.Fatalf("missing %s: %v", skillPath, err)
}
if _, err := os.Stat(readmePath); err != nil {
t.Fatalf("missing %s: %v", readmePath, err)
}
}
func TestDownloadGitHubDirWithAPIBase_404(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
}))
defer server.Close()
dest := t.TempDir()
source := &Source{CloneURL: "https://github.acme.com/acme/repo.git"}
_, err := downloadGitHubDirWithAPIBase("acme", "repo", "skills/missing", dest, server.URL+"/api/v3", source, nil)
if err == nil {
t.Fatal("expected error, got nil")
}
if !strings.Contains(err.Error(), "returned 404") {
t.Fatalf("error = %v, want status 404", err)
}
}
func TestDownloadGitHubDirWithAPIBase_RateLimit(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-RateLimit-Limit", "60")
w.Header().Set("X-RateLimit-Remaining", "0")
w.WriteHeader(http.StatusForbidden)
}))
defer server.Close()
dest := t.TempDir()
source := &Source{CloneURL: "https://github.acme.com/acme/repo.git"}
_, err := downloadGitHubDirWithAPIBase("acme", "repo", "skills/bundle", dest, server.URL+"/api/v3", source, nil)
if err == nil {
t.Fatal("expected error, got nil")
}
var rl *ghclient.RateLimitError
if !errors.As(err, &rl) {
t.Fatalf("error = %T %v, want RateLimitError", err, err)
}
}
func TestGitHubAPIBase(t *testing.T) {
tests := []struct {
name string
source *Source
want string
wantErr bool
}{
{
name: "github.com",
source: &Source{CloneURL: "https://github.com/acme/repo.git"},
want: "https://api.github.com",
},
{
name: "ghe",
source: &Source{CloneURL: "https://github.acme.com/acme/repo.git"},
want: "https://github.acme.com/api/v3",
},
{
name: "non-github host",
source: &Source{CloneURL: "https://gitlab.com/acme/repo.git"},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := gitHubAPIBase(tt.source)
if tt.wantErr {
if err == nil {
t.Fatal("expected error, got nil")
}
return
}
if err != nil {
t.Fatalf("gitHubAPIBase() error = %v", err)
}
if got != tt.want {
t.Fatalf("gitHubAPIBase() = %q, want %q", got, tt.want)
}
})
}
}