Files
magpie/internal/gateway/middleware_test.go
T
wei 920d96af55 tests: provider, plugin, gateway, codexcat, gui and tui tests pass in any order and on any -count (#1146)
Squashed from wei's #1146, onto main. Each test now starts from state of
its own instead of what an earlier test (or an earlier -count run) left:

- plugin: changed() counts the OnChange hooks still running; Told waits
  for them, and Settle ends with Told and UseCached(nil), so a change one
  test made isn't told in the next and the next test's plugin list isn't
  read as changed. Only tests call Settle/Told; in production changed()
  only counts. New TestSettleWaitsForTheHooksItTold and
  TestSettleForgetsTheFoldersPlugins.
- provider: isolate forgets Copilot's state, the plans' quota cards and
  balances; the warm-up, Cursor sign-in, hold and moved-plugin tests wait
  for what they started.
- gateway: fresh() forgets refused seals, and TestRouting,
  TestSmartRouting and TestQuotaRestsUntilItsReset start from
  forgetRouting; codexSignedIn and TestForeignReasoningIsLeftOut start
  from fresh.
- codexcat: XDG_CONFIG_HOME under the test's home. tui: costAt reset.
  gui: the fake Cursor account no longer outlives its test. cli: Settle.

Left out as already on main: the cli_identity keepIdentities switch
(9c3772311 keeps the asks in a sandbox via testenv's marker) and the
omarchy reset (main's TestCurrent already resets cached/seen/asked).

Run (darwin, loaded machine):
- On main 043e0cfc7, -count=3: TestRouting, TestSmartRouting,
  TestQuotaRestsUntilItsReset, TestForeignReasoningIsLeftOut,
  TestStandaloneNativeFallback fail; codexcat and tui fail; provider
  -shuffle=1791359851244745000 fails TestCopilotAuto and
  TestCopilotSignAndModels, and -count=2 -shuffle=1791856220860256000
  -run 'Copilot|TestPlanQuotas|TestCachedCardsAskNobody' fails
  TestPlanQuotas and TestCachedCardsAskNobody. With this change, that
  provider run (-shuffle=on, -count=2), the same gateway tests,
  ./internal/codexcat, ./internal/tui and the Settle/Hold/warm-up tests
  in plugin and provider pass -count=3.
- Breaking Settle by hand: without Told(),
  TestSettleWaitsForTheHooksItTold fails; without UseCached(nil),
  TestSettleForgetsTheFoldersPlugins fails.
- gofmt -l; go vet ./... on darwin, GOOS=linux -tags nogui, GOOS=windows.
Not run: the whole ./... suite at once (the machine's load times it out)
and Windows/Linux test runs.
2026-10-13 09:52:14 +08:00

98 lines
3.7 KiB
Go

package gateway
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/middleware"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// A gateway middleware sees an agent's request before routing and its
// reply's events in the agent's own API, and can turn a request away.
func TestMiddlewareThroughGateway(t *testing.T) {
const key = "sk-proj-abcdEFGH1234ijklMNOP5678qrst"
var got []byte
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
got, _ = io.ReadAll(r.Body)
chunk := func(s string) string {
b, _ := json.Marshal(map[string]any{"id": "c1", "model": "m1", "choices": []any{map[string]any{"index": 0, "delta": map[string]any{"content": s}}}})
return "data: " + string(b)
}
w.Header().Set("Content-Type", "text/event-stream")
// the reply says the key back, which redaction gave it as a placeholder
var sent struct {
Messages []struct{ Content any } `json:"messages"`
}
json.Unmarshal(got, &sent)
last, _ := json.Marshal(sent.Messages[len(sent.Messages)-1].Content)
io.WriteString(w, sse(chunk("hello "), chunk(string(last)), `data: {"id":"c1","model":"m1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":2}}`, "data: [DONE]"))
}))
defer up.Close()
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
// the plugins' list written below reads as changed, and what that
// tells the plugins' hooks ends with the test
t.Cleanup(plugin.Settle)
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"m1"}, Chat: up.URL + "/v1"}); err != nil {
t.Fatal(err)
}
if err := settings.Save(settings.Settings{Redact: true}); err != nil {
t.Fatal(err)
}
mw := filepath.Join(t.TempDir(), "alias.middleware.js")
os.WriteFile(mw, []byte(`
export function onRequest(body, ctx) {
if (body.model === "blocked") return ctx.reject(402, "no budget left");
ctx.state.asked = body.model;
if (body.model === "fast") body.model = "fake/m1";
return body;
}
export const events = ["content_block_delta"];
export function onEvent(ev, ctx) {
if (ev.delta && ev.delta.type === "text_delta") ev.delta.text = ev.delta.text.toUpperCase() + "[" + ctx.protocol + "]";
return ev;
}`), 0o644)
b, _ := json.Marshal(plugin.List{Plugins: []plugin.Entry{{Spec: mw}}})
os.MkdirAll(settings.Dir(), 0o755)
os.WriteFile(filepath.Join(settings.Dir(), "plugins.json"), b, 0o644)
middleware.Reload()
defer middleware.Reload()
code, body := post(t, "/v1/messages", `{"model":"fast","max_tokens":10,"stream":true,"messages":[{"role":"user","content":"key `+key+`"}]}`)
if code != 200 {
t.Fatalf("%d %s", code, body)
}
if strings.Contains(string(got), key) {
t.Fatalf("the middleware's body went out unredacted: %s", got)
}
var text strings.Builder
for _, e := range events(body) {
if d, ok := e["delta"].(map[string]any); ok {
s, _ := d["text"].(string)
text.WriteString(s)
}
}
// the middleware saw the key as the agent will, not the placeholder
if want := "HELLO [anthropic]" + strings.ToUpper(`"key `+key+`"`) + "[anthropic]"; text.String() != want {
t.Fatalf("text %q, want %q", text.String(), want)
}
code, body = post(t, "/v1/messages", `{"model":"blocked","max_tokens":10,"messages":[{"role":"user","content":"hi"}]}`)
if code != 402 || !strings.Contains(body, "no budget left") || !strings.Contains(body, `"type":"error"`) {
t.Fatalf("reject: %d %s", code, body)
}
st := middleware.States()[mw]
if st.Failures != 0 || st.Calls < 4 {
t.Fatalf("state %+v", st)
}
}