Files
magpie/internal/agent/plugged_startup_test.go
T
yetone 1fd02d33ce agentplug: an agent plugin can pick the model its agent starts on in a second file (str_, PiG)
str_ on Discord (#help, v0.1.3005): PiG's row on the Agents page only
switched the model list on, where Pi's picks the model it starts on.
PiG, like Pi, keeps its providers in ~/.pig/agent/models.json and the
model a session starts on in settings.json (defaultProvider,
defaultModel; PiG's docs/site/docs/settings.md, startup_model.go). An
agent plugin could name a model key only in the one file connect
writes, so agent-pig 0.1.0 had to be connect-only.

An agent's descriptor can now say `startup: {config, format, model,
keys}`: the other file, the key that holds the model (the id without
magpie's prefix) and keys written beside it. With it the row gets a
model field as a built-in's does. Picking one of magpie's writes
connect's keys into config and the model and keys into startup.config,
each key kept aside as before; a model of magpie's picked in the agent
itself still reads as magpie's; a model of the user's own picked there
while magpie's provider is listed counts as connected (Beside), not
drift, and is kept on disconnect. Sync rewrites the list and leaves the
startup model where it is. Off, or `magpie <id> default` with the plugin
gone (RestoreGone), puts both files back. startup without connect, beside
agent.model, in config's own file, or naming the model key among keys
is refused. An older magpie ignores the field, so agent-pig 0.1.1
(plugins repo) stays connect-only there. Every other agent plugin
already names its model in its own config; pig is the only
connect-only one.

Run (mote):
- go test -count=1 ./internal/agentplug/: ok
- go test -count=1 -run 'TestPlugged|TestPlugin|TestRestoreGone|TestAgent'
  ./internal/agent/: ok
- go vet, GOOS=linux and GOOS=windows go vet of both packages; gofmt -l
  clean
- the four TestPluggedStartup* tests on the old code: red (the row had
  a provider field and only listed models)
- real PiG 0.5.3 (darwin-arm64 release, SHA256SUMS checked) in a sandbox
  HOME, models.json written by agent-pig 0.1.1's own connect and
  settings.json as startup writes it, against a fake OpenAI server:
  `pig -p` sent model deepseek/flash with the gateway key. With an
  enabledModels scope that leaves it out, PiG started on the scope's
  first model (said in the plugin's README).

Broken by hand: no startRestore in Unwire (TestPluggedStartupModel and
OwnPickKept red); no Beside (OwnPickKept red, "wired false").

Not tried: the GUI and the TUI with the real row (app.js unchanged; the
row is the model field every built-in has), Windows and Linux runs of
PiG, a real magpie gateway in front of PiG, a real vendor model.
2026-11-05 10:46:46 +08:00

218 lines
8.3 KiB
Go

package agent
import (
"encoding/json"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/agentplug"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// pigSrc is an agent plugin as agent-pig is: PiG (a Go port of Pi) keeps
// its providers in models.json and the model a session starts on in
// settings.json, as defaultProvider and defaultModel (PiG's
// docs/site/docs/settings.md), so connect writes models.json and startup
// names the model in settings.json.
const pigSrc = `
export const agent = {
id: "pig", name: "PiG", bin: "pig", dir: "~/.pig/agent",
config: "~/.pig/agent/models.json", format: "json",
startup: { config: "~/.pig/agent/settings.json", model: "defaultModel", keys: { defaultProvider: "magpie" } },
};
export function connect({ gateway, models }) {
return { "providers.magpie": { name: "Magpie", baseUrl: gateway.v1, apiKey: gateway.key, api: "openai-completions", models: models.map((m) => ({ id: m.id })) } };
}
`
// pigSettingsMine and pigModelsMine are PiG's two files as its user wrote
// them, on a provider of their own.
const pigSettingsMine = `{
"defaultProvider": "anthropic",
"defaultModel": "claude-sonnet-4",
"theme": "dark"
}
`
const pigModelsMine = `{
"providers": {
"ollama": { "baseUrl": "http://localhost:11434/v1", "api": "openai-completions", "models": [{ "id": "qwen3" }] }
}
}
`
func pigHome(t *testing.T) (a *Agent, settingsPath, modelsPath string) {
t.Helper()
home := pluggedHome(t, pigSrc)
dir := filepath.Join(home, ".pig", "agent")
os.MkdirAll(dir, 0o755)
settingsPath, modelsPath = filepath.Join(dir, "settings.json"), filepath.Join(dir, "models.json")
os.WriteFile(settingsPath, []byte(pigSettingsMine), 0o644)
os.WriteFile(modelsPath, []byte(pigModelsMine), 0o644)
a, err := Find("pig")
if err != nil {
t.Fatal(err)
}
setUpInMagpie(a.ID)
return a, settingsPath, modelsPath
}
func readJSONFile(t *testing.T, path string) map[string]any {
t.Helper()
b, err := os.ReadFile(path)
var m map[string]any
if err != nil || json.Unmarshal(b, &m) != nil {
t.Fatalf("%s: %v\n%s", path, err, b)
}
return m
}
// An agent plugin whose agent keeps its startup model apart from its
// provider list (str_ on Discord: PiG's row only listed models, where Pi's
// picks the one it starts on) gets a model field: picking one of magpie's
// writes connect's provider into models.json and defaultProvider and
// defaultModel into settings.json, a model picked in PiG among magpie's
// is still magpie's, the catalog follows, and switching it off puts both
// files back as the user had them.
func TestPluggedStartupModel(t *testing.T) {
a, settingsPath, modelsPath := pigHome(t)
f := a.Field("model")
if f == nil || a.Field("provider") != nil || a.ListsModels {
t.Fatalf("fields %+v, lists models %v", a.Fields, a.ListsModels)
}
if !containsOption(f.Options(nil), "magpie/deepseek/pro") {
t.Fatalf("magpie's models aren't offered: %+v", f.Options(nil))
}
if f.Get() != "claude-sonnet-4" || a.Wired() {
t.Fatalf("get %q, wired %v before connecting", f.Get(), a.Wired())
}
if err := f.Set("magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
s := readJSONFile(t, settingsPath)
if s["defaultProvider"] != "magpie" || s["defaultModel"] != "deepseek/pro" || s["theme"] != "dark" {
t.Fatalf("settings.json: %v", s)
}
ps := readJSONFile(t, modelsPath)["providers"].(map[string]any)
m, _ := ps["magpie"].(map[string]any)
if m == nil || m["baseUrl"] != gateway.URL()+"/v1" || m["apiKey"] != gateway.TokenFor("pig") || ps["ollama"] == nil {
t.Fatalf("models.json: %v", ps)
}
if _, ok := readJSONFile(t, modelsPath)["defaultModel"]; ok {
t.Fatal("the model went into models.json")
}
if f.Get() != "magpie/deepseek/pro" || !a.Wired() || a.Check() != "" || a.Drift() != nil {
t.Fatalf("get %q, wired %v, check %q, drift %+v", f.Get(), a.Wired(), a.Check(), a.Drift())
}
// picked in PiG's /model and saved: another of magpie's
b, _ := os.ReadFile(settingsPath)
os.WriteFile(settingsPath, []byte(strings.Replace(string(b), `"deepseek/pro"`, `"deepseek/flash"`, 1)), 0o644)
if f.Get() != "magpie/deepseek/flash" || !a.Wired() || a.Drift() != nil {
t.Fatalf("PiG's own pick: get %q, wired %v, drift %+v", f.Get(), a.Wired(), a.Drift())
}
// the catalog changed: the list follows, the model PiG starts on stays
if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"pro", "flash", "max"}}); err != nil {
t.Fatal(err)
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
var ids []string
for _, x := range readJSONFile(t, modelsPath)["providers"].(map[string]any)["magpie"].(map[string]any)["models"].([]any) {
ids = append(ids, x.(map[string]any)["id"].(string))
}
if !slices.Contains(ids, "deepseek/max") || readJSONFile(t, settingsPath)["defaultModel"] != "deepseek/flash" {
t.Fatalf("after sync: %v, %v", ids, readJSONFile(t, settingsPath))
}
// off: both files as the user had them
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
fileAsMine(t, settingsPath, pigSettingsMine)
if ps := readJSONFile(t, modelsPath)["providers"].(map[string]any); ps["magpie"] != nil || ps["ollama"] == nil {
t.Fatalf("models.json after off: %v", ps)
}
if a.Wired() || f.Get() != "claude-sonnet-4" {
t.Fatalf("after off: wired %v, get %q", a.Wired(), f.Get())
}
}
// A model of their own picked in PiG while magpie's provider is in its
// list is the user's: still connected, as joined, and switching it off
// takes magpie's provider out and leaves the user's pick where it is.
func TestPluggedStartupOwnPickKept(t *testing.T) {
a, settingsPath, modelsPath := pigHome(t)
if err := a.Field("model").Set("magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
mine := "{\n \"defaultProvider\": \"ollama\",\n \"defaultModel\": \"qwen3\",\n \"theme\": \"dark\"\n}\n"
os.WriteFile(settingsPath, []byte(mine), 0o644)
if a.Field("model").Get() != "qwen3" || !a.Wired() || a.Drift() != nil {
t.Fatalf("get %q, wired %v, drift %+v", a.Field("model").Get(), a.Wired(), a.Drift())
}
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
fileAsMine(t, settingsPath, mine)
if ps := readJSONFile(t, modelsPath)["providers"].(map[string]any); ps["magpie"] != nil {
t.Fatalf("magpie's provider left: %v", ps)
}
if _, ok := stashLoad()["plugin:pig:"+settingsPath]; ok {
t.Fatal("the startup stash outlived the wiring")
}
}
// The plugin gone from disk, `magpie pig default` puts both files back
// from the stash alone.
func TestPluggedStartupRestoreGone(t *testing.T) {
a, settingsPath, modelsPath := pigHome(t)
if err := a.Field("model").Set("magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
os.RemoveAll(a.Plugin)
os.WriteFile(filepath.Join(settings.Dir(), "plugins.json"), []byte(`{"plugins":[]}`), 0o644)
agentplug.Reload()
g, ok, err := RestoreGone("pig")
if err != nil || !ok || len(g.Put) != 2 {
t.Fatalf("restored %+v, %v, %v", g, ok, err)
}
fileAsMine(t, settingsPath, pigSettingsMine)
if ps := readJSONFile(t, modelsPath)["providers"].(map[string]any); ps["magpie"] != nil || ps["ollama"] == nil {
t.Fatalf("models.json: %v", ps)
}
}
// startup is checked as config is, and says one model: with agent.model
// too, or in config's own file, it is refused.
func TestPluggedStartupRefused(t *testing.T) {
pluggedHome(t,
`export const agent = { id: "a1", config: "~/.a1/m.json", model: "model", startup: { config: "~/.a1/s.json", model: "m" } };
export function connect() { return { k: 1 }; }`,
`export const agent = { id: "a2", config: "~/.a2/m.json", startup: { config: "~/.a2/m.json", model: "m" } };
export function connect() { return { k: 1 }; }`,
`export const agent = { id: "a3", config: "~/.a3/m.json", startup: { config: "~/.a3/s.toml", model: "m" } };
export function connect() { return { k: 1 }; }`,
`export const agent = { id: "a4", config: "~/.a4/m.json", startup: { config: "~/.a4/s.json", model: "m", keys: { m: "x" } } };
export function connect() { return { k: 1 }; }`,
`export const agent = { id: "a5", config: "~/.a5/m.json", startup: { config: "~/.a5/s.json", model: "m" } };`,
)
errs := PluginErrors()
if len(errs) != 5 {
t.Fatalf("errors: %v", errs)
}
for _, e := range errs {
if !strings.Contains(e, "agent.startup") && !strings.Contains(e, "agent.model") {
t.Fatalf("error: %q", e)
}
}
}