Files
magpie/internal/gui/set_fast_unix_test.go
T
yetone 6d69272732 agents: a model picked for Claude Code (or any agent) shows on its row at once, in the window and the tray panel, and /api/set answers in ~0.1s instead of seconds (Discord: switching Claude Code's model took 5–10s to show, magpie 0.1.410+) — /api/set writes the config and answers with the whole state, and the frontend drew the pick only from that answer. Every state asked the devin CLI (devin models list, ~1.5–3s a run) again several times whenever Devin was installed: its model's family, its options, its effort's options and Drift's re-read, and a CLI that gave no list (signed out, offline) was asked anew on every state, since only a list was cached. With a 4s CLI one set took 28s (measured, before). Now DevinFamilies keeps a failure for a minute, serves a list past its 5 minutes while it is read again in the background, and only a caller with nothing known waits — never one marked DevinNoWait, which Devin's field reads and options are (its Set still waits, so what is written is unchanged). Goose's detection read a binary's whole build info on every state (~150ms); the answer is kept while the file's size and time are the same. In the page, commit() draws the picked model and its flash before /api/set is sent, then draws the answer (a later pick's answer winning over an earlier one's); a refused pick puts the old model back and gives the reason. What is written to the agent's config is as before. Measured in a sandbox HOME with magpie web: /api/state 2.8–3.1s and /api/set 2.9s before, 0.1–0.5s after (state() ~110ms steady in-process); a provider whose endpoint never answers made no difference either way. Tested with internal/gui/set_fast_unix_test.go (devin CLI that sleeps 4s and a provider that never answers: three sets and the states after them each under 1.5s, ~5ms, settings.json as before; fails before the fix at 28s a set) and internal/gui/tests/model-pick.test.cjs (a set held 2.5s: the pick shown within 600ms in the window scrolled and in the tray panel's opened row and line, the page not moved, the answer keeping it, a refusal restoring the old model, en and zh, Chromium and WebKit; fails on main). click-scroll, panel-fold, menu-scroll, agy-launch, cli-update and old-webkit pass. go build -tags nogui (and GOOS=linux, GOOS=windows), go vet and go test -tags nogui ./... pass (TestClaudeAccountNamedAsItsLogin failed under load and passes alone). Not tried in the real Wails window or tray, nor with a real signed-in Devin CLI.
2026-09-30 03:21:24 +08:00

134 lines
4.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//go:build !windows
package gui
import (
"encoding/json"
"net"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
)
// Picking Claude Code's model answers at once, although /api/set answers
// with the whole state: with Devin installed, every state asked `devin
// models list` again (several times: its model's family, its options, its
// efforts), each taking seconds, and a CLI that gave no list was asked anew
// on the next click — the pick took 5–10s to show. Here the devin CLI takes
// 4s to answer and a provider's model endpoint never answers; the set, and
// the state after it, must not wait on either, and Claude Code's config is
// written as before.
func TestSetAnswersWhileDevinAndProvidersHang(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Setenv("XDG_DATA_HOME", filepath.Join(h, ".local", "share"))
t.Setenv("CLAUDE_CONFIG_DIR", "")
bin := filepath.Join(h, "bin")
os.MkdirAll(bin, 0o755)
t.Setenv("PATH", bin+string(os.PathListSeparator)+"/usr/bin"+string(os.PathListSeparator)+"/bin")
// a devin CLI that takes 4s over every answer
devin := filepath.Join(bin, "devin")
os.WriteFile(devin, []byte("#!/bin/sh\nexec sleep 4\n"), 0o755)
old := provider.DevinExecutable
provider.DevinExecutable = func() string { return devin }
t.Cleanup(func() { provider.DevinExecutable = old })
os.MkdirAll(filepath.Join(h, ".config", "devin"), 0o755)
os.WriteFile(filepath.Join(h, ".config", "devin", "config.json"), []byte(`{"agent":{"model":"claude-opus-5-5-high"}}`), 0o644)
// a provider whose endpoint takes the connection and never answers
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { ln.Close() })
go func() {
var held []net.Conn
for {
c, err := ln.Accept()
if err != nil {
for _, c := range held {
c.Close()
}
return
}
held = append(held, c)
}
}()
if _, err := provider.Add(provider.Provider{ID: "slow", Name: "Slow", Chat: "http://" + ln.Addr().String() + "/v1", Key: "sk-slow", Models: []string{"slow-1"}}); err != nil {
t.Fatal(err)
}
settingsPath := filepath.Join(h, ".claude", "settings.json")
os.MkdirAll(filepath.Dir(settingsPath), 0o755)
os.WriteFile(settingsPath, []byte(`{"model":"sonnet"}`), 0o644)
srv := httptest.NewServer(Handler(nil, nil))
t.Cleanup(srv.Close)
set := func(value string) (time.Duration, stateJSON) {
t.Helper()
start := time.Now()
res, err := http.Post(srv.URL+"/api/set", "application/json", strings.NewReader(`{"agent":"claude","field":"model","value":"`+value+`"}`))
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
took := time.Since(start)
var s stateJSON
if res.StatusCode != http.StatusOK || json.NewDecoder(res.Body).Decode(&s) != nil {
t.Fatalf("set %s: %d", value, res.StatusCode)
}
return took, s
}
shown := func(s stateJSON) string {
for _, a := range s.Agents {
if a.ID == "claude" {
for _, f := range a.Fields {
if f.Key == "model" {
return f.Value
}
}
}
}
return "?"
}
for _, v := range []string{"claude-opus-5-5", "slow/slow-1", "claude-sonnet-5-5"} {
took, s := set(v)
t.Logf("set %s: %v", v, took)
if took > 1500*time.Millisecond {
t.Errorf("set %s took %v: it waited on the devin CLI or the provider", v, took)
}
if got := shown(s); got != v {
t.Errorf("set %s: the state shows %q", v, got)
}
start := time.Now()
res, err := http.Get(srv.URL + "/api/state")
if err != nil {
t.Fatal(err)
}
res.Body.Close()
if took := time.Since(start); took > 1500*time.Millisecond {
t.Errorf("the state after set %s took %v", v, took)
}
}
// what is written is what it was: the last pick, Anthropic's own
// model, off magpie's gateway
if m, _ := edit.GetJSON(settingsPath, "model"); m != "claude-sonnet-5-5" {
t.Errorf("settings.json model %q", m)
}
if u, _ := edit.GetJSON(settingsPath, "env.ANTHROPIC_BASE_URL"); u != "" {
t.Errorf("settings.json still routed to %q", u)
}
}