Files
magpie/internal/gateway/gemini_bare_host_test.go
T
yetone 16b8c300fe provider: a Gemini compatible URL saved as a bare host is asked at its /v1beta (equinox0253 on X)
cc0d9c8b0 kept a relay's root typed under "Gemini 兼容" as typed, where it
was saved as …/v1beta before, and requests went to the saved base as it
was. So WandAPI's https://api.wandapi.ai was asked
/models/gemini-3.8-flash:streamGenerateContent?alt=sse with no version, and
the relay refused it (the tweet's second screenshot: Claude Code's
/v1/messages → Gemini, 503).

The saved base stays as typed, and the version is added where a request
goes: provider.GeminiAsked gives a bare host /v1beta, as Google's SDKs add
it to a base_url with no path, and keeps a base with a path of its own
(/v1beta, a relay's /gemini, /proxy/v1) as typed. Base(Gemini) returns it,
so the gateway's requests, the native route and the model list
(catalog.FetchGemini at Base) all take it; a model's test (provider/test.go)
read p.Gemini directly and now reads Base(Gemini) too. The Google preset's
bases already carry /v1beta and are unchanged. docs/subsystems/
providers-accounts.md said GeminiBase adds /v1beta to any bare host, true
before cc0d9c8b0; it now says where the version is added.

Run (mote, -tags nogui, on 5a3f055d7 at origin/main 92bce5c72): the new
TestGeminiBareHostAskedAtV1beta (a fake upstream that answers only at the
expected path; bare host, bare host with a slash, /v1beta, /gemini) red on
the old code (asked /models/gemini-3.8-flash:…, 404), green after; the new
TestGeminiAsked; go test ./internal/gateway ./internal/provider -run
'Gemini|Vertex|OpenCode' ok; go vet darwin, linux, windows on provider,
gateway, catalog; gofmt clean.
Broken by hand: Base(Gemini) and the model test back to p.Gemini: both new
tests red.
Rebased on origin/main 0b38eb21c and run again there (mote, -tags nogui):
go test ./internal/gateway ./internal/provider ./internal/catalog -run
'TestWrongEndpoint$|TestNoChannel|Gemini' ok.
Not tried: a real WandAPI account; the full suite runs after the tag.
2026-11-13 15:59:26 +08:00

56 lines
2.3 KiB
Go

package gateway
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A Gemini compatible URL saved as a relay's bare host is kept as typed
// (cc0d9c8b0, JNDliuliuM on X), and asked as Google's SDKs ask one, at
// its /v1beta (equinox0253 on X: WandAPI's https://api.wandapi.ai under
// "Gemini 兼容", Claude Code's /v1/messages for gemini-3.8-flash went to
// /models/gemini-3.8-flash:streamGenerateContent with no version). A base
// with a path of its own is asked as typed.
func TestGeminiBareHostAskedAtV1beta(t *testing.T) {
for _, c := range []struct{ name, base, want string }{
{"bare host", "", "/v1beta/models/gemini-3.8-flash:streamGenerateContent?alt=sse"},
{"bare host with a slash", "/", "/v1beta/models/gemini-3.8-flash:streamGenerateContent?alt=sse"},
{"versioned", "/v1beta", "/v1beta/models/gemini-3.8-flash:streamGenerateContent?alt=sse"},
{"a path of its own", "/gemini", "/gemini/models/gemini-3.8-flash:streamGenerateContent?alt=sse"},
} {
t.Run(c.name, func(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
var asked []string
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.Copy(io.Discard, r.Body)
asked = append(asked, r.URL.RequestURI())
if r.URL.RequestURI() != c.want {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(
`data: {"candidates":[{"content":{"role":"model","parts":[{"text":"flash ok"}]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":3,"candidatesTokenCount":2,"totalTokenCount":5}}`,
))
}))
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Gemini: up.URL + c.base, Models: []string{"gemini-3.8-flash"}}); err != nil {
t.Fatal(err)
}
code, body := post(t, "/v1/messages", `{"model":"relay/gemini-3.8-flash","max_tokens":64,"stream":true,"messages":[{"role":"user","content":"hi"}]}`)
if code != 200 || !strings.Contains(body, "flash ok") {
t.Fatalf("status %d, asked %v: %s", code, asked, body)
}
if len(asked) != 1 || asked[0] != c.want {
t.Errorf("asked %v, want %s", asked, c.want)
}
})
}
}