Files
magpie/internal/gateway/remote_self_test.go
T
yetone 1826c4bd99 providers: a Remote magpie that is this magpie says so and is left out, unless kept (hanslin1666)
B uses A through a Remote magpie provider, and sync carries that
provider to A, where it points at A. Each of its models was one of A's
own, listed again beside it and taken by A's found groups, and a request
through it passed through A's gateway twice.

A fetch of a Remote magpie's list now reads its models' magpie_via: when
the magpie that listed them (the last entry) is this magpie's own
catalog.Self, remote-self.json keeps the provider id and the address it
answered at (noteSelf). Another lister, or a list with no via, takes
the record away; an empty list or a failed fetch changes nothing; a
provider moved to another address is not self until a fetch there says
so. The file is this computer's own, never in a backup or sync.

Such a provider is left out (SelfLeftOut in On, SwitchedOff and
remoteMagpies): not in /v1/models, the catalog or a found group. A
request naming one of its models, an agent's pick synced from B, is
served here with the remote's id taken off (ThroughSelf in Resolve and
ResolveRoute), once through the gateway. A 404 for it says why and how
to keep it.

The Providers row shows "This magpie" (greyed while left out), with the
reason as its title; the editor has a note and a "Use it anyway" tick
(KeepSelf, POST /api/provider/keepself). KeepSelf belongs to the user
and to this computer: Mirror and Restore keep the local value (false for
a provider new here), davsync's hashes blank it, and a provider save
keeps it. Strings in en, zh, zh-TW, ja, de.

Docs: providers-accounts.md (two magpies row), sync-backup.md (what
stays on each computer).

Run at this tree on origin/main 4e85cac01:
- gofmt -l: nothing. app.js 1,309,132 bytes (budget 1,316,874).
- mote: go vet, GOOS=linux go vet -tags nogui, GOOS=windows go vet of
  provider, gateway, gui, davsync, catalog: ok.
- mote: go test catalog, davsync ok; gui red only in
  TestMainWindowCorners/macOS_26_SDK (#3949, macOS 27); provider and
  gateway did not build there (mote's disk full, "no space left on
  device"), so they were run on:
- Linux (WSL magpie-desktop): go test -tags nogui provider, gateway,
  gui, davsync: all ok.
- Windows (xiaomi-gpu): go test -run 'RemoteSelf|KeepSelf|RemoteMagpie|
  Mirror|KeepLogin' provider, gateway, davsync: ok.
- mote: remote-self.test.cjs 16 of 16 (chromium and webkit, 1100 and
  420, en zh ja de), before the rebase; the rebase touched no line of it.

Broken by hand, each red, then restored:
- noteSelf a no-op: TestRemoteSelfRecord and
  TestRemoteMagpieThatIsThisOneIsLeftOut red.
- ThroughSelf a no-op: the gateway test red (404 for rem/own/m1).
- On() without SelfLeftOut: the gateway test red (listed, 2 passes).
- davsync hashes without the KeepSelf blank: TestKeepSelfIsNoChange red.
- Mirror without keeping KeepSelf: TestMirrorKeepsKeepSelf red.
- The row's badge removed: remote-self.test.cjs red (chromium 1100 en).
- The tick's onchange a no-op: remote-self.test.cjs red (webkit 420 zh).

Not tried: two real computers syncing each other (the case is built in
one process: the gateway test's Remote magpie is its own gateway);
the Linux and Windows apps' webviews; the whole GUI suite (mote's disk is full);
remote-self.test.cjs again after the rebase.
2026-11-05 12:37:30 +08:00

134 lines
4.4 KiB
Go

package gateway
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// A Remote magpie that is this magpie (hanslin1666 on Discord: B uses A
// through one, and sync carried it to A, where it points at A) is left
// out: its models are not listed beside this magpie's own, a found group
// doesn't take them, and a request naming one (an agent's pick synced
// from B) is served here at once, not passed through the gateway again.
// "Use it anyway" (KeepSelf) has it back.
func TestRemoteMagpieThatIsThisOneIsLeftOut(t *testing.T) {
provider.LeaveOutSelf = true
t.Cleanup(func() { provider.LeaveOutSelf = false })
var vendor atomic.Int32
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
io.WriteString(w, `{"object":"list","data":[]}`)
return
}
vendor.Add(1)
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":"ok"}}]}`)
}))
t.Cleanup(up.Close)
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
if err := provider.Save(provider.Provider{ID: "own", Name: "Own", Key: "k", Models: []string{"m1"}, Chat: up.URL + "/v1"}); err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive("own", up.URL+"/v1", []catalog.Model{{ID: "m1"}}); err != nil {
t.Fatal(err)
}
var passes atomic.Int32 // chat requests this gateway was asked
h := New().Handler()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
passes.Add(1)
}
h.ServeHTTP(w, r)
}))
t.Cleanup(srv.Close)
id, err := provider.Add(provider.Provider{ID: "rem", Name: "Rem", Preset: provider.RemoteMagpiePreset, Chat: strings.TrimPrefix(srv.URL, "http://")})
if err != nil {
t.Fatal(err)
}
rem, _ := provider.Find(id)
if _, err := rem.Fetch(context.Background()); err != nil {
t.Fatal(err)
}
rem, _ = provider.Find(id)
if !rem.SelfAnswered() || !rem.SelfLeftOut() || rem.On() {
t.Fatalf("a Remote magpie at this magpie's own address: answered as self %v, left out %v, on %v; want true, true, false", rem.SelfAnswered(), rem.SelfLeftOut(), rem.On())
}
listed := func() map[string]bool {
t.Helper()
req, _ := http.NewRequest(http.MethodGet, srv.URL+"/v1/models", nil)
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
var l struct {
Data []struct {
ID string `json:"id"`
} `json:"data"`
}
if err := json.NewDecoder(resp.Body).Decode(&l); err != nil {
t.Fatal(err)
}
out := map[string]bool{}
for _, m := range l.Data {
out[m.ID] = true
}
return out
}
if l := listed(); l["rem/own/m1"] || !l["own/m1"] {
t.Errorf("listed %v; want own/m1 and not rem/own/m1", l)
}
for _, e := range provider.Catalog() {
if e.Provider.ID == "rem" {
t.Errorf("the catalog has %s through this magpie's own Remote magpie", e.ID)
}
}
ask := func(model string) int {
t.Helper()
req, _ := http.NewRequest(http.MethodPost, srv.URL+"/v1/chat/completions", strings.NewReader(`{"model":"`+model+`","messages":[{"role":"user","content":"hi"}]}`))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "curl/8")
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
return resp.StatusCode
}
if code := ask("rem/own/m1"); code != http.StatusOK {
t.Fatalf("a request for rem/own/m1: %d, want 200", code)
}
if p, v := passes.Load(), vendor.Load(); p != 1 || v != 1 {
t.Errorf("rem/own/m1 took %d passes through the gateway and %d vendor requests; want 1 and 1", p, v)
}
// kept by the user: through the remote as before
if err := provider.SetKeepSelf(id, true); err != nil {
t.Fatal(err)
}
rem, _ = provider.Find(id)
if !rem.SelfAnswered() || rem.SelfLeftOut() || !rem.On() {
t.Fatalf("kept: answered as self %v, left out %v, on %v; want true, false, true", rem.SelfAnswered(), rem.SelfLeftOut(), rem.On())
}
if l := listed(); !l["rem/own/m1"] {
t.Errorf("kept, listed %v; want rem/own/m1 again", l)
}
passes.Store(0)
if code := ask("rem/own/m1"); code != http.StatusOK || passes.Load() != 2 {
t.Errorf("kept, rem/own/m1: %d in %d passes; want 200 in 2", code, passes.Load())
}
}