gui: a test posted with an account is sent to that account or key (tmr11235 on X)

"Test on account..." posted the pick as `account` with provider/test, but
the test action never read it: the body's Account was only used by other
actions, so provider.TestOn was never called from the GUI and every test
went with routing's pick (d96403141 tested TestOn in the provider package
only). Now the action puts req.Account on the context for both the model
tests and the provider's Test, for every kind TestOn covers (keys, pool
sign-ins, plugin accounts), as one handler.

Run: gofmt clean; mote-run go test ./internal/gui (new
TestProviderTestIsPinnedToTheAccountPosted, TestProviderTestStops,
TestProviderInfoSaysTestPicks) ok. Broken by hand: with the TestOn line
back to r.Context() the new test is red (pinned to the second key came
back Account:first, 503 wrong key), which is the report.
Not tried: a real ChatGPT or plugin account pair; their path is the
provider package's, already covered by TestModelTestPinnedToAnAccount.
This commit is contained in:
yetone
2026-11-12 22:12:32 +08:00
parent b6043b8be3
commit f57da2b5b9
2 changed files with 66 additions and 1 deletions
+3 -1
View File
@@ -2810,7 +2810,9 @@ func providerRoutes(mux *http.ServeMux, w Windows) {
fail(rw, err)
return
}
ctx := r.Context()
// the account or key the test is pinned to (provider.TestOn;
// tmr11235 on X: "Test on account…" was posted but never read)
ctx := provider.TestOn(r.Context(), req.Account)
if req.Run != "" {
var stop context.CancelFunc
ctx, stop = context.WithCancel(ctx)
+63
View File
@@ -0,0 +1,63 @@
package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// tmr11235 on X: "Test on account…" posted the pick as `account`, but the
// test action never read it, so the test went with routing's pick and the
// result named another key than the one chosen. The vendor here answers
// only sk-b: a test pinned to it is answered and says "second", and one
// pinned to sk-a is turned away.
func TestProviderTestIsPinnedToTheAccountPosted(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())
up := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Bearer sk-b" {
rw.WriteHeader(http.StatusServiceUnavailable)
_, _ = rw.Write([]byte(`{"error":{"message":"wrong key"}}`))
return
}
rw.Header().Set("Content-Type", "application/json")
_, _ = rw.Write([]byte(`{"choices":[{"message":{"content":"hi"}}]}`))
}))
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Chat: up.URL + "/v1", Icon: "generic",
Key: "sk-a", KeyName: "first", KeyProtocol: provider.Chat, Models: []string{"m"},
Keys: []provider.KeyAccount{{Name: "second", Key: "sk-b", Protocol: provider.Chat}}}); err != nil {
t.Fatal(err)
}
mux := http.NewServeMux()
providerRoutes(mux, nil)
ask := func(account string) provider.Result {
t.Helper()
body, _ := json.Marshal(map[string]any{"id": "relay", "test": []string{"m"}, "account": account})
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/provider/test", strings.NewReader(string(body))))
var out struct{ Results []provider.Result }
if err := json.Unmarshal(w.Body.Bytes(), &out); err != nil || len(out.Results) != 1 {
t.Fatalf("%d %s", w.Code, w.Body)
}
return out.Results[0]
}
p, _ := provider.Find("relay")
picks := p.TestPicks()
if len(picks) != 2 {
t.Fatalf("picks %+v", picks)
}
if r := ask(picks[1].ID); !r.OK || r.Account != "second" {
t.Fatalf("pinned to the second key: %+v", r)
}
if r := ask(picks[0].ID); r.OK || r.Account != "first" {
t.Fatalf("pinned to the first key: %+v", r)
}
}