Files
magpie/internal/gui/providers_detect_draft_test.go
T
yetone 7a9cf4dd4d gui, provider: Detect APIs and Each picked model ask with the form as it stands before any Save, and say what is missing in place (#1545, thedavidweng)
What the user saw: on a custom provider being added (or a Duplicate not
added yet), "Each picked model" said "Pick models first" although models
were typed in the Models field, and on a relay with no model list "Detect
APIs" said "no model to try" on every API. Both worked only after a Save,
when the models became the provider's own. A Duplicate not added sent no
key at all. Hints for a missing URL or models went to the toast at the top.

Why: chosenIds() reads a saved provider's chips only, not a new one's
draft.extra; Detect picked its model from the saved provider's list or the
vendor's, never from the form; an unsaved copy posted no id, so the server
had no key to fill in.

What changed:
- provider.Detect takes the form's picks (picked ...string) and tries one
  of them per API before the saved list or the vendor's.
- /api/provider/detect takes `picked`, and for `new` + `copyOf` fills
  what the draft left empty from the copied provider, as a saved edit
  fills from its id. It still only reads: nothing is written, no provider
  is added, no route or health check changes. No new route; magpie web's
  key guard is the same.
- detectAPIs sends the add form's models (draft.extra) for a new
  provider, copyOf for an unsaved copy, and says a missing URL, missing
  models, a refusal, and "none answered, and no API key is typed" inline
  under the buttons (amber/red text, no border). One new string in zh,
  zh-TW, ja, de.
- docs/subsystems/providers-accounts.md: the detection row says so.

What was run:
- TestProviderDetectDraft (new): new draft with picked models on a relay
  with no list, Each picked model, saved provider with a masked key and an
  unsaved pick, an unsaved copy with no key typed and with a key typed,
  config folder bytes unchanged, provider.All unchanged, magpie web without
  its key -> 401 and the vendor never asked. Fails on the old detect.go /
  providers.go ("no model to try").
- provider-detect-draft.test.cjs (new): chromium + webkit, en/zh/zh-TW/ja/
  de, 900 and 360px, 20/20 pass; no scroll on click, no <select>, no
  left-border, no sideways scroll, nothing but detect posted. Fails on the
  old app.js (no inline hint).
- provider-detect, provider-detect-models, provider-add-models,
  duplicate-pause, i18n-keys: 29/29, both engines.
- go test -tags nogui ./internal/gui/ ./internal/provider/ ok; gofmt -l
  clean; GOOS=linux and GOOS=windows go vet -tags nogui ok.
- Real use: sandbox `magpie web` (nogui build) with a fake upstream. New
  provider, relay with no model list: Detect APIs and Each picked model
  answered with the typed key and models, no providers.json written. Saved
  provider with its key masked plus a model typed in its add box: both
  asked with the saved key. Duplicate not added: asked with the copied
  key. providers.json bytes unchanged. Without the web key: 401, the
  upstream got nothing.
2026-10-11 04:43:43 +08:00

204 lines
6.5 KiB
Go

package gui
import (
"encoding/json"
"io"
"io/fs"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/provider"
)
// #1545 (thedavidweng): Detect APIs and Each picked model ask with the
// form as it stands, not saved: a new provider's typed key and the models
// typed in its Models field, a saved one's picks not saved yet with its
// saved key when none is typed, and a copy not saved yet with the key of
// the one it copies. None of it writes anything: providers.json and the
// rest of the config folder are as they were, and no provider is added.
func TestProviderDetectDraft(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
// a relay with no model list (its /v1/models is a 404) that chats only
// for the model typed in the form, "draft-m", and the keys it knows
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
var body struct{ Model string }
json.Unmarshal(b, &body)
auth := r.Header.Get("Authorization")
mu.Lock()
asked = append(asked, r.Method+" "+r.URL.Path+" "+body.Model+" "+auth)
mu.Unlock()
if r.URL.Path != "/v1/chat/completions" {
http.NotFound(w, r)
return
}
if auth != "Bearer sk-typed" && auth != "Bearer sk-saved" {
http.Error(w, `{"error":{"message":"invalid api key"}}`, http.StatusUnauthorized)
return
}
if body.Model != "draft-m" {
http.Error(w, `{"error":{"message":"model not found"}}`, http.StatusNotFound)
return
}
io.WriteString(w, `{"choices":[{"message":{"content":"hi"}}]}`)
}))
defer up.Close()
mux := http.NewServeMux()
providerRoutes(mux, nil)
post := func(h http.Handler, action, body string) *httptest.ResponseRecorder {
t.Helper()
w := httptest.NewRecorder()
h.ServeHTTP(w, httptest.NewRequest("POST", "/api/provider/"+action, strings.NewReader(body)))
return w
}
type answer struct {
Results []provider.Detection
Models []provider.ModelDetection
}
detect := func(body string) answer {
t.Helper()
w := post(mux, "detect", body)
if w.Code != 200 {
t.Fatalf("%s: %d %s", body, w.Code, w.Body)
}
var got answer
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
return got
}
chat := func(rs []provider.Detection) provider.Detection {
for _, r := range rs {
if r.Protocol == provider.Chat {
return r
}
}
t.Fatalf("no chat in %+v", rs)
return provider.Detection{}
}
keys := func() []string {
mu.Lock()
defer mu.Unlock()
var out []string
for _, a := range asked {
if strings.Contains(a, "/chat/completions") {
out = append(out, a[strings.LastIndex(a, " ")+1:])
}
}
asked = nil
return out
}
base := up.URL + "/v1"
// a new provider: the key just typed and the models typed in its form
if r := chat(detect(`{"typed":true,"key":"sk-typed","base":"` + base + `","model":"","picked":["draft-m","other"]}`).Results); !r.OK || r.Model != "draft-m" {
t.Fatalf("new, models typed: %+v", r)
}
if got := keys(); len(got) != 1 || got[0] != "sk-typed" {
t.Fatalf("new asked with %v", got)
}
got := detect(`{"typed":true,"key":"sk-typed","base":"` + base + `","detectModels":["draft-m","gone"]}`)
if len(got.Models) != 2 || !chat(got.Models[0].Results).OK || chat(got.Models[1].Results).OK {
t.Fatalf("new, each model: %+v", got.Models)
}
keys()
if ps := provider.All(); len(ps) != 0 {
t.Fatalf("a detection added %v", ps)
}
// a saved provider, its key shown masked and not typed again, with a
// model picked in the form and not saved
if w := post(mux, "save", `{"id":"relay","name":"Relay","key":"sk-saved","chat":"`+base+`","models":["saved-m"],"new":true}`); w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body)
}
keys()
before := draftFiles(t, appdir.Config())
if _, ok := before["providers.json"]; !ok {
t.Fatalf("no providers.json in %v", before)
}
if r := chat(detect(`{"id":"relay","typed":true,"key":"","base":"` + base + `","model":"","picked":["draft-m"]}`).Results); !r.OK || r.Model != "draft-m" {
t.Fatalf("saved, a pick not saved: %+v", r)
}
if got := keys(); len(got) != 1 || got[0] != "sk-saved" {
t.Fatalf("saved asked with %v", got)
}
// a copy of it, not saved yet, with no key typed: the copied one's key
if r := chat(detect(`{"new":true,"copyOf":"relay","typed":true,"key":"","base":"` + base + `","model":"","picked":["draft-m"]}`).Results); !r.OK {
t.Fatalf("copy: %+v", r)
}
if got := keys(); len(got) != 1 || got[0] != "sk-saved" {
t.Fatalf("copy asked with %v", got)
}
// a key typed over the saved one is the one asked with
if r := chat(detect(`{"new":true,"copyOf":"relay","typed":true,"key":"sk-typed","base":"` + base + `","model":"draft-m"}`).Results); !r.OK {
t.Fatalf("copy, key typed: %+v", r)
}
if got := keys(); len(got) != 1 || got[0] != "sk-typed" {
t.Fatalf("copy with a key typed asked with %v", got)
}
if after := draftFiles(t, appdir.Config()); !draftSame(before, after) {
t.Fatalf("a detection wrote to the config folder:\nbefore %v\nafter %v", draftNames(before), draftNames(after))
}
if ps := provider.All(); len(ps) != 1 || ps[0].ID != "relay" || len(ps[0].Models) != 1 {
t.Fatalf("providers now %+v", ps)
}
// magpie web: without its key a draft probe is turned away, and the
// vendor is never asked
guarded := webHandler("3999", "0123456789abcdef-1545", time.Hour, mux)
if w := post(guarded, "detect", `{"typed":true,"key":"sk-typed","base":"`+base+`","picked":["draft-m"]}`); w.Code != http.StatusUnauthorized {
t.Fatalf("no web key: %d %s", w.Code, w.Body)
}
if got := keys(); len(got) != 0 {
t.Fatalf("asked without the web key: %v", got)
}
}
// draftFiles is every file under dir, by its path, with its bytes.
func draftFiles(t *testing.T, dir string) map[string]string {
t.Helper()
out := map[string]string{}
filepath.WalkDir(dir, func(p string, d fs.DirEntry, err error) error {
if err != nil || d.IsDir() {
return nil
}
b, _ := os.ReadFile(p)
rel, _ := filepath.Rel(dir, p)
out[filepath.ToSlash(rel)] = string(b)
return nil
})
return out
}
func draftSame(a, b map[string]string) bool {
if len(a) != len(b) {
return false
}
for k, v := range a {
if w, ok := b[k]; !ok || w != v {
return false
}
}
return true
}
func draftNames(m map[string]string) []string {
var out []string
for k := range m {
out = append(out, k)
}
return out
}