Files
yetone 2649d5d727 agents: Dim alpha (dima) is an agent, started on magpie's gateway (T_t__biu on X)
Dim alpha (arcships/dim, npm dimcode-alpha, command dima) gets a row on
the Agents page. Picking a magpie model there starts dima with the
variables Dim documents for any OpenAI-compatible endpoint
(docs/design/model-startup.md): DIM_BASE_URL (gateway /v1),
DIM_API_FORMAT=openai-compatible, DIM_API_KEY (magpie-dim), DIM_MODEL
and both DIM_MAX_*_TOKENS (the catalog's, else 128000/32000). They hold
for that run only; Dim's saved Providers, settings and database are not
written. The pick is in magpie's own dim.json; a dima function in the
user's shell profiles runs `magpie dim run`, and the row gives the
command for an open terminal. Picking Dim's own (or Disconnect) removes
both. Nothing signs in to, reads or proxies Dim's own account or the
DimAgent subscription (removed at its vendor's request, v0.1.471).

Run:
- mica, at this tree (rebased on 574b7b96f): gofmt -l clean; go vet (darwin), GOOS=linux
  -tags nogui and GOOS=windows go vet of internal/agent, cli, agentenv;
  go test -tags nogui -count=1 -timeout 45m ./... -> ok, 101 packages. An earlier run on mote
  was red in internal/library's TestEveryAgentInLibrary (dim had no
  Library place); dim is now in noLibrary as "not looked at (#5897)".
- Broken by hand: DIM_API_FORMAT=openai-chat turns TestDim and
  TestDimRun red; dropping shell(false) turns TestDim red (function
  kept in .zshrc).
- Real dima 0.1.0-alpha.24 on mote, sandbox HOME, sandbox magpie serve
  on 127.0.0.1:38600 with a fake OpenAI upstream: `magpie dim
  magpie/fakeup/echo-1` wrote dim.json and the .zshrc function; `zsh -ic
  'dima exec -p "reply with one word"'` answered through the gateway
  (upstream got POST /v1/chat/completions, model echo-1); usage.jsonl
  recorded agent "dim"; `magpie dim default` removed the file and the
  function, and dima ran as npm installed it. Dim's home was never
  written by magpie.

Not tried: a real model provider (only the fake upstream); Windows and
WSL runs (the shell function is this machine's home only, no WSL
distro support); a Library target (skills, MCP, instructions; #5897); Dim's interactive TUI beyond
exec -p and --version.
2026-11-15 17:16:57 +08:00

176 lines
6.6 KiB
Go

package cli
import (
"errors"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/gateway"
)
// `magpie agy run`, what the function agy in the user's shells runs
// (bestony on Discord): the agy on PATH, given magpie's gateway, key and
// --model while settings.json's model is magpie's, every argument agy's;
// as it is off magpie.
func TestAgyRun(t *testing.T) {
home := helpHome(t)
settings := filepath.Join(home, ".gemini", "antigravity-cli", "magpie-settings.json")
os.MkdirAll(filepath.Dir(settings), 0o755)
t.Setenv("GEMINI_API_KEY", "users-own")
t.Setenv("GOOGLE_GEMINI_BASE_URL", "")
var bin, name string
var args, env []string
oldExec, oldLook := startExec, startLook
t.Cleanup(func() { startExec, startLook = oldExec, oldLook })
startExec = func(b, n string, a, e []string) error {
bin, name, args, env = b, n, a, e
return nil
}
real := filepath.Join(home, "bin", "agy")
startLook = func(b string) (string, error) {
if b != "agy" {
return "", errors.New("looked for " + b)
}
return real, nil
}
envOf := func(k string) []string {
var out []string
for _, kv := range env {
if strings.HasPrefix(kv, k+"=") {
out = append(out, strings.TrimPrefix(kv, k+"="))
}
}
return out
}
os.WriteFile(settings, []byte(`{"model": "magpie/deepseek/pro", "modelProvider": "gemini"}`), 0o644)
for _, c := range []struct{ in, want []string }{
{nil, []string{"--settings", settings, "--model", "magpie/deepseek/pro"}},
{[]string{"--help"}, []string{"--settings", settings, "--model", "magpie/deepseek/pro", "--help"}},
{[]string{"-p", "hi", "--model", "mine"}, []string{"--settings", settings, "-p", "hi", "--model", "mine"}},
{[]string{"--model=mine"}, []string{"--settings", settings, "--model=mine"}},
{[]string{"-model", "mine"}, []string{"--settings", settings, "-model", "mine"}},
} {
if _, err := runOut(t, append([]string{"agy", "run"}, c.in...)...); err != nil {
t.Fatal(err)
}
if bin != real || name != "agy" || !slices.Equal(args, c.want) {
t.Fatalf("agy run %v: %s %v, want %v", c.in, bin, args, c.want)
}
if k, u := envOf("GEMINI_API_KEY"), envOf("GOOGLE_GEMINI_BASE_URL"); !slices.Equal(k, []string{gateway.TokenFor("agy")}) || !slices.Equal(u, []string{gateway.URL()}) {
t.Fatalf("env: key %v, url %v", k, u)
}
}
out, err := runOut(t, "agy", "env")
if want := "GEMINI_API_KEY=" + gateway.TokenFor("agy") + "\nGOOGLE_GEMINI_BASE_URL=" + gateway.URL() + "\nsettings=" + settings + "\nmodel=magpie/deepseek/pro\n"; err != nil || out != want {
t.Fatalf("agy env: %q %v", out, err)
}
// remote control needs agy's Google sign-in: plain, with the user's own
// environment, however magpie has agy wired (baysonfox on X)
for _, in := range [][]string{{"remote-control", "start"}, {"--remote-control"}, {"-p", "hi", "--remote-control"}} {
if _, err := runOut(t, append([]string{"agy", "run"}, in...)...); err != nil {
t.Fatal(err)
}
if !slices.Equal(args, in) || !slices.Equal(envOf("GEMINI_API_KEY"), []string{"users-own"}) {
t.Fatalf("agy run %v: %v, key %v", in, args, envOf("GEMINI_API_KEY"))
}
}
// off magpie: as it is
os.WriteFile(settings, []byte(`{"model": "mine"}`), 0o644)
if _, err := runOut(t, "agy", "run", "-p", "hi"); err != nil {
t.Fatal(err)
}
if !slices.Equal(args, []string{"-p", "hi"}) || !slices.Equal(envOf("GEMINI_API_KEY"), []string{"users-own"}) {
t.Fatalf("off magpie: %v %v", args, envOf("GEMINI_API_KEY"))
}
if out, err := runOut(t, "agy", "env"); err != nil || out != "" {
t.Fatalf("agy env off magpie: %q %v", out, err)
}
// what --help and a stray value say
if out, _ := runOut(t, "agy", "--help"); !strings.Contains(out, "magpie agy run [args…]") {
t.Fatalf("agy --help:\n%s", out)
}
if _, err := runOut(t, "agy", "env", "x"); err == nil {
t.Fatal("agy env x: no error")
}
startLook = func(string) (string, error) { return "", errors.New("not found") }
if _, err := runOut(t, "agy", "run"); err == nil || !strings.Contains(err.Error(), "isn't installed") {
t.Fatalf("no agy: %v", err)
}
}
// `magpie dim run`, what the function dima runs: the dima on PATH with
// the variables Dim documents for an OpenAI-compatible endpoint while
// magpie's dim.json names a magpie model, every argument dima's (a
// subcommand first, so no --model is put before it); as it is otherwise.
func TestDimRun(t *testing.T) {
home := helpHome(t)
t.Setenv("DIM_API_KEY", "users-own")
t.Setenv("DIM_MODEL", "")
var bin, name string
var args, env []string
oldExec, oldLook := startExec, startLook
t.Cleanup(func() { startExec, startLook = oldExec, oldLook })
startExec = func(b, n string, a, e []string) error {
bin, name, args, env = b, n, a, e
return nil
}
real := filepath.Join(home, "bin", "dima")
startLook = func(b string) (string, error) {
if b != "dima" {
return "", errors.New("looked for " + b)
}
return real, nil
}
envOf := func(k string) []string {
var out []string
for _, kv := range env {
if strings.HasPrefix(kv, k+"=") {
out = append(out, strings.TrimPrefix(kv, k+"="))
}
}
return out
}
file := filepath.Join(home, ".config", "magpie", "dim.json")
os.MkdirAll(filepath.Dir(file), 0o755)
os.WriteFile(file, []byte(`{"model": "magpie/deepseek/pro"}`), 0o644)
for _, in := range [][]string{nil, {"exec", "-p", "hi"}, {"--help"}} {
if _, err := runOut(t, append([]string{"dim", "run"}, in...)...); err != nil {
t.Fatal(err)
}
if bin != real || name != "dima" || !slices.Equal(args, in) {
t.Fatalf("dim run %v: %s %s %v", in, bin, name, args)
}
for k, want := range map[string]string{
"DIM_BASE_URL": gateway.URL() + "/v1", "DIM_API_FORMAT": "openai-compatible",
"DIM_API_KEY": gateway.TokenFor("dim"), "DIM_MODEL": "deepseek/pro",
} {
if got := envOf(k); !slices.Equal(got, []string{want}) {
t.Fatalf("%s = %v, want %s", k, got, want)
}
}
if c, o := envOf("DIM_MAX_CONTEXT_TOKENS"), envOf("DIM_MAX_OUTPUT_TOKENS"); len(c) != 1 || len(o) != 1 || c[0] == "0" || o[0] == "0" {
t.Fatalf("limits %v %v", c, o)
}
}
out, err := runOut(t, "dim", "env")
if err != nil || !strings.Contains(out, "DIM_API_FORMAT=openai-compatible\n") || !strings.HasSuffix(out, "model=magpie/deepseek/pro\n") {
t.Fatalf("dim env: %q %v", out, err)
}
// off magpie: dima as it is, with the user's own environment
os.Remove(file)
if _, err := runOut(t, "dim", "run", "exec", "-p", "hi"); err != nil {
t.Fatal(err)
}
if !slices.Equal(args, []string{"exec", "-p", "hi"}) || !slices.Equal(envOf("DIM_API_KEY"), []string{"users-own"}) || len(envOf("DIM_API_FORMAT")) != 0 {
t.Fatalf("off magpie: %v %v", args, envOf("DIM_API_KEY"))
}
}