Files
magpie/internal/cli/serve_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

84 lines
3.0 KiB
Go

package cli
import (
"os"
"strings"
"testing"
"github.com/yetone/magpie/internal/settings"
)
// The serve banner says the gateway takes any key from this machine alone,
// and, when MAGPIE_ADDR puts it on the network (a Docker image) or it is
// shared from Settings, a gateway key from others: never from anyone.
func TestKeyNote(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
for _, c := range []struct{ addr, want string }{
{"", "only listens on localhost"},
{"127.0.0.1:3426", "only listens on localhost"},
{"localhost:3426", "only listens on localhost"},
{"[::1]:3426", "only listens on localhost"},
{"0.0.0.0:3425", "from others, an enabled gateway key"},
{":3425", "from others, an enabled gateway key"},
{"192.168.1.5:3425", "from others, an enabled gateway key"},
} {
t.Setenv("MAGPIE_ADDR", c.addr)
if n := keyNote(); !strings.Contains(n, c.want) || strings.Contains(n, "anyone") {
t.Errorf("MAGPIE_ADDR=%q: %q", c.addr, n)
}
}
for _, key := range []string{"", "sk-magpie-k"} {
if err := settings.Save(settings.Settings{LAN: true, LANKey: key}); err != nil {
t.Fatal(err)
}
for _, addr := range []string{"", "0.0.0.0:3425"} {
t.Setenv("MAGPIE_ADDR", addr)
if n := keyNote(); !strings.Contains(n, "gateway key") || strings.Contains(n, "only listens on localhost") {
t.Error("shared:", n)
}
}
}
}
// The CLIs print the address other machines are told to reach the gateway
// at: a valid MAGPIE_PUBLIC_URL, otherwise the one reached from this
// machine, so `magpie models` and the serve banner never print a bad one.
func TestAdvertisedURL(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("MAGPIE_ADDR", "127.0.0.1:3425")
for _, c := range []struct{ public, want string }{
{"", "http://127.0.0.1:3425"},
{"https://magpie.example.com", "https://magpie.example.com"},
{"nas.lan:3425/", "http://nas.lan:3425"},
{"ftp://magpie.example.com", "http://127.0.0.1:3425"},
{"https://magpie.example.com/magpie?x=1", "http://127.0.0.1:3425"},
} {
t.Setenv("MAGPIE_PUBLIC_URL", c.public)
if got := advertisedURL(); got != c.want {
t.Errorf("MAGPIE_PUBLIC_URL=%q: %q, want %q", c.public, got, c.want)
}
}
}
// Shared, the serve banner (docker logs) says where other machines reach
// the gateway: MAGPIE_PUBLIC_URL when a container sets it.
func TestShareLines(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("MAGPIE_ADDR", "0.0.0.0:3425")
t.Setenv("MAGPIE_PUBLIC_URL", "http://192.168.1.20:3425/")
if l := shareLines(); len(l) != 0 {
t.Error("unshared:", l)
}
if err := settings.Save(settings.Settings{LAN: true, LANKey: "sk-magpie-k"}); err != nil {
t.Fatal(err)
}
if l := strings.Join(shareLines(), "\n"); !strings.Contains(l, "http://192.168.1.20:3425") || strings.Contains(l, "container") {
t.Error("shared:", l)
}
}