Files
magpie/internal/agent/agy_posix_test.go
T
yetone b1e8eb15a6 agent: the function agy in a POSIX profile is agy(), which dash reads
magpie put bash's `function agy {` in ~/.bashrc and ~/.zshrc (and a WSL
distro's). On fnOS ~/.profile reads ~/.bashrc, so sh -lc, which is dash
there, read it too and stopped at a syntax error; Codync's SSH probe
failed (ljlwlcz on X, v0.1.3336). Both POSIX bodies, agyFunction's and
agyDistroFunction's, now open with `agy() {`. The keyword was there so an
alias named agy isn't expanded where the function is defined; that guard
is now an `unalias agy 2>/dev/null || true` on the line before, since a
shell expands aliases a line at a time. Blocks already in a profile are
rewritten between their markers the next time magpie writes them.

Run (mote, macOS; wsl-run, Ubuntu 24.04 where /bin/sh is dash):
- TestAgyFunctionInEveryPOSIXShell (new): each POSIX body read after an
  alias agy by dash, sh, bash, zsh (mksh/busybox when present), then agy
  run: here falls through to the agy on PATH, the distro one passes the
  key, gateway and --model. Green on mote and in WSL.
- Broken by hand: with the old `function agy {` head it is red in dash
  (exit 2, both bodies); with `agy() {` and no unalias it is red in dash,
  sh (bash posix) and zsh.
- go test -timeout 45m ./internal/agent ./internal/update ./internal/cli:
  ok on mote. gofmt -l clean; go vet for darwin, linux, windows ok.
Not run: the full suite, a real fnOS box, Codync itself.
2026-11-09 00:19:51 +08:00

69 lines
2.4 KiB
Go

//go:build !windows
package agent
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// The function agy magpie puts in a POSIX profile is read by every shell
// that reads the file: dash too, as sh -lc when ~/.profile reads ~/.bashrc
// (ljlwlcz on X: fnOS, where bash's "function agy {" failed an SSH probe's
// sh -lc). Each shell here reads the block after an alias named agy, as a
// profile does, and then agy starts the agy on PATH.
func TestAgyFunctionInEveryPOSIXShell(t *testing.T) {
var shells []string
for _, sh := range []string{"dash", "sh", "bash", "zsh", "mksh", "busybox"} {
if p, err := exec.LookPath(sh); err == nil {
shells = append(shells, p)
}
}
if len(shells) == 0 {
t.Skip("no POSIX shell here")
}
dir := t.TempDir()
bin := filepath.Join(dir, "bin")
home := filepath.Join(dir, "home")
settings := filepath.Join(home, ".gemini", "antigravity-cli", "settings.json")
if err := os.MkdirAll(bin, 0o755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Dir(settings), 0o755); err != nil {
t.Fatal(err)
}
stub := "#!/bin/sh\necho \"agy key=$GEMINI_API_KEY gw=$GOOGLE_GEMINI_BASE_URL args=$*\"\n"
if err := os.WriteFile(filepath.Join(bin, "agy"), []byte(stub), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(settings, []byte("{\n \"model\": \"magpie/relay/glm-4.6\"\n}\n"), 0o644); err != nil {
t.Fatal(err)
}
gone := filepath.Join(dir, "no such magpie")
for _, c := range []struct{ name, fn, want string }{
{"here", agyFunction("posix", gone), "agy key= gw= args=a b"},
{"distro", agyDistroFunction("posix", "k'1", "http://172.20.0.1:3425"), "agy key=k'1 gw=http://172.20.0.1:3425 args=--model magpie/relay/glm-4.6 a b"},
} {
profile := filepath.Join(dir, c.name+".profile")
body := "alias agy='echo ALIAS' 2>/dev/null\n# >>> magpie agy >>>\n" + c.fn + "\n# <<< magpie agy <<<\n"
if err := os.WriteFile(profile, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
for _, sh := range shells {
args := []string{"-c", `. "$1" && agy a b`, "sh", profile}
if filepath.Base(sh) == "busybox" {
args = append([]string{"sh"}, args...)
}
cmd := exec.Command(sh, args...)
cmd.Env = []string{"HOME=" + home, "PATH=" + bin + ":/usr/bin:/bin"}
out, err := cmd.CombinedOutput()
if got := strings.TrimSpace(string(out)); err != nil || got != c.want {
t.Errorf("%s in %s: %v\n%s\nwant %s\nprofile:\n%s", c.name, sh, err, got, c.want, body)
}
}
}
}