Files
magpie/internal/gui/providers_list_test.go
T
yetone 0f5eb941e7 providers: a custom provider's models can be picked from its vendor's list while it is added, before its first Save (Sun1090 on #578: 添加供应商的时候,希望添加可以获取全模型的按钮或者下拉框?像cc-switch那样,可以选) — the vendor's list, its chips and Refresh were only in a saved provider's editor; the custom provider's add form had a comma-separated Models field and the hint "magpie asks the vendor for its list after saving", so a user adding one had to save it, open it again and pick there, which nothing in the form said could be done sooner. The add form now has Fetch models (获取模型) beside its Models field: it posts the new provider/list with the form as it stands (the URL and key typed, its headers, models URL and proxy; no id), which builds the draft as detect does (from its preset when it has one, then typed) and asks the vendor with the new Provider.List — Fetch's own path (a NoList preset's models, Cline's feed, Kilo's list, fetchOne) without SaveLive, so nothing is kept — answering the models agents chat with, a model's name where it has one. The list shows as chips (a long one with a filter), each ticked one in the Models field and an id typed there ticking its chip, with "{n} of {all} picked", Pick all (Pick those shown under a filter) and Pick none above the chips, so a pick that reflows them moves no button; one typed by hand stays through Pick none. A vendor refusing says why under the field; no URL asks nothing. The Add sends the picks as the provider's models, as the field did. TestProviderListBeforeSave (the list asked with the key typed, an embedding model left out, no provider saved and no list kept, a 401 told, no URL refused) is new; provider-add-models.test.cjs (WebKit, en and zh: nothing asked without a URL, the post carries URL and key and no id, the refusal shown, chips with names, tick and type in sync, filter and Pick those shown / none / all, the Add's models, no click moving the page, no left-border accent) is new and passes; add-sheet, provider-detect, provider-detect-models, provider-typed and click-scroll pass in WebKit. go vet ./internal/..., go build (darwin GUI), go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME pass (TestFXInState's known TempDir-cleanup flake aside).
2026-10-02 23:48:00 +08:00

92 lines
2.7 KiB
Go

package gui
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// The add form's Fetch models (#578) asks the vendor for its list with the
// URL and key typed, before the provider is saved: the list of models
// agents chat with comes back, nothing is saved — no provider, no list
// kept — and a vendor refusing says why.
func TestProviderListBeforeSave(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
var mu sync.Mutex
var asked []string
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
asked = append(asked, r.Method+" "+r.URL.Path+" "+r.Header.Get("Authorization"))
mu.Unlock()
if r.Header.Get("Authorization") != "Bearer sk-typed" {
w.WriteHeader(401)
io.WriteString(w, `{"error":{"message":"invalid api key"}}`)
return
}
if r.URL.Path != "/v1/models" {
http.NotFound(w, r)
return
}
io.WriteString(w, `{"data":[{"id":"gpt-5.5"},{"id":"claude-sonnet-5"},{"id":"text-embedding-3"}]}`)
}))
defer up.Close()
mux := http.NewServeMux()
providerRoutes(mux, nil)
post := func(body string) *httptest.ResponseRecorder {
t.Helper()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/provider/list", strings.NewReader(body)))
return w
}
w := post(`{"typed":true,"name":"Relay","key":"sk-typed","chat":"` + up.URL + `/v1"}`)
if w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body)
}
var got struct {
Models []struct{ ID, Name string }
}
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
var ids []string
for _, m := range got.Models {
ids = append(ids, m.ID)
}
// the models agents chat with: an embedding model is left out
if s := strings.Join(ids, " "); s != "gpt-5.5 claude-sonnet-5" && s != "claude-sonnet-5 gpt-5.5" {
t.Fatalf("listed %s", s)
}
if ps := provider.All(); len(ps) != 0 {
t.Fatalf("list saved %v", ps)
}
for _, id := range []string{"", "relay"} {
if ms, _, ok := catalog.Live(id); ok || len(ms) != 0 {
t.Fatalf("list kept %q's: %v", id, ms)
}
}
if !strings.Contains(strings.Join(asked, "\n"), "GET /v1/models Bearer sk-typed") {
t.Fatalf("asked %v", asked)
}
// a key the vendor refuses: the form is told why
w = post(`{"typed":true,"key":"sk-wrong","chat":"` + up.URL + `/v1"}`)
if w.Code != 400 || !strings.Contains(w.Body.String(), "401") {
t.Fatalf("refused: %d %s", w.Code, w.Body)
}
// no URL: nothing to ask
if w = post(`{"typed":true,"key":"sk-typed"}`); w.Code != 400 {
t.Fatalf("no URL: %d %s", w.Code, w.Body)
}
}