Files
magpie/internal/cli/agent_default_cli_test.go
T
yetone 23e764d0e0 cli: magpie's commands move from the root into internal/cli; the root keeps only main.go
The root held 89 .go files (30 commands, 59 tests). They move as they are
to internal/cli (package cli), with git mv so history follows them. The
only code change is main() becoming cli.Main(version): the root's new
main.go keeps `var version` in package main, so -ldflags "-X
main.version=…" in the Makefile, the Dockerfile and magpie-releases, and
every workflow, work unchanged. go-winres's rsrc_windows_*.syso still land
next to package main in the root.

Tests that read testdata by a root-relative path now go up one level
(../plugin/testdata/fake, ../sessions/testdata). Five subsystem doc links
point at internal/cli/.

Run:
- go build ./..., gofmt -l, go vet on darwin, linux and windows (nogui)
- builds with tags production, dev, "production nogui"; windows
  -H windowsgui; linux CGO_ENABLED=0
- make release-cli TARGETS="darwin/arm64 linux/amd64 windows/amd64
  android/arm64" VERSION=9.9.9-tidy: all four built, binary prints
  "magpie 9.9.9-tidy"
- docker build --build-arg VERSION=7.7.7-tidy: container prints
  "magpie 7.7.7-tidy"
- go test ./internal/cli (default and nogui): 152 tests, the same count
  as the root on main
- go test ./...: all packages pass (agent, gateway, library, provider
  timed out at 10m under other sessions' load and passed rerun alone
  with -timeout 40m; none of them changed)
- the built binary in a sandbox HOME: version, help, ls, providers,
  claude, and an unknown command's error
2026-10-12 20:40:22 +08:00

94 lines
3.2 KiB
Go

package cli
import (
"os"
"path/filepath"
"strings"
"testing"
)
// `magpie claude default` takes magpie out and puts back the model Claude
// Code was on before it, as the Agents page's Disconnect does, the user's
// own lines kept (__jingling on X: it took their model away with
// magpie's); a field's own default still leaves the agent as installed.
func TestAgentDefaultPutsBackTheModelBefore(t *testing.T) {
groupsHome(t)
t.Setenv("CLAUDE_CONFIG_DIR", "")
path := filepath.Join(os.Getenv("HOME"), ".claude", "settings.json")
os.MkdirAll(filepath.Dir(path), 0o755)
if err := os.WriteFile(path, []byte("{\n // mine\n \"model\": \"opus\",\n \"theme\": \"dark\"\n}\n"), 0o644); err != nil {
t.Fatal(err)
}
read := func() string {
b, _ := os.ReadFile(path)
return string(b)
}
if err := run([]string{"claude", "a/m"}); err != nil {
t.Fatal(err)
}
if s := read(); !strings.Contains(s, `"model": "a/m"`) || !strings.Contains(s, "ANTHROPIC_BASE_URL") {
t.Fatalf("not wired:\n%s", s)
}
if err := run([]string{"claude", "default"}); err != nil {
t.Fatal(err)
}
s := read()
if !strings.Contains(s, `"model": "opus"`) || strings.Contains(s, "ANTHROPIC_BASE_URL") || !strings.Contains(s, "// mine") || !strings.Contains(s, `"theme": "dark"`) {
t.Fatalf("default didn't put back what was there:\n%s", s)
}
if err := run([]string{"claude", "model", "default"}); err != nil {
t.Fatal(err)
}
if s := read(); strings.Contains(s, `"model"`) {
t.Fatalf("the model field's default leaves Claude Code as installed:\n%s", s)
}
}
// `magpie claude model default` on a Claude Code wired into magpie puts
// back the endpoint and token the user had before magpie and leaves them:
// it took them away, and Disconnect after it had nothing to put back
// (__jingling on X, twice).
func TestAgentModelDefaultKeepsTheEndpointBefore(t *testing.T) {
groupsHome(t)
t.Setenv("CLAUDE_CONFIG_DIR", "")
path := filepath.Join(os.Getenv("HOME"), ".claude", "settings.json")
os.MkdirAll(filepath.Dir(path), 0o755)
own := "{\n \"model\": \"opus\",\n \"env\": {\n \"ANTHROPIC_BASE_URL\": \"https://relay.example.com\",\n \"ANTHROPIC_AUTH_TOKEN\": \"sk-mine\"\n }\n}\n"
if err := os.WriteFile(path, []byte(own), 0o644); err != nil {
t.Fatal(err)
}
read := func() string {
b, _ := os.ReadFile(path)
return string(b)
}
mine := func(when string) {
t.Helper()
s := read()
if !strings.Contains(s, `"ANTHROPIC_BASE_URL": "https://relay.example.com"`) || !strings.Contains(s, `"ANTHROPIC_AUTH_TOKEN": "sk-mine"`) {
t.Fatalf("%s: the user's own endpoint and token are gone:\n%s", when, s)
}
}
if err := run([]string{"claude", "a/m"}); err != nil {
t.Fatal(err)
}
if s := read(); strings.Contains(s, "relay.example.com") {
t.Fatalf("not wired:\n%s", s)
}
if err := run([]string{"claude", "model", "default"}); err != nil {
t.Fatal(err)
}
mine("model default")
if s := read(); strings.Contains(s, `"model"`) {
t.Fatalf("model default kept a model:\n%s", s)
}
if err := run([]string{"claude", "default"}); err != nil {
t.Fatal(err)
}
mine("default after it")
// unwired, a field's default leaves the user's own endpoint too
if err := run([]string{"claude", "model", "default"}); err != nil {
t.Fatal(err)
}
mine("model default unwired")
}