Files
magpie/internal/agent/legacy_test.go
T
yetone 4160f464aa groups: a routing group whose member's reasoning levels nothing knows offers its other members' levels, and Pi's thinking field offers and shows what Pi runs the model at (Moody-Sin, #597: 路由组设了推理强度最高是否正确应用?) — Pi on group/deepseek (腾讯DeepSeek) at magpie's 最高 listed off alone in its /thinking, and sent no reasoning_effort: Pi clamps defaultThinkingLevel max to the off it offers, so the group's members were asked at the vendor's default, not max.
The group offered no levels: its levels were what every member has, and a member whose levels magpie doesn't know (a Token Plan's deepseek-v4-pro-202606, which models.dev doesn't list) has none, so the intersection was empty and models.json had the group "reasoning": false. A model a provider was given that its vendor's models.dev list leaves out (TokenHub's deepseek-v4-pro, its list holding the Hy models alone) also had none in the catalog, where the gateway (Provider.Efforts) fits efforts to DeepSeek's low, high, max. Magpie's Pi field offered all seven levels whatever the model.

- provider.Exposed: a picked model the vendor's list doesn't have takes the levels Provider.Known gives it (knownElsewhere), not none.
- groupEntries: a member levelsUnknown (no levels, no account list, its vendor and maker don't list it, models.dev knows no model of its id — catalog.Knows) doesn't narrow the group's levels; the gateway sends it the effort as asked (fitEffort). A member listed with none still narrows them.
- Pi/OmO thinking field: for a model of magpie's, Options are the levels Pi offers for the entry magpie writes (piSupported, as pi-ai's getSupportedThinkingLevels), and Get shows a level it doesn't offer as Pi's clamp of it (piClamp).

Now Pi at max on that group sends reasoning_effort "max", relayed as max to both members (DeepSeek's levels have max; the unknown one is sent it as asked).

New tests: TestGroupLevelsWithUnknownMember (provider), TestPiGroupThinkingIsWhatPiRuns and TestPiClamp (agent); each fails without the change (group entry "reasoning": false; catalog levels []; field offering all seven). TestMoveCursorEfforts gives its model levels, since Pi is offered off alone for one without. go vet, nogui/linux/windows builds and the full test suite pass but the known flake TestFXInState.
2026-10-03 02:08:47 +08:00

153 lines
6.2 KiB
Go

package agent
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
)
func TestRenameLegacy(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
write := func(rel, body string) {
p := filepath.Join(home, rel)
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"deepseek-flash"}}); err != nil {
t.Fatal(err)
}
write(".config/opencode/opencode.jsonc", `{
// keep me
"provider": {
"dial": {"npm": "@ai-sdk/openai-compatible", "name": "dial", "options": {"apiKey": "dial", "baseURL": "http://127.0.0.1:3425/v1"}, "models": {}},
"mine": {"name": "mine"}
},
"model": "dial/deepseek/deepseek-flash",
"theme": "dark"
}
`)
write(".codex/config.toml", "model_reasoning_effort = \"high\"\nmodel = \"deepseek/deepseek-flash\"\nmodel_provider = \"dial\"\nmodel_catalog_json = '"+filepath.Join(home, ".codex", "dial-models.json")+"'\n\n[projects.\"/x\"]\ntrust_level = \"trusted\"\n\n[model_providers.dial]\nname = \"dial\"\nbase_url = \"http://127.0.0.1:3425/v1\"\nwire_api = \"responses\"\nexperimental_bearer_token = \"dial\"\n")
write(".codex/dial-models.json", "{}")
write(".pi/agent/settings.json", `{"defaultProvider": "dial", "defaultModel": "deepseek/deepseek-flash", "theme": "dark"}`)
write(".pi/agent/models.json", `{"providers": {"dial": {"api": "openai-completions", "apiKey": "dial", "baseUrl": "http://127.0.0.1:3425/v1", "models": []}}}`)
write(".claude/settings.json", `{"env": {"ANTHROPIC_BASE_URL": "`+gateway.URL()+`", "ANTHROPIC_AUTH_TOKEN": "dial", "ANTHROPIC_MODEL": "deepseek/deepseek-flash"}, "theme": "dark"}`)
RenameLegacy()
read := func(rel string) string {
b, err := os.ReadFile(filepath.Join(home, rel))
if err != nil {
t.Fatal(err)
}
return string(b)
}
oc := read(".config/opencode/opencode.jsonc")
if v, _ := edit.GetJSON(filepath.Join(home, ".config/opencode/opencode.jsonc"), "model"); v != "magpie/deepseek/deepseek-flash" {
t.Fatalf("opencode model: %q", v)
}
if !strings.Contains(oc, `"magpie": {`) || strings.Contains(oc, `"dial"`) || !strings.Contains(oc, "// keep me") || !strings.Contains(oc, `"mine"`) {
t.Fatalf("opencode:\n%s", oc)
}
cx := read(".codex/config.toml")
if strings.Contains(cx, "dial") || !strings.Contains(cx, "[model_providers.magpie]") || !strings.Contains(cx, `model_provider = "magpie"`) || !strings.Contains(cx, `[projects."/x"]`) {
t.Fatalf("codex:\n%s", cx)
}
if _, err := os.Stat(filepath.Join(home, ".codex", "dial-models.json")); err == nil {
t.Fatal("dial-models.json still there")
}
if _, err := os.Stat(filepath.Join(home, ".codex", "magpie-models.json")); err != nil {
t.Fatal("magpie-models.json missing")
}
if b := stashLoad(); b["codex.provider"] == "dial" {
t.Fatal("dial stashed as the provider to go back to")
}
if v, _ := edit.GetJSON(filepath.Join(home, ".pi/agent/settings.json"), "defaultProvider"); v != "magpie" {
t.Fatalf("pi provider: %q", v)
}
pm := read(".pi/agent/models.json")
if strings.Contains(pm, `"dial"`) || !strings.Contains(pm, `"magpie"`) {
t.Fatalf("pi models:\n%s", pm)
}
cl := read(".claude/settings.json")
if strings.Contains(cl, `"dial"`) || !strings.Contains(cl, `"ANTHROPIC_AUTH_TOKEN": "magpie"`) || !strings.Contains(cl, `"theme": "dark"`) {
t.Fatalf("claude:\n%s", cl)
}
// a second run leaves every file alone
before := map[string]string{}
for _, rel := range []string{".config/opencode/opencode.jsonc", ".codex/config.toml", ".pi/agent/settings.json", ".pi/agent/models.json", ".claude/settings.json"} {
before[rel] = read(rel)
}
RenameLegacy()
for rel, b := range before {
if read(rel) != b {
t.Fatalf("%s changed on the second run", rel)
}
}
}
// An agent set to one of Cursor's ids at an effort, from before magpie
// offered the family as one model, is set to that model, at that effort
// where its own is unset; an id magpie offers no model for is left.
func TestMoveCursorEfforts(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
if err := provider.Save(provider.Provider{ID: "curs", Name: "Curs", Chat: "https://curs.example/v1", Key: "k", Models: []string{"grok-4.7"}}); err != nil {
t.Fatal(err)
}
// Pi is offered the levels its entry gives the model (#597): a model
// with none would be offered off alone, and low not moved
if err := provider.SetModelEfforts("curs/grok-4.7", []string{"low", "high"}); err != nil {
t.Fatal(err)
}
base := func(id string) (string, string, bool) {
if id == "grok-4.7-low" || id == "gone-low" {
return strings.TrimSuffix(id, "-low"), "low", true
}
return "", "", false
}
pi := filepath.Join(home, ".pi", "agent", "settings.json")
write := func(body string) {
if err := os.MkdirAll(filepath.Dir(pi), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(pi, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
write(`{"defaultProvider": "magpie", "defaultModel": "curs/grok-4.7-low", "theme": "dark"}`)
moveEfforts("curs", base)
for k, want := range map[string]string{"defaultProvider": "magpie", "defaultModel": "curs/grok-4.7", "defaultThinkingLevel": "low", "theme": "dark"} {
if v, _ := edit.GetJSON(pi, k); v != want {
t.Errorf("%s = %q, want %q", k, v, want)
}
}
// an effort of its own stays; a model the picker doesn't offer stays
write(`{"defaultProvider": "magpie", "defaultModel": "curs/grok-4.7-low", "defaultThinkingLevel": "high"}`)
moveEfforts("curs", base)
if v, _ := edit.GetJSON(pi, "defaultThinkingLevel"); v != "high" {
t.Errorf("thinking level %q", v)
}
write(`{"defaultProvider": "magpie", "defaultModel": "curs/gone-low"}`)
moveEfforts("curs", base)
if v, _ := edit.GetJSON(pi, "defaultModel"); v != "curs/gone-low" {
t.Errorf("model %q", v)
}
}