Files
magpie/internal/provider/host_speaking_test.go
T
yetone e5b8f381dc provider: a Gemini provider saved as a bare host reads its Host without building a URL (#6479)
16b8c300f made Base(Gemini) return GeminiAsked(p.Gemini), which runs
GeminiBase (a regexp replace and url.Parse) and url.Parse again, and
adds /v1beta to a bare host. Host goes through Base on every look, so
f856d381a's guarantee (Host builds nothing; one list a look was 8 MB
over twenty Providers polls with real accounts) was gone for Gemini
providers. baffcb65d gave GeminiAsked a no-allocation path for a base
that is already versioned (case d of TestHostBuildsNoList), but a bare
host saved as typed, WandAPI's https://api.wandapi.ai (the very case
16b8c300f was for), still allocates 6 times a look, and an untrimmed or
pasted endpoint 5.

Host now asks baseHost, which is HostOf(Base(pr)) without building the
base: for a Gemini base saved with its scheme and a host, the host is
read from the saved URL, since what GeminiAsked trims (spaces, slashes,
a pasted /models/… endpoint) or adds (/v1beta) is all after the host.
Anything else (no scheme, no host, Factory's or Vertex's Gemini) still
goes through Base. Requests are unchanged: Base(Gemini) still returns
the asked URL. The test was right and is unchanged apart from two more
Gemini cases (d2: a bare host; d3: a pasted endpoint with spaces and a
trailing slash). The new TestHostOfGeminiIsAskedHost checks Host against
HostOf(Base(Gemini)) over odd saved values (no scheme,
"https:///models", "https://?x", "/x.example/v1", a port, a fragment).

Run (mica, -tags nogui; mote was at load 184):
- at 456b5c6c7, before baffcb65d: TestHostBuildsNoList red ("d: Host
  allocated 5 times a look", 3 of 3, also on mote), green after.
- rebased on origin/main ea3edd4b1: with Host's old loop put back by
  hand, d2 and d3 red (6 and 5 allocations); with this change
  TestHostBuildsNoList and TestHostOfGeminiIsAskedHost ok -count=3;
  go test ./internal/provider ok (144s); GOOS=darwin/linux/windows go
  vet ./internal/provider ok; gofmt -l clean.
Not run: the full ./... suite; the change is inside Host alone.
2026-11-13 18:59:59 +08:00

58 lines
2.1 KiB
Go

package provider
import "testing"
// Host is the host of the first protocol the provider speaks, as it was
// when it read Speaks' list; it builds no list now: one for each look was
// 8 MB of what twenty polls of the Providers page allocated with real
// accounts (IsBedrock, IsKilo, IsCline and IsOpenCode each look).
func TestHostBuildsNoList(t *testing.T) {
for _, p := range []Provider{
{ID: "a", Chat: "https://api.example.com/v1"},
{ID: "b", Anthropic: "https://api.anthropic.example/v1"},
{ID: "c", Responses: "https://r.example.com/v1", Anthropic: "https://a.example.com"},
{ID: "d", Gemini: "https://generativelanguage.example.com/v1beta"},
{ID: "d2", Gemini: "https://api.wandapi.ai"}, // asked at its /v1beta (#6479)
{ID: "d3", Gemini: " https://relay.example.com/gemini/models/ "},
{ID: "e", Decide: "https://decide.example.com/v1"},
{ID: "f"},
} {
want := ""
for _, pr := range p.Speaks() {
if u := p.Base(pr); u != "" {
want = HostOf(u)
break
}
}
if want == "" && p.Decide != "" {
want = HostOf(p.Decide)
}
if got := p.Host(); got != want {
t.Errorf("%s: Host = %q, want %q", p.ID, got, want)
}
if n := testing.AllocsPerRun(20, func() { p.Host() }); n != 0 {
t.Errorf("%s: Host allocated %.0f times a look", p.ID, n)
}
}
}
// Host takes a Gemini base's host from the saved URL rather than the one
// GeminiAsked builds (#6479); it is the same host for whatever was saved.
func TestHostOfGeminiIsAskedHost(t *testing.T) {
for _, u := range []string{
"https://generativelanguage.googleapis.com", "https://api.wandapi.ai/",
"https://x.example/v1beta", " https://X.example/models/gemini:generateContent?alt=sse ",
"api.wandapi.ai", "api.wandapi.ai/v1beta", "/models", "https:///models", "https://?x",
"https://", "//", " ", "/x.example/v1", "https://x.example#f", "http://x.example:8080/",
} {
p := Provider{ID: "g", Gemini: u}
want := ""
if b := p.Base(Gemini); b != "" {
want = HostOf(b)
}
if got := p.Host(); got != want {
t.Errorf("Gemini %q: Host = %q, want %q (Base %q)", u, got, want, p.Base(Gemini))
}
}
}