Files
magpie/internal/gui/set_fast_unix_test.go
T
yetone 0361189f9e test: every package's tests pass on Windows, and none writes outside its sandbox (the user: 把 Windows 上那几个失败的测试修掉) — on a Windows box the suite failed in most packages, and the root package hung for 25 minutes. Tests set HOME, which os.UserHomeDir ignores on Windows (it reads USERPROFILE), so they read and wrote the runner's own home; each now sets USERPROFILE beside HOME, and Devin's, Goose's, Crush's and Claude Desktop's tests set APPDATA/LOCALAPPDATA, where those keep their files there. said() read the command's output only once it returned, and a pipe holds a few KB on Windows, so magpie model --help blocked forever; it reads while the command writes. Unix permission bits (the gateway key store's 0600 too), exec bits and symlinks Windows lacks are checked only off Windows; TOML strings hold Windows paths as literal strings, JSON as marshalled ones; a URL's path is compared as one. The provider and usage packages run in a home of their own (TestMain, as the gateway's already does), since a CLI's identity kept by a refresh still going when its test ended landed in the runner's ~/.config/magpie — and on a Mac in the user's own. Two product fixes came out of it: imagemcp took a file:///C:/… root or image for the path /C:/…, which doesn't exist on Windows, and now makes a Windows path of it (a host for a UNC share); and session discovery could keep a new Cowork session hidden on Linux, where a folder's mtime is a kernel tick and its size doesn't change, so an entry added within the tick of the last listing left both as they were — a listing made within 2s of its folder's last change is read again, as git does for racy files. TestFilePath and TestRacyDirectoryListedAgain fail without the change. On Windows 11 every package passes (32 of 32) and a run of each package in a fresh home leaves nothing in home, APPDATA or LOCALAPPDATA; on Linux (WSL) and macOS go test -tags nogui ./... passes. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build pass.
2026-10-01 23:15:07 +08:00

135 lines
4.2 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("USERPROFILE", 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)
}
}