Files
magpie/internal/provider/host_speaking_test.go
T
yetone f856d381ac provider: Host finds the first protocol spoken without building a list
Host read Speaks' whole list to take the first protocol with an address.
IsBedrock, IsKilo, IsCline, IsOpenCode and IsBedrockMantle each ask Host,
for every provider on every poll. Building that list was 8 MB of what
twenty polls of the Providers page allocated with real accounts.

Speaks' rule now lives in speaking, an iterator. Speaks collects it, and
Host stops at the first protocol with an address. The order and the
Code Assist, Vertex and Gemini cases are unchanged.

Test: TestHostBuildsNoList. For six providers it checks that Host equals
the host of the first of Speaks() with an address (else Decide's), and
that a look allocates nothing. With range p.Speaks() put back by hand it
fails at 3 to 5 allocations a look.

Run on mote at this tree:
- gofmt, go vet, and GOOS=windows and GOOS=linux vet of internal/provider:
  ok.
- TestHostBuildsNoList, TestNoAPIFindsTheProvidersAPIsOnce, and the
  Speaks, Vertex and Gemini tests alone, 3 times: ok.
- Not run with this change: the whole ./internal/provider suite. It
  passed on mote with only the market commit, before this change was
  made.
2026-11-01 05:51:27 +08:00

36 lines
1.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: "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)
}
}
}