Files
magpie/internal/provider/copilot_entitlement_test.go
T
yetone 9f94b2156b copilot: an account on GitHub Enterprise Cloud with data residency (*.ghe.com) signs in, is read from copilot login --host and is served at its enterprise (#723, guitaoliu: magpie skipped a Copilot CLI sign-in whose host wasn't github.com and its own sign-in was fixed to github.com) — a Copilot account now remembers its host (copilotApp.Host, "host" in its logins.json auth; none for github.com) and every URL it uses follows it, as VS Code's Copilot Chat, copilot.lua and avante do (checked against the issue's references and GitHub's docs): the device flow at https://<name>.ghe.com/login/device/code and /login/oauth/access_token with the same Copilot OAuth app, the device page there, whose account at https://api.<name>.ghe.com/user, its plan and usage at api.<name>.ghe.com/copilot_internal/user, its session token at api.<name>.ghe.com/copilot_internal/v2/token, and its requests, model list, Auto and terms at the session's endpoints.api, or https://copilot-api.<name>.ghe.com when it names none (the provider's base too). A github.com account has no host and goes where it always did, the same vars and the same bytes. CopilotHost (copilot_ghe.go) takes only github.com or one <name>.ghe.com label (a scheme and a trailing slash are dropped; a port, path, user, deeper subdomain or any other host is refused), in the sign-in, the CLI and a host read back from logins.json, so a GitHub token is never sent to a host of the user's choosing. The Copilot CLI's sign-in: a github.com account first, as before; then one copilot login --host <name>.ghe.com signed in, its token under "https://<name>.ghe.com:<login>" in copilotTokens or the keychain; a GitHub Enterprise Server host is still left out. An editor's apps.json entry keyed "<name>.ghe.com:…" is read after the editors' github.com one and the CLI's. GUI: a Copilot device code's panel has "Account on GHE.com?" (zh 账号在 GHE.com 上?), which ends that sign-in and asks "Your enterprise on GHE.com" (你的企业在 GHE.com 上的地址) in a field (acme.ghe.com; Sign in stays off until it reads as <name>.ghe.com), then signs in there; that sign-in's panel offers "Sign in on github.com instead" (改为在 github.com 登录), as does a failed one. CLI: magpie accounts add copilot [--host <name>.ghe.com]. TestCopilotHost, TestCopilotGHESignIn (an httptest server standing in for acme.ghe.com, api.acme.ghe.com and copilot-api.acme.ghe.com through copilotGHEOrigin: sign-in, what is kept, models, a signed request, usage, nothing sent to github.com, a bad host refused) and TestCopilotCLIGHELogin are new and fail on the old code; copilot-ghe.test.cjs (webkit and chromium, en and zh: the link, the field refusing evil.com, a.b.ghe.com, ports and the like, the posted site, the way back, cancels, no native select, no left-border accent) fails on the old code. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass; signin-link, signin-close, signin-again and routing-signin pass in webkit, signin-callback fails before and after the change alike. Not tried with a real GHE.com account: the URLs follow the references, and the CLI's token key for a ghe.com host is taken to be the github.com one's form. Accounts are still told apart by login alone; an EMU login carries its enterprise's suffix.
2026-10-04 06:56:04 +08:00

138 lines
4.7 KiB
Go

package provider
import (
"context"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
func TestCopilotEntitlementLabels(t *testing.T) {
for _, x := range []struct{ plan, sku, want string }{
{"individual", "free_educational_quota", "Education"},
{"free", "free_educational_quota", "Education"},
{"individual", "unknown-sku", "Pro"},
{"individual", "free_limited_copilot", "Free"},
{"individual_pro", "", "Pro+"}, {"business", "", "Business"},
{"enterprise", "", "Enterprise"}, {"free", "", "Free"},
{"", "free_limited_copilot", "Free"}, {"", "unknown-sku", ""},
{"future_plan", "future-sku", "Future_plan"}, {"", "", ""},
} {
e := copilotEntitlement{Plan: x.plan, AccessSKU: x.sku}
if got := e.label(); got != x.want {
t.Errorf("%+v: %q, want %q", e, got, x.want)
}
}
}
func TestCopilotEntitlementRefresh(t *testing.T) {
signIn(t)
if err := addCopilotLogin("hubot", "Pro", "gho_hubot", ""); err != nil {
t.Fatal(err)
}
copilotLoginList() // remember the editor's own account
for _, app := range []copilotApp{{User: "hubot", Token: "gho_hubot"}, {User: "octocat", Token: "gho_x"}} {
refreshCopilotEntitlement(app, "Education", "free_educational_quota")
var found bool
for _, l := range readLogins() {
if l.Agent == "copilot" && l.User == app.User {
found = l.Plan == "Education" && l.AccessSKU == "free_educational_quota"
}
}
if !found {
t.Fatalf("entitlement not refreshed for %s", app.User)
}
before, _ := os.ReadFile(loginsPath())
refreshCopilotEntitlement(app, "Education", "free_educational_quota")
refreshCopilotEntitlement(copilotApp{User: app.User, Token: "replaced-token"}, "Enterprise", "new")
after, _ := os.ReadFile(loginsPath())
if string(before) != string(after) {
t.Fatal("unchanged or stale credentials changed login")
}
}
}
func TestCopilotReplacementClearsSKU(t *testing.T) {
signIn(t)
if err := addCopilotLogin("hubot", "Education", "old", ""); err != nil {
t.Fatal(err)
}
refreshCopilotEntitlement(copilotApp{User: "hubot", Token: "old"}, "Education", "free_educational_quota")
if err := addCopilotLogin("hubot", "Pro", "new", ""); err != nil {
t.Fatal(err)
}
for _, l := range readLogins() {
if l.User == "hubot" && (l.Plan != "Pro" || l.AccessSKU != "") {
t.Fatal("replacement retained prior entitlement")
}
}
}
func TestCopilotOwnEntitlementSwitch(t *testing.T) {
signIn(t)
copilotLoginList()
refreshCopilotEntitlement(copilotApp{User: "octocat", Token: "gho_x"}, "Education", "free_educational_quota")
sideLogins("copilot", "another-user", func(savedLogin) bool { return false })
for _, l := range readLogins() {
if l.Agent == "copilot" && l.own() && (l.Plan != "" || l.AccessSKU != "") {
t.Fatal("new editor account inherited previous entitlement")
}
}
}
func TestCopilotEducationQuotaRefresh(t *testing.T) {
signIn(t)
if err := addCopilotLogin("hubot", "Pro", "gho_hubot", ""); err != nil {
t.Fatal(err)
}
fail := false
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if fail {
w.WriteHeader(503)
return
}
w.Write([]byte(`{"copilot_plan":"individual","access_type_sku":"free_educational_quota","quota_snapshots":{"chat":{"unlimited":true},"completions":{"unlimited":true},"premium_interactions":{"has_quota":true,"entitlement":200,"quota_remaining":200}}}`))
}))
defer srv.Close()
old := CopilotUserURL
CopilotUserURL = srv.URL
defer func() { CopilotUserURL = old }()
q := loginQuota(context.Background(), Login{Agent: "copilot", User: "hubot"})
if q.Error != "" || q.Plan != "Education" || q.AccessSKU != "free_educational_quota" || len(q.Windows) != 3 {
t.Fatalf("quota: %+v", q)
}
if q.Windows[0].Name != "Premium requests" {
t.Fatalf("counted quota not first: %+v", q.Windows)
}
for _, w := range q.Windows[1:] {
if !w.Unlimited || w.Display != "Unlimited" || w.Used != 0 || !w.Aside {
t.Fatalf("unlimited: %+v", w)
}
}
fail = true
q = loginQuota(context.Background(), Login{Agent: "copilot", User: "hubot"})
if q.Error == "" {
t.Fatal("expected refresh failure")
}
for _, l := range readLogins() {
if l.User == "hubot" && l.Plan != "Education" {
t.Fatal("failed read erased plan")
}
}
}
func TestCopilotOwnEntitlementWithoutUser(t *testing.T) {
signIn(t)
writeFile(t, filepath.Join(copilotConfigDir(), "github-copilot", "apps.json"), map[string]any{"github.com": map[string]any{"oauth_token": "gho_x"}})
ls := copilotLoginList()
if len(ls) != 1 || ls[0].User != "GitHub" {
t.Fatalf("logins %+v", ls)
}
refreshCopilotEntitlement(copilotApp{User: "GitHub", Token: "gho_x"}, "Free", "free_limited_copilot")
if got := copilotLoginList()[0].Plan; got != "Free" {
t.Fatalf("plan %q", got)
}
}