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

130 lines
4.0 KiB
Go

package cli
import (
"testing"
"github.com/yetone/magpie/internal/provider"
)
// magpie provider account-concurrency gives a key its own limit on
// requests at once (by its name or id), off for none, default for the
// provider's again; magpie provider queue bounds the queue past it. A
// key the provider hasn't, or a number out of range, is an error.
func TestAccountConcurrencyCmd(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
t.Setenv("DSH_HOME", "")
five := 5
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k1", Chat: "http://127.0.0.1:1/v1", MaxConcurrency: &five}); err != nil {
t.Fatal(err)
}
limit := func() int {
p, err := provider.Find("relay")
if err != nil {
t.Fatal(err)
}
return p.LaneLimit()
}
if n := limit(); n != 5 {
t.Fatalf("before: %d, want the provider's 5", n)
}
id := provider.KeyID("k1")
if err := providerCmd([]string{"provider", "account-concurrency", "relay", id, "2"}); err != nil {
t.Fatal(err)
}
if n := limit(); n != 2 {
t.Fatalf("own: %d, want 2", n)
}
for _, args := range [][]string{{"provider", "account-concurrency", "relay"}, {"provider", "account-concurrency", "relay", id}} {
if err := providerCmd(args); err != nil {
t.Fatal(err)
}
}
if err := providerCmd([]string{"provider", "account-concurrency", "relay", id, "off"}); err != nil {
t.Fatal(err)
}
if n := limit(); n != 0 {
t.Fatalf("off: %d, want none", n)
}
for _, bad := range []string{"-1", "1001", "lots"} {
if err := providerCmd([]string{"provider", "account-concurrency", "relay", id, bad}); err == nil {
t.Fatalf("%s taken", bad)
}
}
if err := providerCmd([]string{"provider", "account-concurrency", "relay", "nokey", "3"}); err == nil {
t.Fatal("a key it hasn't was limited")
}
if err := providerCmd([]string{"provider", "account-concurrency", "relay", id, "default"}); err != nil {
t.Fatal(err)
}
if n := limit(); n != 5 {
t.Fatalf("default: %d, want the provider's 5", n)
}
if err := providerCmd([]string{"provider", "queue", "relay", "4", "30"}); err != nil {
t.Fatal(err)
}
p, _ := provider.Find("relay")
if p.QueueLimit != 4 || p.QueueWait != 30 {
t.Fatalf("queue %d, wait %d", p.QueueLimit, p.QueueWait)
}
if err := providerCmd([]string{"provider", "queue", "relay", "off"}); err != nil {
t.Fatal(err)
}
if p, _ = provider.Find("relay"); p.QueueLimit != 0 || p.QueueWait != 30 {
t.Fatalf("off: queue %d, wait %d", p.QueueLimit, p.QueueWait)
}
for _, bad := range [][]string{{"-3"}, {"20000"}, {"2", "4000"}, {"x"}} {
if err := providerCmd(append([]string{"provider", "queue", "relay"}, bad...)); err == nil {
t.Fatalf("%v taken", bad)
}
}
}
// magpie provider rpm sets how many requests each key sends in a minute
// (coeo91 on Discord), off for none; a number out of range is an error.
func TestRPMCmd(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
t.Setenv("DSH_HOME", "")
if err := provider.Save(provider.Provider{ID: "or", Name: "OpenRouter", Key: "k1", Chat: "http://127.0.0.1:1/v1"}); err != nil {
t.Fatal(err)
}
rpm := func() int {
p, err := provider.Find("or")
if err != nil {
t.Fatal(err)
}
return p.RPMLimit()
}
if err := providerCmd([]string{"provider", "rpm", "or", "20"}); err != nil {
t.Fatal(err)
}
if n := rpm(); n != 20 {
t.Fatalf("set: %d, want 20", n)
}
if err := providerCmd([]string{"provider", "rpm", "or"}); err != nil {
t.Fatal(err)
}
for _, bad := range [][]string{{"-3"}, {"20000"}, {"x"}, {"2", "3"}} {
if err := providerCmd(append([]string{"provider", "rpm", "or"}, bad...)); err == nil {
t.Fatalf("%v taken", bad)
}
}
if n := rpm(); n != 20 {
t.Fatalf("after refused ones: %d, want 20", n)
}
if err := providerCmd([]string{"provider", "rpm", "or", "off"}); err != nil {
t.Fatal(err)
}
if n := rpm(); n != 0 {
t.Fatalf("off: %d", n)
}
}