Files
magpie/internal/gateway/models_wait_test.go
T
yetone 1aabc66fd1 gateway: /v1/models answers its ETag, and 304 to a client that has the list already
clyzhicore (Discord) runs a server magpie as the gateway and a local magpie
that takes its /v1/models and writes the models into dsh. They asked for
an ETag or a list you can wait on, so the local side knows a change at
once and a dead connection costs little. The wait is there since
06c4582cc (v0.1.3172: X-Magpie-List-Wait with If-None-Match, and the
Remote magpie follower). What was missing, and is added here:

- A plain GET /v1/models (no wait header) is answered with the same ETag
  a wait answers (listTag of the bytes this caller gets), Cache-Control
  private, no-cache, and 304 with no body, at once, when If-None-Match
  names it (strong or W/, in a comma list, or *). A tag it doesn't name,
  or none, is answered the list as before, same bytes. A refused list
  (400) stays untagged. Any client (a script, curl, another tool) can
  now poll cheaply.
- A wait's answer, 200 or 304, says in X-Magpie-List-Wait how many
  seconds it held. A plain answer doesn't.
- The follower (waitRemoteList) takes a 304 that comes sooner than
  followQuick (5s) without that header as a magpie that doesn't wait.
  This guards the change above: a proxy that drops X-Magpie-List-Wait
  would now get a tagged plain list and an instant 304, and the follower
  would ask every followGap (1s). It now leaves such a magpie to its
  schedule and asks again after followOldAgain (1h), as for one from
  before the wait. Servers from 06c4582cc on hold each wait to its end
  (50s), so their 304s pass.

Compatibility: an old server (no ETag) is still left to the schedule,
as TestRemoteMagpieOldListNotFollowed checks. An old client never sends
If-None-Match without the wait header and gets the same bytes plus an
ETag. The GUI doesn't read the gateway's /v1/models through Wails (whose
304 on WebView2 is a 505, gui/api.go writeJSONSame), so that doesn't
apply.

Hidden models (their second ask) already exist and are not changed: a
provider's "Only through routing groups" (Provider.Unlisted, `magpie
provider listed <id> no`) and a group's listed=no (Group.Unlists, `magpie
group set ... listed=no`, CLI only) keep models out of /v1/models while
groups still route to them.

docs/subsystems/gateway-routing.md says the above.

Run (on mote, through mote-run, first at 04ed05733):
- gofmt -l on the changed packages: clean.
- go vet ./...; GOOS=linux go vet -tags nogui ./...; GOOS=windows go vet
  ./...; go build ., GOOS=linux -tags nogui, GOOS=windows: all ok.
- go test -tags nogui -race -count=1 -timeout 90m ./internal/gateway:
  ok 2145s, no DATA RACE (mote at load ~100-130).
- go test -tags nogui -count=1 -timeout 45m ./internal/provider: ok 126s.
- After rebasing onto origin/main (5 commits, none in these files):
  gofmt -l clean; go vet (darwin, linux nogui, windows) of both
  packages ok; -race -run "ModelsWait|ModelsList|RemoteMagpie": gateway
  ok 12.5s, provider ok 4.8s.

Broken by hand, each red:
- models() back to listModels (no tag on the plain list):
  TestModelsListConditional ("ETag \"\"") and TestModelsWaitTagged ("a
  plain list: 200, ETag \"\"").
- tagNamed never matching: TestModelsListConditional (200 with the body
  for each of the tag, W/, a list and *).
- No X-Magpie-List-Wait on a wait's answers: TestModelsWaitTagged ("a
  wait's list says it held \"\"", same for its 304).
- The followQuick check off: TestRemoteMagpieStrippedWaitNotAskedOver
  ("asked 28 times in 300ms, want 2").

Not tried: two real magpies, or a real reverse proxy that drops the
header (the stripped case is a fake in the test). Not run on Windows or
Linux. The full suite (go test ./...) and the GUI suite: not run at push.
Following a provider added as a plain OpenAI-compatible endpoint (not the
Remote magpie type) is not done: its list URL comes from fetchOne's
several bases, and it still goes by its #2135 schedule.

Reported-by: clyzhicore (Discord)
2026-11-08 15:45:57 +08:00

229 lines
7.5 KiB
Go

package gateway
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// listAsk asks srv's list, waiting wait seconds at the list tag names when
// wait is set.
func listAsk(t *testing.T, url, wait, tag string) (int, string, string) {
t.Helper()
req, _ := http.NewRequest("GET", url+"/v1/models", nil)
if wait != "" {
req.Header.Set(provider.ListWaitHeader, wait)
}
if tag != "" {
req.Header.Set("If-None-Match", tag)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
b, _ := io.ReadAll(res.Body)
return res.StatusCode, res.Header.Get("ETag"), string(b)
}
// listHeld is the wait srv's list answer says it held.
func listHeld(t *testing.T, url, wait, tag string) string {
t.Helper()
req, _ := http.NewRequest("GET", url+"/v1/models", nil)
if wait != "" {
req.Header.Set(provider.ListWaitHeader, wait)
}
if tag != "" {
req.Header.Set("If-None-Match", tag)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
return res.Header.Get(provider.ListWaitHeader)
}
// A client polling the list with If-None-Match (clyzhicore on Discord: a
// magpie or a script following another's list) is answered 304 with no
// body, at once, while the list is the one it names, and the new list
// with its new tag once it changed. A tag it doesn't name, or none, is
// answered the list.
func TestModelsListConditional(t *testing.T) {
setup(t, provider.Chat, &fake{t: t})
srv := httptest.NewServer(New().Handler())
defer srv.Close()
_, tag, plain := listAsk(t, srv.URL, "", "")
if tag == "" || !strings.HasPrefix(tag, `"`) {
t.Fatalf("ETag %q", tag)
}
if _, again, body := listAsk(t, srv.URL, "", ""); again != tag || body != plain {
t.Fatalf("the same list asked again: ETag %q (was %q), same body %v", again, tag, body == plain)
}
for _, named := range []string{tag, "W/" + tag, `"other", ` + tag, "*"} {
code, again, body := listAsk(t, srv.URL, "", named)
if code != http.StatusNotModified || again != tag || body != "" {
t.Errorf("If-None-Match %s: %d, ETag %q, body %q; want 304 %s and none", named, code, again, body, tag)
}
}
if code, _, body := listAsk(t, srv.URL, "", `"ml-other"`); code != 200 || body != plain {
t.Errorf("another tag: %d, same body %v", code, body == plain)
}
if err := provider.Save(provider.Provider{ID: "fake2", Name: "Fake 2", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m2"}}); err != nil {
t.Fatal(err)
}
code, now, body := listAsk(t, srv.URL, "", tag)
if code != 200 || now == "" || now == tag || !strings.Contains(body, "m2") {
t.Fatalf("after a change: %d, ETag %q (was %q), m2 listed %v", code, now, tag, strings.Contains(body, "m2"))
}
// a list refused is answered as before, untagged
req, _ := http.NewRequest("GET", srv.URL+"/v1/models?kind=bogus", nil)
req.Header.Set("If-None-Match", "*")
res, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
if res.StatusCode != 400 || res.Header.Get("ETag") != "" {
t.Errorf("a refused list: %d, ETag %q; want 400 and none", res.StatusCode, res.Header.Get("ETag"))
}
}
// Another magpie waiting at the list (KaCli) is answered the list's ETag;
// the same list is held to the end of the wait and answered 304. A list
// asked without the header is answered the same bytes with the same ETag
// (clyzhicore), and only a wait's answer says how long it held.
func TestModelsWaitTagged(t *testing.T) {
setup(t, provider.Chat, &fake{t: t})
srv := httptest.NewServer(New().Handler())
defer srv.Close()
code, plainTag, plain := listAsk(t, srv.URL, "", "")
if code != 200 || plainTag == "" {
t.Fatalf("a plain list: %d, ETag %q; want 200 and a tag", code, plainTag)
}
if held := listHeld(t, srv.URL, "", ""); held != "" {
t.Errorf("a plain list says it held %q", held)
}
code, tag, body := listAsk(t, srv.URL, "1", "")
if code != 200 || tag != plainTag || body != plain {
t.Fatalf("a wait with no tag: %d, ETag %q (plain %q), same body %v", code, tag, plainTag, body == plain)
}
if held := listHeld(t, srv.URL, "1", ""); held != "1" {
t.Errorf("a wait's list says it held %q, want 1", held)
}
start := time.Now()
code, again, _ := listAsk(t, srv.URL, "1", tag)
if code != http.StatusNotModified || again != tag {
t.Fatalf("the same list: %d, ETag %q; want 304 %q", code, again, tag)
}
if held := listHeld(t, srv.URL, "1", tag); held != "1" {
t.Errorf("a wait's 304 says it held %q, want 1", held)
}
if d := time.Since(start); d < 900*time.Millisecond {
t.Errorf("the same list answered after %v, before its wait ended", d)
}
}
// A change saved while another magpie waits answers it the new list at
// once, not at the end of its wait.
func TestModelsWaitAnswersAChange(t *testing.T) {
setup(t, provider.Chat, &fake{t: t})
srv := httptest.NewServer(New().Handler())
defer srv.Close()
_, tag, _ := listAsk(t, srv.URL, "1", "")
go func() {
time.Sleep(300 * time.Millisecond)
if err := provider.Save(provider.Provider{ID: "fake2", Name: "Fake 2", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m2"}}); err != nil {
t.Error(err)
}
}()
start := time.Now()
code, now, body := listAsk(t, srv.URL, "30", tag)
if code != 200 || now == "" || now == tag {
t.Fatalf("after a change: %d, ETag %q (was %q)", code, now, tag)
}
if !strings.Contains(body, "m2") {
t.Errorf("the new list lacks m2: %s", body)
}
if d := time.Since(start); d > 3*time.Second {
t.Errorf("the change answered after %v", d)
}
}
// A gateway stopping ends every wait with 304: the other magpie keeps its
// tag and asks the next gateway.
func TestModelsWaitEndsWithTheGateway(t *testing.T) {
setup(t, provider.Chat, &fake{t: t})
s := New()
srv := httptest.NewServer(s.Handler())
defer srv.Close()
_, tag, _ := listAsk(t, srv.URL, "1", "")
go func() {
time.Sleep(200 * time.Millisecond)
s.endStreams()
}()
start := time.Now()
code, _, _ := listAsk(t, srv.URL, "30", tag)
if code != http.StatusNotModified {
t.Fatalf("a wait as the gateway stops: %d, want 304", code)
}
if d := time.Since(start); d > 3*time.Second {
t.Errorf("answered after %v", d)
}
}
// The wait is the list's own route: a caller the list refuses (another
// computer with no key, a foreign page) is refused it the same with the
// header, before anything waits.
func TestModelsWaitGuardedAsTheList(t *testing.T) {
fresh(t)
t.Setenv("MAGPIE_ADDR", "")
setup(t, provider.Chat, &fake{t: t})
newCaller(t, "LAN client")
h := New().Front()
ask := func(remote, origin string, wait bool) (int, string) {
r := httptest.NewRequest("GET", "/v1/models", nil)
r.RemoteAddr = remote
if origin != "" {
r.Header.Set("Origin", origin)
}
if wait {
r.Header.Set(provider.ListWaitHeader, "30")
r.Header.Set("If-None-Match", `"ml-anything"`)
}
w := httptest.NewRecorder()
done := make(chan struct{})
go func() { defer close(done); h.ServeHTTP(w, r) }()
select {
case <-done:
case <-time.After(10 * time.Second):
t.Fatalf("%s %s was held", remote, origin)
}
return w.Code, w.Body.String()
}
for _, c := range []struct{ remote, origin string }{
{"192.168.1.9:5000", ""},
{"127.0.0.1:5000", "https://evil.example"},
{"127.0.0.1:5000", "null"},
} {
plain, _ := ask(c.remote, c.origin, false)
if plain == 200 {
t.Fatalf("%s %s: the plain list was answered; the case tests nothing", c.remote, c.origin)
}
waited, body := ask(c.remote, c.origin, true)
if waited != plain {
t.Errorf("%s %s: %d with the wait, %d without (%s)", c.remote, c.origin, waited, plain, body)
}
}
}