Files
magpie/internal/gui/providers_outputs_retired_test.go
T
yetone f985d6bdb6 provider: a Max output kept for a model no longer listed doesn't block the editor's Save (#3305, LoKi)
LoKi (#3305, Windows, v0.1.1325, Zen Free plugin 0.1.14): settings.json
held "opencode-zen-free/space-bunny-free": 65536 from when Zen Free
listed it. OutputsOf gives the editor every saved limit, the editor
sends them back as `outputs` on every Save, and SetModelOutputs refused
any positive entry for a model the provider doesn't serve, so each Save
failed with "opencode-zen-free has no model space-bunny-free", with no
model picked too.

SetModelOutputs (models.go) and its sibling SetModelCompacts
(compact.go, the editor's Compact at, same check and same round trip)
now refuse an unserved model only when its value is new or changed: an
entry sent back as it is stored is kept, and the rest of the Save goes
through. The user's value isn't deleted, since they set it and the model
may come back on the next refresh. It still shows in Max output as
model=size, and taking it out there clears it, as before. A newly typed
model the provider doesn't serve, or a changed value for a retired one,
is refused as before. SetModelOutput (the CLI's single set) is unchanged.

Siblings checked: the editor's Context window goes through provider.Save,
which never checks it against the list, so a retired model's window
already saved; Names & levels (modelPrefs) sends only the entries
changed in that editor session (modelPrefsOfDraft), so a retired one
isn't resent.

Run at 45593cc1d (origin/main, unchanged at push) plus this change:
- TestRetiredModelLimitsDontBlockTheEditor (provider; outputs and
  compacts) and TestProviderSaveKeepsRetiredModelOutput (gui, through
  /api/provider/save: the model leaves the list, Select none, a stale
  limit beside a new one, a newly typed "typo" refused, cleared): ok.
- Broken by hand (the `!= n` term dropped from both setters): both red,
  "relay has no model luna" (each subtest) and 400 "relay has no model
  space-bunny-free".
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows: ok.
- go test -tags nogui -timeout 45m ./... (darwin, one suite on the
  machine): every package ok (agent 442s, gateway 794s, gui 178s,
  provider 218s), no FAIL in the log.
Not tried: the reporter's Windows machine or the real Zen Free plugin
(the fix is in a provider-kind-independent settings comparison); no GUI
change, so no Playwright run.
2026-10-23 03:35:27 +08:00

79 lines
3.4 KiB
Go

package gui
import (
"maps"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// The reporter's case through the editor's Save (#3305, LoKi): a Max
// output set on a model the provider has since stopped listing
// (opencode-zen-free/space-bunny-free=65536) is given to the editor and
// sent back with every Save, with no model picked too. The Save goes
// through and keeps it; a newly typed unknown model is still refused.
func TestProviderSaveKeepsRetiredModelOutput(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())
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/save", strings.NewReader(body)))
return w
}
if w := post(`{"id":"relay","name":"Relay","key":"k","chat":"http://127.0.0.1:1/v1","models":["sol","space-bunny-free"],"new":true}`); w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body)
}
const head = `{"id":"relay","from":"relay","name":"Relay","chat":"http://127.0.0.1:1/v1"`
if w := post(head + `,"models":["sol","space-bunny-free"],"outputs":{"space-bunny-free":65536},"compacts":{"space-bunny-free":200000}}`); w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body)
}
// the model leaves the list
if w := post(head + `,"models":["sol"],"outputs":{"space-bunny-free":65536},"compacts":{"space-bunny-free":200000}}`); w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body)
}
p, err := provider.Find("relay")
if err != nil {
t.Fatal(err)
}
if provider.ServesModel(*p, "space-bunny-free") {
t.Fatal("space-bunny-free is still served; the test needs it gone")
}
info := providerInfo(*p, nil)
if info.Outputs["space-bunny-free"] != 65536 || info.Compacts["space-bunny-free"] != 200000 {
t.Fatalf("the editor is given %v and %v", info.Outputs, info.Compacts)
}
// Select none, the rest as the editor has it
if w := post(head + `,"models":[],"noModels":true,"outputs":{"space-bunny-free":65536},"compacts":{"space-bunny-free":200000}}`); w.Code != 200 {
t.Fatalf("a Save with no model picked: %d %s", w.Code, w.Body)
}
if w := post(head + `,"models":["sol"],"outputs":{"space-bunny-free":65536,"sol":32000},"compacts":{"space-bunny-free":200000}}`); w.Code != 200 {
t.Fatalf("a Save with the stale limit beside a new one: %d %s", w.Code, w.Body)
}
want := map[string]int{"relay/space-bunny-free": 65536, "relay/sol": 32000}
if got := settings.Load().ModelOutputs; !maps.Equal(got, want) {
t.Errorf("outputs %v; want %v", got, want)
}
if got := settings.Load().ModelCompacts; !maps.Equal(got, map[string]int{"relay/space-bunny-free": 200000}) {
t.Errorf("compacts %v", got)
}
if w := post(head + `,"models":["sol"],"outputs":{"space-bunny-free":65536,"typo":5}}`); w.Code == 200 || !strings.Contains(w.Body.String(), "typo") {
t.Fatalf("a newly typed model it doesn't serve: %d %s", w.Code, w.Body)
}
if w := post(head + `,"models":["sol"],"outputs":{"sol":32000},"compacts":{}}`); w.Code != 200 {
t.Fatalf("cleared: %d %s", w.Code, w.Body)
}
if got := settings.Load().ModelOutputs; !maps.Equal(got, map[string]int{"relay/sol": 32000}) || len(settings.Load().ModelCompacts) != 0 {
t.Errorf("cleared: %v %v", got, settings.Load().ModelCompacts)
}
}