Files
magpie/internal/gateway/budget_plugin_test.go
T
yetone 5666cb0490 search MCP: web_search takes a count, 6 by default, 1 to 20 (treeman)
The web search MCP server's web_search always gave 6 results: searchHits
was both what a search API (a plugin's included) was asked for and where
its pages were cut. It now takes an optional count, described in its
input schema (integer, minimum 1, maximum 20, default 6). A number or a
number in a string is taken, floored; below 1 is held to 1, above 20 to
20 (Brave and Tavily take no more than 20); null or a non-number is the
default 6. The tool's description is unchanged, and the web_search magpie
offers models on vendor requests keeps its schema (no count).

The count rides the search's context (withSearchCount/searchCount), so
every backend reads it: plugin search APIs by key and signed-in (the
count handed to the hook and the pages cut), Tavily max_results, Brave
count, Exa numResults, Firecrawl limit, DeepSeek count, Volcengine
Count, You.com count (all three routes), Yuanbao Search's Cnt (the
fewest of 10/20/... that covers it), SearXNG (cut only), Kimi Code's
limit, GLM's search MCP and Codex's backend search (cut only, neither
takes a count), WorkBuddy (its client's 5 unless more than 6 is asked).
A searcher model's search is prose and has no count.

Run (mote): go test -run 'Search|Codex|Kimi|GLM|WorkBuddy|Plugin'
./internal/gateway ok; -race of the new tests and TestSearchMCP* ok;
go vet, GOOS=linux go vet -tags nogui, GOOS=windows go vet of the
package ok; gofmt -l empty.
Broken by hand: searchCount ignoring the count went red in both new
tests (plugin asked for 6, said 6, every vendor asked 6); withSearchCount
without the clamp went red for 50, 0 and -3.
Not tried: a real search API or plugin with count > 6; the full suite
runs after the push.
2026-11-06 00:31:14 +08:00

99 lines
3.5 KiB
Go

package gateway
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/budget"
"github.com/yetone/magpie/internal/gwbudget"
)
// The budget point with plugins/budget on and off, for a key held to a
// limit beside one that isn't. On, the limited key is let in while it has
// something left and refused with a 429 after. Off, the limited key is
// refused with a 503 and nothing is sent, in the chat API and the search
// MCP server alike (the vision one: TestVisionMCPHeldToKeyLimit), and the
// refusal is recorded; the key
// with no limit and a keyless request from this computer go on as before.
// A refresh that turns the plugin on lets the limited key's next request
// in at once.
func TestKeyLimitsOnAndOff(t *testing.T) {
fresh(t)
budget.Forget()
pinLimitClock(t)
calls := limitedUpstream(t, 0)
keys, secrets := newCaller(t, "Capped", "Free")
setLimit(t, keys[0].ID, &access.Limit{Period: "day", Tokens: 700})
s := New()
h := s.Handler()
call := func(secret string) *httptest.ResponseRecorder {
t.Helper()
r := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(chatReq))
if secret != "" {
r.Header.Set("Authorization", "Bearer "+secret)
} else {
r.RemoteAddr = "127.0.0.1:5000"
}
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
return w
}
capped := access.Identity{KeyID: keys[0].ID, KeyName: "Capped", Limit: &access.Limit{Period: "day", Tokens: 700}}
mcp := func() error {
r := httptest.NewRequest("POST", SearchMCPPath, nil).WithContext(access.WithIdentity(t.Context(), capped))
_, err := s.searchMCPRun(r, "latest go", nil)
return err
}
// on
if w := call(secrets[0]); w.Code != 200 {
t.Fatal("on: within the limit", w.Code, w.Body.String())
}
if w := call(secrets[0]); w.Code != 200 {
t.Fatal("on: within the limit", w.Code, w.Body.String())
}
if w := call(secrets[0]); w.Code != http.StatusTooManyRequests {
t.Fatal("on: a spent key let in", w.Code, w.Body.String())
}
if calls.Load() != 2 {
t.Fatalf("on: %d calls reached the provider", calls.Load())
}
// off
withoutLimits(t)
setLimit(t, keys[0].ID, &access.Limit{Period: "day", Tokens: 100000})
w := call(secrets[0])
if w.Code != http.StatusServiceUnavailable || !strings.Contains(w.Body.String(), "Key limits plugin") || w.Header().Get("X-Should-Retry") != "false" {
t.Fatal("off: a limited key's request", w.Code, w.Body.String(), w.Header())
}
if calls.Load() != 2 {
t.Fatal("off: a limited key's request reached the provider")
}
if rec := lastUsage(t); !rec.Rejected || rec.CallerKeyID != keys[0].ID || rec.ErrType != "gateway_key_limit_off" || rec.Status != 503 {
t.Fatalf("off: refusal recorded as %+v", rec)
}
if err := mcp(); err == nil || !strings.Contains(err.Error(), "Key limits plugin") {
t.Errorf("off: the search MCP server said %v, not that the plugin is off", err)
}
if w := call(secrets[1]); w.Code != 200 {
t.Fatal("off: a key with no limit refused", w.Code, w.Body.String())
}
if w := call(""); w.Code != 200 {
t.Fatal("off: a keyless loopback request refused", w.Code, w.Body.String())
}
// the gateway's refresh turns it on for the request that needs it
var on func()
stop := gwbudget.Refresh(func() { on = gwbudget.Limit(budget.Limiter{}) })
defer stop()
gwbudget.ForgetRefreshForTest()
before := calls.Load()
if w := call(secrets[0]); w.Code != 200 || on == nil || calls.Load() != before+1 {
t.Fatal("a refresh that turned the plugin on still refused", w.Code, w.Body.String())
}
on()
}