Files
magpie/internal/gateway/key_nourl_test.go
T
yetone bf60d32bde gateway: a key set to a protocol its provider has no URL for is said on Routing and in the editor, not passed over quietly (#1702, edussss)
edussss set a relay key to Responses only on a provider with a Chat URL
and no Responses URL. perKeyBarred skipped the key (no endpoint for its
protocol), every key was skipped, so the provider was tried whole and the
request went out on Chat with nothing saying why. The owner: it shouldn't
fall back quietly.

Requests are still served on the URLs the provider has (so working setups
keep working), but now it is said:
- fallback.go: perKeyBarred remembers the protocol of the first key it
  skipped for having no URL, and the whole-provider candidate carries it
  as noURL.
- trace.go: Weighed.NoURL, so the Routing page sees it.
- routing.js: the row and the activity line say "key set to {api} only,
  no {api} URL · sent on the others".
- app.js/app.css: the editor's key row shows an amber "No {api} URL" chip
  (keyLacksURL) with a title that says how to fix it.
- i18n.js: the three strings in zh, zh-TW, ja and de.
- docs/subsystems/providers-accounts.md: the Keys row says so.

Run: go test -run 'TestKeyForAProtocolWithNoURLIsSaid' ./internal/gateway/
(the reporter's case: upstream /v1/chat/completions with k-resp, NoURL
"responses"; with an Any key added it goes to k-any and NoURL is empty);
key-nourl.test.cjs in chromium and webkit, en/zh/zh-TW/ja/de, at 420 and
1000 wide, no click-scroll, no sideways scroll: 40 pass; with i18n-keys,
gui-zh-tw/ja/de, key-copy, key-weight, key-weight-fit, key-import,
routing-key-windows, routing-keys-fold, proto-menu-overflow, nokey-editor
and ledger-protocol: 174 pass, 0 fail.
Broken by hand: without the noURL mark the Go test fails (noURL = "",
want "responses"); with keyLacksURL/noURLWord removed chromium fails 20
of 20.
Not tried: a real relay, or the GUI against a live gateway.
2026-10-13 07:07:18 +08:00

73 lines
2.7 KiB
Go

package gateway
import (
"context"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/testenv"
)
// A key set to Responses on a provider with only a Chat URL (#1702,
// edussss: one key, Responses picked, the address typed in the
// OpenAI-compatible field only) has no endpoint of its own. The request
// still goes out on the Chat URL the provider has, as before, but the
// routing trace marks the provider with the protocol it lacks, so Routing
// says why it went to /v1/chat/completions. A key set to Any beside it is
// used as ever and nothing is marked.
func TestKeyForAProtocolWithNoURLIsSaid(t *testing.T) {
testenv.SetHome(t, t.TempDir())
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
var mu sync.Mutex
var seen []string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
seen = append(seen, r.URL.Path+" "+strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer "))
mu.Unlock()
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(
`data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":"ok"}}]}`,
`data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1}}`,
`data: [DONE]`))
}))
defer srv.Close()
body := `{"model":"relay/gpt-5.5","stream":false,"input":"hi"}`
for _, c := range []struct {
name string
keys []provider.KeyAccount // after the first, k-resp set to Responses
noURL provider.Protocol
want string
}{
{"the reporter's: one key, Responses", nil, provider.Responses, "/v1/chat/completions k-resp"},
{"a key for Any beside it", []provider.KeyAccount{{Key: "k-any"}}, "", "/v1/chat/completions k-any"},
} {
p := provider.Provider{ID: "relay", Name: "Relay", Chat: srv.URL + "/v1", Models: []string{"gpt-5.5"},
Key: "k-resp", KeyProtocol: provider.Responses, Keys: c.keys}
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
s := New()
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/responses", strings.NewReader(body)))
mu.Lock()
got := seen
seen = nil
mu.Unlock()
if rec.Code != 200 || len(got) != 1 || got[0] != c.want {
t.Fatalf("%s: %d %s, upstream saw %v", c.name, rec.Code, rec.Body.String(), got)
}
routes := s.Trace(context.Background(), 0, 0).Routes
if len(routes) != 1 || len(routes[0].Order) != 1 {
t.Fatalf("%s: routes %+v", c.name, routes)
}
if w := routes[0].Order[0]; w.NoURL != c.noURL {
t.Fatalf("%s: noURL = %q, want %q (%+v)", c.name, w.NoURL, c.noURL, w)
}
}
}