Files
magpie/internal/provider/compact_test.go
T
yetone c63c2136b2 agent: with Claude Code's own models hidden, a tier's model keeps its /model row and the switch rewrites settings.json at once (#4548)
Two gaps in the previous commit's replaceBuiltInOptions:

- writePicker leaves out the row of a model a tier is on at an effort
  (#1616), since Claude Code shows it as that tier's row. With
  replaceBuiltInOptions Claude Code drops the tier rows along with its
  built-in lineup (2.1.316: only the Default row and the picker's own
  stay), so such a model was in /model nowhere. While the switch hides
  Claude Code's own models, every model keeps its row.
- SetOwnModelsHidden saved the setting without catalog.Touched, unlike
  SetHiddenModels, so flipping the switch in the GUI left Claude Code's
  settings.json as it was until some other change re-synced it.

Tests: TestClaudePickerLeavesOutATiersModelAtItsEffort gains the
replace case (Terra on Haiku at max is listed); TestOwnModelsSwitchTellsTheAgents
counts catalog.Changed per flip.

Reviewed and run by yetone at the batch head over cac655c14 (this commit,
the four others from ggbdpq's #4495/#4548/#4512/#3260 patches and the
#4548 follow-up), on mote:
- gofmt -l clean; go vet ./... for darwin, linux (-tags nogui), windows;
  go build ./... for darwin and windows.
- go test -tags nogui -count=1 -timeout 45m ./...: every package ok but
  internal/gui, with two filed reds: TestSettingsSaveKeepsWhatItDoesNotSend
  (#4514), red alone at origin/main cac655c14 too, and TestColdTurnsTaken
  (#4510), red only in the full run. Rebased afterwards onto e94fb18b8,
  a usage-page change that touches none of these files.
- Windows (xiaomi-gpu): go test -run 'TestVSCode|TestClaudePicker'
  ./internal/agent ok. TestCopilotFolderFromAnIDEPluginIsNotTheCLI is red
  there at cac655c14 too (#1827).
- Broken by hand: each half reverted alone -> TestClaudePickerLeavesOutATiersModelAtItsEffort and TestOwnModelsSwitchTellsTheAgents red.
2026-10-31 20:54:37 +08:00

142 lines
5.2 KiB
Go

package provider
import (
"maps"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/settings"
)
// A provider's editor sets where Codex and Claude Code compact on its
// models (#876, hisiling): "*" for all of them and model=size for one,
// the model's own first; those it leaves out come off, another
// provider's stay, a model it doesn't serve is refused, and a save that
// changes nothing tells the agents nothing. A group takes the smallest
// its models have.
func TestModelCompactsSetFromTheEditor(t *testing.T) {
limitsHome(t)
for _, id := range []string{"deepseek", "other"} {
if err := Save(Provider{ID: id, Name: id, Chat: "https://relay.example/v1", Key: "k", Models: []string{"deepseek-v4-flash", "deepseek-v4-pro"}}); err != nil {
t.Fatal(err)
}
}
if err := SetModelCompacts("other", map[string]int{"deepseek-v4-pro": 300000}); err != nil {
t.Fatal(err)
}
told := 0
catalog.Changed = func() { told++ }
t.Cleanup(func() { catalog.Changed = nil })
if err := SetModelCompacts("deepseek", map[string]int{"*": 500000, "deepseek-v4-flash": 272000}); err != nil {
t.Fatal(err)
}
if told != 1 {
t.Errorf("told the agents %d times; want once", told)
}
want := map[string]int{"deepseek/*": 500000, "deepseek/deepseek-v4-flash": 272000, "other/deepseek-v4-pro": 300000}
if got := settings.Load().ModelCompacts; !maps.Equal(got, want) {
t.Errorf("compacts %v; want %v", got, want)
}
if got := CompactsOf("deepseek"); !maps.Equal(got, map[string]int{"*": 500000, "deepseek-v4-flash": 272000}) {
t.Errorf("CompactsOf: %v", got)
}
for id, n := range map[string]int{
"deepseek/deepseek-v4-flash": 272000,
"magpie/deepseek/deepseek-v4-pro": 500000,
"deepseek/deepseek-v4-pro[1m]": 500000,
"other/deepseek-v4-flash": 0,
"other/deepseek-v4-pro": 300000,
"nobody/deepseek-v4-pro": 0,
} {
if got := CompactSet(id); got != n {
t.Errorf("CompactSet(%s) = %d; want %d", id, got, n)
}
}
group := func(string) (Group, []Member, bool) {
dp, _ := Find("deepseek")
ot, _ := Find("other")
return Group{}, []Member{
{Path: []string{"deepseek/deepseek-v4-pro"}, Provider: *dp, Model: "deepseek-v4-pro"},
{Path: []string{"other/deepseek-v4-pro"}, Provider: *ot, Model: "wire-name"},
{Path: []string{"other/deepseek-v4-flash"}, Provider: *ot, Model: "deepseek-v4-flash"},
}, true
}
if got := compactSet(settings.Load(), GroupPrefix+"mix", group); got != 300000 {
t.Errorf("a group: %d; want its smallest, 300000", got)
}
told = 0
if err := SetModelCompacts("deepseek", map[string]int{"*": 500000, "deepseek-v4-flash": 272000}); err != nil || told != 0 {
t.Errorf("the same again: %v, told %d", err, told)
}
if err := SetModelCompacts("deepseek", map[string]int{"typo": 5}); err == nil {
t.Error("a model deepseek doesn't serve was accepted")
}
if got := settings.Load().ModelCompacts; !maps.Equal(got, want) {
t.Errorf("a refused save changed them: %v", got)
}
if err := SetModelCompacts("deepseek", map[string]int{}); err != nil {
t.Fatal(err)
}
if got := settings.Load().ModelCompacts; !maps.Equal(got, map[string]int{"other/deepseek-v4-pro": 300000}) {
t.Errorf("emptied: %v", got)
}
}
// The threshold for every model can be any size, not 272K alone (#876):
// typing one turns Full window off, 272K or nothing is the default again.
func TestSetCompactAt(t *testing.T) {
limitsHome(t)
if err := settings.Save(settings.Settings{FullContext: true}); err != nil {
t.Fatal(err)
}
if err := SetCompactAt(500000); err != nil {
t.Fatal(err)
}
if s := settings.Load(); s.FullContext || s.CompactAt != 500000 || s.Compact() != 500000 || s.Working(1000000) != 500000 || s.Working(200000) != 200000 {
t.Errorf("at 500K: %+v", s)
}
if err := SetCompactAt(settings.WorkingWindow); err != nil {
t.Fatal(err)
}
if s := settings.Load(); s.CompactAt != 0 || s.Compact() != settings.WorkingWindow {
t.Errorf("at 272K: %+v", s)
}
if err := SetCompactAt(-1); err == nil {
t.Error("a negative size was accepted")
}
if s := (settings.Settings{CompactAt: 500000, FullContext: true}); s.Compact() != 0 || s.Working(1000000) != 1000000 {
t.Errorf("full window: %d", s.Compact())
}
}
// The own-models switch reaches an agent's own config too (Claude Code's
// modelPicker says replaceBuiltInOptions, #4548), so flipping it tells the
// agents, as a hidden model's change does; a flip to where it already is
// tells them nothing.
func TestOwnModelsSwitchTellsTheAgents(t *testing.T) {
limitsHome(t)
told := 0
catalog.Changed = func() { told++ }
t.Cleanup(func() { catalog.Changed = nil })
if err := SetOwnModelsHidden("claude", true); err != nil {
t.Fatal(err)
}
if told != 1 || !OwnModelsHidden("claude") {
t.Fatalf("hidden: told %d times, hidden=%v; want once, true", told, OwnModelsHidden("claude"))
}
if err := SetOwnModelsHidden("claude", true); err != nil {
t.Fatal(err)
}
if told != 1 {
t.Fatalf("told %d times for a flip that changed nothing; want once in all", told)
}
if err := SetOwnModelsHidden("claude", false); err != nil {
t.Fatal(err)
}
if told != 2 || OwnModelsHidden("claude") {
t.Fatalf("shown: told %d times, hidden=%v; want twice, false", told, OwnModelsHidden("claude"))
}
}