Files
magpie/internal/gui/providers_fetching_unix_test.go
T
yetone 6586c556db providers: the Providers tab answers at once, the accounts' lists asked for behind it (#541: with 200-odd models the Agents and Providers tabs showed placeholders for ten seconds and more at start-up) — GET /api/providers ran provider.FetchNew(8s) before answering, which asks each signed-in account without its vendor's list yet, one after another, up to 8 s each, under newFetches' lock; at start-up that lock is held by startBackend's own FetchNew(20s), run after the models.dev sync, so the page waited for every account start-up was asking, each up to 20 s, and an account whose list can't be had (cursor-agent hanging, a vendor out of reach) was waited on again a minute later. The page now starts FetchNew in the background (provider.FetchNewBehind, at once unless one it started still runs) and is told fetching (provider.FetchingNew, any FetchNew under way); while it is, the page asks again every 2.5 s, draws the answer only when it changed, never under an open editor, and stops once the lists are in. A sign-in done in magpie still waits for the new account's list (GET /api/providers?wait, #204), as the editor opens on it. TestProvidersAnswerWhileListsComeIn (a cursor-agent taking 4 s over its models: the page answers within 2 s, told fetching, and lists them once in) fails without the change ("the page waited 4.02s"); providers-fetching.test.cjs (WebKit) fails without the page's asking again. The Agents tab's wait (GET /api/state) was not reproduced: with 240 models over 13 agents state() takes ~50 ms here; currentFX can hold it up to 4 s when the CNY rate is stale, left as it is. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME pass; Chromium's launch fails here, WebKit only.
2026-10-02 17:31:54 +08:00

95 lines
2.6 KiB
Go

//go:build !windows
package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// The Providers page answers at once while an account's vendor is slow to
// list its models (#541: ten seconds and more of placeholders at start-up,
// the page waiting on each account's list in turn). Here Cursor's CLI takes
// 4s to list them: the page is told they are on their way, and has them
// once they are in.
func TestProvidersAnswerWhileListsComeIn(t *testing.T) {
h := sandboxHome(t)
t.Setenv("PATH", "/usr/bin"+string(os.PathListSeparator)+"/bin")
exe := filepath.Join(h, "cursor-agent")
os.WriteFile(exe, []byte(`#!/bin/sh
case "$1" in
about) echo '{"userEmail":"me@example.com","subscriptionTier":"Pro"}' ;;
models) sleep 4; echo 'slow-1 - Slow One' ;;
esac
`), 0o755)
old := provider.CursorExecutable
provider.CursorExecutable = func() string { return exe }
provider.ForgetAccounts()
t.Cleanup(func() {
for deadline := time.Now().Add(10 * time.Second); provider.FetchingNew() && time.Now().Before(deadline); {
time.Sleep(20 * time.Millisecond)
}
provider.CursorExecutable = old
provider.ForgetAccounts()
})
mux := http.NewServeMux()
providerRoutes(mux, nil)
get := func(path string) (providersJSON, time.Duration) {
t.Helper()
start := time.Now()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("GET", path, nil))
took := time.Since(start)
var s providersJSON
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &s) != nil {
t.Fatalf("%s: %d %s", path, w.Code, w.Body)
}
return s, took
}
slow := func(s providersJSON) (listed, found bool) {
for _, p := range s.Providers {
if p.ID == "cursor" {
for _, m := range p.Models {
if m.ID == "slow-1" {
return true, true
}
}
return false, true
}
}
return false, false
}
// who is signed in is known already, as it is once magpie has run
provider.All()
s, took := get("/api/providers")
if took > 2*time.Second {
t.Fatalf("the page waited %v for Cursor's list", took)
}
if listed, found := slow(s); !found || listed {
t.Fatalf("cursor found %v, its list in %v before it was fetched", found, listed)
}
if !s.Fetching {
t.Fatal("not told the lists are on their way")
}
deadline := time.Now().Add(10 * time.Second)
for {
s, _ = get("/api/providers")
if listed, _ := slow(s); listed && !s.Fetching {
break
}
if time.Now().After(deadline) {
t.Fatalf("Cursor's list never came in: fetching %v", s.Fetching)
}
time.Sleep(100 * time.Millisecond)
}
}