Files
magpie/internal/profile/inuse_test.go
T
yetone bfaedf1830 gui, profile: the profile in use is ticked and named on the panel's Profiles button, not a count read as its name (superhtoooth on X)
superhtoooth (macOS): 保存方案设置了方案名字,但是应用方案没有用这个名字。
menu 里面变成了方案 1,方案 2。而且在应用里面没有显示当前使用的方案.

The bug: the tray panel's Profiles button showed the number of saved
profiles next to its label ("方案" + "2"), which reads as a profile
named 方案 2. Profiles were saved and applied under the typed name all
along; nothing anywhere said which profile was in use.

What changed:
- profile.InUse: a profile is in use while applying it would change
  nothing on this computer - every detected agent's saved field is as
  the profile has it (compared with the same renamed-ref spelling Apply
  writes), its model lists are held (provider.ModelListsHeld, a dry run
  of SetModelLists via the extracted putModelLists) and its library
  setup is as saved (library.Holds, the mirror of Restore). A profile
  that touches no agent here is not in use.
- Why not "last applied": that would stay ticked after the user changes
  a model by hand, a tick that lies. With this definition a profile
  saved from the current setup is in use at once, a hand change clears
  the tick, and applying puts it back. Two identical profiles are both
  ticked; the button names the first.
- GUI: /api/state carries inUse. The panel's button names the profile
  in use (a pop-up button showing what is picked), else counts them as
  before; its chip gets a check mark and aria-current, in panel and
  window; the chip's tooltip and its details say "In use" (string
  already in every language). CLI `magpie profiles` marks it, the TUI
  notes "in use". Docs (reference, start en/zh/ja) say so.

What was run:
- go test ./internal/profile (new TestProfileInUse: save, hand change,
  apply, hidden model list, library instructions, absent agents) and
  ./internal/gui (new TestStateProfileInUse through the HTTP handler),
  scratch HOME. Broken by hand: field compare, ModelListsHeld,
  library.Holds and state's InUse each turned a test red.
- go test -tags nogui ./... in a scratch HOME (the four slow packages
  rerun with -timeout 40m under load ~45).
- GUI: new profile-in-use.test.cjs (reporter's steps in the panel at
  360px, every language, plus the window) and panel-profiles,
  profile-confirm, profile-details, old-webkit, i18n-keys, gui-zh-tw,
  gui-ja, gui-de in chromium and webkit: all pass. Old app.js fails the
  new test.
- Real CLI in a sandbox HOME with a codex config: save/use/profiles
  show the mark, and it goes after a hand edit.
- gofmt; go vet darwin, linux (nogui), windows.
Not tried: the real tray app with real agents, and Windows/Linux at
runtime.
2026-10-12 19:14:54 +08:00

147 lines
4.4 KiB
Go

package profile
import (
"testing"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/library"
"github.com/yetone/magpie/internal/provider"
)
// inUse is which of the saved profiles InUse names, read as the GUI's state
// reads them: every detected agent's values once, passed in.
func inUse(t *testing.T) []string {
t.Helper()
detected := agent.Detected()
now := map[string]map[string]string{}
for _, a := range detected {
now[a.ID] = a.Values()
}
ps := must[map[string]Profile](t)(Load())
var out []string
for _, n := range Names(ps) {
if InUse(ps[n], detected, now) {
out = append(out, n)
}
// read here, the same answer (the TUI and the CLI pass none)
if InUse(ps[n], detected, now) != InUse(ps[n], detected, nil) {
t.Errorf("%s: in use with the values passed differs from read", n)
}
}
return out
}
func setModel(t *testing.T, id, model string) {
t.Helper()
for _, a := range agent.Detected() {
if a.ID == id {
if err := a.Apply("model", model); err != nil {
t.Fatal(err)
}
return
}
}
t.Fatalf("no %s", id)
}
func same(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
// Which profile is in use (superhtoooth on X: nothing in magpie said). A
// profile is in use while applying it would change nothing: the agents'
// fields, their model lists and the library's setup are as it has them.
// Saved, it is in use at once; another applied, that one is; a setting
// changed by hand since, neither is, until one is applied again.
func TestProfileInUse(t *testing.T) {
modelsHome(t)
if got := inUse(t); len(got) != 0 {
t.Fatalf("no profiles yet, in use %v", got)
}
// 工作 saved under the name typed, Codex on m1: in use at once
setModel(t, "codex", "relay/m1")
if err := Save("工作", must[Profile](t)(Snapshot())); err != nil {
t.Fatal(err)
}
if got := inUse(t); !same(got, []string{"工作"}) {
t.Fatalf("saved 工作: in use %v", got)
}
// 游戏, Codex on m2: it is the one in use, 工作 no longer
setModel(t, "codex", "relay/m2")
if got := inUse(t); len(got) != 0 {
t.Fatalf("model changed by hand: in use %v", got)
}
if err := Save("游戏", must[Profile](t)(Snapshot())); err != nil {
t.Fatal(err)
}
if got := inUse(t); !same(got, []string{"游戏"}) {
t.Fatalf("saved 游戏: in use %v", got)
}
// 工作 applied
ps := must[map[string]Profile](t)(Load())
must[Applied](t)(Apply(ps["工作"]))
if got := inUse(t); !same(got, []string{"工作"}) {
t.Fatalf("applied 工作: in use %v", got)
}
// a model taken out of Codex's list by hand: the list isn't 工作's
if err := provider.SetHiddenModels("codex", []string{"relay/m3"}); err != nil {
t.Fatal(err)
}
if got := inUse(t); len(got) != 0 {
t.Fatalf("model list changed: in use %v", got)
}
must[Applied](t)(Apply(ps["工作"]))
if got := inUse(t); !same(got, []string{"工作"}) {
t.Fatalf("applied 工作 again: in use %v", got)
}
// the library's setup is part of it: instructions changed since, 带库
// isn't in use; applied, it is
shared := "Use tabs."
must[*library.Result](t)(library.SaveInstructions(library.InstructionsChange{Shared: &shared, Agents: []string{"codex"}}))
must[*library.Result](t)(library.SaveServer("", library.Server{Name: "fs", Transport: "stdio", Command: "npx", Agents: []string{"codex"}}))
if err := Save("带库", must[Profile](t)(Snapshot())); err != nil {
t.Fatal(err)
}
if got := inUse(t); !same(got, []string{"带库"}) {
// 工作 has no library setup, so it is in use with any library
if !same(got, []string{"工作", "带库"}) {
t.Fatalf("saved 带库: in use %v", got)
}
}
other := "Use spaces."
must[*library.Result](t)(library.SaveInstructions(library.InstructionsChange{Shared: &other, Agents: []string{"codex"}}))
for _, n := range inUse(t) {
if n == "带库" {
t.Fatal("instructions changed: 带库 still in use")
}
}
must[*library.Result](t)(library.ServerAgents("fs", nil))
must[Applied](t)(Apply(must[map[string]Profile](t)(Load())["带库"]))
found := false
for _, n := range inUse(t) {
found = found || n == "带库"
}
if !found {
t.Fatalf("applied 带库: in use %v", inUse(t))
}
// one synced from a computer whose agents aren't here does nothing
// applied, so it isn't the setup in use
if InUse(Profile{Fields: map[string]string{"opencode.model": "relay/m1"}}, agent.Detected(), nil) {
t.Error("a profile of agents not here is in use")
}
}