Files
magpie/internal/gui/providers_list_test.go
T
yetone acde144a7a providers: a vendor of embedding and rerank models alone is detected on /embeddings and /rerank (heatonz)
heatonz on Discord (#help, 11-05): a custom provider whose models are only
embedding and rerank models couldn't be added. Detect APIs ("检测协议")
picked the first listed model, an embedding model, and asked it to chat on
every API, so nothing answered.

- tiny: an embedding or rerank model (catalog.ServesOf) on Chat is asked
  where the gateway's retrieve sends it: the OpenAI base's /embeddings
  ({"model","input"}) or /rerank ({"model","query","documents"}). tested
  adds no extra body there, as the gateway sends none.
- detectModel asks each API a model that chats when the list has one, so
  a chat vendor's detection is unchanged. A retrieval model is a last
  resort on Chat only. Responses, Anthropic and Gemini say why they weren't
  asked (DetectModels too).
- testModel: a saved provider with nothing to chat with tests on a
  retrieval model its list keeps apart (LiveServing). testOne asks such a
  model on Chat and never moves it to another API.
- provider.ListAll is List with Serves models kept. /api/provider/list and
  the provider's JSON carry a `retrieval` count. The add form's Fetch models
  and the saved editor say "Only embedding and rerank models are listed.
  They need no pick: ask them on /v1/embeddings and /v1/rerank." in place
  of "the list is empty". One new string, in zh, zh-TW, ja and de.
- app.js stays under TestCoreScriptBudget with no raise: +42 bytes, paid
  for by two add-form comments cut.
- The gateway already routes /v1/embeddings and /v1/rerank to such a
  provider (#765, resolveRetrievalModel by prefix or KindGroup), so nothing
  changed there.

Run (mote, at this tree on origin/main 60fe6645a):
- go test ./internal/provider ./internal/catalog: ok.
- go test ./internal/gui: ok, except TestMainWindowCorners/macOS_26_SDK,
  which is #3949 (mote is macOS 27).
- go build ./..., go vet (darwin), GOOS=linux go vet -tags nogui and
  GOOS=windows go vet of provider and gui: ok. gofmt -l: clean.
- node --test, chromium and webkit: provider-retrieval-only (new: en, zh,
  zh-TW, ja, de, with and without retrieval models, add form and saved
  editor), provider-detect, provider-add-models, azure-endpoint,
  provider-listed-as, provider-add-gemini, i18n-en-no-cjk and i18n-keys.
  94 of 95 pass. The red is i18n-keys "a plugin's words are its own", red
  on origin/main 60fe6645a too (#5332/#5327).

Broken by hand:
- New tests on origin/main's code. TestDetectEmbeddingOnly red ("The
  model BAAI/bge-m3 does not support chat" on chat, 404 on the rest).
  TestTestEmbeddingOnly red ("no model to try"). TestProviderListBeforeSave
  red (retrieval = 0).
- Retrieval models not kept out of chat probes: the mixed-list case is red
  (bge-m3 asked beside glm-5).
- tested adding the extra body to /embeddings: red (prompt_cache_key
  sent).
- detectOne asking non-Chat APIs with a retrieval model: red (404 on
  /v1/responses).
- testModel's LiveServing fallback off: TestTestEmbeddingOnly red.
- provider-retrieval-only on the old app.js with the new i18n.js: the 5
  retrieval cases red, the 5 empty-list ones green.

Not tried:
- A real embedding/rerank vendor (heatonz's is unknown). Fake httptest
  servers only.
- The whole GUI suite.
- Windows and Linux test runs (vet only).
- Models whose ids don't say embed, bge- or rerank (gte-, e5-, m3e-,
  text2vec-, voyage-) are still taken as chat models. The /embeddings probe
  goes by the id, and a Xinference model_type isn't read.
2026-11-06 05:21:05 +08:00

97 lines
2.9 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 }
Retrieval int
}
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
// counted apart, for a list of them alone to say so (heatonz)
if got.Retrieval != 1 {
t.Fatalf("retrieval = %d, want 1: %s", got.Retrieval, w.Body)
}
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)
}
}