Files
magpie/internal/library/github_test.go
T
yetone 0f044e9447 plugins: a GITHUB_TOKEN GitHub refuses is dropped for anonymous requests, not shown as an error (till on Discord)
till's Plugins page said "无法连接 GitHub: GitHub refused GITHUB_TOKEN
(no GitHub token is set in Settings → Network and sharing)". ghtoken.Token
falls back to the environment's GITHUB_TOKEN / GH_TOKEN, and a stale one
there made every GitHub read fail, though GitHub serves the same reads
without a token.

ghtoken.Do now sends a GitHub API request with the token. When the token
came from the environment and GitHub answers 401, it remembers that token
as refused for the rest of the process (Token() skips it, so GH_TOKEN is
still tried after a refused GITHUB_TOKEN, and Settings no longer shows it)
and asks once more without Authorization, returning that answer. A token
set in Settings is never dropped: its 401 is still Refused()'s error. An
anonymous rate limit after the drop is said by LimitedBy as before.

Every caller goes through it: plugin fetchJSONFrom (tagged-repo search,
git plugin checks, githubHead) and getURLFrom (Bun), and library apiGet,
skills fetch, skilltree fetchBlob and latestRTK via doGitHub.
internal/davsync/github.go stays strict: its token is the one the user gave
sync to write a private repo, outside ghtoken, and an anonymous retry can't
write there, so it would only hide why sync failed.

Tests (httptest only): ghtoken_test covers an env token refused then asked
again without, not sent again (and GH_TOKEN tried next), a Settings token
refused stays an error, an env token refused then an anonymous rate limit
is reported, and another host untouched. plugin TestTaggedReposStaleEnvTokenDropped
is till's case: TaggedRepos with GITHUB_TOKEN=stale-till lists 7 repos, no
error. github_token_test and library TestGitHubRateLimitSaid now expect the
env token dropped and the Settings token still refused.

Broken by hand, each red: (a) no anonymous retry: every new env test, the
plugin and library tests red, and TestTaggedReposStaleEnvTokenDropped shows
till's exact error; (b) no memory of the refusal: NotSentAgain, the plugin
and library tests red; (c) a Settings token also retried:
SettingsTokenRefusedStaysAnError, the plugin and library tests red.

Run on the commit before the rebase (base 9b04c4e20):
- gofmt -l: clean. go vet ./... on darwin, GOOS=linux -tags nogui, GOOS=windows: clean.
- set -o pipefail; go test -timeout 45m ./... at load ~30: 62 packages ok.
  agent, gateway and library ran past 45m while still progressing; gui
  failed TestSetAnswersWhileDevinAndProvidersHang (set took 10.26s, #1375)
  and TestMainWindowCorners (measuring killed, #1936/#2645); provider failed
  TestPluginPicksComeBackWithTheirList (#3051, fixed on main in c922225f1).
- Each rerun alone at load 21-56: library ok 809.7s, provider ok 308.8s,
  gui ok 159.5s, agent ok 1673.3s, gateway ok 780.4s.

After rebasing onto a3ebed202 (main moved 8 commits): gofmt -l clean; go vet
darwin, linux -tags nogui, windows clean; go test -count=1 at load ~57:
ghtoken ok 2.9s, plugin ok 277.6s, library ok 144.2s.
2026-10-21 06:16:31 +08:00

275 lines
9.0 KiB
Go

package library
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"github.com/yetone/magpie/internal/settings"
)
// fakeGitHub stands in for GitHub's API and codeload: it notes the
// Authorization each was asked with, and answers the API as answer says.
type fakeGitHub struct {
mu sync.Mutex
apiAuth []string // the API's requests' Authorization, in order
tarAuth []string // codeload's
ifNone []string // the API's requests' If-None-Match
answer func(w http.ResponseWriter, r *http.Request) bool
}
func newFakeGitHub(t *testing.T) *fakeGitHub {
t.Helper()
g := &fakeGitHub{}
files := map[string]string{"skills/pdf/SKILL.md": "---\nname: pdf\ndescription: PDFs\n---\n"}
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
g.mu.Lock()
g.apiAuth = append(g.apiAuth, r.Header.Get("Authorization"))
g.ifNone = append(g.ifNone, r.Header.Get("If-None-Match"))
answer := g.answer
g.mu.Unlock()
if answer != nil && answer(w, r) {
return
}
fmt.Fprint(w, `[{"sha":"p1","commit":{"message":"Change","committer":{"date":"2026-09-01T10:00:00Z"}}}]`)
}))
tar := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
g.mu.Lock()
g.tarAuth = append(g.tarAuth, r.Header.Get("Authorization"))
g.mu.Unlock()
w.Write(tarball(t, files))
}))
oldT, oldA := tarballURL, githubAPI
tarballURL = func(repo, ref string) string { return tar.URL + "/tar/" + repo }
githubAPI = api.URL
t.Cleanup(func() {
tarballURL, githubAPI = oldT, oldA
api.Close()
tar.Close()
})
return g
}
func (g *fakeGitHub) lastAPIAuth() string {
g.mu.Lock()
defer g.mu.Unlock()
if len(g.apiAuth) == 0 {
return "(not asked)"
}
return g.apiAuth[len(g.apiAuth)-1]
}
func setGitHubToken(t *testing.T, tok string) {
t.Helper()
s := settings.Load()
s.GitHubToken = tok
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
}
// Each test installs from a repository of its own: InstallSkills keeps
// what it found of one by name (probes), for the other tests too.
// The API is asked with the token set in Settings, else GITHUB_TOKEN's,
// else GH_TOKEN's, and with none when there is none; codeload never gets it.
func TestGitHubTokenSent(t *testing.T) {
sandbox(t)
g := newFakeGitHub(t)
ok(t)(InstallSkills("owner/ghtoken-sent", []string{"skills/pdf"}, []string{"claude"}))
for _, c := range []struct {
name, setting, github, gh, want string
}{
{"none", "", "", "", ""},
{"GH_TOKEN", "", "", "gh-env", "Bearer gh-env"},
{"GITHUB_TOKEN", "", "github-env", "gh-env", "Bearer github-env"},
{"settings", "ghp_settings", "github-env", "gh-env", "Bearer ghp_settings"},
} {
t.Setenv("GITHUB_TOKEN", c.github)
t.Setenv("GH_TOKEN", c.gh)
setGitHubToken(t, c.setting)
got := check(t)
if got["pdf"].Status == "unknown" {
t.Errorf("%s: %+v", c.name, got["pdf"])
}
if a := g.lastAPIAuth(); a != c.want {
t.Errorf("%s: the API was asked with Authorization %q, want %q", c.name, a, c.want)
}
}
g.mu.Lock()
defer g.mu.Unlock()
if len(g.tarAuth) == 0 {
t.Fatal("codeload was never asked")
}
for _, a := range g.tarAuth {
if a != "" {
t.Errorf("codeload was sent Authorization %q", a)
}
}
}
// The token goes to GitHub's API's host alone.
func TestGitHubTokenOnlyToTheAPI(t *testing.T) {
sandbox(t)
t.Setenv("GITHUB_TOKEN", "secret-token")
for u, want := range map[string]bool{
"https://api.github.com/repos/a/b/commits": true,
"https://api.github.com/repos/rtk-ai/rtk/releases/latest": true,
"https://codeload.github.com/a/b/tar.gz/HEAD": false,
"https://raw.githubusercontent.com/a/b/main/x": false,
"https://api.github.com.evil.example/repos": false,
"https://example.com/api.github.com": false,
"http://127.0.0.1:3425/v1/models": false,
} {
req, _ := http.NewRequest("GET", u, nil)
if got := withGitHubToken(req); got != want || (req.Header.Get("Authorization") != "") != want {
t.Errorf("%s: sent %v (%q), want %v", u, got, req.Header.Get("Authorization"), want)
}
}
// a redirect off GitHub's API drops it, as Go's client does for
// another host
var other string
elsewhere := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
other = r.Header.Get("Authorization")
fmt.Fprint(w, "[]")
}))
defer elsewhere.Close()
g := newFakeGitHub(t)
g.answer = func(w http.ResponseWriter, r *http.Request) bool {
http.Redirect(w, r, strings.Replace(elsewhere.URL, "127.0.0.1", "localhost", 1)+"/x", http.StatusFound)
return true
}
lastCommit(Source{Kind: "github", Repo: "a/b"})
if g.lastAPIAuth() != "Bearer secret-token" {
t.Errorf("the API was asked with %q", g.lastAPIAuth())
}
if other != "" {
t.Errorf("the host redirected to was sent Authorization %q", other)
}
}
// Rate limited, the check says so plainly: until when, and, without a
// token, that one in Settings raises the limit; the page is told the same.
func TestGitHubRateLimitSaid(t *testing.T) {
sandbox(t)
g := newFakeGitHub(t)
ok(t)(InstallSkills("owner/ghtoken-limited", []string{"skills/pdf"}, []string{"claude"}))
reset := time.Date(2030, 1, 2, 3, 4, 0, 0, time.UTC)
g.answer = func(w http.ResponseWriter, r *http.Request) bool {
w.Header().Set("X-RateLimit-Remaining", "0")
w.Header().Set("X-RateLimit-Reset", fmt.Sprint(reset.Unix()))
w.WriteHeader(403)
fmt.Fprint(w, `{"message":"API rate limit exceeded for 1.2.3.4."}`)
return true
}
at := reset.Local().Format("15:04")
c := check(t)["pdf"]
if c.Status != "unknown" || c.Limited == nil || c.Limited.Token || c.Limited.Until != "2030-01-02T03:04:00Z" {
t.Fatalf("without a token: %+v %+v", c, c.Limited)
}
for _, w := range []string{"60 an hour", "until " + at, "GitHub token in Settings → Network and sharing", "5,000"} {
if !strings.Contains(c.Error, w) {
t.Errorf("without a token, %q doesn't say %q", c.Error, w)
}
}
setGitHubToken(t, "ghp_x")
c = check(t)["pdf"]
if c.Limited == nil || !c.Limited.Token || !strings.Contains(c.Error, "your GitHub token") || !strings.Contains(c.Error, "until "+at) ||
strings.Contains(c.Error, "Add a GitHub token") {
t.Errorf("with a token: %+v", c)
}
if strings.Contains(c.Error, "ghp_x") {
t.Errorf("the token is in the error: %q", c.Error)
}
// a secondary limit: 403 with Retry-After
g.answer = func(w http.ResponseWriter, r *http.Request) bool {
w.Header().Set("Retry-After", "120")
w.WriteHeader(403)
fmt.Fprint(w, `{"message":"You have exceeded a secondary rate limit."}`)
return true
}
c = check(t)["pdf"]
if c.Limited == nil || c.Limited.Until == "" {
t.Errorf("secondary limit: %+v %+v", c, c.Limited)
}
// a token GitHub refuses says which one when it is Settings'
g.answer = func(w http.ResponseWriter, r *http.Request) bool {
if r.Header.Get("Authorization") == "" {
return false
}
w.WriteHeader(401)
fmt.Fprint(w, `{"message":"Bad credentials"}`)
return true
}
c = check(t)["pdf"]
if c.Limited != nil || c.Error != "GitHub refused the GitHub token in Settings → Network and sharing: it may have expired or been revoked" {
t.Errorf("refused: %+v", c)
}
// one from the environment (till on Discord: a stale GITHUB_TOKEN)
// is dropped: the check is asked again without it, and isn't sent
// again
setGitHubToken(t, "")
t.Setenv("GH_TOKEN", "bad-env-skills")
g.mu.Lock()
n := len(g.apiAuth)
g.mu.Unlock()
c = check(t)["pdf"]
if c.Error != "" || c.Limited != nil || c.Commit == "" {
t.Errorf("refused from the environment: %+v", c)
}
g.mu.Lock()
if a := g.apiAuth[n:]; len(a) < 2 || a[0] != "Bearer bad-env-skills" || a[1] != "" {
t.Errorf("refused from the environment: GitHub was asked with %q, want the token then none", a)
}
n = len(g.apiAuth)
g.mu.Unlock()
c = check(t)["pdf"]
if c.Error != "" || c.Commit == "" {
t.Errorf("refused from the environment, checked again: %+v", c)
}
g.mu.Lock()
defer g.mu.Unlock()
for _, a := range g.apiAuth[n:] {
if a != "" {
t.Errorf("the refused GH_TOKEN was sent again: %q", g.apiAuth[n:])
break
}
}
}
// A folder asked about again is asked with the ETag GitHub gave, and its
// 304, which doesn't count against the limit, answers as the first did.
func TestGitHubConditionalRequests(t *testing.T) {
sandbox(t)
g := newFakeGitHub(t)
ok(t)(InstallSkills("owner/ghtoken-etag", []string{"skills/pdf"}, []string{"claude"}))
g.answer = func(w http.ResponseWriter, r *http.Request) bool {
if r.Header.Get("If-None-Match") == `"v1"` {
w.WriteHeader(http.StatusNotModified)
return true
}
w.Header().Set("ETag", `"v1"`)
fmt.Fprint(w, `[{"sha":"e1","commit":{"message":"Etagged","committer":{"date":"2026-09-01T10:00:00Z"}}}]`)
return true
}
first := check(t)["pdf"]
second := check(t)["pdf"]
if first.Commit != "e1" || second.Commit != "e1" || second.Message != "Etagged" || second.Status != first.Status {
t.Errorf("first %+v, second %+v", first, second)
}
g.mu.Lock()
defer g.mu.Unlock()
if n := len(g.ifNone); n < 2 || g.ifNone[0] != "" || g.ifNone[n-1] != `"v1"` {
t.Errorf("If-None-Match sent: %q", g.ifNone)
}
}