Files
CLIProxyAPI/internal/runtime/executor/copilot_executor_refresh_test.go
hkfires d059cf2891 feat(copilot)!: add preset-based in-memory catalog
Add copilot presets to the registry models catalog and intersect
them with per-account /models capabilities, so usable credentials
get cold-start preset routes before discovery and account data
overrides limits, modalities, thinking and endpoint afterwards.

Replace the persisted metadata catalog with an immutable
in-memory Auth.CopilotCatalog with preset and unavailable
sentinels, preserving or revoking it across reloads, account
and origin changes, and re-resolving routes on each attempt.

Run discovery in a dedicated worker pool with per-account
transport, queued cancellation and skip-persist commits, gate
native-executor plugin models on catalog availability, and
remove login-time catalog fetch and embedded effort lists.

Add cmd/fetch_copilot_models to export the raw upstream /models
response atomically without modifying the stored credential.

BREAKING CHANGE: upstream-only Copilot models without a registry
preset are no longer routed, and the persisted copilot_catalog
metadata, ReadCatalog and string FetchCatalog APIs are removed
2026-11-21 17:11:35 +08:00

48 lines
1.9 KiB
Go

package executor
import (
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
"time"
"github.com/router-for-me/CLIProxyAPI/v8/internal/auth/copilot"
"github.com/router-for-me/CLIProxyAPI/v8/internal/config"
)
func TestCopilotRefreshOnlyExchangesCredentials(t *testing.T) {
for _, base := range []string{"https://api.individual.githubcopilot.com", "https://api.business.githubcopilot.com"} {
t.Run(base, func(t *testing.T) {
auth := copilotTestAuth(t, "a", "claude-opus-5", copilot.Chat)
auth.Metadata["refresh_token"] = "github-grant"
previous := auth.CopilotCatalog
calls := 0
ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", roundTripFunc(func(r *http.Request) (*http.Response, error) {
calls++
if r.URL.Path != "/copilot_internal/v2/token" || r.URL.Host != "api.github.com" || r.Header.Get("Authorization") != "Bearer github-grant" {
t.Fatalf("credential refresh must not fetch the directory: %s", r.URL.Path)
}
payload := fmt.Sprintf(`{"token":"replacement","expires_at":%d,"endpoints":{"api":%q}}`, time.Now().Add(time.Hour).Unix(), base)
return &http.Response{StatusCode: http.StatusOK, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(payload)), Request: r}, nil
}))
updated, err := NewCopilotExecutor(&config.Config{}).Refresh(ctx, auth)
if err != nil || calls != 1 || updated.Metadata["access_token"] != "replacement" {
t.Fatalf("refresh did not return the new token: %v (calls=%d)", err, calls)
}
if base == auth.Metadata["base_url"] {
if updated.CopilotCatalog != previous {
t.Fatal("same-origin refresh discarded the cached catalog")
}
} else if updated.CopilotCatalog != copilot.UnavailableCatalog {
t.Fatal("changed origin retained the old entitlement")
}
if auth.CopilotCatalog != previous || auth.Metadata["access_token"] == "replacement" {
t.Fatal("refresh mutated its input")
}
})
}
}